file_analysis.c 13 KB

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