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;
92 static void add_files(struct manifest *m, const char *dir)
103 err(1, "Opening directory %s", dir[0] ? dir : ".");
105 while ((ent = readdir(d)) != NULL) {
108 struct list_head *dest;
111 if (ent->d_name[0] == '.')
114 f = new_ccan_file(m, m->dir,
115 talloc_asprintf(m, "%s%s",
117 if (lstat(f->name, &st) != 0)
118 err(1, "lstat %s", f->name);
120 if (S_ISDIR(st.st_mode)) {
121 size_t len = talloc_array_length(subs);
122 subs = talloc_realloc(m, subs, char *, len+1);
123 subs[len] = talloc_append_string(f->name, "/");
126 if (!S_ISREG(st.st_mode)) {
131 if (streq(f->name, "_info")) {
136 is_c_src = strends(f->name, ".c");
137 if (!is_c_src && !strends(f->name, ".h")) {
138 dest = &m->other_files;
139 } else if (!strchr(f->name, '/')) {
144 } else if (strstarts(f->name, "test/")) {
146 if (strstarts(f->name, "test/api"))
147 dest = &m->api_tests;
148 else if (strstarts(f->name, "test/run"))
149 dest = &m->run_tests;
150 else if (strstarts(f->name, "test/compile_ok"))
151 dest = &m->compile_ok_tests;
152 else if (strstarts(f->name, "test/compile_fail"))
153 dest = &m->compile_fail_tests;
155 dest = &m->other_test_c_files;
157 dest = &m->other_test_files;
159 dest = &m->other_files;
161 list_add(dest, &f->list);
165 /* Before we recurse, sanity check this is a ccan module. */
170 && list_empty(&m->c_files)
171 && list_empty(&m->h_files))
172 errx(1, "No _info, C or H files found here!");
174 for (i = 0; i < talloc_array_length(subs); i++)
175 add_files(m, subs[i]);
180 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
183 return strcmp((*a)->name, (*b)->name);
186 static void sort_files(struct list_head *list)
188 struct ccan_file **files = NULL, *f;
192 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
193 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
197 asort(files, num, cmp_names, NULL);
199 for (i = 0; i < num; i++)
200 list_add_tail(list, &files[i]->list);
204 struct manifest *get_manifest(const void *ctx, const char *dir)
207 char *olddir, *canon_dir;
209 struct list_head *list;
212 manifests = htable_manifest_new();
214 olddir = talloc_getcwd(NULL);
216 err(1, "Getting current directory");
219 err(1, "Failed to chdir to %s", dir);
221 canon_dir = talloc_getcwd(olddir);
223 err(1, "Getting current directory");
225 m = htable_manifest_get(manifests, canon_dir);
229 m = talloc_linked(ctx, talloc(NULL, struct manifest));
232 m->dir = talloc_steal(m, canon_dir);
233 list_head_init(&m->c_files);
234 list_head_init(&m->h_files);
235 list_head_init(&m->api_tests);
236 list_head_init(&m->run_tests);
237 list_head_init(&m->compile_ok_tests);
238 list_head_init(&m->compile_fail_tests);
239 list_head_init(&m->other_test_c_files);
240 list_head_init(&m->other_test_files);
241 list_head_init(&m->other_files);
242 list_head_init(&m->examples);
243 list_head_init(&m->mangled_examples);
244 list_head_init(&m->deps);
246 len = strlen(m->dir);
247 while (len && m->dir[len-1] == '/')
248 m->dir[--len] = '\0';
250 m->basename = strrchr(m->dir, '/');
252 errx(1, "I don't expect to be run from the root directory");
255 /* We expect the ccan dir to be two levels above module dir. */
258 dir = talloc_strdup(NULL, m->dir);
259 p = strrchr(dir, '/');
261 errx(1, "I expect the ccan root directory in ../..");
263 p = strrchr(dir, '/');
265 errx(1, "I expect the ccan root directory in ../..");
272 /* Nicer to run tests in a predictable order. */
273 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
274 &m->compile_fail_tests)
277 htable_manifest_add(manifests, m);
280 if (chdir(olddir) != 0)
281 err(1, "Returning to original directory '%s'", olddir);
289 * remove_comments - strip comments from a line, return copy.
290 * @line: line to copy
291 * @in_comment: are we already within a comment (from prev line).
292 * @unterminated: are we still in a comment for next line.
294 static char *remove_comments(const char *line, bool in_comment,
297 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
302 /* Find first comment. */
303 const char *old_comment = strstr(line, "/*");
304 const char *new_comment = strstr(line, "//");
307 if (new_comment && old_comment)
308 comment = new_comment < old_comment
309 ? new_comment : old_comment;
310 else if (old_comment)
311 comment = old_comment;
312 else if (new_comment)
313 comment = new_comment;
317 *unterminated = false;
321 /* Copy up to comment. */
322 memcpy(p, line, comment - line);
324 line += comment - line + 2;
326 if (comment == new_comment) {
327 /* We're done: goes to EOL. */
329 *unterminated = false;
336 const char *end = strstr(line, "*/");
338 *unterminated = true;
349 static bool is_empty(const char *line)
351 return strspn(line, " \t") == strlen(line);
354 static bool continues(const char *line)
356 /* Technically, any odd number of these. But who cares? */
357 return strends(line, "\\");
360 /* Get token if it's equal to token. */
361 bool get_token(const char **line, const char *token)
365 *line += strspn(*line, " \t");
366 if (cisalnum(token[0]) || token[0] == '_')
367 toklen = strspn(*line, IDENT_CHARS);
369 /* FIXME: real tokenizer handles ++ and other multi-chars. */
370 toklen = strlen(token);
373 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
380 char *get_symbol_token(void *ctx, const char **line)
385 *line += strspn(*line, " \t");
386 toklen = strspn(*line, IDENT_CHARS);
389 ret = talloc_strndup(ctx, *line, toklen);
394 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
396 bool brackets, defined;
398 cond->inverse = get_token(line, "!");
399 defined = get_token(line, "defined");
400 brackets = get_token(line, "(");
401 cond->symbol = get_symbol_token(cond, line);
404 if (brackets && !get_token(line, ")"))
407 cond->type = PP_COND_IF;
409 /* FIXME: We just chain them, ignoring operators. */
410 if (get_token(line, "||") || get_token(line, "&&")) {
411 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
413 sub->parent = cond->parent;
414 sub->type = PP_COND_IFDEF;
415 if (parse_hash_if(sub, line))
422 /* FIXME: Get serious! */
423 static struct pp_conditions *analyze_directive(struct ccan_file *f,
425 struct pp_conditions *parent)
427 struct pp_conditions *cond = talloc(f, struct pp_conditions);
430 line = remove_comments(line, false, &unused);
432 cond->parent = parent;
433 cond->type = PP_COND_IFDEF;
435 if (!get_token(&line, "#"))
438 if (get_token(&line, "if")) {
439 if (!parse_hash_if(cond, &line))
441 } else if (get_token(&line, "elif")) {
445 cond->parent = parent->parent;
446 /* FIXME: Not quite true. This implies !parent, but we don't
447 * do multiple conditionals yet. */
448 if (!parse_hash_if(cond, &line))
450 } else if (get_token(&line, "ifdef")) {
452 cond->inverse = false;
453 brackets = get_token(&line, "(");
454 cond->symbol = get_symbol_token(cond, &line);
457 if (brackets && !get_token(&line, ")"))
459 } else if (get_token(&line, "ifndef")) {
461 cond->inverse = true;
462 brackets = get_token(&line, "(");
463 cond->symbol = get_symbol_token(cond, &line);
466 if (brackets && !get_token(&line, ")"))
468 } else if (get_token(&line, "else")) {
474 cond->inverse = !cond->inverse;
476 } else if (get_token(&line, "endif")) {
482 return parent->parent;
484 /* Not a conditional. */
494 cond->type = PP_COND_UNKNOWN;
498 /* This parser is rough, but OK if code is reasonably neat. */
499 struct line_info *get_ccan_line_info(struct ccan_file *f)
501 bool continued = false, in_comment = false;
502 struct pp_conditions *cond = NULL;
508 get_ccan_file_lines(f);
509 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
511 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
515 /* Current conditions apply to this line. */
516 f->line_info[i].cond = cond;
517 f->line_info[i].continued = continued;
520 /* Same as last line. */
521 f->line_info[i].type = f->line_info[i-1].type;
522 /* Update in_comment. */
523 remove_comments(f->lines[i], in_comment, &in_comment);
527 /* Preprocessor directive? */
529 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
530 f->line_info[i].type = PREPROC_LINE;
531 cond = analyze_directive(f, f->lines[i], cond);
535 still_doc_line = (in_comment
536 && f->line_info[i-1].type == DOC_LINE);
538 p = remove_comments(f->lines[i], in_comment, &in_comment);
540 if (strstarts(f->lines[i], "/**") || still_doc_line)
541 f->line_info[i].type = DOC_LINE;
543 f->line_info[i].type = COMMENT_LINE;
545 f->line_info[i].type = CODE_LINE;
552 struct list_node list;
554 const unsigned int *value;
557 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
561 list_for_each(syms, i, list)
562 if (streq(sym, i->name))
567 static enum line_compiled get_pp(struct pp_conditions *cond,
568 struct list_head *syms)
572 enum line_compiled parent, ret;
574 /* No conditions? Easy. */
578 /* Check we get here at all. */
579 parent = get_pp(cond->parent, syms);
580 if (parent == NOT_COMPILED)
583 if (cond->type == PP_COND_UNKNOWN)
584 return MAYBE_COMPILED;
586 sym = find_symbol(syms, cond->symbol);
588 return MAYBE_COMPILED;
590 switch (cond->type) {
592 /* Undefined is 0. */
593 val = sym->value ? *sym->value : 0;
594 if (!val == cond->inverse)
601 if (cond->inverse == !sym->value)
611 /* If parent didn't know, NO == NO, but YES == MAYBE. */
612 if (parent == MAYBE_COMPILED && ret == COMPILED)
613 ret = MAYBE_COMPILED;
617 static void add_symbol(struct list_head *head,
618 const char *symbol, const unsigned int *value)
620 struct symbol *sym = talloc(head, struct symbol);
623 list_add(head, &sym->list);
626 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
628 const unsigned int *value,
631 enum line_compiled ret;
632 struct list_head *head;
635 head = talloc(NULL, struct list_head);
636 list_head_init(head);
639 add_symbol(head, symbol, value);
641 while ((symbol = va_arg(ap, const char *)) != NULL) {
642 value = va_arg(ap, const unsigned int *);
643 add_symbol(head, symbol, value);
645 ret = get_pp(cond, head);
650 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
651 const char *errorfmt, ...)
655 struct file_error *fe = talloc(score, struct file_error);
658 list_add_tail(&score->per_file_errors, &fe->list);
661 score->error = talloc_strdup(score, "");
663 if (verbose < 2 && strcount(score->error, "\n") > 5)
667 score->error = talloc_asprintf_append(score->error,
671 score->error = talloc_asprintf_append(score->error,
674 va_start(ap, errorfmt);
675 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
677 score->error = talloc_append_string(score->error, "\n");
679 if (verbose < 2 && strcount(score->error, "\n") > 5)
680 score->error = talloc_append_string(score->error,
681 "... more (use -vv to see them all)\n");