file_analysis.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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, *dir;
  219. dir = talloc_strdup(NULL, m->dir);
  220. p = strrchr(dir, '/');
  221. if (!p)
  222. errx(1, "I expect the ccan root directory in ../..");
  223. *p = '\0';
  224. p = strrchr(dir, '/');
  225. if (!p)
  226. errx(1, "I expect the ccan root directory in ../..");
  227. *p = '\0';
  228. ccan_dir = dir;
  229. }
  230. add_files(m, "");
  231. /* Nicer to run tests in a predictable order. */
  232. foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
  233. &m->compile_fail_tests)
  234. sort_files(list);
  235. htable_manifest_add(manifests, m);
  236. done:
  237. if (chdir(olddir) != 0)
  238. err(1, "Returning to original directory '%s'", olddir);
  239. talloc_free(olddir);
  240. return m;
  241. }
  242. /**
  243. * remove_comments - strip comments from a line, return copy.
  244. * @line: line to copy
  245. * @in_comment: are we already within a comment (from prev line).
  246. * @unterminated: are we still in a comment for next line.
  247. */
  248. static char *remove_comments(const char *line, bool in_comment,
  249. bool *unterminated)
  250. {
  251. char *p, *ret = talloc_array(line, char, strlen(line) + 1);
  252. p = ret;
  253. for (;;) {
  254. if (!in_comment) {
  255. /* Find first comment. */
  256. const char *old_comment = strstr(line, "/*");
  257. const char *new_comment = strstr(line, "//");
  258. const char *comment;
  259. if (new_comment && old_comment)
  260. comment = new_comment < old_comment
  261. ? new_comment : old_comment;
  262. else if (old_comment)
  263. comment = old_comment;
  264. else if (new_comment)
  265. comment = new_comment;
  266. else {
  267. /* Nothing more. */
  268. strcpy(p, line);
  269. *unterminated = false;
  270. break;
  271. }
  272. /* Copy up to comment. */
  273. memcpy(p, line, comment - line);
  274. p += comment - line;
  275. line += comment - line + 2;
  276. if (comment == new_comment) {
  277. /* We're done: goes to EOL. */
  278. p[0] = '\0';
  279. *unterminated = false;
  280. break;
  281. }
  282. in_comment = true;
  283. }
  284. if (in_comment) {
  285. const char *end = strstr(line, "*/");
  286. if (!end) {
  287. *unterminated = true;
  288. p[0] = '\0';
  289. break;
  290. }
  291. line = end+2;
  292. in_comment = false;
  293. }
  294. }
  295. return ret;
  296. }
  297. static bool is_empty(const char *line)
  298. {
  299. return strspn(line, " \t") == strlen(line);
  300. }
  301. static bool continues(const char *line)
  302. {
  303. /* Technically, any odd number of these. But who cares? */
  304. return strends(line, "\\");
  305. }
  306. /* Get token if it's equal to token. */
  307. bool get_token(const char **line, const char *token)
  308. {
  309. unsigned int toklen;
  310. *line += strspn(*line, " \t");
  311. if (cisalnum(token[0]) || token[0] == '_')
  312. toklen = strspn(*line, IDENT_CHARS);
  313. else {
  314. /* FIXME: real tokenizer handles ++ and other multi-chars. */
  315. toklen = strlen(token);
  316. }
  317. if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
  318. *line += toklen;
  319. return true;
  320. }
  321. return false;
  322. }
  323. char *get_symbol_token(void *ctx, const char **line)
  324. {
  325. unsigned int toklen;
  326. char *ret;
  327. *line += strspn(*line, " \t");
  328. toklen = strspn(*line, IDENT_CHARS);
  329. if (!toklen)
  330. return NULL;
  331. ret = talloc_strndup(ctx, *line, toklen);
  332. *line += toklen;
  333. return ret;
  334. }
  335. static bool parse_hash_if(struct pp_conditions *cond, const char **line)
  336. {
  337. bool brackets, defined;
  338. cond->inverse = get_token(line, "!");
  339. defined = get_token(line, "defined");
  340. brackets = get_token(line, "(");
  341. cond->symbol = get_symbol_token(cond, line);
  342. if (!cond->symbol)
  343. return false;
  344. if (brackets && !get_token(line, ")"))
  345. return false;
  346. if (!defined)
  347. cond->type = PP_COND_IF;
  348. /* FIXME: We just chain them, ignoring operators. */
  349. if (get_token(line, "||") || get_token(line, "&&")) {
  350. struct pp_conditions *sub = talloc(cond, struct pp_conditions);
  351. sub->parent = cond->parent;
  352. sub->type = PP_COND_IFDEF;
  353. if (parse_hash_if(sub, line))
  354. cond->parent = sub;
  355. }
  356. return true;
  357. }
  358. /* FIXME: Get serious! */
  359. static struct pp_conditions *analyze_directive(struct ccan_file *f,
  360. const char *line,
  361. struct pp_conditions *parent)
  362. {
  363. struct pp_conditions *cond = talloc(f, struct pp_conditions);
  364. bool unused;
  365. line = remove_comments(line, false, &unused);
  366. cond->parent = parent;
  367. cond->type = PP_COND_IFDEF;
  368. if (!get_token(&line, "#"))
  369. abort();
  370. if (get_token(&line, "if")) {
  371. if (!parse_hash_if(cond, &line))
  372. goto unknown;
  373. } else if (get_token(&line, "elif")) {
  374. /* Malformed? */
  375. if (!parent)
  376. return NULL;
  377. cond->parent = parent->parent;
  378. /* FIXME: Not quite true. This implies !parent, but we don't
  379. * do multiple conditionals yet. */
  380. if (!parse_hash_if(cond, &line))
  381. goto unknown;
  382. } else if (get_token(&line, "ifdef")) {
  383. bool brackets;
  384. cond->inverse = false;
  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, "ifndef")) {
  392. bool brackets;
  393. cond->inverse = true;
  394. brackets = get_token(&line, "(");
  395. cond->symbol = get_symbol_token(cond, &line);
  396. if (!cond->symbol)
  397. goto unknown;
  398. if (brackets && !get_token(&line, ")"))
  399. goto unknown;
  400. } else if (get_token(&line, "else")) {
  401. /* Malformed? */
  402. if (!parent)
  403. return NULL;
  404. *cond = *parent;
  405. cond->inverse = !cond->inverse;
  406. return cond;
  407. } else if (get_token(&line, "endif")) {
  408. talloc_free(cond);
  409. /* Malformed? */
  410. if (!parent)
  411. return NULL;
  412. /* Back up one! */
  413. return parent->parent;
  414. } else {
  415. /* Not a conditional. */
  416. talloc_free(cond);
  417. return parent;
  418. }
  419. if (!is_empty(line))
  420. goto unknown;
  421. return cond;
  422. unknown:
  423. cond->type = PP_COND_UNKNOWN;
  424. return cond;
  425. }
  426. /* This parser is rough, but OK if code is reasonably neat. */
  427. struct line_info *get_ccan_line_info(struct ccan_file *f)
  428. {
  429. bool continued = false, in_comment = false;
  430. struct pp_conditions *cond = NULL;
  431. unsigned int i;
  432. if (f->line_info)
  433. return f->line_info;
  434. get_ccan_file_lines(f);
  435. f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
  436. for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
  437. char *p;
  438. bool still_doc_line;
  439. /* Current conditions apply to this line. */
  440. f->line_info[i].cond = cond;
  441. f->line_info[i].continued = continued;
  442. if (continued) {
  443. /* Same as last line. */
  444. f->line_info[i].type = f->line_info[i-1].type;
  445. /* Update in_comment. */
  446. remove_comments(f->lines[i], in_comment, &in_comment);
  447. continue;
  448. }
  449. /* Preprocessor directive? */
  450. if (!in_comment
  451. && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
  452. f->line_info[i].type = PREPROC_LINE;
  453. cond = analyze_directive(f, f->lines[i], cond);
  454. continue;
  455. }
  456. still_doc_line = (in_comment
  457. && f->line_info[i-1].type == DOC_LINE);
  458. p = remove_comments(f->lines[i], in_comment, &in_comment);
  459. if (is_empty(p)) {
  460. if (strstarts(f->lines[i], "/**") || still_doc_line)
  461. f->line_info[i].type = DOC_LINE;
  462. else
  463. f->line_info[i].type = COMMENT_LINE;
  464. } else
  465. f->line_info[i].type = CODE_LINE;
  466. talloc_free(p);
  467. }
  468. return f->line_info;
  469. }
  470. struct symbol {
  471. struct list_node list;
  472. const char *name;
  473. const unsigned int *value;
  474. };
  475. static struct symbol *find_symbol(struct list_head *syms, const char *sym)
  476. {
  477. struct symbol *i;
  478. list_for_each(syms, i, list)
  479. if (streq(sym, i->name))
  480. return i;
  481. return NULL;
  482. }
  483. static enum line_compiled get_pp(struct pp_conditions *cond,
  484. struct list_head *syms)
  485. {
  486. struct symbol *sym;
  487. unsigned int val;
  488. enum line_compiled parent, ret;
  489. /* No conditions? Easy. */
  490. if (!cond)
  491. return COMPILED;
  492. /* Check we get here at all. */
  493. parent = get_pp(cond->parent, syms);
  494. if (parent == NOT_COMPILED)
  495. return NOT_COMPILED;
  496. if (cond->type == PP_COND_UNKNOWN)
  497. return MAYBE_COMPILED;
  498. sym = find_symbol(syms, cond->symbol);
  499. if (!sym)
  500. return MAYBE_COMPILED;
  501. switch (cond->type) {
  502. case PP_COND_IF:
  503. /* Undefined is 0. */
  504. val = sym->value ? *sym->value : 0;
  505. if (!val == cond->inverse)
  506. ret = COMPILED;
  507. else
  508. ret = NOT_COMPILED;
  509. break;
  510. case PP_COND_IFDEF:
  511. if (cond->inverse == !sym->value)
  512. ret = COMPILED;
  513. else
  514. ret = NOT_COMPILED;
  515. break;
  516. default:
  517. abort();
  518. }
  519. /* If parent didn't know, NO == NO, but YES == MAYBE. */
  520. if (parent == MAYBE_COMPILED && ret == COMPILED)
  521. ret = MAYBE_COMPILED;
  522. return ret;
  523. }
  524. static void add_symbol(struct list_head *head,
  525. const char *symbol, const unsigned int *value)
  526. {
  527. struct symbol *sym = talloc(head, struct symbol);
  528. sym->name = symbol;
  529. sym->value = value;
  530. list_add(head, &sym->list);
  531. }
  532. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  533. const char *symbol,
  534. const unsigned int *value,
  535. ...)
  536. {
  537. enum line_compiled ret;
  538. struct list_head *head;
  539. va_list ap;
  540. head = talloc(NULL, struct list_head);
  541. list_head_init(head);
  542. va_start(ap, value);
  543. add_symbol(head, symbol, value);
  544. while ((symbol = va_arg(ap, const char *)) != NULL) {
  545. value = va_arg(ap, const unsigned int *);
  546. add_symbol(head, symbol, value);
  547. }
  548. ret = get_pp(cond, head);
  549. talloc_free(head);
  550. return ret;
  551. }
  552. void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
  553. const char *errorfmt, ...)
  554. {
  555. va_list ap;
  556. struct file_error *fe = talloc(score, struct file_error);
  557. fe->file = f;
  558. fe->line = line;
  559. list_add_tail(&score->per_file_errors, &fe->list);
  560. if (!score->error)
  561. score->error = talloc_strdup(score, "");
  562. if (verbose < 2 && strcount(score->error, "\n") > 5)
  563. return;
  564. if (line)
  565. score->error = talloc_asprintf_append(score->error,
  566. "%s:%u:",
  567. f->fullname, line);
  568. else
  569. score->error = talloc_asprintf_append(score->error,
  570. "%s:", f->fullname);
  571. va_start(ap, errorfmt);
  572. score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
  573. va_end(ap);
  574. score->error = talloc_append_string(score->error, "\n");
  575. if (verbose < 2 && strcount(score->error, "\n") > 5)
  576. score->error = talloc_append_string(score->error,
  577. "... more (use -vv to see them all)\n");
  578. }