file_analysis.c 13 KB

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