file_analysis.c 15 KB

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