file_analysis.c 15 KB

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