file_analysis.c 13 KB

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