file_analysis.c 15 KB

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