file_analysis.c 12 KB

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