file_analysis.c 12 KB

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