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/noerr/noerr.h>
10 #include <ccan/foreach/foreach.h>
11 #include <ccan/asort/asort.h>
12 #include <ccan/array_size/array_size.h>
15 #include <sys/types.h>
25 struct list_head *get_ccan_file_docs(struct ccan_file *f)
27 if (!f->doc_sections) {
28 get_ccan_file_lines(f);
29 f->doc_sections = extract_doc_sections(f->lines, f->name);
31 return f->doc_sections;
36 * remove_comments - strip comments from a line, return copy.
38 * @in_comment: are we already within a comment (from prev line).
39 * @unterminated: are we still in a comment for next line.
41 static char *remove_comments(const char *line, bool in_comment,
44 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
49 /* Find first comment. */
50 const char *old_comment = strstr(line, "/*");
51 const char *new_comment = strstr(line, "//");
54 if (new_comment && old_comment)
55 comment = new_comment < old_comment
56 ? new_comment : old_comment;
58 comment = old_comment;
60 comment = new_comment;
64 *unterminated = false;
68 /* Copy up to comment. */
69 memcpy(p, line, comment - line);
71 line += comment - line + 2;
73 if (comment == new_comment) {
74 /* We're done: goes to EOL. */
76 *unterminated = false;
83 const char *end = strstr(line, "*/");
96 static bool is_empty(const char *line)
98 return strspn(line, " \r\t") == strlen(line);
101 static bool continues(const char *line)
103 /* Technically, any odd number of these. But who cares? */
104 return strends(line, "\\");
107 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
109 bool brackets, defined;
111 cond->inverse = get_token(line, "!");
112 defined = get_token(line, "defined");
113 brackets = get_token(line, "(");
114 cond->symbol = get_symbol_token(cond, line);
117 if (brackets && !get_token(line, ")"))
120 cond->type = PP_COND_IF;
122 /* FIXME: We just chain them, ignoring operators. */
123 if (get_token(line, "||") || get_token(line, "&&")) {
124 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
126 sub->parent = cond->parent;
127 sub->type = PP_COND_IFDEF;
128 if (parse_hash_if(sub, line))
135 /* FIXME: Get serious! */
136 static struct pp_conditions *analyze_directive(struct ccan_file *f,
138 struct pp_conditions *parent)
140 struct pp_conditions *cond = talloc(f, struct pp_conditions);
143 line = remove_comments(line, false, &unused);
145 cond->parent = parent;
146 cond->type = PP_COND_IFDEF;
148 if (!get_token(&line, "#"))
151 if (get_token(&line, "if")) {
152 if (!parse_hash_if(cond, &line))
154 } else if (get_token(&line, "elif")) {
158 cond->parent = parent->parent;
159 /* FIXME: Not quite true. This implies !parent, but we don't
160 * do multiple conditionals yet. */
161 if (!parse_hash_if(cond, &line))
163 } else if (get_token(&line, "ifdef")) {
165 cond->inverse = false;
166 brackets = get_token(&line, "(");
167 cond->symbol = get_symbol_token(cond, &line);
170 if (brackets && !get_token(&line, ")"))
172 } else if (get_token(&line, "ifndef")) {
174 cond->inverse = true;
175 brackets = get_token(&line, "(");
176 cond->symbol = get_symbol_token(cond, &line);
179 if (brackets && !get_token(&line, ")"))
181 } else if (get_token(&line, "else")) {
187 cond->inverse = !cond->inverse;
189 } else if (get_token(&line, "endif")) {
195 return parent->parent;
197 /* Not a conditional. */
207 cond->type = PP_COND_UNKNOWN;
211 /* This parser is rough, but OK if code is reasonably neat. */
212 struct line_info *get_ccan_line_info(struct ccan_file *f)
214 bool continued = false, in_comment = false;
215 struct pp_conditions *cond = NULL;
221 get_ccan_file_lines(f);
222 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
224 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
228 /* Current conditions apply to this line. */
229 f->line_info[i].cond = cond;
230 f->line_info[i].continued = continued;
233 /* Same as last line. */
234 f->line_info[i].type = f->line_info[i-1].type;
235 /* Update in_comment. */
236 remove_comments(f->lines[i], in_comment, &in_comment);
240 /* Preprocessor directive? */
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);
248 still_doc_line = (in_comment
249 && f->line_info[i-1].type == DOC_LINE);
251 p = remove_comments(f->lines[i], in_comment, &in_comment);
253 if (strstarts(f->lines[i], "/**") || still_doc_line)
254 f->line_info[i].type = DOC_LINE;
256 f->line_info[i].type = COMMENT_LINE;
258 f->line_info[i].type = CODE_LINE;
265 struct list_node list;
267 const unsigned int *value;
270 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
274 list_for_each(syms, i, list)
275 if (streq(sym, i->name))
280 static enum line_compiled get_pp(struct pp_conditions *cond,
281 struct list_head *syms)
285 enum line_compiled parent, ret;
287 /* No conditions? Easy. */
291 /* Check we get here at all. */
292 parent = get_pp(cond->parent, syms);
293 if (parent == NOT_COMPILED)
296 if (cond->type == PP_COND_UNKNOWN)
297 return MAYBE_COMPILED;
299 sym = find_symbol(syms, cond->symbol);
301 return MAYBE_COMPILED;
303 switch (cond->type) {
305 /* Undefined is 0. */
306 val = sym->value ? *sym->value : 0;
307 if (!val == cond->inverse)
314 if (cond->inverse == !sym->value)
324 /* If parent didn't know, NO == NO, but YES == MAYBE. */
325 if (parent == MAYBE_COMPILED && ret == COMPILED)
326 ret = MAYBE_COMPILED;
330 static void add_symbol(struct list_head *head,
331 const char *symbol, const unsigned int *value)
333 struct symbol *sym = talloc(head, struct symbol);
336 list_add(head, &sym->list);
339 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
341 const unsigned int *value,
344 enum line_compiled ret;
345 struct list_head *head;
348 head = talloc(NULL, struct list_head);
349 list_head_init(head);
352 add_symbol(head, symbol, value);
354 while ((symbol = va_arg(ap, const char *)) != NULL) {
355 value = va_arg(ap, const unsigned int *);
356 add_symbol(head, symbol, value);
358 ret = get_pp(cond, head);
363 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
364 const char *errorfmt, ...)
368 struct file_error *fe = talloc(score, struct file_error);
371 list_add_tail(&score->per_file_errors, &fe->list);
374 score->error = talloc_strdup(score, "");
376 if (verbose < 2 && strcount(score->error, "\n") > 5)
380 score->error = talloc_asprintf_append(score->error,
384 score->error = talloc_asprintf_append(score->error,
387 va_start(ap, errorfmt);
388 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
390 score->error = talloc_append_string(score->error, "\n");
392 if (verbose < 2 && strcount(score->error, "\n") > 5)
393 score->error = talloc_append_string(score->error,
394 "... more (use -vv to see them all)\n");
397 char *get_or_compile_info(const void *ctx, const char *dir)
399 struct manifest *m = get_manifest(NULL, dir);
401 if (!m->info_file->compiled[COMPILE_NORMAL])
402 m->info_file->compiled[COMPILE_NORMAL] = compile_info(m, dir);
404 return m->info_file->compiled[COMPILE_NORMAL];