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->examples);
178 list_head_init(&m->dep_dirs);
180 olddir = talloc_getcwd(NULL);
182 err(1, "Getting current directory");
185 err(1, "Failed to chdir to %s", dir);
187 m->dir = talloc_getcwd(m);
189 err(1, "Getting current directory");
191 len = strlen(m->dir);
192 while (len && m->dir[len-1] == '/')
193 m->dir[--len] = '\0';
195 m->basename = strrchr(m->dir, '/');
197 errx(1, "I don't expect to be run from the root directory");
200 /* We expect the ccan dir to be two levels above module dir. */
203 ccan_dir = talloc_strdup(NULL, m->dir);
204 p = strrchr(ccan_dir, '/');
206 p = strrchr(ccan_dir, '/');
212 if (chdir(olddir) != 0)
213 err(1, "Returning to original directory '%s'", olddir);
221 * remove_comments - strip comments from a line, return copy.
222 * @line: line to copy
223 * @in_comment: are we already within a comment (from prev line).
224 * @unterminated: are we still in a comment for next line.
226 static char *remove_comments(const char *line, bool in_comment,
229 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
234 /* Find first comment. */
235 const char *old_comment = strstr(line, "/*");
236 const char *new_comment = strstr(line, "//");
239 if (new_comment && old_comment)
240 comment = new_comment < old_comment
241 ? new_comment : old_comment;
242 else if (old_comment)
243 comment = old_comment;
244 else if (new_comment)
245 comment = new_comment;
249 *unterminated = false;
253 /* Copy up to comment. */
254 memcpy(p, line, comment - line);
256 line += comment - line + 2;
258 if (comment == new_comment) {
259 /* We're done: goes to EOL. */
261 *unterminated = false;
268 const char *end = strstr(line, "*/");
270 *unterminated = true;
281 static bool is_empty(const char *line)
283 return strspn(line, " \t") == strlen(line);
286 static bool continues(const char *line)
288 /* Technically, any odd number of these. But who cares? */
289 return strends(line, "\\");
292 /* Get token if it's equal to token. */
293 bool get_token(const char **line, const char *token)
297 *line += strspn(*line, " \t");
298 if (isalnum(token[0]) || token[0] == '_')
299 toklen = strspn(*line, IDENT_CHARS);
301 /* FIXME: real tokenizer handles ++ and other multi-chars. */
302 toklen = strlen(token);
305 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
312 char *get_symbol_token(void *ctx, const char **line)
317 *line += strspn(*line, " \t");
318 toklen = strspn(*line, IDENT_CHARS);
321 ret = talloc_strndup(ctx, *line, toklen);
326 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
328 bool brackets, defined;
330 cond->inverse = get_token(line, "!");
331 defined = get_token(line, "defined");
332 brackets = get_token(line, "(");
333 cond->symbol = get_symbol_token(cond, line);
336 if (brackets && !get_token(line, ")"))
339 cond->type = PP_COND_IF;
343 /* FIXME: Get serious! */
344 static struct pp_conditions *analyze_directive(struct ccan_file *f,
346 struct pp_conditions *parent)
348 struct pp_conditions *cond = talloc(f, struct pp_conditions);
351 line = remove_comments(line, false, &unused);
353 cond->parent = parent;
354 cond->type = PP_COND_IFDEF;
356 if (!get_token(&line, "#"))
359 if (get_token(&line, "if")) {
360 if (!parse_hash_if(cond, &line))
362 } else if (get_token(&line, "elif")) {
366 cond->parent = parent->parent;
367 /* FIXME: Not quite true. This implies !parent, but we don't
368 * do multiple conditionals yet. */
369 if (!parse_hash_if(cond, &line))
371 } else if (get_token(&line, "ifdef")) {
373 cond->inverse = false;
374 brackets = get_token(&line, "(");
375 cond->symbol = get_symbol_token(cond, &line);
378 if (brackets && !get_token(&line, ")"))
380 } else if (get_token(&line, "ifndef")) {
382 cond->inverse = true;
383 brackets = get_token(&line, "(");
384 cond->symbol = get_symbol_token(cond, &line);
387 if (brackets && !get_token(&line, ")"))
389 } else if (get_token(&line, "else")) {
395 cond->inverse = !cond->inverse;
397 } else if (get_token(&line, "endif")) {
403 return parent->parent;
405 /* Not a conditional. */
415 cond->type = PP_COND_UNKNOWN;
419 /* This parser is rough, but OK if code is reasonably neat. */
420 struct line_info *get_ccan_line_info(struct ccan_file *f)
422 bool continued = false, in_comment = false;
423 struct pp_conditions *cond = NULL;
429 get_ccan_file_lines(f);
430 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
432 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
436 /* Current conditions apply to this line. */
437 f->line_info[i].cond = cond;
438 f->line_info[i].continued = continued;
441 /* Same as last line. */
442 f->line_info[i].type = f->line_info[i-1].type;
443 /* Update in_comment. */
444 remove_comments(f->lines[i], in_comment, &in_comment);
448 /* Preprocessor directive? */
450 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
451 f->line_info[i].type = PREPROC_LINE;
452 cond = analyze_directive(f, f->lines[i], cond);
456 still_doc_line = (in_comment
457 && f->line_info[i-1].type == DOC_LINE);
459 p = remove_comments(f->lines[i], in_comment, &in_comment);
461 if (strstarts(f->lines[i], "/**") || still_doc_line)
462 f->line_info[i].type = DOC_LINE;
464 f->line_info[i].type = COMMENT_LINE;
466 f->line_info[i].type = CODE_LINE;
473 struct list_node list;
475 const unsigned int *value;
478 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
482 list_for_each(syms, i, list)
483 if (streq(sym, i->name))
488 static enum line_compiled get_pp(struct pp_conditions *cond,
489 struct list_head *syms)
493 enum line_compiled parent, ret;
495 /* No conditions? Easy. */
499 /* Check we get here at all. */
500 parent = get_pp(cond->parent, syms);
501 if (parent == NOT_COMPILED)
504 if (cond->type == PP_COND_UNKNOWN)
505 return MAYBE_COMPILED;
507 sym = find_symbol(syms, cond->symbol);
509 return MAYBE_COMPILED;
511 switch (cond->type) {
513 /* Undefined is 0. */
514 val = sym->value ? *sym->value : 0;
515 if (!val == cond->inverse)
522 if (cond->inverse == !sym->value)
532 /* If parent didn't know, NO == NO, but YES == MAYBE. */
533 if (parent == MAYBE_COMPILED && ret == COMPILED)
534 ret = MAYBE_COMPILED;
538 static void add_symbol(struct list_head *head,
539 const char *symbol, const unsigned int *value)
541 struct symbol *sym = talloc(head, struct symbol);
544 list_add(head, &sym->list);
547 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
549 const unsigned int *value,
552 enum line_compiled ret;
553 struct list_head *head;
556 head = talloc(NULL, struct list_head);
557 list_head_init(head);
560 add_symbol(head, symbol, value);
562 while ((symbol = va_arg(ap, const char *)) != NULL) {
563 value = va_arg(ap, const unsigned int *);
564 add_symbol(head, symbol, value);
566 ret = get_pp(cond, head);