file_analysis.c 12 KB

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