file_analysis.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. #include "config.h"
  2. #include "ccanlint.h"
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/str_talloc/str_talloc.h>
  6. #include <ccan/talloc_link/talloc_link.h>
  7. #include <ccan/hash/hash.h>
  8. #include <ccan/htable/htable_type.h>
  9. #include <ccan/grab_file/grab_file.h>
  10. #include <ccan/noerr/noerr.h>
  11. #include <ccan/foreach/foreach.h>
  12. #include <ccan/asort/asort.h>
  13. #include "../tools.h"
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <err.h>
  19. #include <errno.h>
  20. #include <dirent.h>
  21. #include <ctype.h>
  22. #include <stdarg.h>
  23. #include <assert.h>
  24. const char *ccan_dir;
  25. static size_t dir_hash(const char *name)
  26. {
  27. return hash(name, strlen(name), 0);
  28. }
  29. static const char *manifest_name(const struct manifest *m)
  30. {
  31. return m->dir;
  32. }
  33. static bool dir_cmp(const struct manifest *m, const char *dir)
  34. {
  35. return strcmp(m->dir, dir) == 0;
  36. }
  37. HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp, manifest);
  38. static struct htable_manifest *manifests;
  39. const char *get_ccan_file_contents(struct ccan_file *f)
  40. {
  41. if (!f->contents) {
  42. f->contents = grab_file(f, f->fullname, &f->contents_size);
  43. if (!f->contents)
  44. err(1, "Reading file %s", f->fullname);
  45. }
  46. return f->contents;
  47. }
  48. char **get_ccan_file_lines(struct ccan_file *f)
  49. {
  50. if (!f->lines)
  51. f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
  52. /* FIXME: is f->num_lines necessary? */
  53. f->num_lines = talloc_array_length(f->lines) - 1;
  54. return f->lines;
  55. }
  56. struct list_head *get_ccan_file_docs(struct ccan_file *f)
  57. {
  58. if (!f->doc_sections) {
  59. get_ccan_file_lines(f);
  60. f->doc_sections = extract_doc_sections(f->lines);
  61. }
  62. return f->doc_sections;
  63. }
  64. struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
  65. {
  66. struct ccan_file *f;
  67. assert(dir[0] == '/');
  68. f = talloc(ctx, struct ccan_file);
  69. f->lines = NULL;
  70. f->line_info = NULL;
  71. f->doc_sections = NULL;
  72. f->compiled = NULL;
  73. f->name = talloc_steal(f, name);
  74. f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
  75. f->contents = NULL;
  76. f->cov_compiled = NULL;
  77. return f;
  78. }
  79. static void add_files(struct manifest *m, const char *dir)
  80. {
  81. DIR *d;
  82. struct dirent *ent;
  83. char **subs = NULL;
  84. if (dir[0])
  85. d = opendir(dir);
  86. else
  87. d = opendir(".");
  88. if (!d)
  89. err(1, "Opening directory %s", dir[0] ? dir : ".");
  90. while ((ent = readdir(d)) != NULL) {
  91. struct stat st;
  92. struct ccan_file *f;
  93. struct list_head *dest;
  94. bool is_c_src;
  95. if (ent->d_name[0] == '.')
  96. continue;
  97. f = new_ccan_file(m, m->dir,
  98. talloc_asprintf(m, "%s%s",
  99. dir, ent->d_name));
  100. if (lstat(f->name, &st) != 0)
  101. err(1, "lstat %s", f->name);
  102. if (S_ISDIR(st.st_mode)) {
  103. size_t len = talloc_array_length(subs);
  104. subs = talloc_realloc(m, subs, char *, len+1);
  105. subs[len] = talloc_append_string(f->name, "/");
  106. continue;
  107. }
  108. if (!S_ISREG(st.st_mode)) {
  109. talloc_free(f);
  110. continue;
  111. }
  112. if (streq(f->name, "_info")) {
  113. m->info_file = f;
  114. continue;
  115. }
  116. is_c_src = strends(f->name, ".c");
  117. if (!is_c_src && !strends(f->name, ".h")) {
  118. dest = &m->other_files;
  119. } else if (!strchr(f->name, '/')) {
  120. if (is_c_src)
  121. dest = &m->c_files;
  122. else
  123. dest = &m->h_files;
  124. } else if (strstarts(f->name, "test/")) {
  125. if (is_c_src) {
  126. if (strstarts(f->name, "test/api"))
  127. dest = &m->api_tests;
  128. else if (strstarts(f->name, "test/run"))
  129. dest = &m->run_tests;
  130. else if (strstarts(f->name, "test/compile_ok"))
  131. dest = &m->compile_ok_tests;
  132. else if (strstarts(f->name, "test/compile_fail"))
  133. dest = &m->compile_fail_tests;
  134. else
  135. dest = &m->other_test_c_files;
  136. } else
  137. dest = &m->other_test_files;
  138. } else
  139. dest = &m->other_files;
  140. list_add(dest, &f->list);
  141. }
  142. closedir(d);
  143. /* Before we recurse, sanity check this is a ccan module. */
  144. if (!dir[0]) {
  145. size_t i;
  146. if (!m->info_file
  147. && list_empty(&m->c_files)
  148. && list_empty(&m->h_files))
  149. errx(1, "No _info, C or H files found here!");
  150. for (i = 0; i < talloc_array_length(subs); i++)
  151. add_files(m, subs[i]);
  152. }
  153. talloc_free(subs);
  154. }
  155. static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
  156. void *unused)
  157. {
  158. return strcmp((*a)->name, (*b)->name);
  159. }
  160. static void sort_files(struct list_head *list)
  161. {
  162. struct ccan_file **files = NULL, *f;
  163. unsigned int i, num;
  164. num = 0;
  165. while ((f = list_top(list, struct ccan_file, list)) != NULL) {
  166. files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
  167. files[num++] = f;
  168. list_del(&f->list);
  169. }
  170. asort(files, num, cmp_names, NULL);
  171. for (i = 0; i < num; i++)
  172. list_add_tail(list, &files[i]->list);
  173. talloc_free(files);
  174. }
  175. struct manifest *get_manifest(const void *ctx, const char *dir)
  176. {
  177. struct manifest *m;
  178. char *olddir, *canon_dir;
  179. unsigned int len;
  180. struct list_head *list;
  181. if (!manifests)
  182. manifests = htable_manifest_new();
  183. olddir = talloc_getcwd(NULL);
  184. if (!olddir)
  185. err(1, "Getting current directory");
  186. if (chdir(dir) != 0)
  187. err(1, "Failed to chdir to %s", dir);
  188. canon_dir = talloc_getcwd(olddir);
  189. if (!canon_dir)
  190. err(1, "Getting current directory");
  191. m = htable_manifest_get(manifests, canon_dir);
  192. if (m)
  193. goto done;
  194. m = talloc_linked(ctx, talloc(NULL, struct manifest));
  195. m->info_file = NULL;
  196. m->compiled = NULL;
  197. m->dir = talloc_steal(m, canon_dir);
  198. list_head_init(&m->c_files);
  199. list_head_init(&m->h_files);
  200. list_head_init(&m->api_tests);
  201. list_head_init(&m->run_tests);
  202. list_head_init(&m->compile_ok_tests);
  203. list_head_init(&m->compile_fail_tests);
  204. list_head_init(&m->other_test_c_files);
  205. list_head_init(&m->other_test_files);
  206. list_head_init(&m->other_files);
  207. list_head_init(&m->examples);
  208. list_head_init(&m->mangled_examples);
  209. list_head_init(&m->deps);
  210. len = strlen(m->dir);
  211. while (len && m->dir[len-1] == '/')
  212. m->dir[--len] = '\0';
  213. m->basename = strrchr(m->dir, '/');
  214. if (!m->basename)
  215. errx(1, "I don't expect to be run from the root directory");
  216. m->basename++;
  217. /* We expect the ccan dir to be two levels above module dir. */
  218. if (!ccan_dir) {
  219. char *p, *dir;
  220. dir = talloc_strdup(NULL, m->dir);
  221. p = strrchr(dir, '/');
  222. if (!p)
  223. errx(1, "I expect the ccan root directory in ../..");
  224. *p = '\0';
  225. p = strrchr(dir, '/');
  226. if (!p)
  227. errx(1, "I expect the ccan root directory in ../..");
  228. *p = '\0';
  229. ccan_dir = dir;
  230. }
  231. add_files(m, "");
  232. /* Nicer to run tests in a predictable order. */
  233. foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
  234. &m->compile_fail_tests)
  235. sort_files(list);
  236. htable_manifest_add(manifests, m);
  237. done:
  238. if (chdir(olddir) != 0)
  239. err(1, "Returning to original directory '%s'", olddir);
  240. talloc_free(olddir);
  241. return m;
  242. }
  243. /**
  244. * remove_comments - strip comments from a line, return copy.
  245. * @line: line to copy
  246. * @in_comment: are we already within a comment (from prev line).
  247. * @unterminated: are we still in a comment for next line.
  248. */
  249. static char *remove_comments(const char *line, bool in_comment,
  250. bool *unterminated)
  251. {
  252. char *p, *ret = talloc_array(line, char, strlen(line) + 1);
  253. p = ret;
  254. for (;;) {
  255. if (!in_comment) {
  256. /* Find first comment. */
  257. const char *old_comment = strstr(line, "/*");
  258. const char *new_comment = strstr(line, "//");
  259. const char *comment;
  260. if (new_comment && old_comment)
  261. comment = new_comment < old_comment
  262. ? new_comment : old_comment;
  263. else if (old_comment)
  264. comment = old_comment;
  265. else if (new_comment)
  266. comment = new_comment;
  267. else {
  268. /* Nothing more. */
  269. strcpy(p, line);
  270. *unterminated = false;
  271. break;
  272. }
  273. /* Copy up to comment. */
  274. memcpy(p, line, comment - line);
  275. p += comment - line;
  276. line += comment - line + 2;
  277. if (comment == new_comment) {
  278. /* We're done: goes to EOL. */
  279. p[0] = '\0';
  280. *unterminated = false;
  281. break;
  282. }
  283. in_comment = true;
  284. }
  285. if (in_comment) {
  286. const char *end = strstr(line, "*/");
  287. if (!end) {
  288. *unterminated = true;
  289. p[0] = '\0';
  290. break;
  291. }
  292. line = end+2;
  293. in_comment = false;
  294. }
  295. }
  296. return ret;
  297. }
  298. static bool is_empty(const char *line)
  299. {
  300. return strspn(line, " \t") == strlen(line);
  301. }
  302. static bool continues(const char *line)
  303. {
  304. /* Technically, any odd number of these. But who cares? */
  305. return strends(line, "\\");
  306. }
  307. /* Get token if it's equal to token. */
  308. bool get_token(const char **line, const char *token)
  309. {
  310. unsigned int toklen;
  311. *line += strspn(*line, " \t");
  312. if (cisalnum(token[0]) || token[0] == '_')
  313. toklen = strspn(*line, IDENT_CHARS);
  314. else {
  315. /* FIXME: real tokenizer handles ++ and other multi-chars. */
  316. toklen = strlen(token);
  317. }
  318. if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
  319. *line += toklen;
  320. return true;
  321. }
  322. return false;
  323. }
  324. char *get_symbol_token(void *ctx, const char **line)
  325. {
  326. unsigned int toklen;
  327. char *ret;
  328. *line += strspn(*line, " \t");
  329. toklen = strspn(*line, IDENT_CHARS);
  330. if (!toklen)
  331. return NULL;
  332. ret = talloc_strndup(ctx, *line, toklen);
  333. *line += toklen;
  334. return ret;
  335. }
  336. static bool parse_hash_if(struct pp_conditions *cond, const char **line)
  337. {
  338. bool brackets, defined;
  339. cond->inverse = get_token(line, "!");
  340. defined = get_token(line, "defined");
  341. brackets = get_token(line, "(");
  342. cond->symbol = get_symbol_token(cond, line);
  343. if (!cond->symbol)
  344. return false;
  345. if (brackets && !get_token(line, ")"))
  346. return false;
  347. if (!defined)
  348. cond->type = PP_COND_IF;
  349. /* FIXME: We just chain them, ignoring operators. */
  350. if (get_token(line, "||") || get_token(line, "&&")) {
  351. struct pp_conditions *sub = talloc(cond, struct pp_conditions);
  352. sub->parent = cond->parent;
  353. sub->type = PP_COND_IFDEF;
  354. if (parse_hash_if(sub, line))
  355. cond->parent = sub;
  356. }
  357. return true;
  358. }
  359. /* FIXME: Get serious! */
  360. static struct pp_conditions *analyze_directive(struct ccan_file *f,
  361. const char *line,
  362. struct pp_conditions *parent)
  363. {
  364. struct pp_conditions *cond = talloc(f, struct pp_conditions);
  365. bool unused;
  366. line = remove_comments(line, false, &unused);
  367. cond->parent = parent;
  368. cond->type = PP_COND_IFDEF;
  369. if (!get_token(&line, "#"))
  370. abort();
  371. if (get_token(&line, "if")) {
  372. if (!parse_hash_if(cond, &line))
  373. goto unknown;
  374. } else if (get_token(&line, "elif")) {
  375. /* Malformed? */
  376. if (!parent)
  377. return NULL;
  378. cond->parent = parent->parent;
  379. /* FIXME: Not quite true. This implies !parent, but we don't
  380. * do multiple conditionals yet. */
  381. if (!parse_hash_if(cond, &line))
  382. goto unknown;
  383. } else if (get_token(&line, "ifdef")) {
  384. bool brackets;
  385. cond->inverse = false;
  386. brackets = get_token(&line, "(");
  387. cond->symbol = get_symbol_token(cond, &line);
  388. if (!cond->symbol)
  389. goto unknown;
  390. if (brackets && !get_token(&line, ")"))
  391. goto unknown;
  392. } else if (get_token(&line, "ifndef")) {
  393. bool brackets;
  394. cond->inverse = true;
  395. brackets = get_token(&line, "(");
  396. cond->symbol = get_symbol_token(cond, &line);
  397. if (!cond->symbol)
  398. goto unknown;
  399. if (brackets && !get_token(&line, ")"))
  400. goto unknown;
  401. } else if (get_token(&line, "else")) {
  402. /* Malformed? */
  403. if (!parent)
  404. return NULL;
  405. *cond = *parent;
  406. cond->inverse = !cond->inverse;
  407. return cond;
  408. } else if (get_token(&line, "endif")) {
  409. talloc_free(cond);
  410. /* Malformed? */
  411. if (!parent)
  412. return NULL;
  413. /* Back up one! */
  414. return parent->parent;
  415. } else {
  416. /* Not a conditional. */
  417. talloc_free(cond);
  418. return parent;
  419. }
  420. if (!is_empty(line))
  421. goto unknown;
  422. return cond;
  423. unknown:
  424. cond->type = PP_COND_UNKNOWN;
  425. return cond;
  426. }
  427. /* This parser is rough, but OK if code is reasonably neat. */
  428. struct line_info *get_ccan_line_info(struct ccan_file *f)
  429. {
  430. bool continued = false, in_comment = false;
  431. struct pp_conditions *cond = NULL;
  432. unsigned int i;
  433. if (f->line_info)
  434. return f->line_info;
  435. get_ccan_file_lines(f);
  436. f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
  437. for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
  438. char *p;
  439. bool still_doc_line;
  440. /* Current conditions apply to this line. */
  441. f->line_info[i].cond = cond;
  442. f->line_info[i].continued = continued;
  443. if (continued) {
  444. /* Same as last line. */
  445. f->line_info[i].type = f->line_info[i-1].type;
  446. /* Update in_comment. */
  447. remove_comments(f->lines[i], in_comment, &in_comment);
  448. continue;
  449. }
  450. /* Preprocessor directive? */
  451. if (!in_comment
  452. && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
  453. f->line_info[i].type = PREPROC_LINE;
  454. cond = analyze_directive(f, f->lines[i], cond);
  455. continue;
  456. }
  457. still_doc_line = (in_comment
  458. && f->line_info[i-1].type == DOC_LINE);
  459. p = remove_comments(f->lines[i], in_comment, &in_comment);
  460. if (is_empty(p)) {
  461. if (strstarts(f->lines[i], "/**") || still_doc_line)
  462. f->line_info[i].type = DOC_LINE;
  463. else
  464. f->line_info[i].type = COMMENT_LINE;
  465. } else
  466. f->line_info[i].type = CODE_LINE;
  467. talloc_free(p);
  468. }
  469. return f->line_info;
  470. }
  471. struct symbol {
  472. struct list_node list;
  473. const char *name;
  474. const unsigned int *value;
  475. };
  476. static struct symbol *find_symbol(struct list_head *syms, const char *sym)
  477. {
  478. struct symbol *i;
  479. list_for_each(syms, i, list)
  480. if (streq(sym, i->name))
  481. return i;
  482. return NULL;
  483. }
  484. static enum line_compiled get_pp(struct pp_conditions *cond,
  485. struct list_head *syms)
  486. {
  487. struct symbol *sym;
  488. unsigned int val;
  489. enum line_compiled parent, ret;
  490. /* No conditions? Easy. */
  491. if (!cond)
  492. return COMPILED;
  493. /* Check we get here at all. */
  494. parent = get_pp(cond->parent, syms);
  495. if (parent == NOT_COMPILED)
  496. return NOT_COMPILED;
  497. if (cond->type == PP_COND_UNKNOWN)
  498. return MAYBE_COMPILED;
  499. sym = find_symbol(syms, cond->symbol);
  500. if (!sym)
  501. return MAYBE_COMPILED;
  502. switch (cond->type) {
  503. case PP_COND_IF:
  504. /* Undefined is 0. */
  505. val = sym->value ? *sym->value : 0;
  506. if (!val == cond->inverse)
  507. ret = COMPILED;
  508. else
  509. ret = NOT_COMPILED;
  510. break;
  511. case PP_COND_IFDEF:
  512. if (cond->inverse == !sym->value)
  513. ret = COMPILED;
  514. else
  515. ret = NOT_COMPILED;
  516. break;
  517. default:
  518. abort();
  519. }
  520. /* If parent didn't know, NO == NO, but YES == MAYBE. */
  521. if (parent == MAYBE_COMPILED && ret == COMPILED)
  522. ret = MAYBE_COMPILED;
  523. return ret;
  524. }
  525. static void add_symbol(struct list_head *head,
  526. const char *symbol, const unsigned int *value)
  527. {
  528. struct symbol *sym = talloc(head, struct symbol);
  529. sym->name = symbol;
  530. sym->value = value;
  531. list_add(head, &sym->list);
  532. }
  533. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  534. const char *symbol,
  535. const unsigned int *value,
  536. ...)
  537. {
  538. enum line_compiled ret;
  539. struct list_head *head;
  540. va_list ap;
  541. head = talloc(NULL, struct list_head);
  542. list_head_init(head);
  543. va_start(ap, value);
  544. add_symbol(head, symbol, value);
  545. while ((symbol = va_arg(ap, const char *)) != NULL) {
  546. value = va_arg(ap, const unsigned int *);
  547. add_symbol(head, symbol, value);
  548. }
  549. ret = get_pp(cond, head);
  550. talloc_free(head);
  551. return ret;
  552. }
  553. void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
  554. const char *errorfmt, ...)
  555. {
  556. va_list ap;
  557. struct file_error *fe = talloc(score, struct file_error);
  558. fe->file = f;
  559. fe->line = line;
  560. list_add_tail(&score->per_file_errors, &fe->list);
  561. if (!score->error)
  562. score->error = talloc_strdup(score, "");
  563. if (verbose < 2 && strcount(score->error, "\n") > 5)
  564. return;
  565. if (line)
  566. score->error = talloc_asprintf_append(score->error,
  567. "%s:%u:",
  568. f->fullname, line);
  569. else
  570. score->error = talloc_asprintf_append(score->error,
  571. "%s:", f->fullname);
  572. va_start(ap, errorfmt);
  573. score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
  574. va_end(ap);
  575. score->error = talloc_append_string(score->error, "\n");
  576. if (verbose < 2 && strcount(score->error, "\n") > 5)
  577. score->error = talloc_append_string(score->error,
  578. "... more (use -vv to see them all)\n");
  579. }