2 #include <ccan/talloc/talloc.h>
3 #include <ccan/str/str.h>
4 #include <ccan/str_talloc/str_talloc.h>
5 #include <ccan/grab_file/grab_file.h>
6 #include <ccan/noerr/noerr.h>
18 char **get_ccan_file_lines(struct ccan_file *f)
21 f->lines = strsplit(f, f->contents, "\n", &f->num_lines);
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, f->num_lines);
32 return f->doc_sections;
35 struct ccan_file *new_ccan_file(const void *ctx, char *name)
39 f = talloc(ctx, struct ccan_file);
42 f->doc_sections = NULL;
44 f->name = talloc_steal(f, name);
48 static void add_files(struct manifest *m, const char *dir)
58 err(1, "Opening directory %s", dir[0] ? dir : ".");
60 while ((ent = readdir(d)) != NULL) {
63 struct list_head *dest;
66 if (ent->d_name[0] == '.')
69 f = new_ccan_file(m, talloc_asprintf(m, "%s%s",
71 if (lstat(f->name, &st) != 0)
72 err(1, "lstat %s", f->name);
74 if (S_ISDIR(st.st_mode)) {
75 f->name = talloc_append_string(f->name, "/");
76 add_files(m, f->name);
79 if (!S_ISREG(st.st_mode)) {
84 if (streq(f->name, "_info")) {
86 f->contents = grab_file(f, f->name, &f->contents_size);
88 err(1, "Reading file %s", f->name);
92 is_c_src = strends(f->name, ".c");
93 if (!is_c_src && !strends(f->name, ".h")) {
94 /* We don't pull in contents of non-source files */
95 dest = &m->other_files;
99 f->contents = grab_file(f, f->name, &f->contents_size);
101 err(1, "Reading file %s", f->name);
103 if (!strchr(f->name, '/')) {
108 } else if (strstarts(f->name, "test/")) {
110 if (strstarts(f->name, "test/api"))
111 dest = &m->api_tests;
112 else if (strstarts(f->name, "test/run"))
113 dest = &m->run_tests;
114 else if (strstarts(f->name, "test/compile_ok"))
115 dest = &m->compile_ok_tests;
116 else if (strstarts(f->name, "test/compile_fail"))
117 dest = &m->compile_fail_tests;
119 dest = &m->other_test_c_files;
121 dest = &m->other_test_files;
123 dest = &m->other_files;
125 list_add(dest, &f->list);
130 char *report_on_lines(struct list_head *files,
131 char *(*report)(const char *),
136 list_for_each(files, f, list) {
138 char **lines = get_ccan_file_lines(f);
140 for (i = 0; i < f->num_lines; i++) {
141 char *r = report(lines[i]);
145 sofar = talloc_asprintf_append(sofar,
154 struct manifest *get_manifest(const void *ctx)
156 struct manifest *m = talloc(ctx, struct manifest);
160 list_head_init(&m->c_files);
161 list_head_init(&m->h_files);
162 list_head_init(&m->api_tests);
163 list_head_init(&m->run_tests);
164 list_head_init(&m->compile_ok_tests);
165 list_head_init(&m->compile_fail_tests);
166 list_head_init(&m->other_test_c_files);
167 list_head_init(&m->other_test_files);
168 list_head_init(&m->other_files);
169 list_head_init(&m->dep_dirs);
170 list_head_init(&m->dep_objs);
172 m->basename = talloc_getcwd(m);
174 err(1, "Getting current directory");
175 len = strlen(m->basename);
176 while (len && m->basename[len-1] == '/')
177 m->basename[--len] = '\0';
179 m->basename = strrchr(m->basename, '/');
181 errx(1, "I don't expect to be run from the root directory");
190 * remove_comments - strip comments from a line, return copy.
191 * @line: line to copy
192 * @in_comment: are we already within a comment (from prev line).
193 * @unterminated: are we still in a comment for next line.
195 static char *remove_comments(const char *line, bool in_comment,
198 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
203 /* Find first comment. */
204 const char *old_comment = strstr(line, "/*");
205 const char *new_comment = strstr(line, "//");
208 if (new_comment && old_comment)
209 comment = new_comment < old_comment
210 ? new_comment : old_comment;
211 else if (old_comment)
212 comment = old_comment;
213 else if (new_comment)
214 comment = new_comment;
218 *unterminated = false;
222 /* Copy up to comment. */
223 memcpy(p, line, comment - line);
225 line += comment - line + 2;
227 if (comment == new_comment) {
228 /* We're done: goes to EOL. */
230 *unterminated = false;
237 const char *end = strstr(line, "*/");
239 *unterminated = true;
250 static bool is_empty(const char *line)
252 return strspn(line, " \t") == strlen(line);
255 static bool continues(const char *line)
257 /* Technically, any odd number of these. But who cares? */
258 return strends(line, "\\");
261 /* Get token if it's equal to token. */
262 bool get_token(const char **line, const char *token)
266 *line += strspn(*line, " \t");
267 if (isalnum(token[0]) || token[0] == '_')
268 toklen = strspn(*line, IDENT_CHARS);
270 /* FIXME: real tokenizer handles ++ and other multi-chars. */
271 toklen = strlen(token);
274 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
281 char *get_symbol_token(void *ctx, const char **line)
286 *line += strspn(*line, " \t");
287 toklen = strspn(*line, IDENT_CHARS);
290 ret = talloc_strndup(ctx, *line, toklen);
295 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
297 bool brackets, defined;
299 cond->inverse = get_token(line, "!");
300 defined = get_token(line, "defined");
301 brackets = get_token(line, "(");
302 cond->symbol = get_symbol_token(cond, line);
305 if (brackets && !get_token(line, ")"))
308 cond->type = PP_COND_IF;
312 /* FIXME: Get serious! */
313 static struct pp_conditions *analyze_directive(struct ccan_file *f,
315 struct pp_conditions *parent)
317 struct pp_conditions *cond = talloc(f, struct pp_conditions);
320 line = remove_comments(line, false, &unused);
322 cond->parent = parent;
323 cond->type = PP_COND_IFDEF;
325 if (!get_token(&line, "#"))
328 if (get_token(&line, "if")) {
329 if (!parse_hash_if(cond, &line))
331 } else if (get_token(&line, "elif")) {
335 cond->parent = parent->parent;
336 /* FIXME: Not quite true. This implies !parent, but we don't
337 * do multiple conditionals yet. */
338 if (!parse_hash_if(cond, &line))
340 } else if (get_token(&line, "ifdef")) {
342 cond->inverse = false;
343 brackets = get_token(&line, "(");
344 cond->symbol = get_symbol_token(cond, &line);
347 if (brackets && !get_token(&line, ")"))
349 } else if (get_token(&line, "ifndef")) {
351 cond->inverse = true;
352 brackets = get_token(&line, "(");
353 cond->symbol = get_symbol_token(cond, &line);
356 if (brackets && !get_token(&line, ")"))
358 } else if (get_token(&line, "else")) {
364 cond->inverse = !cond->inverse;
366 } else if (get_token(&line, "endif")) {
372 return parent->parent;
374 /* Not a conditional. */
384 cond->type = PP_COND_UNKNOWN;
388 /* This parser is rough, but OK if code is reasonably neat. */
389 struct line_info *get_ccan_line_info(struct ccan_file *f)
391 bool continued = false, in_comment = false;
392 struct pp_conditions *cond = NULL;
398 get_ccan_file_lines(f);
399 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
401 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
405 /* Current conditions apply to this line. */
406 f->line_info[i].cond = cond;
407 f->line_info[i].continued = continued;
410 /* Same as last line. */
411 f->line_info[i].type = f->line_info[i-1].type;
412 /* Update in_comment. */
413 remove_comments(f->lines[i], in_comment, &in_comment);
417 /* Preprocessor directive? */
419 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
420 f->line_info[i].type = PREPROC_LINE;
421 cond = analyze_directive(f, f->lines[i], cond);
425 still_doc_line = (in_comment
426 && f->line_info[i-1].type == DOC_LINE);
428 p = remove_comments(f->lines[i], in_comment, &in_comment);
430 if (strstarts(f->lines[i], "/**") || still_doc_line)
431 f->line_info[i].type = DOC_LINE;
433 f->line_info[i].type = COMMENT_LINE;
435 f->line_info[i].type = CODE_LINE;
442 struct list_node list;
444 const unsigned int *value;
447 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
451 list_for_each(syms, i, list)
452 if (streq(sym, i->name))
457 static enum line_compiled get_pp(struct pp_conditions *cond,
458 struct list_head *syms)
462 enum line_compiled parent, ret;
464 /* No conditions? Easy. */
468 /* Check we get here at all. */
469 parent = get_pp(cond->parent, syms);
470 if (parent == NOT_COMPILED)
473 if (cond->type == PP_COND_UNKNOWN)
474 return MAYBE_COMPILED;
476 sym = find_symbol(syms, cond->symbol);
478 return MAYBE_COMPILED;
480 switch (cond->type) {
482 /* Undefined is 0. */
483 val = sym->value ? *sym->value : 0;
484 if (!val == cond->inverse)
491 if (cond->inverse == !sym->value)
501 /* If parent didn't know, NO == NO, but YES == MAYBE. */
502 if (parent == MAYBE_COMPILED && ret == COMPILED)
503 ret = MAYBE_COMPILED;
507 static void add_symbol(struct list_head *head,
508 const char *symbol, const unsigned int *value)
510 struct symbol *sym = talloc(head, struct symbol);
513 list_add(head, &sym->list);
516 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
518 const unsigned int *value,
521 enum line_compiled ret;
522 struct list_head *head;
525 head = talloc(NULL, struct list_head);
526 list_head_init(head);
529 add_symbol(head, symbol, value);
531 while ((symbol = va_arg(ap, const char *)) != NULL) {
532 value = va_arg(ap, const unsigned int *);
533 add_symbol(head, symbol, value);
535 ret = get_pp(cond, head);