file_analysis.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. continue;
  119. }
  120. 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 = 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;
  221. ccan_dir = talloc_strdup(NULL, m->dir);
  222. p = strrchr(ccan_dir, '/');
  223. if (!p)
  224. errx(1, "I expect the ccan root directory in ../..");
  225. *p = '\0';
  226. p = strrchr(ccan_dir, '/');
  227. if (!p)
  228. errx(1, "I expect the ccan root directory in ../..");
  229. *p = '\0';
  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 (isalnum(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. return true;
  350. }
  351. /* FIXME: Get serious! */
  352. static struct pp_conditions *analyze_directive(struct ccan_file *f,
  353. const char *line,
  354. struct pp_conditions *parent)
  355. {
  356. struct pp_conditions *cond = talloc(f, struct pp_conditions);
  357. bool unused;
  358. line = remove_comments(line, false, &unused);
  359. cond->parent = parent;
  360. cond->type = PP_COND_IFDEF;
  361. if (!get_token(&line, "#"))
  362. abort();
  363. if (get_token(&line, "if")) {
  364. if (!parse_hash_if(cond, &line))
  365. goto unknown;
  366. } else if (get_token(&line, "elif")) {
  367. /* Malformed? */
  368. if (!parent)
  369. return NULL;
  370. cond->parent = parent->parent;
  371. /* FIXME: Not quite true. This implies !parent, but we don't
  372. * do multiple conditionals yet. */
  373. if (!parse_hash_if(cond, &line))
  374. goto unknown;
  375. } else if (get_token(&line, "ifdef")) {
  376. bool brackets;
  377. cond->inverse = false;
  378. brackets = get_token(&line, "(");
  379. cond->symbol = get_symbol_token(cond, &line);
  380. if (!cond->symbol)
  381. goto unknown;
  382. if (brackets && !get_token(&line, ")"))
  383. goto unknown;
  384. } else if (get_token(&line, "ifndef")) {
  385. bool brackets;
  386. cond->inverse = true;
  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, "else")) {
  394. /* Malformed? */
  395. if (!parent)
  396. return NULL;
  397. *cond = *parent;
  398. cond->inverse = !cond->inverse;
  399. return cond;
  400. } else if (get_token(&line, "endif")) {
  401. talloc_free(cond);
  402. /* Malformed? */
  403. if (!parent)
  404. return NULL;
  405. /* Back up one! */
  406. return parent->parent;
  407. } else {
  408. /* Not a conditional. */
  409. talloc_free(cond);
  410. return parent;
  411. }
  412. if (!is_empty(line))
  413. goto unknown;
  414. return cond;
  415. unknown:
  416. cond->type = PP_COND_UNKNOWN;
  417. return cond;
  418. }
  419. /* This parser is rough, but OK if code is reasonably neat. */
  420. struct line_info *get_ccan_line_info(struct ccan_file *f)
  421. {
  422. bool continued = false, in_comment = false;
  423. struct pp_conditions *cond = NULL;
  424. unsigned int i;
  425. if (f->line_info)
  426. return f->line_info;
  427. get_ccan_file_lines(f);
  428. f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
  429. for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
  430. char *p;
  431. bool still_doc_line;
  432. /* Current conditions apply to this line. */
  433. f->line_info[i].cond = cond;
  434. f->line_info[i].continued = continued;
  435. if (continued) {
  436. /* Same as last line. */
  437. f->line_info[i].type = f->line_info[i-1].type;
  438. /* Update in_comment. */
  439. remove_comments(f->lines[i], in_comment, &in_comment);
  440. continue;
  441. }
  442. /* Preprocessor directive? */
  443. if (!in_comment
  444. && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
  445. f->line_info[i].type = PREPROC_LINE;
  446. cond = analyze_directive(f, f->lines[i], cond);
  447. continue;
  448. }
  449. still_doc_line = (in_comment
  450. && f->line_info[i-1].type == DOC_LINE);
  451. p = remove_comments(f->lines[i], in_comment, &in_comment);
  452. if (is_empty(p)) {
  453. if (strstarts(f->lines[i], "/**") || still_doc_line)
  454. f->line_info[i].type = DOC_LINE;
  455. else
  456. f->line_info[i].type = COMMENT_LINE;
  457. } else
  458. f->line_info[i].type = CODE_LINE;
  459. talloc_free(p);
  460. }
  461. return f->line_info;
  462. }
  463. struct symbol {
  464. struct list_node list;
  465. const char *name;
  466. const unsigned int *value;
  467. };
  468. static struct symbol *find_symbol(struct list_head *syms, const char *sym)
  469. {
  470. struct symbol *i;
  471. list_for_each(syms, i, list)
  472. if (streq(sym, i->name))
  473. return i;
  474. return NULL;
  475. }
  476. static enum line_compiled get_pp(struct pp_conditions *cond,
  477. struct list_head *syms)
  478. {
  479. struct symbol *sym;
  480. unsigned int val;
  481. enum line_compiled parent, ret;
  482. /* No conditions? Easy. */
  483. if (!cond)
  484. return COMPILED;
  485. /* Check we get here at all. */
  486. parent = get_pp(cond->parent, syms);
  487. if (parent == NOT_COMPILED)
  488. return NOT_COMPILED;
  489. if (cond->type == PP_COND_UNKNOWN)
  490. return MAYBE_COMPILED;
  491. sym = find_symbol(syms, cond->symbol);
  492. if (!sym)
  493. return MAYBE_COMPILED;
  494. switch (cond->type) {
  495. case PP_COND_IF:
  496. /* Undefined is 0. */
  497. val = sym->value ? *sym->value : 0;
  498. if (!val == cond->inverse)
  499. ret = COMPILED;
  500. else
  501. ret = NOT_COMPILED;
  502. break;
  503. case PP_COND_IFDEF:
  504. if (cond->inverse == !sym->value)
  505. ret = COMPILED;
  506. else
  507. ret = NOT_COMPILED;
  508. break;
  509. default:
  510. abort();
  511. }
  512. /* If parent didn't know, NO == NO, but YES == MAYBE. */
  513. if (parent == MAYBE_COMPILED && ret == COMPILED)
  514. ret = MAYBE_COMPILED;
  515. return ret;
  516. }
  517. static void add_symbol(struct list_head *head,
  518. const char *symbol, const unsigned int *value)
  519. {
  520. struct symbol *sym = talloc(head, struct symbol);
  521. sym->name = symbol;
  522. sym->value = value;
  523. list_add(head, &sym->list);
  524. }
  525. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  526. const char *symbol,
  527. const unsigned int *value,
  528. ...)
  529. {
  530. enum line_compiled ret;
  531. struct list_head *head;
  532. va_list ap;
  533. head = talloc(NULL, struct list_head);
  534. list_head_init(head);
  535. va_start(ap, value);
  536. add_symbol(head, symbol, value);
  537. while ((symbol = va_arg(ap, const char *)) != NULL) {
  538. value = va_arg(ap, const unsigned int *);
  539. add_symbol(head, symbol, value);
  540. }
  541. ret = get_pp(cond, head);
  542. talloc_free(head);
  543. return ret;
  544. }
  545. void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
  546. const char *errorfmt, ...)
  547. {
  548. va_list ap;
  549. struct file_error *fe = talloc(score, struct file_error);
  550. fe->file = f;
  551. fe->line = line;
  552. list_add_tail(&score->per_file_errors, &fe->list);
  553. if (!score->error)
  554. score->error = talloc_strdup(score, "");
  555. if (verbose < 2 && strcount(score->error, "\n") > 5)
  556. return;
  557. if (line)
  558. score->error = talloc_asprintf_append(score->error,
  559. "%s:%u:",
  560. f->fullname, line);
  561. else
  562. score->error = talloc_asprintf_append(score->error,
  563. "%s:", f->fullname);
  564. va_start(ap, errorfmt);
  565. score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
  566. va_end(ap);
  567. score->error = talloc_append_string(score->error, "\n");
  568. if (verbose < 2 && strcount(score->error, "\n") > 5)
  569. score->error = talloc_append_string(score->error,
  570. "... more (use -vv to see them all)\n");
  571. }