file_analysis.c 16 KB

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