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;
141 if (!strchr(f->name, '/')) {
146 } else if (strstarts(f->name, "test/")) {
148 if (strstarts(f->name, "test/api"))
149 dest = &m->api_tests;
150 else if (strstarts(f->name, "test/run"))
151 dest = &m->run_tests;
152 else if (strstarts(f->name, "test/compile_ok"))
153 dest = &m->compile_ok_tests;
154 else if (strstarts(f->name, "test/compile_fail"))
155 dest = &m->compile_fail_tests;
157 dest = &m->other_test_c_files;
159 dest = &m->other_test_files;
161 dest = &m->other_files;
163 list_add(dest, &f->list);
167 /* Before we recurse, sanity check this is a ccan module. */
172 && list_empty(&m->c_files)
173 && list_empty(&m->h_files))
174 errx(1, "No _info, C or H files found here!");
176 for (i = 0; i < talloc_array_length(subs); i++)
177 add_files(m, subs[i]);
182 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
185 return strcmp((*a)->name, (*b)->name);
188 static void sort_files(struct list_head *list)
190 struct ccan_file **files = NULL, *f;
194 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
195 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
199 asort(files, num, cmp_names, NULL);
201 for (i = 0; i < num; i++)
202 list_add_tail(list, &files[i]->list);
206 struct manifest *get_manifest(const void *ctx, const char *dir)
209 char *olddir, *canon_dir;
211 struct list_head *list;
214 manifests = htable_manifest_new();
216 olddir = talloc_getcwd(NULL);
218 err(1, "Getting current directory");
221 err(1, "Failed to chdir to %s", dir);
223 canon_dir = talloc_getcwd(olddir);
225 err(1, "Getting current directory");
227 m = htable_manifest_get(manifests, canon_dir);
231 m = talloc_linked(ctx, talloc(NULL, struct manifest));
234 m->dir = talloc_steal(m, canon_dir);
235 list_head_init(&m->c_files);
236 list_head_init(&m->h_files);
237 list_head_init(&m->api_tests);
238 list_head_init(&m->run_tests);
239 list_head_init(&m->compile_ok_tests);
240 list_head_init(&m->compile_fail_tests);
241 list_head_init(&m->other_test_c_files);
242 list_head_init(&m->other_test_files);
243 list_head_init(&m->other_files);
244 list_head_init(&m->examples);
245 list_head_init(&m->mangled_examples);
246 list_head_init(&m->deps);
248 len = strlen(m->dir);
249 while (len && m->dir[len-1] == '/')
250 m->dir[--len] = '\0';
252 m->basename = strrchr(m->dir, '/');
254 errx(1, "I don't expect to be run from the root directory");
257 /* We expect the ccan dir to be two levels above module dir. */
260 ccan_dir = talloc_strdup(NULL, m->dir);
261 p = strrchr(ccan_dir, '/');
263 errx(1, "I expect the ccan root directory in ../..");
265 p = strrchr(ccan_dir, '/');
267 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 (isalnum(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;
412 /* FIXME: Get serious! */
413 static struct pp_conditions *analyze_directive(struct ccan_file *f,
415 struct pp_conditions *parent)
417 struct pp_conditions *cond = talloc(f, struct pp_conditions);
420 line = remove_comments(line, false, &unused);
422 cond->parent = parent;
423 cond->type = PP_COND_IFDEF;
425 if (!get_token(&line, "#"))
428 if (get_token(&line, "if")) {
429 if (!parse_hash_if(cond, &line))
431 } else if (get_token(&line, "elif")) {
435 cond->parent = parent->parent;
436 /* FIXME: Not quite true. This implies !parent, but we don't
437 * do multiple conditionals yet. */
438 if (!parse_hash_if(cond, &line))
440 } else if (get_token(&line, "ifdef")) {
442 cond->inverse = false;
443 brackets = get_token(&line, "(");
444 cond->symbol = get_symbol_token(cond, &line);
447 if (brackets && !get_token(&line, ")"))
449 } else if (get_token(&line, "ifndef")) {
451 cond->inverse = true;
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, "else")) {
464 cond->inverse = !cond->inverse;
466 } else if (get_token(&line, "endif")) {
472 return parent->parent;
474 /* Not a conditional. */
484 cond->type = PP_COND_UNKNOWN;
488 /* This parser is rough, but OK if code is reasonably neat. */
489 struct line_info *get_ccan_line_info(struct ccan_file *f)
491 bool continued = false, in_comment = false;
492 struct pp_conditions *cond = NULL;
498 get_ccan_file_lines(f);
499 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
501 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
505 /* Current conditions apply to this line. */
506 f->line_info[i].cond = cond;
507 f->line_info[i].continued = continued;
510 /* Same as last line. */
511 f->line_info[i].type = f->line_info[i-1].type;
512 /* Update in_comment. */
513 remove_comments(f->lines[i], in_comment, &in_comment);
517 /* Preprocessor directive? */
519 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
520 f->line_info[i].type = PREPROC_LINE;
521 cond = analyze_directive(f, f->lines[i], cond);
525 still_doc_line = (in_comment
526 && f->line_info[i-1].type == DOC_LINE);
528 p = remove_comments(f->lines[i], in_comment, &in_comment);
530 if (strstarts(f->lines[i], "/**") || still_doc_line)
531 f->line_info[i].type = DOC_LINE;
533 f->line_info[i].type = COMMENT_LINE;
535 f->line_info[i].type = CODE_LINE;
542 struct list_node list;
544 const unsigned int *value;
547 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
551 list_for_each(syms, i, list)
552 if (streq(sym, i->name))
557 static enum line_compiled get_pp(struct pp_conditions *cond,
558 struct list_head *syms)
562 enum line_compiled parent, ret;
564 /* No conditions? Easy. */
568 /* Check we get here at all. */
569 parent = get_pp(cond->parent, syms);
570 if (parent == NOT_COMPILED)
573 if (cond->type == PP_COND_UNKNOWN)
574 return MAYBE_COMPILED;
576 sym = find_symbol(syms, cond->symbol);
578 return MAYBE_COMPILED;
580 switch (cond->type) {
582 /* Undefined is 0. */
583 val = sym->value ? *sym->value : 0;
584 if (!val == cond->inverse)
591 if (cond->inverse == !sym->value)
601 /* If parent didn't know, NO == NO, but YES == MAYBE. */
602 if (parent == MAYBE_COMPILED && ret == COMPILED)
603 ret = MAYBE_COMPILED;
607 static void add_symbol(struct list_head *head,
608 const char *symbol, const unsigned int *value)
610 struct symbol *sym = talloc(head, struct symbol);
613 list_add(head, &sym->list);
616 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
618 const unsigned int *value,
621 enum line_compiled ret;
622 struct list_head *head;
625 head = talloc(NULL, struct list_head);
626 list_head_init(head);
629 add_symbol(head, symbol, value);
631 while ((symbol = va_arg(ap, const char *)) != NULL) {
632 value = va_arg(ap, const unsigned int *);
633 add_symbol(head, symbol, value);
635 ret = get_pp(cond, head);
640 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
641 const char *errorfmt, ...)
645 struct file_error *fe = talloc(score, struct file_error);
648 list_add_tail(&score->per_file_errors, &fe->list);
651 score->error = talloc_strdup(score, "");
653 if (verbose < 2 && strcount(score->error, "\n") > 5)
657 score->error = talloc_asprintf_append(score->error,
661 score->error = talloc_asprintf_append(score->error,
664 va_start(ap, errorfmt);
665 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
667 score->error = talloc_append_string(score->error, "\n");
669 if (verbose < 2 && strcount(score->error, "\n") > 5)
670 score->error = talloc_append_string(score->error,
671 "... more (use -vv to see them all)\n");