file_analysis.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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);
  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. /* Get token if it's equal to token. */
  98. bool get_token(const char **line, const char *token)
  99. {
  100. unsigned int toklen;
  101. *line += strspn(*line, " \t");
  102. if (cisalnum(token[0]) || token[0] == '_')
  103. toklen = strspn(*line, IDENT_CHARS);
  104. else {
  105. /* FIXME: real tokenizer handles ++ and other multi-chars. */
  106. toklen = strlen(token);
  107. }
  108. if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
  109. *line += toklen;
  110. return true;
  111. }
  112. return false;
  113. }
  114. char *get_symbol_token(void *ctx, const char **line)
  115. {
  116. unsigned int toklen;
  117. char *ret;
  118. *line += strspn(*line, " \t");
  119. toklen = strspn(*line, IDENT_CHARS);
  120. if (!toklen)
  121. return NULL;
  122. ret = talloc_strndup(ctx, *line, toklen);
  123. *line += toklen;
  124. return ret;
  125. }
  126. static bool parse_hash_if(struct pp_conditions *cond, const char **line)
  127. {
  128. bool brackets, defined;
  129. cond->inverse = get_token(line, "!");
  130. defined = get_token(line, "defined");
  131. brackets = get_token(line, "(");
  132. cond->symbol = get_symbol_token(cond, line);
  133. if (!cond->symbol)
  134. return false;
  135. if (brackets && !get_token(line, ")"))
  136. return false;
  137. if (!defined)
  138. cond->type = PP_COND_IF;
  139. /* FIXME: We just chain them, ignoring operators. */
  140. if (get_token(line, "||") || get_token(line, "&&")) {
  141. struct pp_conditions *sub = talloc(cond, struct pp_conditions);
  142. sub->parent = cond->parent;
  143. sub->type = PP_COND_IFDEF;
  144. if (parse_hash_if(sub, line))
  145. cond->parent = sub;
  146. }
  147. return true;
  148. }
  149. /* FIXME: Get serious! */
  150. static struct pp_conditions *analyze_directive(struct ccan_file *f,
  151. const char *line,
  152. struct pp_conditions *parent)
  153. {
  154. struct pp_conditions *cond = talloc(f, struct pp_conditions);
  155. bool unused;
  156. line = remove_comments(line, false, &unused);
  157. cond->parent = parent;
  158. cond->type = PP_COND_IFDEF;
  159. if (!get_token(&line, "#"))
  160. abort();
  161. if (get_token(&line, "if")) {
  162. if (!parse_hash_if(cond, &line))
  163. goto unknown;
  164. } else if (get_token(&line, "elif")) {
  165. /* Malformed? */
  166. if (!parent)
  167. return NULL;
  168. cond->parent = parent->parent;
  169. /* FIXME: Not quite true. This implies !parent, but we don't
  170. * do multiple conditionals yet. */
  171. if (!parse_hash_if(cond, &line))
  172. goto unknown;
  173. } else if (get_token(&line, "ifdef")) {
  174. bool brackets;
  175. cond->inverse = false;
  176. brackets = get_token(&line, "(");
  177. cond->symbol = get_symbol_token(cond, &line);
  178. if (!cond->symbol)
  179. goto unknown;
  180. if (brackets && !get_token(&line, ")"))
  181. goto unknown;
  182. } else if (get_token(&line, "ifndef")) {
  183. bool brackets;
  184. cond->inverse = true;
  185. brackets = get_token(&line, "(");
  186. cond->symbol = get_symbol_token(cond, &line);
  187. if (!cond->symbol)
  188. goto unknown;
  189. if (brackets && !get_token(&line, ")"))
  190. goto unknown;
  191. } else if (get_token(&line, "else")) {
  192. /* Malformed? */
  193. if (!parent)
  194. return NULL;
  195. *cond = *parent;
  196. cond->inverse = !cond->inverse;
  197. return cond;
  198. } else if (get_token(&line, "endif")) {
  199. talloc_free(cond);
  200. /* Malformed? */
  201. if (!parent)
  202. return NULL;
  203. /* Back up one! */
  204. return parent->parent;
  205. } else {
  206. /* Not a conditional. */
  207. talloc_free(cond);
  208. return parent;
  209. }
  210. if (!is_empty(line))
  211. goto unknown;
  212. return cond;
  213. unknown:
  214. cond->type = PP_COND_UNKNOWN;
  215. return cond;
  216. }
  217. /* This parser is rough, but OK if code is reasonably neat. */
  218. struct line_info *get_ccan_line_info(struct ccan_file *f)
  219. {
  220. bool continued = false, in_comment = false;
  221. struct pp_conditions *cond = NULL;
  222. unsigned int i;
  223. if (f->line_info)
  224. return f->line_info;
  225. get_ccan_file_lines(f);
  226. f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
  227. for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
  228. char *p;
  229. bool still_doc_line;
  230. /* Current conditions apply to this line. */
  231. f->line_info[i].cond = cond;
  232. f->line_info[i].continued = continued;
  233. if (continued) {
  234. /* Same as last line. */
  235. f->line_info[i].type = f->line_info[i-1].type;
  236. /* Update in_comment. */
  237. remove_comments(f->lines[i], in_comment, &in_comment);
  238. continue;
  239. }
  240. /* Preprocessor directive? */
  241. if (!in_comment
  242. && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
  243. f->line_info[i].type = PREPROC_LINE;
  244. cond = analyze_directive(f, f->lines[i], cond);
  245. continue;
  246. }
  247. still_doc_line = (in_comment
  248. && f->line_info[i-1].type == DOC_LINE);
  249. p = remove_comments(f->lines[i], in_comment, &in_comment);
  250. if (is_empty(p)) {
  251. if (strstarts(f->lines[i], "/**") || still_doc_line)
  252. f->line_info[i].type = DOC_LINE;
  253. else
  254. f->line_info[i].type = COMMENT_LINE;
  255. } else
  256. f->line_info[i].type = CODE_LINE;
  257. talloc_free(p);
  258. }
  259. return f->line_info;
  260. }
  261. struct symbol {
  262. struct list_node list;
  263. const char *name;
  264. const unsigned int *value;
  265. };
  266. static struct symbol *find_symbol(struct list_head *syms, const char *sym)
  267. {
  268. struct symbol *i;
  269. list_for_each(syms, i, list)
  270. if (streq(sym, i->name))
  271. return i;
  272. return NULL;
  273. }
  274. static enum line_compiled get_pp(struct pp_conditions *cond,
  275. struct list_head *syms)
  276. {
  277. struct symbol *sym;
  278. unsigned int val;
  279. enum line_compiled parent, ret;
  280. /* No conditions? Easy. */
  281. if (!cond)
  282. return COMPILED;
  283. /* Check we get here at all. */
  284. parent = get_pp(cond->parent, syms);
  285. if (parent == NOT_COMPILED)
  286. return NOT_COMPILED;
  287. if (cond->type == PP_COND_UNKNOWN)
  288. return MAYBE_COMPILED;
  289. sym = find_symbol(syms, cond->symbol);
  290. if (!sym)
  291. return MAYBE_COMPILED;
  292. switch (cond->type) {
  293. case PP_COND_IF:
  294. /* Undefined is 0. */
  295. val = sym->value ? *sym->value : 0;
  296. if (!val == cond->inverse)
  297. ret = COMPILED;
  298. else
  299. ret = NOT_COMPILED;
  300. break;
  301. case PP_COND_IFDEF:
  302. if (cond->inverse == !sym->value)
  303. ret = COMPILED;
  304. else
  305. ret = NOT_COMPILED;
  306. break;
  307. default:
  308. abort();
  309. }
  310. /* If parent didn't know, NO == NO, but YES == MAYBE. */
  311. if (parent == MAYBE_COMPILED && ret == COMPILED)
  312. ret = MAYBE_COMPILED;
  313. return ret;
  314. }
  315. static void add_symbol(struct list_head *head,
  316. const char *symbol, const unsigned int *value)
  317. {
  318. struct symbol *sym = talloc(head, struct symbol);
  319. sym->name = symbol;
  320. sym->value = value;
  321. list_add(head, &sym->list);
  322. }
  323. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  324. const char *symbol,
  325. const unsigned int *value,
  326. ...)
  327. {
  328. enum line_compiled ret;
  329. struct list_head *head;
  330. va_list ap;
  331. head = talloc(NULL, struct list_head);
  332. list_head_init(head);
  333. va_start(ap, value);
  334. add_symbol(head, symbol, value);
  335. while ((symbol = va_arg(ap, const char *)) != NULL) {
  336. value = va_arg(ap, const unsigned int *);
  337. add_symbol(head, symbol, value);
  338. }
  339. ret = get_pp(cond, head);
  340. talloc_free(head);
  341. return ret;
  342. }
  343. void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
  344. const char *errorfmt, ...)
  345. {
  346. va_list ap;
  347. struct file_error *fe = talloc(score, struct file_error);
  348. fe->file = f;
  349. fe->line = line;
  350. list_add_tail(&score->per_file_errors, &fe->list);
  351. if (!score->error)
  352. score->error = talloc_strdup(score, "");
  353. if (verbose < 2 && strcount(score->error, "\n") > 5)
  354. return;
  355. if (line)
  356. score->error = talloc_asprintf_append(score->error,
  357. "%s:%u:",
  358. f->fullname, line);
  359. else
  360. score->error = talloc_asprintf_append(score->error,
  361. "%s:", f->fullname);
  362. va_start(ap, errorfmt);
  363. score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
  364. va_end(ap);
  365. score->error = talloc_append_string(score->error, "\n");
  366. if (verbose < 2 && strcount(score->error, "\n") > 5)
  367. score->error = talloc_append_string(score->error,
  368. "... more (use -vv to see them all)\n");
  369. }