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>
15 #include <sys/types.h>
27 static size_t dir_hash(const char *name)
29 return hash(name, strlen(name), 0);
32 static const char *manifest_name(const struct manifest *m)
37 static bool dir_cmp(const struct manifest *m, const char *dir)
39 return strcmp(m->dir, dir) == 0;
42 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp, manifest);
43 static struct htable_manifest *manifests;
45 const char *get_ccan_file_contents(struct ccan_file *f)
48 f->contents = grab_file(f, f->fullname, &f->contents_size);
50 err(1, "Reading file %s", f->fullname);
55 char **get_ccan_file_lines(struct ccan_file *f)
58 f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
60 /* FIXME: is f->num_lines necessary? */
61 f->num_lines = talloc_array_length(f->lines) - 1;
65 struct list_head *get_ccan_file_docs(struct ccan_file *f)
67 if (!f->doc_sections) {
68 get_ccan_file_lines(f);
69 f->doc_sections = extract_doc_sections(f->lines);
71 return f->doc_sections;
74 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
78 assert(dir[0] == '/');
80 f = talloc(ctx, struct ccan_file);
83 f->doc_sections = NULL;
85 f->name = talloc_steal(f, name);
86 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
88 f->cov_compiled = NULL;
93 static void add_files(struct manifest *m, const char *dir)
104 err(1, "Opening directory %s", dir[0] ? dir : ".");
106 while ((ent = readdir(d)) != NULL) {
109 struct list_head *dest;
112 if (ent->d_name[0] == '.')
115 f = new_ccan_file(m, m->dir,
116 talloc_asprintf(m, "%s%s",
118 if (lstat(f->name, &st) != 0)
119 err(1, "lstat %s", f->name);
121 if (S_ISDIR(st.st_mode)) {
122 size_t len = talloc_array_length(subs);
123 subs = talloc_realloc(m, subs, char *, len+1);
124 subs[len] = talloc_append_string(f->name, "/");
127 if (!S_ISREG(st.st_mode)) {
132 if (streq(f->name, "_info")) {
137 is_c_src = strends(f->name, ".c");
138 if (!is_c_src && !strends(f->name, ".h")) {
139 dest = &m->other_files;
140 } else if (!strchr(f->name, '/')) {
145 } else if (strstarts(f->name, "test/")) {
147 if (strstarts(f->name, "test/api"))
148 dest = &m->api_tests;
149 else if (strstarts(f->name, "test/run"))
150 dest = &m->run_tests;
151 else if (strstarts(f->name, "test/compile_ok"))
152 dest = &m->compile_ok_tests;
153 else if (strstarts(f->name, "test/compile_fail"))
154 dest = &m->compile_fail_tests;
156 dest = &m->other_test_c_files;
158 dest = &m->other_test_files;
160 dest = &m->other_files;
162 list_add(dest, &f->list);
166 /* Before we recurse, sanity check this is a ccan module. */
171 && list_empty(&m->c_files)
172 && list_empty(&m->h_files))
173 errx(1, "No _info, C or H files found here!");
175 for (i = 0; i < talloc_array_length(subs); i++)
176 add_files(m, subs[i]);
181 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
184 return strcmp((*a)->name, (*b)->name);
187 static void sort_files(struct list_head *list)
189 struct ccan_file **files = NULL, *f;
193 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
194 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
198 asort(files, num, cmp_names, NULL);
200 for (i = 0; i < num; i++)
201 list_add_tail(list, &files[i]->list);
205 struct manifest *get_manifest(const void *ctx, const char *dir)
208 char *olddir, *canon_dir;
210 struct list_head *list;
213 manifests = htable_manifest_new();
215 olddir = talloc_getcwd(NULL);
217 err(1, "Getting current directory");
220 err(1, "Failed to chdir to %s", dir);
222 canon_dir = talloc_getcwd(olddir);
224 err(1, "Getting current directory");
226 m = htable_manifest_get(manifests, canon_dir);
230 m = talloc_linked(ctx, talloc(NULL, struct manifest));
233 m->dir = talloc_steal(m, canon_dir);
234 list_head_init(&m->c_files);
235 list_head_init(&m->h_files);
236 list_head_init(&m->api_tests);
237 list_head_init(&m->run_tests);
238 list_head_init(&m->compile_ok_tests);
239 list_head_init(&m->compile_fail_tests);
240 list_head_init(&m->other_test_c_files);
241 list_head_init(&m->other_test_files);
242 list_head_init(&m->other_files);
243 list_head_init(&m->examples);
244 list_head_init(&m->mangled_examples);
245 list_head_init(&m->deps);
247 len = strlen(m->dir);
248 while (len && m->dir[len-1] == '/')
249 m->dir[--len] = '\0';
251 m->basename = strrchr(m->dir, '/');
253 errx(1, "I don't expect to be run from the root directory");
256 /* We expect the ccan dir to be two levels above module dir. */
259 dir = talloc_strdup(NULL, m->dir);
260 p = strrchr(dir, '/');
262 errx(1, "I expect the ccan root directory in ../..");
264 p = strrchr(dir, '/');
266 errx(1, "I expect the ccan root directory in ../..");
273 /* Nicer to run tests in a predictable order. */
274 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
275 &m->compile_fail_tests)
278 htable_manifest_add(manifests, m);
281 if (chdir(olddir) != 0)
282 err(1, "Returning to original directory '%s'", olddir);
290 * remove_comments - strip comments from a line, return copy.
291 * @line: line to copy
292 * @in_comment: are we already within a comment (from prev line).
293 * @unterminated: are we still in a comment for next line.
295 static char *remove_comments(const char *line, bool in_comment,
298 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
303 /* Find first comment. */
304 const char *old_comment = strstr(line, "/*");
305 const char *new_comment = strstr(line, "//");
308 if (new_comment && old_comment)
309 comment = new_comment < old_comment
310 ? new_comment : old_comment;
311 else if (old_comment)
312 comment = old_comment;
313 else if (new_comment)
314 comment = new_comment;
318 *unterminated = false;
322 /* Copy up to comment. */
323 memcpy(p, line, comment - line);
325 line += comment - line + 2;
327 if (comment == new_comment) {
328 /* We're done: goes to EOL. */
330 *unterminated = false;
337 const char *end = strstr(line, "*/");
339 *unterminated = true;
350 static bool is_empty(const char *line)
352 return strspn(line, " \t") == strlen(line);
355 static bool continues(const char *line)
357 /* Technically, any odd number of these. But who cares? */
358 return strends(line, "\\");
361 /* Get token if it's equal to token. */
362 bool get_token(const char **line, const char *token)
366 *line += strspn(*line, " \t");
367 if (cisalnum(token[0]) || token[0] == '_')
368 toklen = strspn(*line, IDENT_CHARS);
370 /* FIXME: real tokenizer handles ++ and other multi-chars. */
371 toklen = strlen(token);
374 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
381 char *get_symbol_token(void *ctx, const char **line)
386 *line += strspn(*line, " \t");
387 toklen = strspn(*line, IDENT_CHARS);
390 ret = talloc_strndup(ctx, *line, toklen);
395 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
397 bool brackets, defined;
399 cond->inverse = get_token(line, "!");
400 defined = get_token(line, "defined");
401 brackets = get_token(line, "(");
402 cond->symbol = get_symbol_token(cond, line);
405 if (brackets && !get_token(line, ")"))
408 cond->type = PP_COND_IF;
410 /* FIXME: We just chain them, ignoring operators. */
411 if (get_token(line, "||") || get_token(line, "&&")) {
412 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
414 sub->parent = cond->parent;
415 sub->type = PP_COND_IFDEF;
416 if (parse_hash_if(sub, line))
423 /* FIXME: Get serious! */
424 static struct pp_conditions *analyze_directive(struct ccan_file *f,
426 struct pp_conditions *parent)
428 struct pp_conditions *cond = talloc(f, struct pp_conditions);
431 line = remove_comments(line, false, &unused);
433 cond->parent = parent;
434 cond->type = PP_COND_IFDEF;
436 if (!get_token(&line, "#"))
439 if (get_token(&line, "if")) {
440 if (!parse_hash_if(cond, &line))
442 } else if (get_token(&line, "elif")) {
446 cond->parent = parent->parent;
447 /* FIXME: Not quite true. This implies !parent, but we don't
448 * do multiple conditionals yet. */
449 if (!parse_hash_if(cond, &line))
451 } else if (get_token(&line, "ifdef")) {
453 cond->inverse = false;
454 brackets = get_token(&line, "(");
455 cond->symbol = get_symbol_token(cond, &line);
458 if (brackets && !get_token(&line, ")"))
460 } else if (get_token(&line, "ifndef")) {
462 cond->inverse = true;
463 brackets = get_token(&line, "(");
464 cond->symbol = get_symbol_token(cond, &line);
467 if (brackets && !get_token(&line, ")"))
469 } else if (get_token(&line, "else")) {
475 cond->inverse = !cond->inverse;
477 } else if (get_token(&line, "endif")) {
483 return parent->parent;
485 /* Not a conditional. */
495 cond->type = PP_COND_UNKNOWN;
499 /* This parser is rough, but OK if code is reasonably neat. */
500 struct line_info *get_ccan_line_info(struct ccan_file *f)
502 bool continued = false, in_comment = false;
503 struct pp_conditions *cond = NULL;
509 get_ccan_file_lines(f);
510 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
512 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
516 /* Current conditions apply to this line. */
517 f->line_info[i].cond = cond;
518 f->line_info[i].continued = continued;
521 /* Same as last line. */
522 f->line_info[i].type = f->line_info[i-1].type;
523 /* Update in_comment. */
524 remove_comments(f->lines[i], in_comment, &in_comment);
528 /* Preprocessor directive? */
530 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
531 f->line_info[i].type = PREPROC_LINE;
532 cond = analyze_directive(f, f->lines[i], cond);
536 still_doc_line = (in_comment
537 && f->line_info[i-1].type == DOC_LINE);
539 p = remove_comments(f->lines[i], in_comment, &in_comment);
541 if (strstarts(f->lines[i], "/**") || still_doc_line)
542 f->line_info[i].type = DOC_LINE;
544 f->line_info[i].type = COMMENT_LINE;
546 f->line_info[i].type = CODE_LINE;
553 struct list_node list;
555 const unsigned int *value;
558 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
562 list_for_each(syms, i, list)
563 if (streq(sym, i->name))
568 static enum line_compiled get_pp(struct pp_conditions *cond,
569 struct list_head *syms)
573 enum line_compiled parent, ret;
575 /* No conditions? Easy. */
579 /* Check we get here at all. */
580 parent = get_pp(cond->parent, syms);
581 if (parent == NOT_COMPILED)
584 if (cond->type == PP_COND_UNKNOWN)
585 return MAYBE_COMPILED;
587 sym = find_symbol(syms, cond->symbol);
589 return MAYBE_COMPILED;
591 switch (cond->type) {
593 /* Undefined is 0. */
594 val = sym->value ? *sym->value : 0;
595 if (!val == cond->inverse)
602 if (cond->inverse == !sym->value)
612 /* If parent didn't know, NO == NO, but YES == MAYBE. */
613 if (parent == MAYBE_COMPILED && ret == COMPILED)
614 ret = MAYBE_COMPILED;
618 static void add_symbol(struct list_head *head,
619 const char *symbol, const unsigned int *value)
621 struct symbol *sym = talloc(head, struct symbol);
624 list_add(head, &sym->list);
627 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
629 const unsigned int *value,
632 enum line_compiled ret;
633 struct list_head *head;
636 head = talloc(NULL, struct list_head);
637 list_head_init(head);
640 add_symbol(head, symbol, value);
642 while ((symbol = va_arg(ap, const char *)) != NULL) {
643 value = va_arg(ap, const unsigned int *);
644 add_symbol(head, symbol, value);
646 ret = get_pp(cond, head);
651 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
652 const char *errorfmt, ...)
656 struct file_error *fe = talloc(score, struct file_error);
659 list_add_tail(&score->per_file_errors, &fe->list);
662 score->error = talloc_strdup(score, "");
664 if (verbose < 2 && strcount(score->error, "\n") > 5)
668 score->error = talloc_asprintf_append(score->error,
672 score->error = talloc_asprintf_append(score->error,
675 va_start(ap, errorfmt);
676 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
678 score->error = talloc_append_string(score->error, "\n");
680 if (verbose < 2 && strcount(score->error, "\n") > 5)
681 score->error = talloc_append_string(score->error,
682 "... more (use -vv to see them all)\n");