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>
16 #include <sys/types.h>
26 struct list_head *get_ccan_file_docs(struct ccan_file *f)
28 if (!f->doc_sections) {
29 get_ccan_file_lines(f);
30 f->doc_sections = extract_doc_sections(f->lines);
32 return f->doc_sections;
37 * remove_comments - strip comments from a line, return copy.
39 * @in_comment: are we already within a comment (from prev line).
40 * @unterminated: are we still in a comment for next line.
42 static char *remove_comments(const char *line, bool in_comment,
45 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
50 /* Find first comment. */
51 const char *old_comment = strstr(line, "/*");
52 const char *new_comment = strstr(line, "//");
55 if (new_comment && old_comment)
56 comment = new_comment < old_comment
57 ? new_comment : old_comment;
59 comment = old_comment;
61 comment = new_comment;
65 *unterminated = false;
69 /* Copy up to comment. */
70 memcpy(p, line, comment - line);
72 line += comment - line + 2;
74 if (comment == new_comment) {
75 /* We're done: goes to EOL. */
77 *unterminated = false;
84 const char *end = strstr(line, "*/");
97 static bool is_empty(const char *line)
99 return strspn(line, " \r\t") == strlen(line);
102 static bool continues(const char *line)
104 /* Technically, any odd number of these. But who cares? */
105 return strends(line, "\\");
108 /* Get token if it's equal to token. */
109 bool get_token(const char **line, const char *token)
113 *line += strspn(*line, " \t");
114 if (cisalnum(token[0]) || token[0] == '_')
115 toklen = strspn(*line, IDENT_CHARS);
117 /* FIXME: real tokenizer handles ++ and other multi-chars. */
118 toklen = strlen(token);
121 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
128 char *get_symbol_token(void *ctx, const char **line)
133 *line += strspn(*line, " \t");
134 toklen = strspn(*line, IDENT_CHARS);
137 ret = talloc_strndup(ctx, *line, toklen);
142 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
144 bool brackets, defined;
146 cond->inverse = get_token(line, "!");
147 defined = get_token(line, "defined");
148 brackets = get_token(line, "(");
149 cond->symbol = get_symbol_token(cond, line);
152 if (brackets && !get_token(line, ")"))
155 cond->type = PP_COND_IF;
157 /* FIXME: We just chain them, ignoring operators. */
158 if (get_token(line, "||") || get_token(line, "&&")) {
159 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
161 sub->parent = cond->parent;
162 sub->type = PP_COND_IFDEF;
163 if (parse_hash_if(sub, line))
170 /* FIXME: Get serious! */
171 static struct pp_conditions *analyze_directive(struct ccan_file *f,
173 struct pp_conditions *parent)
175 struct pp_conditions *cond = talloc(f, struct pp_conditions);
178 line = remove_comments(line, false, &unused);
180 cond->parent = parent;
181 cond->type = PP_COND_IFDEF;
183 if (!get_token(&line, "#"))
186 if (get_token(&line, "if")) {
187 if (!parse_hash_if(cond, &line))
189 } else if (get_token(&line, "elif")) {
193 cond->parent = parent->parent;
194 /* FIXME: Not quite true. This implies !parent, but we don't
195 * do multiple conditionals yet. */
196 if (!parse_hash_if(cond, &line))
198 } else if (get_token(&line, "ifdef")) {
200 cond->inverse = false;
201 brackets = get_token(&line, "(");
202 cond->symbol = get_symbol_token(cond, &line);
205 if (brackets && !get_token(&line, ")"))
207 } else if (get_token(&line, "ifndef")) {
209 cond->inverse = true;
210 brackets = get_token(&line, "(");
211 cond->symbol = get_symbol_token(cond, &line);
214 if (brackets && !get_token(&line, ")"))
216 } else if (get_token(&line, "else")) {
222 cond->inverse = !cond->inverse;
224 } else if (get_token(&line, "endif")) {
230 return parent->parent;
232 /* Not a conditional. */
242 cond->type = PP_COND_UNKNOWN;
246 /* This parser is rough, but OK if code is reasonably neat. */
247 struct line_info *get_ccan_line_info(struct ccan_file *f)
249 bool continued = false, in_comment = false;
250 struct pp_conditions *cond = NULL;
256 get_ccan_file_lines(f);
257 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
259 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
263 /* Current conditions apply to this line. */
264 f->line_info[i].cond = cond;
265 f->line_info[i].continued = continued;
268 /* Same as last line. */
269 f->line_info[i].type = f->line_info[i-1].type;
270 /* Update in_comment. */
271 remove_comments(f->lines[i], in_comment, &in_comment);
275 /* Preprocessor directive? */
277 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
278 f->line_info[i].type = PREPROC_LINE;
279 cond = analyze_directive(f, f->lines[i], cond);
283 still_doc_line = (in_comment
284 && f->line_info[i-1].type == DOC_LINE);
286 p = remove_comments(f->lines[i], in_comment, &in_comment);
288 if (strstarts(f->lines[i], "/**") || still_doc_line)
289 f->line_info[i].type = DOC_LINE;
291 f->line_info[i].type = COMMENT_LINE;
293 f->line_info[i].type = CODE_LINE;
300 struct list_node list;
302 const unsigned int *value;
305 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
309 list_for_each(syms, i, list)
310 if (streq(sym, i->name))
315 static enum line_compiled get_pp(struct pp_conditions *cond,
316 struct list_head *syms)
320 enum line_compiled parent, ret;
322 /* No conditions? Easy. */
326 /* Check we get here at all. */
327 parent = get_pp(cond->parent, syms);
328 if (parent == NOT_COMPILED)
331 if (cond->type == PP_COND_UNKNOWN)
332 return MAYBE_COMPILED;
334 sym = find_symbol(syms, cond->symbol);
336 return MAYBE_COMPILED;
338 switch (cond->type) {
340 /* Undefined is 0. */
341 val = sym->value ? *sym->value : 0;
342 if (!val == cond->inverse)
349 if (cond->inverse == !sym->value)
359 /* If parent didn't know, NO == NO, but YES == MAYBE. */
360 if (parent == MAYBE_COMPILED && ret == COMPILED)
361 ret = MAYBE_COMPILED;
365 static void add_symbol(struct list_head *head,
366 const char *symbol, const unsigned int *value)
368 struct symbol *sym = talloc(head, struct symbol);
371 list_add(head, &sym->list);
374 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
376 const unsigned int *value,
379 enum line_compiled ret;
380 struct list_head *head;
383 head = talloc(NULL, struct list_head);
384 list_head_init(head);
387 add_symbol(head, symbol, value);
389 while ((symbol = va_arg(ap, const char *)) != NULL) {
390 value = va_arg(ap, const unsigned int *);
391 add_symbol(head, symbol, value);
393 ret = get_pp(cond, head);
398 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
399 const char *errorfmt, ...)
403 struct file_error *fe = talloc(score, struct file_error);
406 list_add_tail(&score->per_file_errors, &fe->list);
409 score->error = talloc_strdup(score, "");
411 if (verbose < 2 && strcount(score->error, "\n") > 5)
415 score->error = talloc_asprintf_append(score->error,
419 score->error = talloc_asprintf_append(score->error,
422 va_start(ap, errorfmt);
423 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
425 score->error = talloc_append_string(score->error, "\n");
427 if (verbose < 2 && strcount(score->error, "\n") > 5)
428 score->error = talloc_append_string(score->error,
429 "... more (use -vv to see them all)\n");