file_analysis.c 16 KB

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