file_analysis.c 16 KB

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