file_analysis.c 12 KB

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