file_analysis.c 12 KB

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