file_analysis.c 12 KB

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