file_analysis.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "config.h"
  2. #include "ccanlint.h"
  3. #include <ccan/str/str.h>
  4. #include <ccan/take/take.h>
  5. #include <ccan/hash/hash.h>
  6. #include <ccan/htable/htable_type.h>
  7. #include <ccan/noerr/noerr.h>
  8. #include <ccan/foreach/foreach.h>
  9. #include <ccan/asort/asort.h>
  10. #include <ccan/array_size/array_size.h>
  11. #include "../tools.h"
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <err.h>
  17. #include <errno.h>
  18. #include <dirent.h>
  19. #include <ctype.h>
  20. #include <stdarg.h>
  21. #include <assert.h>
  22. struct list_head *get_ccan_file_docs(struct ccan_file *f)
  23. {
  24. if (!f->doc_sections) {
  25. get_ccan_file_lines(f);
  26. f->doc_sections = extract_doc_sections(f->lines, f->name);
  27. }
  28. return f->doc_sections;
  29. }
  30. /**
  31. * remove_comments - strip comments from a line, return copy.
  32. * @line: line to copy
  33. * @in_comment: are we already within a comment (from prev line).
  34. * @unterminated: are we still in a comment for next line.
  35. */
  36. static char *remove_comments(const tal_t *ctx,
  37. const char *line, bool in_comment,
  38. bool *unterminated)
  39. {
  40. char *p, *ret = tal_arr(ctx, char, strlen(line) + 1);
  41. p = ret;
  42. for (;;) {
  43. if (!in_comment) {
  44. /* Find first comment. */
  45. const char *old_comment = strstr(line, "/*");
  46. const char *new_comment = strstr(line, "//");
  47. const char *comment;
  48. if (new_comment && old_comment)
  49. comment = new_comment < old_comment
  50. ? new_comment : old_comment;
  51. else if (old_comment)
  52. comment = old_comment;
  53. else if (new_comment)
  54. comment = new_comment;
  55. else {
  56. /* Nothing more. */
  57. strcpy(p, line);
  58. *unterminated = false;
  59. break;
  60. }
  61. /* Copy up to comment. */
  62. memcpy(p, line, comment - line);
  63. p += comment - line;
  64. line += comment - line + 2;
  65. if (comment == new_comment) {
  66. /* We're done: goes to EOL. */
  67. p[0] = '\0';
  68. *unterminated = false;
  69. break;
  70. }
  71. in_comment = true;
  72. }
  73. if (in_comment) {
  74. const char *end = strstr(line, "*/");
  75. if (!end) {
  76. *unterminated = true;
  77. p[0] = '\0';
  78. break;
  79. }
  80. line = end+2;
  81. in_comment = false;
  82. }
  83. }
  84. return ret;
  85. }
  86. static bool is_empty(const char *line)
  87. {
  88. return strspn(line, " \r\t") == strlen(line);
  89. }
  90. static bool continues(const char *line)
  91. {
  92. /* Technically, any odd number of these. But who cares? */
  93. return strends(line, "\\");
  94. }
  95. static bool parse_hash_if(struct pp_conditions *cond, const char **line)
  96. {
  97. bool brackets, defined;
  98. cond->inverse = get_token(line, "!");
  99. defined = get_token(line, "defined");
  100. brackets = get_token(line, "(");
  101. cond->symbol = get_symbol_token(cond, line);
  102. if (!cond->symbol)
  103. return false;
  104. if (brackets && !get_token(line, ")"))
  105. return false;
  106. if (!defined)
  107. cond->type = PP_COND_IF;
  108. /* FIXME: We just chain them, ignoring operators. */
  109. if (get_token(line, "||") || get_token(line, "&&")) {
  110. struct pp_conditions *sub = tal(cond, struct pp_conditions);
  111. sub->parent = cond->parent;
  112. sub->type = PP_COND_IFDEF;
  113. if (parse_hash_if(sub, line))
  114. cond->parent = sub;
  115. }
  116. return true;
  117. }
  118. /* FIXME: Get serious! */
  119. static struct pp_conditions *analyze_directive(struct ccan_file *f,
  120. const char *line,
  121. struct pp_conditions *parent)
  122. {
  123. struct pp_conditions *cond = tal(f, struct pp_conditions);
  124. bool unused;
  125. line = remove_comments(f, line, false, &unused);
  126. cond->parent = parent;
  127. cond->type = PP_COND_IFDEF;
  128. if (!get_token(&line, "#"))
  129. abort();
  130. if (get_token(&line, "if")) {
  131. if (!parse_hash_if(cond, &line))
  132. goto unknown;
  133. } else if (get_token(&line, "elif")) {
  134. /* Malformed? */
  135. if (!parent)
  136. return NULL;
  137. cond->parent = parent->parent;
  138. /* FIXME: Not quite true. This implies !parent, but we don't
  139. * do multiple conditionals yet. */
  140. if (!parse_hash_if(cond, &line))
  141. goto unknown;
  142. } else if (get_token(&line, "ifdef")) {
  143. bool brackets;
  144. cond->inverse = false;
  145. brackets = get_token(&line, "(");
  146. cond->symbol = get_symbol_token(cond, &line);
  147. if (!cond->symbol)
  148. goto unknown;
  149. if (brackets && !get_token(&line, ")"))
  150. goto unknown;
  151. } else if (get_token(&line, "ifndef")) {
  152. bool brackets;
  153. cond->inverse = true;
  154. brackets = get_token(&line, "(");
  155. cond->symbol = get_symbol_token(cond, &line);
  156. if (!cond->symbol)
  157. goto unknown;
  158. if (brackets && !get_token(&line, ")"))
  159. goto unknown;
  160. } else if (get_token(&line, "else")) {
  161. /* Malformed? */
  162. if (!parent)
  163. return NULL;
  164. *cond = *parent;
  165. cond->inverse = !cond->inverse;
  166. return cond;
  167. } else if (get_token(&line, "endif")) {
  168. tal_free(cond);
  169. /* Malformed? */
  170. if (!parent)
  171. return NULL;
  172. /* Back up one! */
  173. return parent->parent;
  174. } else {
  175. /* Not a conditional. */
  176. tal_free(cond);
  177. return parent;
  178. }
  179. if (!is_empty(line))
  180. goto unknown;
  181. return cond;
  182. unknown:
  183. cond->type = PP_COND_UNKNOWN;
  184. return cond;
  185. }
  186. /* This parser is rough, but OK if code is reasonably neat. */
  187. struct line_info *get_ccan_line_info(struct ccan_file *f)
  188. {
  189. bool continued = false, in_comment = false;
  190. struct pp_conditions *cond = NULL;
  191. unsigned int i;
  192. if (f->line_info)
  193. return f->line_info;
  194. get_ccan_file_lines(f);
  195. f->line_info = tal_arr(f->lines, struct line_info,
  196. tal_count(f->lines)-1);
  197. for (i = 0; f->lines[i]; continued = continues(f->lines[i++])) {
  198. char *p;
  199. bool still_doc_line;
  200. /* Current conditions apply to this line. */
  201. f->line_info[i].cond = cond;
  202. f->line_info[i].continued = continued;
  203. if (continued) {
  204. /* Same as last line. */
  205. f->line_info[i].type = f->line_info[i-1].type;
  206. /* Update in_comment. */
  207. remove_comments(f, f->lines[i], in_comment, &in_comment);
  208. continue;
  209. }
  210. /* Preprocessor directive? */
  211. if (!in_comment
  212. && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
  213. f->line_info[i].type = PREPROC_LINE;
  214. cond = analyze_directive(f, f->lines[i], cond);
  215. continue;
  216. }
  217. still_doc_line = (in_comment
  218. && f->line_info[i-1].type == DOC_LINE);
  219. p = remove_comments(f, f->lines[i], in_comment, &in_comment);
  220. if (is_empty(p)) {
  221. if (strstarts(f->lines[i], "/**") || still_doc_line)
  222. f->line_info[i].type = DOC_LINE;
  223. else
  224. f->line_info[i].type = COMMENT_LINE;
  225. } else
  226. f->line_info[i].type = CODE_LINE;
  227. tal_free(p);
  228. }
  229. return f->line_info;
  230. }
  231. struct symbol {
  232. struct list_node list;
  233. const char *name;
  234. const unsigned int *value;
  235. };
  236. static struct symbol *find_symbol(struct list_head *syms, const char *sym)
  237. {
  238. struct symbol *i;
  239. list_for_each(syms, i, list)
  240. if (streq(sym, i->name))
  241. return i;
  242. return NULL;
  243. }
  244. static enum line_compiled get_pp(struct pp_conditions *cond,
  245. struct list_head *syms)
  246. {
  247. struct symbol *sym;
  248. unsigned int val;
  249. enum line_compiled parent, ret;
  250. /* No conditions? Easy. */
  251. if (!cond)
  252. return COMPILED;
  253. /* Check we get here at all. */
  254. parent = get_pp(cond->parent, syms);
  255. if (parent == NOT_COMPILED)
  256. return NOT_COMPILED;
  257. if (cond->type == PP_COND_UNKNOWN)
  258. return MAYBE_COMPILED;
  259. sym = find_symbol(syms, cond->symbol);
  260. if (!sym)
  261. return MAYBE_COMPILED;
  262. switch (cond->type) {
  263. case PP_COND_IF:
  264. /* Undefined is 0. */
  265. val = sym->value ? *sym->value : 0;
  266. if (!val == cond->inverse)
  267. ret = COMPILED;
  268. else
  269. ret = NOT_COMPILED;
  270. break;
  271. case PP_COND_IFDEF:
  272. if (cond->inverse == !sym->value)
  273. ret = COMPILED;
  274. else
  275. ret = NOT_COMPILED;
  276. break;
  277. default:
  278. abort();
  279. }
  280. /* If parent didn't know, NO == NO, but YES == MAYBE. */
  281. if (parent == MAYBE_COMPILED && ret == COMPILED)
  282. ret = MAYBE_COMPILED;
  283. return ret;
  284. }
  285. static void add_symbol(struct list_head *head,
  286. const char *symbol, const unsigned int *value)
  287. {
  288. struct symbol *sym = tal(head, struct symbol);
  289. sym->name = symbol;
  290. sym->value = value;
  291. list_add(head, &sym->list);
  292. }
  293. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  294. const char *symbol,
  295. const unsigned int *value,
  296. ...)
  297. {
  298. enum line_compiled ret;
  299. struct list_head *head;
  300. va_list ap;
  301. head = tal(NULL, struct list_head);
  302. list_head_init(head);
  303. va_start(ap, value);
  304. add_symbol(head, symbol, value);
  305. while ((symbol = va_arg(ap, const char *)) != NULL) {
  306. value = va_arg(ap, const unsigned int *);
  307. add_symbol(head, symbol, value);
  308. }
  309. ret = get_pp(cond, head);
  310. tal_free(head);
  311. va_end(ap);
  312. return ret;
  313. }
  314. static void score_error_vfmt(struct score *score, const char *source,
  315. const char *errorfmt, va_list ap)
  316. {
  317. if (!score->error)
  318. score->error = tal_strdup(score, "");
  319. if (verbose < 2 && strcount(score->error, "\n") > 5) {
  320. if (!strends(score->error,
  321. "... more (use -vv to see them all)\n")) {
  322. score->error = tal_strcat(score,
  323. take(score->error),
  324. "... more (use -vv to see"
  325. " them all)\n");
  326. }
  327. return;
  328. }
  329. tal_append_fmt(&score->error, "%s:", source);
  330. tal_append_vfmt(&score->error, errorfmt, ap);
  331. score->error = tal_strcat(score, take(score->error), "\n");
  332. }
  333. void score_error(struct score *score, const char *source,
  334. const char *errorfmt, ...)
  335. {
  336. va_list ap;
  337. va_start(ap, errorfmt);
  338. score_error_vfmt(score, source, errorfmt, ap);
  339. va_end(ap);
  340. }
  341. void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
  342. const char *errorfmt, ...)
  343. {
  344. va_list ap;
  345. char *source;
  346. struct file_error *fe = tal(score, struct file_error);
  347. fe->file = f;
  348. fe->line = line;
  349. list_add_tail(&score->per_file_errors, &fe->list);
  350. if (line)
  351. source = tal_fmt(score, "%s:%u", f->fullname, line);
  352. else
  353. source = tal_fmt(score, "%s", f->fullname);
  354. va_start(ap, errorfmt);
  355. score_error_vfmt(score, source, errorfmt, ap);
  356. va_end(ap);
  357. }
  358. char *get_or_compile_info(const void *ctx UNNEEDED, const char *dir)
  359. {
  360. struct manifest *m = get_manifest(NULL, dir);
  361. if (!m->info_file->compiled[COMPILE_NORMAL])
  362. m->info_file->compiled[COMPILE_NORMAL] = compile_info(m, dir);
  363. return m->info_file->compiled[COMPILE_NORMAL];
  364. }