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 char **get_ccan_file_lines(struct ccan_file *f)
24 f->lines = strsplit(f, f->contents, "\n", &f->num_lines);
29 struct list_head *get_ccan_file_docs(struct ccan_file *f)
31 if (!f->doc_sections) {
32 get_ccan_file_lines(f);
33 f->doc_sections = extract_doc_sections(f->lines, f->num_lines);
35 return f->doc_sections;
38 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
42 assert(dir[0] == '/');
44 f = talloc(ctx, struct ccan_file);
47 f->doc_sections = NULL;
49 f->name = talloc_steal(f, name);
50 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
54 static void add_files(struct manifest *m, const char *dir)
64 err(1, "Opening directory %s", dir[0] ? dir : ".");
66 while ((ent = readdir(d)) != NULL) {
69 struct list_head *dest;
72 if (ent->d_name[0] == '.')
75 f = new_ccan_file(m, m->dir,
76 talloc_asprintf(m, "%s%s",
78 if (lstat(f->name, &st) != 0)
79 err(1, "lstat %s", f->name);
81 if (S_ISDIR(st.st_mode)) {
82 f->name = talloc_append_string(f->name, "/");
83 add_files(m, f->name);
86 if (!S_ISREG(st.st_mode)) {
91 if (streq(f->name, "_info")) {
93 f->contents = grab_file(f, f->name, &f->contents_size);
95 err(1, "Reading file %s", f->name);
99 is_c_src = strends(f->name, ".c");
100 if (!is_c_src && !strends(f->name, ".h")) {
101 /* We don't pull in contents of non-source files */
102 dest = &m->other_files;
106 f->contents = grab_file(f, f->name, &f->contents_size);
108 err(1, "Reading file %s", f->name);
110 if (!strchr(f->name, '/')) {
115 } else if (strstarts(f->name, "test/")) {
117 if (strstarts(f->name, "test/api"))
118 dest = &m->api_tests;
119 else if (strstarts(f->name, "test/run"))
120 dest = &m->run_tests;
121 else if (strstarts(f->name, "test/compile_ok"))
122 dest = &m->compile_ok_tests;
123 else if (strstarts(f->name, "test/compile_fail"))
124 dest = &m->compile_fail_tests;
126 dest = &m->other_test_c_files;
128 dest = &m->other_test_files;
130 dest = &m->other_files;
132 list_add(dest, &f->list);
137 char *report_on_lines(struct list_head *files,
138 char *(*report)(const char *),
143 list_for_each(files, f, list) {
145 char **lines = get_ccan_file_lines(f);
147 for (i = 0; i < f->num_lines; i++) {
148 char *r = report(lines[i]);
152 sofar = talloc_asprintf_append(sofar,
161 struct manifest *get_manifest(const void *ctx, const char *dir)
163 struct manifest *m = talloc(ctx, struct manifest);
168 list_head_init(&m->c_files);
169 list_head_init(&m->h_files);
170 list_head_init(&m->api_tests);
171 list_head_init(&m->run_tests);
172 list_head_init(&m->compile_ok_tests);
173 list_head_init(&m->compile_fail_tests);
174 list_head_init(&m->other_test_c_files);
175 list_head_init(&m->other_test_files);
176 list_head_init(&m->other_files);
177 list_head_init(&m->dep_dirs);
179 olddir = talloc_getcwd(NULL);
181 err(1, "Getting current directory");
184 err(1, "Failed to chdir to %s", dir);
186 m->dir = talloc_getcwd(m);
188 err(1, "Getting current directory");
190 len = strlen(m->dir);
191 while (len && m->dir[len-1] == '/')
192 m->dir[--len] = '\0';
194 m->basename = strrchr(m->dir, '/');
196 errx(1, "I don't expect to be run from the root directory");
199 /* We expect the ccan dir to be two levels above module dir. */
202 ccan_dir = talloc_strdup(NULL, m->dir);
203 p = strrchr(ccan_dir, '/');
205 p = strrchr(ccan_dir, '/');
211 if (chdir(olddir) != 0)
212 err(1, "Returning to original directory '%s'", olddir);
220 * remove_comments - strip comments from a line, return copy.
221 * @line: line to copy
222 * @in_comment: are we already within a comment (from prev line).
223 * @unterminated: are we still in a comment for next line.
225 static char *remove_comments(const char *line, bool in_comment,
228 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
233 /* Find first comment. */
234 const char *old_comment = strstr(line, "/*");
235 const char *new_comment = strstr(line, "//");
238 if (new_comment && old_comment)
239 comment = new_comment < old_comment
240 ? new_comment : old_comment;
241 else if (old_comment)
242 comment = old_comment;
243 else if (new_comment)
244 comment = new_comment;
248 *unterminated = false;
252 /* Copy up to comment. */
253 memcpy(p, line, comment - line);
255 line += comment - line + 2;
257 if (comment == new_comment) {
258 /* We're done: goes to EOL. */
260 *unterminated = false;
267 const char *end = strstr(line, "*/");
269 *unterminated = true;
280 static bool is_empty(const char *line)
282 return strspn(line, " \t") == strlen(line);
285 static bool continues(const char *line)
287 /* Technically, any odd number of these. But who cares? */
288 return strends(line, "\\");
291 /* Get token if it's equal to token. */
292 bool get_token(const char **line, const char *token)
296 *line += strspn(*line, " \t");
297 if (isalnum(token[0]) || token[0] == '_')
298 toklen = strspn(*line, IDENT_CHARS);
300 /* FIXME: real tokenizer handles ++ and other multi-chars. */
301 toklen = strlen(token);
304 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
311 char *get_symbol_token(void *ctx, const char **line)
316 *line += strspn(*line, " \t");
317 toklen = strspn(*line, IDENT_CHARS);
320 ret = talloc_strndup(ctx, *line, toklen);
325 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
327 bool brackets, defined;
329 cond->inverse = get_token(line, "!");
330 defined = get_token(line, "defined");
331 brackets = get_token(line, "(");
332 cond->symbol = get_symbol_token(cond, line);
335 if (brackets && !get_token(line, ")"))
338 cond->type = PP_COND_IF;
342 /* FIXME: Get serious! */
343 static struct pp_conditions *analyze_directive(struct ccan_file *f,
345 struct pp_conditions *parent)
347 struct pp_conditions *cond = talloc(f, struct pp_conditions);
350 line = remove_comments(line, false, &unused);
352 cond->parent = parent;
353 cond->type = PP_COND_IFDEF;
355 if (!get_token(&line, "#"))
358 if (get_token(&line, "if")) {
359 if (!parse_hash_if(cond, &line))
361 } else if (get_token(&line, "elif")) {
365 cond->parent = parent->parent;
366 /* FIXME: Not quite true. This implies !parent, but we don't
367 * do multiple conditionals yet. */
368 if (!parse_hash_if(cond, &line))
370 } else if (get_token(&line, "ifdef")) {
372 cond->inverse = false;
373 brackets = get_token(&line, "(");
374 cond->symbol = get_symbol_token(cond, &line);
377 if (brackets && !get_token(&line, ")"))
379 } else if (get_token(&line, "ifndef")) {
381 cond->inverse = true;
382 brackets = get_token(&line, "(");
383 cond->symbol = get_symbol_token(cond, &line);
386 if (brackets && !get_token(&line, ")"))
388 } else if (get_token(&line, "else")) {
394 cond->inverse = !cond->inverse;
396 } else if (get_token(&line, "endif")) {
402 return parent->parent;
404 /* Not a conditional. */
414 cond->type = PP_COND_UNKNOWN;
418 /* This parser is rough, but OK if code is reasonably neat. */
419 struct line_info *get_ccan_line_info(struct ccan_file *f)
421 bool continued = false, in_comment = false;
422 struct pp_conditions *cond = NULL;
428 get_ccan_file_lines(f);
429 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
431 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
435 /* Current conditions apply to this line. */
436 f->line_info[i].cond = cond;
437 f->line_info[i].continued = continued;
440 /* Same as last line. */
441 f->line_info[i].type = f->line_info[i-1].type;
442 /* Update in_comment. */
443 remove_comments(f->lines[i], in_comment, &in_comment);
447 /* Preprocessor directive? */
449 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
450 f->line_info[i].type = PREPROC_LINE;
451 cond = analyze_directive(f, f->lines[i], cond);
455 still_doc_line = (in_comment
456 && f->line_info[i-1].type == DOC_LINE);
458 p = remove_comments(f->lines[i], in_comment, &in_comment);
460 if (strstarts(f->lines[i], "/**") || still_doc_line)
461 f->line_info[i].type = DOC_LINE;
463 f->line_info[i].type = COMMENT_LINE;
465 f->line_info[i].type = CODE_LINE;
472 struct list_node list;
474 const unsigned int *value;
477 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
481 list_for_each(syms, i, list)
482 if (streq(sym, i->name))
487 static enum line_compiled get_pp(struct pp_conditions *cond,
488 struct list_head *syms)
492 enum line_compiled parent, ret;
494 /* No conditions? Easy. */
498 /* Check we get here at all. */
499 parent = get_pp(cond->parent, syms);
500 if (parent == NOT_COMPILED)
503 if (cond->type == PP_COND_UNKNOWN)
504 return MAYBE_COMPILED;
506 sym = find_symbol(syms, cond->symbol);
508 return MAYBE_COMPILED;
510 switch (cond->type) {
512 /* Undefined is 0. */
513 val = sym->value ? *sym->value : 0;
514 if (!val == cond->inverse)
521 if (cond->inverse == !sym->value)
531 /* If parent didn't know, NO == NO, but YES == MAYBE. */
532 if (parent == MAYBE_COMPILED && ret == COMPILED)
533 ret = MAYBE_COMPILED;
537 static void add_symbol(struct list_head *head,
538 const char *symbol, const unsigned int *value)
540 struct symbol *sym = talloc(head, struct symbol);
543 list_add(head, &sym->list);
546 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
548 const unsigned int *value,
551 enum line_compiled ret;
552 struct list_head *head;
555 head = talloc(NULL, struct list_head);
556 list_head_init(head);
559 add_symbol(head, symbol, value);
561 while ((symbol = va_arg(ap, const char *)) != NULL) {
562 value = va_arg(ap, const unsigned int *);
563 add_symbol(head, symbol, value);
565 ret = get_pp(cond, head);