file_analysis.c 15 KB

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