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 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
110 bool brackets, defined;
112 cond->inverse = get_token(line, "!");
113 defined = get_token(line, "defined");
114 brackets = get_token(line, "(");
115 cond->symbol = get_symbol_token(cond, line);
118 if (brackets && !get_token(line, ")"))
121 cond->type = PP_COND_IF;
123 /* FIXME: We just chain them, ignoring operators. */
124 if (get_token(line, "||") || get_token(line, "&&")) {
125 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
127 sub->parent = cond->parent;
128 sub->type = PP_COND_IFDEF;
129 if (parse_hash_if(sub, line))
136 /* FIXME: Get serious! */
137 static struct pp_conditions *analyze_directive(struct ccan_file *f,
139 struct pp_conditions *parent)
141 struct pp_conditions *cond = talloc(f, struct pp_conditions);
144 line = remove_comments(line, false, &unused);
146 cond->parent = parent;
147 cond->type = PP_COND_IFDEF;
149 if (!get_token(&line, "#"))
152 if (get_token(&line, "if")) {
153 if (!parse_hash_if(cond, &line))
155 } else if (get_token(&line, "elif")) {
159 cond->parent = parent->parent;
160 /* FIXME: Not quite true. This implies !parent, but we don't
161 * do multiple conditionals yet. */
162 if (!parse_hash_if(cond, &line))
164 } else if (get_token(&line, "ifdef")) {
166 cond->inverse = false;
167 brackets = get_token(&line, "(");
168 cond->symbol = get_symbol_token(cond, &line);
171 if (brackets && !get_token(&line, ")"))
173 } else if (get_token(&line, "ifndef")) {
175 cond->inverse = true;
176 brackets = get_token(&line, "(");
177 cond->symbol = get_symbol_token(cond, &line);
180 if (brackets && !get_token(&line, ")"))
182 } else if (get_token(&line, "else")) {
188 cond->inverse = !cond->inverse;
190 } else if (get_token(&line, "endif")) {
196 return parent->parent;
198 /* Not a conditional. */
208 cond->type = PP_COND_UNKNOWN;
212 /* This parser is rough, but OK if code is reasonably neat. */
213 struct line_info *get_ccan_line_info(struct ccan_file *f)
215 bool continued = false, in_comment = false;
216 struct pp_conditions *cond = NULL;
222 get_ccan_file_lines(f);
223 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
225 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
229 /* Current conditions apply to this line. */
230 f->line_info[i].cond = cond;
231 f->line_info[i].continued = 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);
241 /* Preprocessor directive? */
243 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
244 f->line_info[i].type = PREPROC_LINE;
245 cond = analyze_directive(f, f->lines[i], cond);
249 still_doc_line = (in_comment
250 && f->line_info[i-1].type == DOC_LINE);
252 p = remove_comments(f->lines[i], in_comment, &in_comment);
254 if (strstarts(f->lines[i], "/**") || still_doc_line)
255 f->line_info[i].type = DOC_LINE;
257 f->line_info[i].type = COMMENT_LINE;
259 f->line_info[i].type = CODE_LINE;
266 struct list_node list;
268 const unsigned int *value;
271 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
275 list_for_each(syms, i, list)
276 if (streq(sym, i->name))
281 static enum line_compiled get_pp(struct pp_conditions *cond,
282 struct list_head *syms)
286 enum line_compiled parent, ret;
288 /* No conditions? Easy. */
292 /* Check we get here at all. */
293 parent = get_pp(cond->parent, syms);
294 if (parent == NOT_COMPILED)
297 if (cond->type == PP_COND_UNKNOWN)
298 return MAYBE_COMPILED;
300 sym = find_symbol(syms, cond->symbol);
302 return MAYBE_COMPILED;
304 switch (cond->type) {
306 /* Undefined is 0. */
307 val = sym->value ? *sym->value : 0;
308 if (!val == cond->inverse)
315 if (cond->inverse == !sym->value)
325 /* If parent didn't know, NO == NO, but YES == MAYBE. */
326 if (parent == MAYBE_COMPILED && ret == COMPILED)
327 ret = MAYBE_COMPILED;
331 static void add_symbol(struct list_head *head,
332 const char *symbol, const unsigned int *value)
334 struct symbol *sym = talloc(head, struct symbol);
337 list_add(head, &sym->list);
340 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
342 const unsigned int *value,
345 enum line_compiled ret;
346 struct list_head *head;
349 head = talloc(NULL, struct list_head);
350 list_head_init(head);
353 add_symbol(head, symbol, value);
355 while ((symbol = va_arg(ap, const char *)) != NULL) {
356 value = va_arg(ap, const unsigned int *);
357 add_symbol(head, symbol, value);
359 ret = get_pp(cond, head);
364 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
365 const char *errorfmt, ...)
369 struct file_error *fe = talloc(score, struct file_error);
372 list_add_tail(&score->per_file_errors, &fe->list);
375 score->error = talloc_strdup(score, "");
377 if (verbose < 2 && strcount(score->error, "\n") > 5)
381 score->error = talloc_asprintf_append(score->error,
385 score->error = talloc_asprintf_append(score->error,
388 va_start(ap, errorfmt);
389 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
391 score->error = talloc_append_string(score->error, "\n");
393 if (verbose < 2 && strcount(score->error, "\n") > 5)
394 score->error = talloc_append_string(score->error,
395 "... more (use -vv to see them all)\n");