file_analysis.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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, f->num_lines);
  196. for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
  197. char *p;
  198. bool still_doc_line;
  199. /* Current conditions apply to this line. */
  200. f->line_info[i].cond = cond;
  201. f->line_info[i].continued = continued;
  202. if (continued) {
  203. /* Same as last line. */
  204. f->line_info[i].type = f->line_info[i-1].type;
  205. /* Update in_comment. */
  206. remove_comments(f, f->lines[i], in_comment, &in_comment);
  207. continue;
  208. }
  209. /* Preprocessor directive? */
  210. if (!in_comment
  211. && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
  212. f->line_info[i].type = PREPROC_LINE;
  213. cond = analyze_directive(f, f->lines[i], cond);
  214. continue;
  215. }
  216. still_doc_line = (in_comment
  217. && f->line_info[i-1].type == DOC_LINE);
  218. p = remove_comments(f, f->lines[i], in_comment, &in_comment);
  219. if (is_empty(p)) {
  220. if (strstarts(f->lines[i], "/**") || still_doc_line)
  221. f->line_info[i].type = DOC_LINE;
  222. else
  223. f->line_info[i].type = COMMENT_LINE;
  224. } else
  225. f->line_info[i].type = CODE_LINE;
  226. tal_free(p);
  227. }
  228. return f->line_info;
  229. }
  230. struct symbol {
  231. struct list_node list;
  232. const char *name;
  233. const unsigned int *value;
  234. };
  235. static struct symbol *find_symbol(struct list_head *syms, const char *sym)
  236. {
  237. struct symbol *i;
  238. list_for_each(syms, i, list)
  239. if (streq(sym, i->name))
  240. return i;
  241. return NULL;
  242. }
  243. static enum line_compiled get_pp(struct pp_conditions *cond,
  244. struct list_head *syms)
  245. {
  246. struct symbol *sym;
  247. unsigned int val;
  248. enum line_compiled parent, ret;
  249. /* No conditions? Easy. */
  250. if (!cond)
  251. return COMPILED;
  252. /* Check we get here at all. */
  253. parent = get_pp(cond->parent, syms);
  254. if (parent == NOT_COMPILED)
  255. return NOT_COMPILED;
  256. if (cond->type == PP_COND_UNKNOWN)
  257. return MAYBE_COMPILED;
  258. sym = find_symbol(syms, cond->symbol);
  259. if (!sym)
  260. return MAYBE_COMPILED;
  261. switch (cond->type) {
  262. case PP_COND_IF:
  263. /* Undefined is 0. */
  264. val = sym->value ? *sym->value : 0;
  265. if (!val == cond->inverse)
  266. ret = COMPILED;
  267. else
  268. ret = NOT_COMPILED;
  269. break;
  270. case PP_COND_IFDEF:
  271. if (cond->inverse == !sym->value)
  272. ret = COMPILED;
  273. else
  274. ret = NOT_COMPILED;
  275. break;
  276. default:
  277. abort();
  278. }
  279. /* If parent didn't know, NO == NO, but YES == MAYBE. */
  280. if (parent == MAYBE_COMPILED && ret == COMPILED)
  281. ret = MAYBE_COMPILED;
  282. return ret;
  283. }
  284. static void add_symbol(struct list_head *head,
  285. const char *symbol, const unsigned int *value)
  286. {
  287. struct symbol *sym = tal(head, struct symbol);
  288. sym->name = symbol;
  289. sym->value = value;
  290. list_add(head, &sym->list);
  291. }
  292. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  293. const char *symbol,
  294. const unsigned int *value,
  295. ...)
  296. {
  297. enum line_compiled ret;
  298. struct list_head *head;
  299. va_list ap;
  300. head = tal(NULL, struct list_head);
  301. list_head_init(head);
  302. va_start(ap, value);
  303. add_symbol(head, symbol, value);
  304. while ((symbol = va_arg(ap, const char *)) != NULL) {
  305. value = va_arg(ap, const unsigned int *);
  306. add_symbol(head, symbol, value);
  307. }
  308. ret = get_pp(cond, head);
  309. tal_free(head);
  310. return ret;
  311. }
  312. void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
  313. const char *errorfmt, ...)
  314. {
  315. va_list ap;
  316. struct file_error *fe = tal(score, struct file_error);
  317. fe->file = f;
  318. fe->line = line;
  319. list_add_tail(&score->per_file_errors, &fe->list);
  320. if (!score->error)
  321. score->error = tal_strdup(score, "");
  322. if (verbose < 2 && strcount(score->error, "\n") > 5) {
  323. if (!strends(score->error,
  324. "... more (use -vv to see them all)\n")) {
  325. score->error = tal_strcat(score,
  326. take(score->error),
  327. "... more (use -vv to see"
  328. " them all)\n");
  329. }
  330. return;
  331. }
  332. if (line)
  333. tal_append_fmt(&score->error, "%s:%u:", f->fullname, line);
  334. else
  335. tal_append_fmt(&score->error, "%s:", f->fullname);
  336. va_start(ap, errorfmt);
  337. tal_append_vfmt(&score->error, errorfmt, ap);
  338. va_end(ap);
  339. score->error = tal_strcat(score, take(score->error),"\n");
  340. }
  341. char *get_or_compile_info(const void *ctx, const char *dir)
  342. {
  343. struct manifest *m = get_manifest(NULL, dir);
  344. if (!m->info_file->compiled[COMPILE_NORMAL])
  345. m->info_file->compiled[COMPILE_NORMAL] = compile_info(m, dir);
  346. return m->info_file->compiled[COMPILE_NORMAL];
  347. }