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 char *dir1, const char *dir2)
38 return strcmp(dir1, dir2) == 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),
63 struct list_head *get_ccan_file_docs(struct ccan_file *f)
65 if (!f->doc_sections) {
66 get_ccan_file_lines(f);
67 f->doc_sections = extract_doc_sections(f->lines, f->num_lines);
69 return f->doc_sections;
72 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
76 assert(dir[0] == '/');
78 f = talloc(ctx, struct ccan_file);
81 f->doc_sections = NULL;
83 f->name = talloc_steal(f, name);
84 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
86 f->cov_compiled = NULL;
90 static void add_files(struct manifest *m, const char *dir)
100 err(1, "Opening directory %s", dir[0] ? dir : ".");
102 while ((ent = readdir(d)) != NULL) {
105 struct list_head *dest;
108 if (ent->d_name[0] == '.')
111 f = new_ccan_file(m, m->dir,
112 talloc_asprintf(m, "%s%s",
114 if (lstat(f->name, &st) != 0)
115 err(1, "lstat %s", f->name);
117 if (S_ISDIR(st.st_mode)) {
118 f->name = talloc_append_string(f->name, "/");
119 add_files(m, f->name);
122 if (!S_ISREG(st.st_mode)) {
127 if (streq(f->name, "_info")) {
132 is_c_src = strends(f->name, ".c");
133 if (!is_c_src && !strends(f->name, ".h")) {
134 dest = &m->other_files;
138 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);
165 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
168 return strcmp((*a)->name, (*b)->name);
171 static void sort_files(struct list_head *list)
173 struct ccan_file **files = NULL, *f;
177 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
178 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
182 asort(files, num, cmp_names, NULL);
184 for (i = 0; i < num; i++)
185 list_add_tail(list, &files[i]->list);
189 struct manifest *get_manifest(const void *ctx, const char *dir)
192 char *olddir, *canon_dir;
194 struct list_head *list;
197 manifests = htable_manifest_new();
199 olddir = talloc_getcwd(NULL);
201 err(1, "Getting current directory");
204 err(1, "Failed to chdir to %s", dir);
206 canon_dir = talloc_getcwd(olddir);
208 err(1, "Getting current directory");
210 m = htable_manifest_get(manifests, canon_dir);
214 m = talloc_linked(ctx, talloc(NULL, struct manifest));
217 m->dir = talloc_steal(m, canon_dir);
218 list_head_init(&m->c_files);
219 list_head_init(&m->h_files);
220 list_head_init(&m->api_tests);
221 list_head_init(&m->run_tests);
222 list_head_init(&m->compile_ok_tests);
223 list_head_init(&m->compile_fail_tests);
224 list_head_init(&m->other_test_c_files);
225 list_head_init(&m->other_test_files);
226 list_head_init(&m->other_files);
227 list_head_init(&m->examples);
228 list_head_init(&m->mangled_examples);
229 list_head_init(&m->deps);
231 len = strlen(m->dir);
232 while (len && m->dir[len-1] == '/')
233 m->dir[--len] = '\0';
235 m->basename = strrchr(m->dir, '/');
237 errx(1, "I don't expect to be run from the root directory");
240 /* We expect the ccan dir to be two levels above module dir. */
243 ccan_dir = talloc_strdup(NULL, m->dir);
244 p = strrchr(ccan_dir, '/');
246 p = strrchr(ccan_dir, '/');
252 /* Nicer to run tests in a predictable order. */
253 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
254 &m->compile_fail_tests)
257 htable_manifest_add(manifests, m);
260 if (chdir(olddir) != 0)
261 err(1, "Returning to original directory '%s'", olddir);
269 * remove_comments - strip comments from a line, return copy.
270 * @line: line to copy
271 * @in_comment: are we already within a comment (from prev line).
272 * @unterminated: are we still in a comment for next line.
274 static char *remove_comments(const char *line, bool in_comment,
277 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
282 /* Find first comment. */
283 const char *old_comment = strstr(line, "/*");
284 const char *new_comment = strstr(line, "//");
287 if (new_comment && old_comment)
288 comment = new_comment < old_comment
289 ? new_comment : old_comment;
290 else if (old_comment)
291 comment = old_comment;
292 else if (new_comment)
293 comment = new_comment;
297 *unterminated = false;
301 /* Copy up to comment. */
302 memcpy(p, line, comment - line);
304 line += comment - line + 2;
306 if (comment == new_comment) {
307 /* We're done: goes to EOL. */
309 *unterminated = false;
316 const char *end = strstr(line, "*/");
318 *unterminated = true;
329 static bool is_empty(const char *line)
331 return strspn(line, " \t") == strlen(line);
334 static bool continues(const char *line)
336 /* Technically, any odd number of these. But who cares? */
337 return strends(line, "\\");
340 /* Get token if it's equal to token. */
341 bool get_token(const char **line, const char *token)
345 *line += strspn(*line, " \t");
346 if (isalnum(token[0]) || token[0] == '_')
347 toklen = strspn(*line, IDENT_CHARS);
349 /* FIXME: real tokenizer handles ++ and other multi-chars. */
350 toklen = strlen(token);
353 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
360 char *get_symbol_token(void *ctx, const char **line)
365 *line += strspn(*line, " \t");
366 toklen = strspn(*line, IDENT_CHARS);
369 ret = talloc_strndup(ctx, *line, toklen);
374 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
376 bool brackets, defined;
378 cond->inverse = get_token(line, "!");
379 defined = get_token(line, "defined");
380 brackets = get_token(line, "(");
381 cond->symbol = get_symbol_token(cond, line);
384 if (brackets && !get_token(line, ")"))
387 cond->type = PP_COND_IF;
391 /* FIXME: Get serious! */
392 static struct pp_conditions *analyze_directive(struct ccan_file *f,
394 struct pp_conditions *parent)
396 struct pp_conditions *cond = talloc(f, struct pp_conditions);
399 line = remove_comments(line, false, &unused);
401 cond->parent = parent;
402 cond->type = PP_COND_IFDEF;
404 if (!get_token(&line, "#"))
407 if (get_token(&line, "if")) {
408 if (!parse_hash_if(cond, &line))
410 } else if (get_token(&line, "elif")) {
414 cond->parent = parent->parent;
415 /* FIXME: Not quite true. This implies !parent, but we don't
416 * do multiple conditionals yet. */
417 if (!parse_hash_if(cond, &line))
419 } else if (get_token(&line, "ifdef")) {
421 cond->inverse = false;
422 brackets = get_token(&line, "(");
423 cond->symbol = get_symbol_token(cond, &line);
426 if (brackets && !get_token(&line, ")"))
428 } else if (get_token(&line, "ifndef")) {
430 cond->inverse = true;
431 brackets = get_token(&line, "(");
432 cond->symbol = get_symbol_token(cond, &line);
435 if (brackets && !get_token(&line, ")"))
437 } else if (get_token(&line, "else")) {
443 cond->inverse = !cond->inverse;
445 } else if (get_token(&line, "endif")) {
451 return parent->parent;
453 /* Not a conditional. */
463 cond->type = PP_COND_UNKNOWN;
467 /* This parser is rough, but OK if code is reasonably neat. */
468 struct line_info *get_ccan_line_info(struct ccan_file *f)
470 bool continued = false, in_comment = false;
471 struct pp_conditions *cond = NULL;
477 get_ccan_file_lines(f);
478 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
480 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
484 /* Current conditions apply to this line. */
485 f->line_info[i].cond = cond;
486 f->line_info[i].continued = continued;
489 /* Same as last line. */
490 f->line_info[i].type = f->line_info[i-1].type;
491 /* Update in_comment. */
492 remove_comments(f->lines[i], in_comment, &in_comment);
496 /* Preprocessor directive? */
498 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
499 f->line_info[i].type = PREPROC_LINE;
500 cond = analyze_directive(f, f->lines[i], cond);
504 still_doc_line = (in_comment
505 && f->line_info[i-1].type == DOC_LINE);
507 p = remove_comments(f->lines[i], in_comment, &in_comment);
509 if (strstarts(f->lines[i], "/**") || still_doc_line)
510 f->line_info[i].type = DOC_LINE;
512 f->line_info[i].type = COMMENT_LINE;
514 f->line_info[i].type = CODE_LINE;
521 struct list_node list;
523 const unsigned int *value;
526 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
530 list_for_each(syms, i, list)
531 if (streq(sym, i->name))
536 static enum line_compiled get_pp(struct pp_conditions *cond,
537 struct list_head *syms)
541 enum line_compiled parent, ret;
543 /* No conditions? Easy. */
547 /* Check we get here at all. */
548 parent = get_pp(cond->parent, syms);
549 if (parent == NOT_COMPILED)
552 if (cond->type == PP_COND_UNKNOWN)
553 return MAYBE_COMPILED;
555 sym = find_symbol(syms, cond->symbol);
557 return MAYBE_COMPILED;
559 switch (cond->type) {
561 /* Undefined is 0. */
562 val = sym->value ? *sym->value : 0;
563 if (!val == cond->inverse)
570 if (cond->inverse == !sym->value)
580 /* If parent didn't know, NO == NO, but YES == MAYBE. */
581 if (parent == MAYBE_COMPILED && ret == COMPILED)
582 ret = MAYBE_COMPILED;
586 static void add_symbol(struct list_head *head,
587 const char *symbol, const unsigned int *value)
589 struct symbol *sym = talloc(head, struct symbol);
592 list_add(head, &sym->list);
595 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
597 const unsigned int *value,
600 enum line_compiled ret;
601 struct list_head *head;
604 head = talloc(NULL, struct list_head);
605 list_head_init(head);
608 add_symbol(head, symbol, value);
610 while ((symbol = va_arg(ap, const char *)) != NULL) {
611 value = va_arg(ap, const unsigned int *);
612 add_symbol(head, symbol, value);
614 ret = get_pp(cond, head);
619 void score_file_error(struct score *score, struct ccan_file *f, unsigned line,
622 struct file_error *fe = talloc(score, struct file_error);
626 list_add_tail(&score->per_file_errors, &fe->list);