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>
13 #include <ccan/array_size/array_size.h>
16 #include <sys/types.h>
28 static size_t dir_hash(const char *name)
30 return hash(name, strlen(name), 0);
33 static const char *manifest_name(const struct manifest *m)
38 static bool dir_cmp(const struct manifest *m, const char *dir)
40 return strcmp(m->dir, dir) == 0;
43 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
45 static struct htable_manifest *manifests;
47 const char *get_ccan_file_contents(struct ccan_file *f)
50 f->contents = grab_file(f, f->fullname, &f->contents_size);
52 err(1, "Reading file %s", f->fullname);
57 char **get_ccan_file_lines(struct ccan_file *f)
60 f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
62 /* FIXME: is f->num_lines necessary? */
63 f->num_lines = talloc_array_length(f->lines) - 1;
67 struct list_head *get_ccan_file_docs(struct ccan_file *f)
69 if (!f->doc_sections) {
70 get_ccan_file_lines(f);
71 f->doc_sections = extract_doc_sections(f->lines);
73 return f->doc_sections;
76 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
81 assert(dir[0] == '/');
83 f = talloc(ctx, struct ccan_file);
86 f->doc_sections = NULL;
87 for (i = 0; i < ARRAY_SIZE(f->compiled); i++)
88 f->compiled[i] = NULL;
89 f->name = talloc_steal(f, name);
90 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
96 static void add_files(struct manifest *m, const char *dir)
107 err(1, "Opening directory %s", dir[0] ? dir : ".");
109 while ((ent = readdir(d)) != NULL) {
112 struct list_head *dest;
115 if (ent->d_name[0] == '.')
118 f = new_ccan_file(m, m->dir,
119 talloc_asprintf(m, "%s%s",
121 if (lstat(f->name, &st) != 0)
122 err(1, "lstat %s", f->name);
124 if (S_ISDIR(st.st_mode)) {
125 size_t len = talloc_array_length(subs);
126 subs = talloc_realloc(m, subs, char *, len+1);
127 subs[len] = talloc_append_string(f->name, "/");
130 if (!S_ISREG(st.st_mode)) {
135 if (streq(f->name, "_info")) {
140 is_c_src = strends(f->name, ".c");
141 if (!is_c_src && !strends(f->name, ".h")) {
142 dest = &m->other_files;
143 } else if (!strchr(f->name, '/')) {
148 } else if (strstarts(f->name, "test/")) {
150 if (strstarts(f->name, "test/api"))
151 dest = &m->api_tests;
152 else if (strstarts(f->name, "test/run"))
153 dest = &m->run_tests;
154 else if (strstarts(f->name, "test/compile_ok"))
155 dest = &m->compile_ok_tests;
156 else if (strstarts(f->name, "test/compile_fail"))
157 dest = &m->compile_fail_tests;
159 dest = &m->other_test_c_files;
161 dest = &m->other_test_files;
163 dest = &m->other_files;
165 list_add(dest, &f->list);
169 /* Before we recurse, sanity check this is a ccan module. */
174 && list_empty(&m->c_files)
175 && list_empty(&m->h_files))
176 errx(1, "No _info, C or H files found here!");
178 for (i = 0; i < talloc_array_length(subs); i++)
179 add_files(m, subs[i]);
184 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
187 return strcmp((*a)->name, (*b)->name);
190 static void sort_files(struct list_head *list)
192 struct ccan_file **files = NULL, *f;
196 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
197 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
201 asort(files, num, cmp_names, NULL);
203 for (i = 0; i < num; i++)
204 list_add_tail(list, &files[i]->list);
208 struct manifest *get_manifest(const void *ctx, const char *dir)
211 char *olddir, *canon_dir;
213 struct list_head *list;
216 manifests = talloc(NULL, struct htable_manifest);
217 htable_manifest_init(manifests);
220 olddir = talloc_getcwd(NULL);
222 err(1, "Getting current directory");
225 err(1, "Failed to chdir to %s", dir);
227 canon_dir = talloc_getcwd(olddir);
229 err(1, "Getting current directory");
231 m = htable_manifest_get(manifests, canon_dir);
235 m = talloc_linked(ctx, talloc(NULL, struct manifest));
237 m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
238 m->dir = talloc_steal(m, canon_dir);
239 list_head_init(&m->c_files);
240 list_head_init(&m->h_files);
241 list_head_init(&m->api_tests);
242 list_head_init(&m->run_tests);
243 list_head_init(&m->compile_ok_tests);
244 list_head_init(&m->compile_fail_tests);
245 list_head_init(&m->other_test_c_files);
246 list_head_init(&m->other_test_files);
247 list_head_init(&m->other_files);
248 list_head_init(&m->examples);
249 list_head_init(&m->mangled_examples);
250 list_head_init(&m->deps);
252 len = strlen(m->dir);
253 while (len && m->dir[len-1] == '/')
254 m->dir[--len] = '\0';
256 m->basename = strrchr(m->dir, '/');
258 errx(1, "I don't expect to be run from the root directory");
261 /* We expect the ccan dir to be two levels above module dir. */
264 dir = talloc_strdup(NULL, m->dir);
265 p = strrchr(dir, '/');
267 errx(1, "I expect the ccan root directory in ../..");
269 p = strrchr(dir, '/');
271 errx(1, "I expect the ccan root directory in ../..");
278 /* Nicer to run tests in a predictable order. */
279 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
280 &m->compile_fail_tests)
283 htable_manifest_add(manifests, m);
286 if (chdir(olddir) != 0)
287 err(1, "Returning to original directory '%s'", olddir);
295 * remove_comments - strip comments from a line, return copy.
296 * @line: line to copy
297 * @in_comment: are we already within a comment (from prev line).
298 * @unterminated: are we still in a comment for next line.
300 static char *remove_comments(const char *line, bool in_comment,
303 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
308 /* Find first comment. */
309 const char *old_comment = strstr(line, "/*");
310 const char *new_comment = strstr(line, "//");
313 if (new_comment && old_comment)
314 comment = new_comment < old_comment
315 ? new_comment : old_comment;
316 else if (old_comment)
317 comment = old_comment;
318 else if (new_comment)
319 comment = new_comment;
323 *unterminated = false;
327 /* Copy up to comment. */
328 memcpy(p, line, comment - line);
330 line += comment - line + 2;
332 if (comment == new_comment) {
333 /* We're done: goes to EOL. */
335 *unterminated = false;
342 const char *end = strstr(line, "*/");
344 *unterminated = true;
355 static bool is_empty(const char *line)
357 return strspn(line, " \r\t") == strlen(line);
360 static bool continues(const char *line)
362 /* Technically, any odd number of these. But who cares? */
363 return strends(line, "\\");
366 /* Get token if it's equal to token. */
367 bool get_token(const char **line, const char *token)
371 *line += strspn(*line, " \t");
372 if (cisalnum(token[0]) || token[0] == '_')
373 toklen = strspn(*line, IDENT_CHARS);
375 /* FIXME: real tokenizer handles ++ and other multi-chars. */
376 toklen = strlen(token);
379 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
386 char *get_symbol_token(void *ctx, const char **line)
391 *line += strspn(*line, " \t");
392 toklen = strspn(*line, IDENT_CHARS);
395 ret = talloc_strndup(ctx, *line, toklen);
400 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
402 bool brackets, defined;
404 cond->inverse = get_token(line, "!");
405 defined = get_token(line, "defined");
406 brackets = get_token(line, "(");
407 cond->symbol = get_symbol_token(cond, line);
410 if (brackets && !get_token(line, ")"))
413 cond->type = PP_COND_IF;
415 /* FIXME: We just chain them, ignoring operators. */
416 if (get_token(line, "||") || get_token(line, "&&")) {
417 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
419 sub->parent = cond->parent;
420 sub->type = PP_COND_IFDEF;
421 if (parse_hash_if(sub, line))
428 /* FIXME: Get serious! */
429 static struct pp_conditions *analyze_directive(struct ccan_file *f,
431 struct pp_conditions *parent)
433 struct pp_conditions *cond = talloc(f, struct pp_conditions);
436 line = remove_comments(line, false, &unused);
438 cond->parent = parent;
439 cond->type = PP_COND_IFDEF;
441 if (!get_token(&line, "#"))
444 if (get_token(&line, "if")) {
445 if (!parse_hash_if(cond, &line))
447 } else if (get_token(&line, "elif")) {
451 cond->parent = parent->parent;
452 /* FIXME: Not quite true. This implies !parent, but we don't
453 * do multiple conditionals yet. */
454 if (!parse_hash_if(cond, &line))
456 } else if (get_token(&line, "ifdef")) {
458 cond->inverse = false;
459 brackets = get_token(&line, "(");
460 cond->symbol = get_symbol_token(cond, &line);
463 if (brackets && !get_token(&line, ")"))
465 } else if (get_token(&line, "ifndef")) {
467 cond->inverse = true;
468 brackets = get_token(&line, "(");
469 cond->symbol = get_symbol_token(cond, &line);
472 if (brackets && !get_token(&line, ")"))
474 } else if (get_token(&line, "else")) {
480 cond->inverse = !cond->inverse;
482 } else if (get_token(&line, "endif")) {
488 return parent->parent;
490 /* Not a conditional. */
500 cond->type = PP_COND_UNKNOWN;
504 /* This parser is rough, but OK if code is reasonably neat. */
505 struct line_info *get_ccan_line_info(struct ccan_file *f)
507 bool continued = false, in_comment = false;
508 struct pp_conditions *cond = NULL;
514 get_ccan_file_lines(f);
515 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
517 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
521 /* Current conditions apply to this line. */
522 f->line_info[i].cond = cond;
523 f->line_info[i].continued = continued;
526 /* Same as last line. */
527 f->line_info[i].type = f->line_info[i-1].type;
528 /* Update in_comment. */
529 remove_comments(f->lines[i], in_comment, &in_comment);
533 /* Preprocessor directive? */
535 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
536 f->line_info[i].type = PREPROC_LINE;
537 cond = analyze_directive(f, f->lines[i], cond);
541 still_doc_line = (in_comment
542 && f->line_info[i-1].type == DOC_LINE);
544 p = remove_comments(f->lines[i], in_comment, &in_comment);
546 if (strstarts(f->lines[i], "/**") || still_doc_line)
547 f->line_info[i].type = DOC_LINE;
549 f->line_info[i].type = COMMENT_LINE;
551 f->line_info[i].type = CODE_LINE;
558 struct list_node list;
560 const unsigned int *value;
563 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
567 list_for_each(syms, i, list)
568 if (streq(sym, i->name))
573 static enum line_compiled get_pp(struct pp_conditions *cond,
574 struct list_head *syms)
578 enum line_compiled parent, ret;
580 /* No conditions? Easy. */
584 /* Check we get here at all. */
585 parent = get_pp(cond->parent, syms);
586 if (parent == NOT_COMPILED)
589 if (cond->type == PP_COND_UNKNOWN)
590 return MAYBE_COMPILED;
592 sym = find_symbol(syms, cond->symbol);
594 return MAYBE_COMPILED;
596 switch (cond->type) {
598 /* Undefined is 0. */
599 val = sym->value ? *sym->value : 0;
600 if (!val == cond->inverse)
607 if (cond->inverse == !sym->value)
617 /* If parent didn't know, NO == NO, but YES == MAYBE. */
618 if (parent == MAYBE_COMPILED && ret == COMPILED)
619 ret = MAYBE_COMPILED;
623 static void add_symbol(struct list_head *head,
624 const char *symbol, const unsigned int *value)
626 struct symbol *sym = talloc(head, struct symbol);
629 list_add(head, &sym->list);
632 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
634 const unsigned int *value,
637 enum line_compiled ret;
638 struct list_head *head;
641 head = talloc(NULL, struct list_head);
642 list_head_init(head);
645 add_symbol(head, symbol, value);
647 while ((symbol = va_arg(ap, const char *)) != NULL) {
648 value = va_arg(ap, const unsigned int *);
649 add_symbol(head, symbol, value);
651 ret = get_pp(cond, head);
656 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
657 const char *errorfmt, ...)
661 struct file_error *fe = talloc(score, struct file_error);
664 list_add_tail(&score->per_file_errors, &fe->list);
667 score->error = talloc_strdup(score, "");
669 if (verbose < 2 && strcount(score->error, "\n") > 5)
673 score->error = talloc_asprintf_append(score->error,
677 score->error = talloc_asprintf_append(score->error,
680 va_start(ap, errorfmt);
681 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
683 score->error = talloc_append_string(score->error, "\n");
685 if (verbose < 2 && strcount(score->error, "\n") > 5)
686 score->error = talloc_append_string(score->error,
687 "... more (use -vv to see them all)\n");