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, manifest);
44 static struct htable_manifest *manifests;
46 const char *get_ccan_file_contents(struct ccan_file *f)
49 f->contents = grab_file(f, f->fullname, &f->contents_size);
51 err(1, "Reading file %s", f->fullname);
56 char **get_ccan_file_lines(struct ccan_file *f)
59 f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
61 /* FIXME: is f->num_lines necessary? */
62 f->num_lines = talloc_array_length(f->lines) - 1;
66 struct list_head *get_ccan_file_docs(struct ccan_file *f)
68 if (!f->doc_sections) {
69 get_ccan_file_lines(f);
70 f->doc_sections = extract_doc_sections(f->lines);
72 return f->doc_sections;
75 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
80 assert(dir[0] == '/');
82 f = talloc(ctx, struct ccan_file);
85 f->doc_sections = NULL;
86 for (i = 0; i < ARRAY_SIZE(f->compiled); i++)
87 f->compiled[i] = NULL;
88 f->name = talloc_steal(f, name);
89 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
95 static void add_files(struct manifest *m, const char *dir)
106 err(1, "Opening directory %s", dir[0] ? dir : ".");
108 while ((ent = readdir(d)) != NULL) {
111 struct list_head *dest;
114 if (ent->d_name[0] == '.')
117 f = new_ccan_file(m, m->dir,
118 talloc_asprintf(m, "%s%s",
120 if (lstat(f->name, &st) != 0)
121 err(1, "lstat %s", f->name);
123 if (S_ISDIR(st.st_mode)) {
124 size_t len = talloc_array_length(subs);
125 subs = talloc_realloc(m, subs, char *, len+1);
126 subs[len] = talloc_append_string(f->name, "/");
129 if (!S_ISREG(st.st_mode)) {
134 if (streq(f->name, "_info")) {
139 is_c_src = strends(f->name, ".c");
140 if (!is_c_src && !strends(f->name, ".h")) {
141 dest = &m->other_files;
142 } else if (!strchr(f->name, '/')) {
147 } else if (strstarts(f->name, "test/")) {
149 if (strstarts(f->name, "test/api"))
150 dest = &m->api_tests;
151 else if (strstarts(f->name, "test/run"))
152 dest = &m->run_tests;
153 else if (strstarts(f->name, "test/compile_ok"))
154 dest = &m->compile_ok_tests;
155 else if (strstarts(f->name, "test/compile_fail"))
156 dest = &m->compile_fail_tests;
158 dest = &m->other_test_c_files;
160 dest = &m->other_test_files;
162 dest = &m->other_files;
164 list_add(dest, &f->list);
168 /* Before we recurse, sanity check this is a ccan module. */
173 && list_empty(&m->c_files)
174 && list_empty(&m->h_files))
175 errx(1, "No _info, C or H files found here!");
177 for (i = 0; i < talloc_array_length(subs); i++)
178 add_files(m, subs[i]);
183 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
186 return strcmp((*a)->name, (*b)->name);
189 static void sort_files(struct list_head *list)
191 struct ccan_file **files = NULL, *f;
195 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
196 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
200 asort(files, num, cmp_names, NULL);
202 for (i = 0; i < num; i++)
203 list_add_tail(list, &files[i]->list);
207 struct manifest *get_manifest(const void *ctx, const char *dir)
210 char *olddir, *canon_dir;
212 struct list_head *list;
215 manifests = htable_manifest_new();
217 olddir = talloc_getcwd(NULL);
219 err(1, "Getting current directory");
222 err(1, "Failed to chdir to %s", dir);
224 canon_dir = talloc_getcwd(olddir);
226 err(1, "Getting current directory");
228 m = htable_manifest_get(manifests, canon_dir);
232 m = talloc_linked(ctx, talloc(NULL, struct manifest));
234 m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
235 m->dir = talloc_steal(m, canon_dir);
236 list_head_init(&m->c_files);
237 list_head_init(&m->h_files);
238 list_head_init(&m->api_tests);
239 list_head_init(&m->run_tests);
240 list_head_init(&m->compile_ok_tests);
241 list_head_init(&m->compile_fail_tests);
242 list_head_init(&m->other_test_c_files);
243 list_head_init(&m->other_test_files);
244 list_head_init(&m->other_files);
245 list_head_init(&m->examples);
246 list_head_init(&m->mangled_examples);
247 list_head_init(&m->deps);
249 len = strlen(m->dir);
250 while (len && m->dir[len-1] == '/')
251 m->dir[--len] = '\0';
253 m->basename = strrchr(m->dir, '/');
255 errx(1, "I don't expect to be run from the root directory");
258 /* We expect the ccan dir to be two levels above module dir. */
261 dir = talloc_strdup(NULL, m->dir);
262 p = strrchr(dir, '/');
264 errx(1, "I expect the ccan root directory in ../..");
266 p = strrchr(dir, '/');
268 errx(1, "I expect the ccan root directory in ../..");
275 /* Nicer to run tests in a predictable order. */
276 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
277 &m->compile_fail_tests)
280 htable_manifest_add(manifests, m);
283 if (chdir(olddir) != 0)
284 err(1, "Returning to original directory '%s'", olddir);
292 * remove_comments - strip comments from a line, return copy.
293 * @line: line to copy
294 * @in_comment: are we already within a comment (from prev line).
295 * @unterminated: are we still in a comment for next line.
297 static char *remove_comments(const char *line, bool in_comment,
300 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
305 /* Find first comment. */
306 const char *old_comment = strstr(line, "/*");
307 const char *new_comment = strstr(line, "//");
310 if (new_comment && old_comment)
311 comment = new_comment < old_comment
312 ? new_comment : old_comment;
313 else if (old_comment)
314 comment = old_comment;
315 else if (new_comment)
316 comment = new_comment;
320 *unterminated = false;
324 /* Copy up to comment. */
325 memcpy(p, line, comment - line);
327 line += comment - line + 2;
329 if (comment == new_comment) {
330 /* We're done: goes to EOL. */
332 *unterminated = false;
339 const char *end = strstr(line, "*/");
341 *unterminated = true;
352 static bool is_empty(const char *line)
354 return strspn(line, " \r\t") == strlen(line);
357 static bool continues(const char *line)
359 /* Technically, any odd number of these. But who cares? */
360 return strends(line, "\\");
363 /* Get token if it's equal to token. */
364 bool get_token(const char **line, const char *token)
368 *line += strspn(*line, " \t");
369 if (cisalnum(token[0]) || token[0] == '_')
370 toklen = strspn(*line, IDENT_CHARS);
372 /* FIXME: real tokenizer handles ++ and other multi-chars. */
373 toklen = strlen(token);
376 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
383 char *get_symbol_token(void *ctx, const char **line)
388 *line += strspn(*line, " \t");
389 toklen = strspn(*line, IDENT_CHARS);
392 ret = talloc_strndup(ctx, *line, toklen);
397 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
399 bool brackets, defined;
401 cond->inverse = get_token(line, "!");
402 defined = get_token(line, "defined");
403 brackets = get_token(line, "(");
404 cond->symbol = get_symbol_token(cond, line);
407 if (brackets && !get_token(line, ")"))
410 cond->type = PP_COND_IF;
412 /* FIXME: We just chain them, ignoring operators. */
413 if (get_token(line, "||") || get_token(line, "&&")) {
414 struct pp_conditions *sub = talloc(cond, struct pp_conditions);
416 sub->parent = cond->parent;
417 sub->type = PP_COND_IFDEF;
418 if (parse_hash_if(sub, line))
425 /* FIXME: Get serious! */
426 static struct pp_conditions *analyze_directive(struct ccan_file *f,
428 struct pp_conditions *parent)
430 struct pp_conditions *cond = talloc(f, struct pp_conditions);
433 line = remove_comments(line, false, &unused);
435 cond->parent = parent;
436 cond->type = PP_COND_IFDEF;
438 if (!get_token(&line, "#"))
441 if (get_token(&line, "if")) {
442 if (!parse_hash_if(cond, &line))
444 } else if (get_token(&line, "elif")) {
448 cond->parent = parent->parent;
449 /* FIXME: Not quite true. This implies !parent, but we don't
450 * do multiple conditionals yet. */
451 if (!parse_hash_if(cond, &line))
453 } else if (get_token(&line, "ifdef")) {
455 cond->inverse = false;
456 brackets = get_token(&line, "(");
457 cond->symbol = get_symbol_token(cond, &line);
460 if (brackets && !get_token(&line, ")"))
462 } else if (get_token(&line, "ifndef")) {
464 cond->inverse = true;
465 brackets = get_token(&line, "(");
466 cond->symbol = get_symbol_token(cond, &line);
469 if (brackets && !get_token(&line, ")"))
471 } else if (get_token(&line, "else")) {
477 cond->inverse = !cond->inverse;
479 } else if (get_token(&line, "endif")) {
485 return parent->parent;
487 /* Not a conditional. */
497 cond->type = PP_COND_UNKNOWN;
501 /* This parser is rough, but OK if code is reasonably neat. */
502 struct line_info *get_ccan_line_info(struct ccan_file *f)
504 bool continued = false, in_comment = false;
505 struct pp_conditions *cond = NULL;
511 get_ccan_file_lines(f);
512 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
514 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
518 /* Current conditions apply to this line. */
519 f->line_info[i].cond = cond;
520 f->line_info[i].continued = continued;
523 /* Same as last line. */
524 f->line_info[i].type = f->line_info[i-1].type;
525 /* Update in_comment. */
526 remove_comments(f->lines[i], in_comment, &in_comment);
530 /* Preprocessor directive? */
532 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
533 f->line_info[i].type = PREPROC_LINE;
534 cond = analyze_directive(f, f->lines[i], cond);
538 still_doc_line = (in_comment
539 && f->line_info[i-1].type == DOC_LINE);
541 p = remove_comments(f->lines[i], in_comment, &in_comment);
543 if (strstarts(f->lines[i], "/**") || still_doc_line)
544 f->line_info[i].type = DOC_LINE;
546 f->line_info[i].type = COMMENT_LINE;
548 f->line_info[i].type = CODE_LINE;
555 struct list_node list;
557 const unsigned int *value;
560 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
564 list_for_each(syms, i, list)
565 if (streq(sym, i->name))
570 static enum line_compiled get_pp(struct pp_conditions *cond,
571 struct list_head *syms)
575 enum line_compiled parent, ret;
577 /* No conditions? Easy. */
581 /* Check we get here at all. */
582 parent = get_pp(cond->parent, syms);
583 if (parent == NOT_COMPILED)
586 if (cond->type == PP_COND_UNKNOWN)
587 return MAYBE_COMPILED;
589 sym = find_symbol(syms, cond->symbol);
591 return MAYBE_COMPILED;
593 switch (cond->type) {
595 /* Undefined is 0. */
596 val = sym->value ? *sym->value : 0;
597 if (!val == cond->inverse)
604 if (cond->inverse == !sym->value)
614 /* If parent didn't know, NO == NO, but YES == MAYBE. */
615 if (parent == MAYBE_COMPILED && ret == COMPILED)
616 ret = MAYBE_COMPILED;
620 static void add_symbol(struct list_head *head,
621 const char *symbol, const unsigned int *value)
623 struct symbol *sym = talloc(head, struct symbol);
626 list_add(head, &sym->list);
629 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
631 const unsigned int *value,
634 enum line_compiled ret;
635 struct list_head *head;
638 head = talloc(NULL, struct list_head);
639 list_head_init(head);
642 add_symbol(head, symbol, value);
644 while ((symbol = va_arg(ap, const char *)) != NULL) {
645 value = va_arg(ap, const unsigned int *);
646 add_symbol(head, symbol, value);
648 ret = get_pp(cond, head);
653 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
654 const char *errorfmt, ...)
658 struct file_error *fe = talloc(score, struct file_error);
661 list_add_tail(&score->per_file_errors, &fe->list);
664 score->error = talloc_strdup(score, "");
666 if (verbose < 2 && strcount(score->error, "\n") > 5)
670 score->error = talloc_asprintf_append(score->error,
674 score->error = talloc_asprintf_append(score->error,
677 va_start(ap, errorfmt);
678 score->error = talloc_vasprintf_append(score->error, errorfmt, ap);
680 score->error = talloc_append_string(score->error, "\n");
682 if (verbose < 2 && strcount(score->error, "\n") > 5)
683 score->error = talloc_append_string(score->error,
684 "... more (use -vv to see them all)\n");