2 #include <ccan/talloc/talloc.h>
3 #include <ccan/str/str.h>
4 #include <ccan/str_talloc/str_talloc.h>
5 #include <ccan/talloc_link/talloc_link.h>
6 #include <ccan/hash/hash.h>
7 #include <ccan/htable/htable_type.h>
8 #include <ccan/grab_file/grab_file.h>
9 #include <ccan/noerr/noerr.h>
10 #include <ccan/foreach/foreach.h>
11 #include <ccan/asort/asort.h>
14 #include <sys/types.h>
26 static size_t dir_hash(const char *name)
28 return hash(name, strlen(name), 0);
31 static const char *manifest_name(const struct manifest *m)
36 static bool dir_cmp(const struct manifest *m, const char *dir)
38 return strcmp(m->dir, dir) == 0;
41 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp, manifest);
42 static struct htable_manifest *manifests;
44 const char *get_ccan_file_contents(struct ccan_file *f)
47 f->contents = grab_file(f, f->fullname, &f->contents_size);
49 err(1, "Reading file %s", f->fullname);
54 char **get_ccan_file_lines(struct ccan_file *f)
57 f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
59 /* FIXME: is f->num_lines necessary? */
60 f->num_lines = talloc_array_length(f->lines) - 1;
64 struct list_head *get_ccan_file_docs(struct ccan_file *f)
66 if (!f->doc_sections) {
67 get_ccan_file_lines(f);
68 f->doc_sections = extract_doc_sections(f->lines);
70 return f->doc_sections;
73 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
77 assert(dir[0] == '/');
79 f = talloc(ctx, struct ccan_file);
82 f->doc_sections = NULL;
84 f->name = talloc_steal(f, name);
85 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
87 f->cov_compiled = NULL;
91 static void add_files(struct manifest *m, const char *dir)
102 err(1, "Opening directory %s", dir[0] ? dir : ".");
104 while ((ent = readdir(d)) != NULL) {
107 struct list_head *dest;
110 if (ent->d_name[0] == '.')
113 f = new_ccan_file(m, m->dir,
114 talloc_asprintf(m, "%s%s",
116 if (lstat(f->name, &st) != 0)
117 err(1, "lstat %s", f->name);
119 if (S_ISDIR(st.st_mode)) {
120 size_t len = talloc_array_length(subs);
121 subs = talloc_realloc(m, subs, char *, len+1);
122 subs[len] = talloc_append_string(f->name, "/");
125 if (!S_ISREG(st.st_mode)) {
130 if (streq(f->name, "_info")) {
135 is_c_src = strends(f->name, ".c");
136 if (!is_c_src && !strends(f->name, ".h")) {
137 dest = &m->other_files;
138 } else if (!strchr(f->name, '/')) {
143 } else if (strstarts(f->name, "test/")) {
145 if (strstarts(f->name, "test/api"))
146 dest = &m->api_tests;
147 else if (strstarts(f->name, "test/run"))
148 dest = &m->run_tests;
149 else if (strstarts(f->name, "test/compile_ok"))
150 dest = &m->compile_ok_tests;
151 else if (strstarts(f->name, "test/compile_fail"))
152 dest = &m->compile_fail_tests;
154 dest = &m->other_test_c_files;
156 dest = &m->other_test_files;
158 dest = &m->other_files;
160 list_add(dest, &f->list);
164 /* Before we recurse, sanity check this is a ccan module. */
169 && list_empty(&m->c_files)
170 && list_empty(&m->h_files))
171 errx(1, "No _info, C or H files found here!");
173 for (i = 0; i < talloc_array_length(subs); i++)
174 add_files(m, subs[i]);
179 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
182 return strcmp((*a)->name, (*b)->name);
185 static void sort_files(struct list_head *list)
187 struct ccan_file **files = NULL, *f;
191 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
192 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
196 asort(files, num, cmp_names, NULL);
198 for (i = 0; i < num; i++)
199 list_add_tail(list, &files[i]->list);
203 struct manifest *get_manifest(const void *ctx, const char *dir)
206 char *olddir, *canon_dir;
208 struct list_head *list;
211 manifests = htable_manifest_new();
213 olddir = talloc_getcwd(NULL);
215 err(1, "Getting current directory");
218 err(1, "Failed to chdir to %s", dir);
220 canon_dir = talloc_getcwd(olddir);
222 err(1, "Getting current directory");
224 m = htable_manifest_get(manifests, canon_dir);
228 m = talloc_linked(ctx, talloc(NULL, struct manifest));
231 m->dir = talloc_steal(m, canon_dir);
232 list_head_init(&m->c_files);
233 list_head_init(&m->h_files);
234 list_head_init(&m->api_tests);
235 list_head_init(&m->run_tests);
236 list_head_init(&m->compile_ok_tests);
237 list_head_init(&m->compile_fail_tests);
238 list_head_init(&m->other_test_c_files);
239 list_head_init(&m->other_test_files);
240 list_head_init(&m->other_files);
241 list_head_init(&m->examples);
242 list_head_init(&m->mangled_examples);
243 list_head_init(&m->deps);
245 len = strlen(m->dir);
246 while (len && m->dir[len-1] == '/')
247 m->dir[--len] = '\0';
249 m->basename = strrchr(m->dir, '/');
251 errx(1, "I don't expect to be run from the root directory");
254 /* We expect the ccan dir to be two levels above module dir. */
257 dir = talloc_strdup(NULL, m->dir);
258 p = strrchr(dir, '/');
260 errx(1, "I expect the ccan root directory in ../..");
262 p = strrchr(dir, '/');
264 errx(1, "I expect the ccan root directory in ../..");
271 /* Nicer to run tests in a predictable order. */
272 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
273 &m->compile_fail_tests)
276 htable_manifest_add(manifests, m);
279 if (chdir(olddir) != 0)
280 err(1, "Returning to original directory '%s'", olddir);
288 * remove_comments - strip comments from a line, return copy.
289 * @line: line to copy
290 * @in_comment: are we already within a comment (from prev line).
291 * @unterminated: are we still in a comment for next line.
293 static char *remove_comments(const char *line, bool in_comment,
296 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
301 /* Find first comment. */
302 const char *old_comment = strstr(line, "/*");
303 const char *new_comment = strstr(line, "//");
306 if (new_comment && old_comment)
307 comment = new_comment < old_comment
308 ? new_comment : old_comment;
309 else if (old_comment)
310 comment = old_comment;
311 else if (new_comment)
312 comment = new_comment;
316 *unterminated = false;
320 /* Copy up to comment. */
321 memcpy(p, line, comment - line);
323 line += comment - line + 2;
325 if (comment == new_comment) {
326 /* We're done: goes to EOL. */
328 *unterminated = false;
335 const char *end = strstr(line, "*/");
337 *unterminated = true;
348 static bool is_empty(const char *line)
350 return strspn(line, " \t") == strlen(line);
353 static bool continues(const char *line)
355 /* Technically, any odd number of these. But who cares? */
356 return strends(line, "\\");
359 /* Get token if it's equal to token. */
360 bool get_token(const char **line, const char *token)
364 *line += strspn(*line, " \t");
365 if (cisalnum(token[0]) || token[0] == '_')
366 toklen = strspn(*line, IDENT_CHARS);
368 /* FIXME: real tokenizer handles ++ and other multi-chars. */
369 toklen = strlen(token);
372 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
379 char *get_symbol_token(void *ctx, const char **line)
384 *line += strspn(*line, " \t");
385 toklen = strspn(*line, IDENT_CHARS);
388 ret = talloc_strndup(ctx, *line, toklen);
393 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
395 bool brackets, defined;
397 cond->inverse = get_token(line, "!");
398 defined = get_token(line, "defined");
399 brackets = get_token(line, "(");
400 cond->symbol = get_symbol_token(cond, line);
403 if (brackets && !get_token(line, ")"))
406 cond->type = PP_COND_IF;
408 /* FIXME: We just chain them, ignoring operators. */
409 if (get_token(line, "||") || get_token(line, "&&")) {
410 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
412 sub->parent = cond->parent;
413 sub->type = PP_COND_IFDEF;
414 if (parse_hash_if(sub, line))
421 /* FIXME: Get serious! */
422 static struct pp_conditions *analyze_directive(struct ccan_file *f,
424 struct pp_conditions *parent)
426 struct pp_conditions *cond = talloc(f, struct pp_conditions);
429 line = remove_comments(line, false, &unused);
431 cond->parent = parent;
432 cond->type = PP_COND_IFDEF;
434 if (!get_token(&line, "#"))
437 if (get_token(&line, "if")) {
438 if (!parse_hash_if(cond, &line))
440 } else if (get_token(&line, "elif")) {
444 cond->parent = parent->parent;
445 /* FIXME: Not quite true. This implies !parent, but we don't
446 * do multiple conditionals yet. */
447 if (!parse_hash_if(cond, &line))
449 } else if (get_token(&line, "ifdef")) {
451 cond->inverse = false;
452 brackets = get_token(&line, "(");
453 cond->symbol = get_symbol_token(cond, &line);
456 if (brackets && !get_token(&line, ")"))
458 } else if (get_token(&line, "ifndef")) {
460 cond->inverse = true;
461 brackets = get_token(&line, "(");
462 cond->symbol = get_symbol_token(cond, &line);
465 if (brackets && !get_token(&line, ")"))
467 } else if (get_token(&line, "else")) {
473 cond->inverse = !cond->inverse;
475 } else if (get_token(&line, "endif")) {
481 return parent->parent;
483 /* Not a conditional. */
493 cond->type = PP_COND_UNKNOWN;
497 /* This parser is rough, but OK if code is reasonably neat. */
498 struct line_info *get_ccan_line_info(struct ccan_file *f)
500 bool continued = false, in_comment = false;
501 struct pp_conditions *cond = NULL;
507 get_ccan_file_lines(f);
508 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
510 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
514 /* Current conditions apply to this line. */
515 f->line_info[i].cond = cond;
516 f->line_info[i].continued = continued;
519 /* Same as last line. */
520 f->line_info[i].type = f->line_info[i-1].type;
521 /* Update in_comment. */
522 remove_comments(f->lines[i], in_comment, &in_comment);
526 /* Preprocessor directive? */
528 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
529 f->line_info[i].type = PREPROC_LINE;
530 cond = analyze_directive(f, f->lines[i], cond);
534 still_doc_line = (in_comment
535 && f->line_info[i-1].type == DOC_LINE);
537 p = remove_comments(f->lines[i], in_comment, &in_comment);
539 if (strstarts(f->lines[i], "/**") || still_doc_line)
540 f->line_info[i].type = DOC_LINE;
542 f->line_info[i].type = COMMENT_LINE;
544 f->line_info[i].type = CODE_LINE;
551 struct list_node list;
553 const unsigned int *value;
556 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
560 list_for_each(syms, i, list)
561 if (streq(sym, i->name))
566 static enum line_compiled get_pp(struct pp_conditions *cond,
567 struct list_head *syms)
571 enum line_compiled parent, ret;
573 /* No conditions? Easy. */
577 /* Check we get here at all. */
578 parent = get_pp(cond->parent, syms);
579 if (parent == NOT_COMPILED)
582 if (cond->type == PP_COND_UNKNOWN)
583 return MAYBE_COMPILED;
585 sym = find_symbol(syms, cond->symbol);
587 return MAYBE_COMPILED;
589 switch (cond->type) {
591 /* Undefined is 0. */
592 val = sym->value ? *sym->value : 0;
593 if (!val == cond->inverse)
600 if (cond->inverse == !sym->value)
610 /* If parent didn't know, NO == NO, but YES == MAYBE. */
611 if (parent == MAYBE_COMPILED && ret == COMPILED)
612 ret = MAYBE_COMPILED;
616 static void add_symbol(struct list_head *head,
617 const char *symbol, const unsigned int *value)
619 struct symbol *sym = talloc(head, struct symbol);
622 list_add(head, &sym->list);
625 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
627 const unsigned int *value,
630 enum line_compiled ret;
631 struct list_head *head;
634 head = talloc(NULL, struct list_head);
635 list_head_init(head);
638 add_symbol(head, symbol, value);
640 while ((symbol = va_arg(ap, const char *)) != NULL) {
641 value = va_arg(ap, const unsigned int *);
642 add_symbol(head, symbol, value);
644 ret = get_pp(cond, head);
649 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
650 const char *errorfmt, ...)
654 struct file_error *fe = talloc(score, struct file_error);
657 list_add_tail(&score->per_file_errors, &fe->list);
660 score->error = talloc_strdup(score, "");
662 if (verbose < 2 && strcount(score->error, "\n") > 5)
666 score->error = talloc_asprintf_append(score->error,
670 score->error = talloc_asprintf_append(score->error,
673 va_start(ap, errorfmt);
674 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
676 score->error = talloc_append_string(score->error, "\n");
678 if (verbose < 2 && strcount(score->error, "\n") > 5)
679 score->error = talloc_append_string(score->error,
680 "... more (use -vv to see them all)\n");