3 #include <ccan/str/str.h>
4 #include <ccan/take/take.h>
5 #include <ccan/hash/hash.h>
6 #include <ccan/htable/htable_type.h>
7 #include <ccan/noerr/noerr.h>
8 #include <ccan/foreach/foreach.h>
9 #include <ccan/asort/asort.h>
10 #include <ccan/array_size/array_size.h>
13 #include <sys/types.h>
23 struct list_head *get_ccan_file_docs(struct ccan_file *f)
25 if (!f->doc_sections) {
26 get_ccan_file_lines(f);
27 f->doc_sections = extract_doc_sections(f->lines, f->name);
29 return f->doc_sections;
34 * remove_comments - strip comments from a line, return copy.
36 * @in_comment: are we already within a comment (from prev line).
37 * @unterminated: are we still in a comment for next line.
39 static char *remove_comments(const tal_t *ctx,
40 const char *line, bool in_comment,
43 char *p, *ret = tal_arr(ctx, char, strlen(line) + 1);
48 /* Find first comment. */
49 const char *old_comment = strstr(line, "/*");
50 const char *new_comment = strstr(line, "//");
53 if (new_comment && old_comment)
54 comment = new_comment < old_comment
55 ? new_comment : old_comment;
57 comment = old_comment;
59 comment = new_comment;
63 *unterminated = false;
67 /* Copy up to comment. */
68 memcpy(p, line, comment - line);
70 line += comment - line + 2;
72 if (comment == new_comment) {
73 /* We're done: goes to EOL. */
75 *unterminated = false;
82 const char *end = strstr(line, "*/");
95 static bool is_empty(const char *line)
97 return strspn(line, " \r\t") == strlen(line);
100 static bool continues(const char *line)
102 /* Technically, any odd number of these. But who cares? */
103 return strends(line, "\\");
106 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
108 bool brackets, defined;
110 cond->inverse = get_token(line, "!");
111 defined = get_token(line, "defined");
112 brackets = get_token(line, "(");
113 cond->symbol = get_symbol_token(cond, line);
116 if (brackets && !get_token(line, ")"))
119 cond->type = PP_COND_IF;
121 /* FIXME: We just chain them, ignoring operators. */
122 if (get_token(line, "||") || get_token(line, "&&")) {
123 struct pp_conditions *sub = tal(cond, struct pp_conditions);
125 sub->parent = cond->parent;
126 sub->type = PP_COND_IFDEF;
127 if (parse_hash_if(sub, line))
134 /* FIXME: Get serious! */
135 static struct pp_conditions *analyze_directive(struct ccan_file *f,
137 struct pp_conditions *parent)
139 struct pp_conditions *cond = tal(f, struct pp_conditions);
142 line = remove_comments(f, line, false, &unused);
144 cond->parent = parent;
145 cond->type = PP_COND_IFDEF;
147 if (!get_token(&line, "#"))
150 if (get_token(&line, "if")) {
151 if (!parse_hash_if(cond, &line))
153 } else if (get_token(&line, "elif")) {
157 cond->parent = parent->parent;
158 /* FIXME: Not quite true. This implies !parent, but we don't
159 * do multiple conditionals yet. */
160 if (!parse_hash_if(cond, &line))
162 } else if (get_token(&line, "ifdef")) {
164 cond->inverse = false;
165 brackets = get_token(&line, "(");
166 cond->symbol = get_symbol_token(cond, &line);
169 if (brackets && !get_token(&line, ")"))
171 } else if (get_token(&line, "ifndef")) {
173 cond->inverse = true;
174 brackets = get_token(&line, "(");
175 cond->symbol = get_symbol_token(cond, &line);
178 if (brackets && !get_token(&line, ")"))
180 } else if (get_token(&line, "else")) {
186 cond->inverse = !cond->inverse;
188 } else if (get_token(&line, "endif")) {
194 return parent->parent;
196 /* Not a conditional. */
206 cond->type = PP_COND_UNKNOWN;
210 /* This parser is rough, but OK if code is reasonably neat. */
211 struct line_info *get_ccan_line_info(struct ccan_file *f)
213 bool continued = false, in_comment = false;
214 struct pp_conditions *cond = NULL;
220 get_ccan_file_lines(f);
221 f->line_info = tal_arr(f->lines, struct line_info,
222 tal_count(f->lines)-1);
224 for (i = 0; f->lines[i]; 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, 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, 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 = tal(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 = tal(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 = tal(score, struct file_error);
371 list_add_tail(&score->per_file_errors, &fe->list);
374 score->error = tal_strdup(score, "");
376 if (verbose < 2 && strcount(score->error, "\n") > 5) {
377 if (!strends(score->error,
378 "... more (use -vv to see them all)\n")) {
379 score->error = tal_strcat(score,
381 "... more (use -vv to see"
388 tal_append_fmt(&score->error, "%s:%u:", f->fullname, line);
390 tal_append_fmt(&score->error, "%s:", f->fullname);
392 va_start(ap, errorfmt);
393 tal_append_vfmt(&score->error, errorfmt, ap);
395 score->error = tal_strcat(score, take(score->error),"\n");
398 char *get_or_compile_info(const void *ctx, const char *dir)
400 struct manifest *m = get_manifest(NULL, dir);
402 if (!m->info_file->compiled[COMPILE_NORMAL])
403 m->info_file->compiled[COMPILE_NORMAL] = compile_info(m, dir);
405 return m->info_file->compiled[COMPILE_NORMAL];