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 ccan_dir = talloc_strdup(NULL, m->dir);
258 p = strrchr(ccan_dir, '/');
260 errx(1, "I expect the ccan root directory in ../..");
262 p = strrchr(ccan_dir, '/');
264 errx(1, "I expect the ccan root directory in ../..");
270 /* Nicer to run tests in a predictable order. */
271 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
272 &m->compile_fail_tests)
275 htable_manifest_add(manifests, m);
278 if (chdir(olddir) != 0)
279 err(1, "Returning to original directory '%s'", olddir);
287 * remove_comments - strip comments from a line, return copy.
288 * @line: line to copy
289 * @in_comment: are we already within a comment (from prev line).
290 * @unterminated: are we still in a comment for next line.
292 static char *remove_comments(const char *line, bool in_comment,
295 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
300 /* Find first comment. */
301 const char *old_comment = strstr(line, "/*");
302 const char *new_comment = strstr(line, "//");
305 if (new_comment && old_comment)
306 comment = new_comment < old_comment
307 ? new_comment : old_comment;
308 else if (old_comment)
309 comment = old_comment;
310 else if (new_comment)
311 comment = new_comment;
315 *unterminated = false;
319 /* Copy up to comment. */
320 memcpy(p, line, comment - line);
322 line += comment - line + 2;
324 if (comment == new_comment) {
325 /* We're done: goes to EOL. */
327 *unterminated = false;
334 const char *end = strstr(line, "*/");
336 *unterminated = true;
347 static bool is_empty(const char *line)
349 return strspn(line, " \t") == strlen(line);
352 static bool continues(const char *line)
354 /* Technically, any odd number of these. But who cares? */
355 return strends(line, "\\");
358 /* Get token if it's equal to token. */
359 bool get_token(const char **line, const char *token)
363 *line += strspn(*line, " \t");
364 if (isalnum(token[0]) || token[0] == '_')
365 toklen = strspn(*line, IDENT_CHARS);
367 /* FIXME: real tokenizer handles ++ and other multi-chars. */
368 toklen = strlen(token);
371 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
378 char *get_symbol_token(void *ctx, const char **line)
383 *line += strspn(*line, " \t");
384 toklen = strspn(*line, IDENT_CHARS);
387 ret = talloc_strndup(ctx, *line, toklen);
392 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
394 bool brackets, defined;
396 cond->inverse = get_token(line, "!");
397 defined = get_token(line, "defined");
398 brackets = get_token(line, "(");
399 cond->symbol = get_symbol_token(cond, line);
402 if (brackets && !get_token(line, ")"))
405 cond->type = PP_COND_IF;
409 /* FIXME: Get serious! */
410 static struct pp_conditions *analyze_directive(struct ccan_file *f,
412 struct pp_conditions *parent)
414 struct pp_conditions *cond = talloc(f, struct pp_conditions);
417 line = remove_comments(line, false, &unused);
419 cond->parent = parent;
420 cond->type = PP_COND_IFDEF;
422 if (!get_token(&line, "#"))
425 if (get_token(&line, "if")) {
426 if (!parse_hash_if(cond, &line))
428 } else if (get_token(&line, "elif")) {
432 cond->parent = parent->parent;
433 /* FIXME: Not quite true. This implies !parent, but we don't
434 * do multiple conditionals yet. */
435 if (!parse_hash_if(cond, &line))
437 } else if (get_token(&line, "ifdef")) {
439 cond->inverse = false;
440 brackets = get_token(&line, "(");
441 cond->symbol = get_symbol_token(cond, &line);
444 if (brackets && !get_token(&line, ")"))
446 } else if (get_token(&line, "ifndef")) {
448 cond->inverse = true;
449 brackets = get_token(&line, "(");
450 cond->symbol = get_symbol_token(cond, &line);
453 if (brackets && !get_token(&line, ")"))
455 } else if (get_token(&line, "else")) {
461 cond->inverse = !cond->inverse;
463 } else if (get_token(&line, "endif")) {
469 return parent->parent;
471 /* Not a conditional. */
481 cond->type = PP_COND_UNKNOWN;
485 /* This parser is rough, but OK if code is reasonably neat. */
486 struct line_info *get_ccan_line_info(struct ccan_file *f)
488 bool continued = false, in_comment = false;
489 struct pp_conditions *cond = NULL;
495 get_ccan_file_lines(f);
496 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
498 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
502 /* Current conditions apply to this line. */
503 f->line_info[i].cond = cond;
504 f->line_info[i].continued = continued;
507 /* Same as last line. */
508 f->line_info[i].type = f->line_info[i-1].type;
509 /* Update in_comment. */
510 remove_comments(f->lines[i], in_comment, &in_comment);
514 /* Preprocessor directive? */
516 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
517 f->line_info[i].type = PREPROC_LINE;
518 cond = analyze_directive(f, f->lines[i], cond);
522 still_doc_line = (in_comment
523 && f->line_info[i-1].type == DOC_LINE);
525 p = remove_comments(f->lines[i], in_comment, &in_comment);
527 if (strstarts(f->lines[i], "/**") || still_doc_line)
528 f->line_info[i].type = DOC_LINE;
530 f->line_info[i].type = COMMENT_LINE;
532 f->line_info[i].type = CODE_LINE;
539 struct list_node list;
541 const unsigned int *value;
544 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
548 list_for_each(syms, i, list)
549 if (streq(sym, i->name))
554 static enum line_compiled get_pp(struct pp_conditions *cond,
555 struct list_head *syms)
559 enum line_compiled parent, ret;
561 /* No conditions? Easy. */
565 /* Check we get here at all. */
566 parent = get_pp(cond->parent, syms);
567 if (parent == NOT_COMPILED)
570 if (cond->type == PP_COND_UNKNOWN)
571 return MAYBE_COMPILED;
573 sym = find_symbol(syms, cond->symbol);
575 return MAYBE_COMPILED;
577 switch (cond->type) {
579 /* Undefined is 0. */
580 val = sym->value ? *sym->value : 0;
581 if (!val == cond->inverse)
588 if (cond->inverse == !sym->value)
598 /* If parent didn't know, NO == NO, but YES == MAYBE. */
599 if (parent == MAYBE_COMPILED && ret == COMPILED)
600 ret = MAYBE_COMPILED;
604 static void add_symbol(struct list_head *head,
605 const char *symbol, const unsigned int *value)
607 struct symbol *sym = talloc(head, struct symbol);
610 list_add(head, &sym->list);
613 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
615 const unsigned int *value,
618 enum line_compiled ret;
619 struct list_head *head;
622 head = talloc(NULL, struct list_head);
623 list_head_init(head);
626 add_symbol(head, symbol, value);
628 while ((symbol = va_arg(ap, const char *)) != NULL) {
629 value = va_arg(ap, const unsigned int *);
630 add_symbol(head, symbol, value);
632 ret = get_pp(cond, head);
637 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
638 const char *errorfmt, ...)
642 struct file_error *fe = talloc(score, struct file_error);
645 list_add_tail(&score->per_file_errors, &fe->list);
648 score->error = talloc_strdup(score, "");
650 if (verbose < 2 && strcount(score->error, "\n") > 5)
654 score->error = talloc_asprintf_append(score->error,
658 score->error = talloc_asprintf_append(score->error,
661 va_start(ap, errorfmt);
662 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
664 score->error = talloc_append_string(score->error, "\n");
666 if (verbose < 2 && strcount(score->error, "\n") > 5)
667 score->error = talloc_append_string(score->error,
668 "... more (use -vv to see them all)\n");