file_analysis.c 12 KB

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