file_analysis.c 12 KB

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