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)
101 err(1, "Opening directory %s", dir[0] ? dir : ".");
103 while ((ent = readdir(d)) != NULL) {
106 struct list_head *dest;
109 if (ent->d_name[0] == '.')
112 f = new_ccan_file(m, m->dir,
113 talloc_asprintf(m, "%s%s",
115 if (lstat(f->name, &st) != 0)
116 err(1, "lstat %s", f->name);
118 if (S_ISDIR(st.st_mode)) {
119 f->name = talloc_append_string(f->name, "/");
120 add_files(m, f->name);
123 if (!S_ISREG(st.st_mode)) {
128 if (streq(f->name, "_info")) {
133 is_c_src = strends(f->name, ".c");
134 if (!is_c_src && !strends(f->name, ".h")) {
135 dest = &m->other_files;
139 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);
166 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
169 return strcmp((*a)->name, (*b)->name);
172 static void sort_files(struct list_head *list)
174 struct ccan_file **files = NULL, *f;
178 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
179 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
183 asort(files, num, cmp_names, NULL);
185 for (i = 0; i < num; i++)
186 list_add_tail(list, &files[i]->list);
190 struct manifest *get_manifest(const void *ctx, const char *dir)
193 char *olddir, *canon_dir;
195 struct list_head *list;
198 manifests = htable_manifest_new();
200 olddir = talloc_getcwd(NULL);
202 err(1, "Getting current directory");
205 err(1, "Failed to chdir to %s", dir);
207 canon_dir = talloc_getcwd(olddir);
209 err(1, "Getting current directory");
211 m = htable_manifest_get(manifests, canon_dir);
215 m = talloc_linked(ctx, talloc(NULL, struct manifest));
218 m->dir = talloc_steal(m, canon_dir);
219 list_head_init(&m->c_files);
220 list_head_init(&m->h_files);
221 list_head_init(&m->api_tests);
222 list_head_init(&m->run_tests);
223 list_head_init(&m->compile_ok_tests);
224 list_head_init(&m->compile_fail_tests);
225 list_head_init(&m->other_test_c_files);
226 list_head_init(&m->other_test_files);
227 list_head_init(&m->other_files);
228 list_head_init(&m->examples);
229 list_head_init(&m->mangled_examples);
230 list_head_init(&m->deps);
232 len = strlen(m->dir);
233 while (len && m->dir[len-1] == '/')
234 m->dir[--len] = '\0';
236 m->basename = strrchr(m->dir, '/');
238 errx(1, "I don't expect to be run from the root directory");
241 /* We expect the ccan dir to be two levels above module dir. */
244 ccan_dir = talloc_strdup(NULL, m->dir);
245 p = strrchr(ccan_dir, '/');
247 p = strrchr(ccan_dir, '/');
253 /* Nicer to run tests in a predictable order. */
254 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
255 &m->compile_fail_tests)
258 htable_manifest_add(manifests, m);
261 if (chdir(olddir) != 0)
262 err(1, "Returning to original directory '%s'", olddir);
270 * remove_comments - strip comments from a line, return copy.
271 * @line: line to copy
272 * @in_comment: are we already within a comment (from prev line).
273 * @unterminated: are we still in a comment for next line.
275 static char *remove_comments(const char *line, bool in_comment,
278 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
283 /* Find first comment. */
284 const char *old_comment = strstr(line, "/*");
285 const char *new_comment = strstr(line, "//");
288 if (new_comment && old_comment)
289 comment = new_comment < old_comment
290 ? new_comment : old_comment;
291 else if (old_comment)
292 comment = old_comment;
293 else if (new_comment)
294 comment = new_comment;
298 *unterminated = false;
302 /* Copy up to comment. */
303 memcpy(p, line, comment - line);
305 line += comment - line + 2;
307 if (comment == new_comment) {
308 /* We're done: goes to EOL. */
310 *unterminated = false;
317 const char *end = strstr(line, "*/");
319 *unterminated = true;
330 static bool is_empty(const char *line)
332 return strspn(line, " \t") == strlen(line);
335 static bool continues(const char *line)
337 /* Technically, any odd number of these. But who cares? */
338 return strends(line, "\\");
341 /* Get token if it's equal to token. */
342 bool get_token(const char **line, const char *token)
346 *line += strspn(*line, " \t");
347 if (isalnum(token[0]) || token[0] == '_')
348 toklen = strspn(*line, IDENT_CHARS);
350 /* FIXME: real tokenizer handles ++ and other multi-chars. */
351 toklen = strlen(token);
354 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
361 char *get_symbol_token(void *ctx, const char **line)
366 *line += strspn(*line, " \t");
367 toklen = strspn(*line, IDENT_CHARS);
370 ret = talloc_strndup(ctx, *line, toklen);
375 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
377 bool brackets, defined;
379 cond->inverse = get_token(line, "!");
380 defined = get_token(line, "defined");
381 brackets = get_token(line, "(");
382 cond->symbol = get_symbol_token(cond, line);
385 if (brackets && !get_token(line, ")"))
388 cond->type = PP_COND_IF;
392 /* FIXME: Get serious! */
393 static struct pp_conditions *analyze_directive(struct ccan_file *f,
395 struct pp_conditions *parent)
397 struct pp_conditions *cond = talloc(f, struct pp_conditions);
400 line = remove_comments(line, false, &unused);
402 cond->parent = parent;
403 cond->type = PP_COND_IFDEF;
405 if (!get_token(&line, "#"))
408 if (get_token(&line, "if")) {
409 if (!parse_hash_if(cond, &line))
411 } else if (get_token(&line, "elif")) {
415 cond->parent = parent->parent;
416 /* FIXME: Not quite true. This implies !parent, but we don't
417 * do multiple conditionals yet. */
418 if (!parse_hash_if(cond, &line))
420 } else if (get_token(&line, "ifdef")) {
422 cond->inverse = false;
423 brackets = get_token(&line, "(");
424 cond->symbol = get_symbol_token(cond, &line);
427 if (brackets && !get_token(&line, ")"))
429 } else if (get_token(&line, "ifndef")) {
431 cond->inverse = true;
432 brackets = get_token(&line, "(");
433 cond->symbol = get_symbol_token(cond, &line);
436 if (brackets && !get_token(&line, ")"))
438 } else if (get_token(&line, "else")) {
444 cond->inverse = !cond->inverse;
446 } else if (get_token(&line, "endif")) {
452 return parent->parent;
454 /* Not a conditional. */
464 cond->type = PP_COND_UNKNOWN;
468 /* This parser is rough, but OK if code is reasonably neat. */
469 struct line_info *get_ccan_line_info(struct ccan_file *f)
471 bool continued = false, in_comment = false;
472 struct pp_conditions *cond = NULL;
478 get_ccan_file_lines(f);
479 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
481 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
485 /* Current conditions apply to this line. */
486 f->line_info[i].cond = cond;
487 f->line_info[i].continued = continued;
490 /* Same as last line. */
491 f->line_info[i].type = f->line_info[i-1].type;
492 /* Update in_comment. */
493 remove_comments(f->lines[i], in_comment, &in_comment);
497 /* Preprocessor directive? */
499 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
500 f->line_info[i].type = PREPROC_LINE;
501 cond = analyze_directive(f, f->lines[i], cond);
505 still_doc_line = (in_comment
506 && f->line_info[i-1].type == DOC_LINE);
508 p = remove_comments(f->lines[i], in_comment, &in_comment);
510 if (strstarts(f->lines[i], "/**") || still_doc_line)
511 f->line_info[i].type = DOC_LINE;
513 f->line_info[i].type = COMMENT_LINE;
515 f->line_info[i].type = CODE_LINE;
522 struct list_node list;
524 const unsigned int *value;
527 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
531 list_for_each(syms, i, list)
532 if (streq(sym, i->name))
537 static enum line_compiled get_pp(struct pp_conditions *cond,
538 struct list_head *syms)
542 enum line_compiled parent, ret;
544 /* No conditions? Easy. */
548 /* Check we get here at all. */
549 parent = get_pp(cond->parent, syms);
550 if (parent == NOT_COMPILED)
553 if (cond->type == PP_COND_UNKNOWN)
554 return MAYBE_COMPILED;
556 sym = find_symbol(syms, cond->symbol);
558 return MAYBE_COMPILED;
560 switch (cond->type) {
562 /* Undefined is 0. */
563 val = sym->value ? *sym->value : 0;
564 if (!val == cond->inverse)
571 if (cond->inverse == !sym->value)
581 /* If parent didn't know, NO == NO, but YES == MAYBE. */
582 if (parent == MAYBE_COMPILED && ret == COMPILED)
583 ret = MAYBE_COMPILED;
587 static void add_symbol(struct list_head *head,
588 const char *symbol, const unsigned int *value)
590 struct symbol *sym = talloc(head, struct symbol);
593 list_add(head, &sym->list);
596 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
598 const unsigned int *value,
601 enum line_compiled ret;
602 struct list_head *head;
605 head = talloc(NULL, struct list_head);
606 list_head_init(head);
609 add_symbol(head, symbol, value);
611 while ((symbol = va_arg(ap, const char *)) != NULL) {
612 value = va_arg(ap, const unsigned int *);
613 add_symbol(head, symbol, value);
615 ret = get_pp(cond, head);
620 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
621 const char *errorfmt, ...)
625 struct file_error *fe = talloc(score, struct file_error);
628 list_add_tail(&score->per_file_errors, &fe->list);
631 score->error = talloc_strdup(score, "");
633 if (verbose < 2 && strcount(score->error, "\n") > 5)
637 score->error = talloc_asprintf_append(score->error,
641 score->error = talloc_asprintf_append(score->error,
644 va_start(ap, errorfmt);
645 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
647 score->error = talloc_append_string(score->error, "\n");
649 if (verbose < 2 && strcount(score->error, "\n") > 5)
650 score->error = talloc_append_string(score->error,
651 "... more (use -vv to see them all)\n");