file_analysis.c 9.3 KB

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