2 #include <ccan/talloc/talloc.h>
3 #include <ccan/str/str.h>
4 #include <ccan/str_talloc/str_talloc.h>
5 #include <ccan/grab_file/grab_file.h>
6 #include <ccan/noerr/noerr.h>
21 const char *get_ccan_file_contents(struct ccan_file *f)
24 f->contents = grab_file(f, f->fullname, &f->contents_size);
26 err(1, "Reading file %s", f->fullname);
31 char **get_ccan_file_lines(struct ccan_file *f)
34 f->lines = strsplit(f, get_ccan_file_contents(f),
40 struct list_head *get_ccan_file_docs(struct ccan_file *f)
42 if (!f->doc_sections) {
43 get_ccan_file_lines(f);
44 f->doc_sections = extract_doc_sections(f->lines, f->num_lines);
46 return f->doc_sections;
49 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
53 assert(dir[0] == '/');
55 f = talloc(ctx, struct ccan_file);
58 f->doc_sections = NULL;
60 f->name = talloc_steal(f, name);
61 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
63 f->cov_compiled = NULL;
67 static void add_files(struct manifest *m, const char *dir)
77 err(1, "Opening directory %s", dir[0] ? dir : ".");
79 while ((ent = readdir(d)) != NULL) {
82 struct list_head *dest;
85 if (ent->d_name[0] == '.')
88 f = new_ccan_file(m, m->dir,
89 talloc_asprintf(m, "%s%s",
91 if (lstat(f->name, &st) != 0)
92 err(1, "lstat %s", f->name);
94 if (S_ISDIR(st.st_mode)) {
95 f->name = talloc_append_string(f->name, "/");
96 add_files(m, f->name);
99 if (!S_ISREG(st.st_mode)) {
104 if (streq(f->name, "_info")) {
109 is_c_src = strends(f->name, ".c");
110 if (!is_c_src && !strends(f->name, ".h")) {
111 dest = &m->other_files;
115 if (!strchr(f->name, '/')) {
120 } else if (strstarts(f->name, "test/")) {
122 if (strstarts(f->name, "test/api"))
123 dest = &m->api_tests;
124 else if (strstarts(f->name, "test/run"))
125 dest = &m->run_tests;
126 else if (strstarts(f->name, "test/compile_ok"))
127 dest = &m->compile_ok_tests;
128 else if (strstarts(f->name, "test/compile_fail"))
129 dest = &m->compile_fail_tests;
131 dest = &m->other_test_c_files;
133 dest = &m->other_test_files;
135 dest = &m->other_files;
137 list_add(dest, &f->list);
142 char *report_on_lines(struct list_head *files,
143 char *(*report)(const char *),
148 list_for_each(files, f, list) {
150 char **lines = get_ccan_file_lines(f);
152 for (i = 0; i < f->num_lines; i++) {
153 char *r = report(lines[i]);
157 sofar = talloc_asprintf_append(sofar,
166 struct manifest *get_manifest(const void *ctx, const char *dir)
168 struct manifest *m = talloc(ctx, struct manifest);
173 list_head_init(&m->c_files);
174 list_head_init(&m->h_files);
175 list_head_init(&m->api_tests);
176 list_head_init(&m->run_tests);
177 list_head_init(&m->compile_ok_tests);
178 list_head_init(&m->compile_fail_tests);
179 list_head_init(&m->other_test_c_files);
180 list_head_init(&m->other_test_files);
181 list_head_init(&m->other_files);
182 list_head_init(&m->examples);
183 list_head_init(&m->dep_dirs);
185 olddir = talloc_getcwd(NULL);
187 err(1, "Getting current directory");
190 err(1, "Failed to chdir to %s", dir);
192 m->dir = talloc_getcwd(m);
194 err(1, "Getting current directory");
196 len = strlen(m->dir);
197 while (len && m->dir[len-1] == '/')
198 m->dir[--len] = '\0';
200 m->basename = strrchr(m->dir, '/');
202 errx(1, "I don't expect to be run from the root directory");
205 /* We expect the ccan dir to be two levels above module dir. */
208 ccan_dir = talloc_strdup(NULL, m->dir);
209 p = strrchr(ccan_dir, '/');
211 p = strrchr(ccan_dir, '/');
217 if (chdir(olddir) != 0)
218 err(1, "Returning to original directory '%s'", olddir);
226 * remove_comments - strip comments from a line, return copy.
227 * @line: line to copy
228 * @in_comment: are we already within a comment (from prev line).
229 * @unterminated: are we still in a comment for next line.
231 static char *remove_comments(const char *line, bool in_comment,
234 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
239 /* Find first comment. */
240 const char *old_comment = strstr(line, "/*");
241 const char *new_comment = strstr(line, "//");
244 if (new_comment && old_comment)
245 comment = new_comment < old_comment
246 ? new_comment : old_comment;
247 else if (old_comment)
248 comment = old_comment;
249 else if (new_comment)
250 comment = new_comment;
254 *unterminated = false;
258 /* Copy up to comment. */
259 memcpy(p, line, comment - line);
261 line += comment - line + 2;
263 if (comment == new_comment) {
264 /* We're done: goes to EOL. */
266 *unterminated = false;
273 const char *end = strstr(line, "*/");
275 *unterminated = true;
286 static bool is_empty(const char *line)
288 return strspn(line, " \t") == strlen(line);
291 static bool continues(const char *line)
293 /* Technically, any odd number of these. But who cares? */
294 return strends(line, "\\");
297 /* Get token if it's equal to token. */
298 bool get_token(const char **line, const char *token)
302 *line += strspn(*line, " \t");
303 if (isalnum(token[0]) || token[0] == '_')
304 toklen = strspn(*line, IDENT_CHARS);
306 /* FIXME: real tokenizer handles ++ and other multi-chars. */
307 toklen = strlen(token);
310 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
317 char *get_symbol_token(void *ctx, const char **line)
322 *line += strspn(*line, " \t");
323 toklen = strspn(*line, IDENT_CHARS);
326 ret = talloc_strndup(ctx, *line, toklen);
331 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
333 bool brackets, defined;
335 cond->inverse = get_token(line, "!");
336 defined = get_token(line, "defined");
337 brackets = get_token(line, "(");
338 cond->symbol = get_symbol_token(cond, line);
341 if (brackets && !get_token(line, ")"))
344 cond->type = PP_COND_IF;
348 /* FIXME: Get serious! */
349 static struct pp_conditions *analyze_directive(struct ccan_file *f,
351 struct pp_conditions *parent)
353 struct pp_conditions *cond = talloc(f, struct pp_conditions);
356 line = remove_comments(line, false, &unused);
358 cond->parent = parent;
359 cond->type = PP_COND_IFDEF;
361 if (!get_token(&line, "#"))
364 if (get_token(&line, "if")) {
365 if (!parse_hash_if(cond, &line))
367 } else if (get_token(&line, "elif")) {
371 cond->parent = parent->parent;
372 /* FIXME: Not quite true. This implies !parent, but we don't
373 * do multiple conditionals yet. */
374 if (!parse_hash_if(cond, &line))
376 } else if (get_token(&line, "ifdef")) {
378 cond->inverse = false;
379 brackets = get_token(&line, "(");
380 cond->symbol = get_symbol_token(cond, &line);
383 if (brackets && !get_token(&line, ")"))
385 } else if (get_token(&line, "ifndef")) {
387 cond->inverse = true;
388 brackets = get_token(&line, "(");
389 cond->symbol = get_symbol_token(cond, &line);
392 if (brackets && !get_token(&line, ")"))
394 } else if (get_token(&line, "else")) {
400 cond->inverse = !cond->inverse;
402 } else if (get_token(&line, "endif")) {
408 return parent->parent;
410 /* Not a conditional. */
420 cond->type = PP_COND_UNKNOWN;
424 /* This parser is rough, but OK if code is reasonably neat. */
425 struct line_info *get_ccan_line_info(struct ccan_file *f)
427 bool continued = false, in_comment = false;
428 struct pp_conditions *cond = NULL;
434 get_ccan_file_lines(f);
435 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
437 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
441 /* Current conditions apply to this line. */
442 f->line_info[i].cond = cond;
443 f->line_info[i].continued = continued;
446 /* Same as last line. */
447 f->line_info[i].type = f->line_info[i-1].type;
448 /* Update in_comment. */
449 remove_comments(f->lines[i], in_comment, &in_comment);
453 /* Preprocessor directive? */
455 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
456 f->line_info[i].type = PREPROC_LINE;
457 cond = analyze_directive(f, f->lines[i], cond);
461 still_doc_line = (in_comment
462 && f->line_info[i-1].type == DOC_LINE);
464 p = remove_comments(f->lines[i], in_comment, &in_comment);
466 if (strstarts(f->lines[i], "/**") || still_doc_line)
467 f->line_info[i].type = DOC_LINE;
469 f->line_info[i].type = COMMENT_LINE;
471 f->line_info[i].type = CODE_LINE;
478 struct list_node list;
480 const unsigned int *value;
483 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
487 list_for_each(syms, i, list)
488 if (streq(sym, i->name))
493 static enum line_compiled get_pp(struct pp_conditions *cond,
494 struct list_head *syms)
498 enum line_compiled parent, ret;
500 /* No conditions? Easy. */
504 /* Check we get here at all. */
505 parent = get_pp(cond->parent, syms);
506 if (parent == NOT_COMPILED)
509 if (cond->type == PP_COND_UNKNOWN)
510 return MAYBE_COMPILED;
512 sym = find_symbol(syms, cond->symbol);
514 return MAYBE_COMPILED;
516 switch (cond->type) {
518 /* Undefined is 0. */
519 val = sym->value ? *sym->value : 0;
520 if (!val == cond->inverse)
527 if (cond->inverse == !sym->value)
537 /* If parent didn't know, NO == NO, but YES == MAYBE. */
538 if (parent == MAYBE_COMPILED && ret == COMPILED)
539 ret = MAYBE_COMPILED;
543 static void add_symbol(struct list_head *head,
544 const char *symbol, const unsigned int *value)
546 struct symbol *sym = talloc(head, struct symbol);
549 list_add(head, &sym->list);
552 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
554 const unsigned int *value,
557 enum line_compiled ret;
558 struct list_head *head;
561 head = talloc(NULL, struct list_head);
562 list_head_init(head);
565 add_symbol(head, symbol, value);
567 while ((symbol = va_arg(ap, const char *)) != NULL) {
568 value = va_arg(ap, const unsigned int *);
569 add_symbol(head, symbol, value);
571 ret = get_pp(cond, head);