file_analysis.c 12 KB

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