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>
18 char **get_ccan_file_lines(struct ccan_file *f)
21 f->lines = strsplit(f, f->contents, "\n", &f->num_lines);
26 struct list_head *get_ccan_file_docs(struct ccan_file *f)
28 if (!f->doc_sections) {
29 get_ccan_file_lines(f);
30 f->doc_sections = extract_doc_sections(f->lines, f->num_lines);
32 return f->doc_sections;
35 struct ccan_file *new_ccan_file(const void *ctx, char *name)
39 f = talloc(ctx, struct ccan_file);
42 f->doc_sections = NULL;
44 f->name = talloc_steal(f, name);
48 static void add_files(struct manifest *m, const char *dir)
58 err(1, "Opening directory %s", dir[0] ? dir : ".");
60 while ((ent = readdir(d)) != NULL) {
63 struct list_head *dest;
66 if (ent->d_name[0] == '.')
69 f = new_ccan_file(m, talloc_asprintf(m, "%s%s",
71 if (lstat(f->name, &st) != 0)
72 err(1, "lstat %s", f->name);
74 if (S_ISDIR(st.st_mode)) {
75 f->name = talloc_append_string(f->name, "/");
76 add_files(m, f->name);
79 if (!S_ISREG(st.st_mode)) {
84 if (streq(f->name, "_info")) {
86 f->contents = grab_file(f, f->name, &f->contents_size);
88 err(1, "Reading file %s", f->name);
92 is_c_src = strends(f->name, ".c");
93 if (!is_c_src && !strends(f->name, ".h")) {
94 /* We don't pull in contents of non-source files */
95 dest = &m->other_files;
99 f->contents = grab_file(f, f->name, &f->contents_size);
101 err(1, "Reading file %s", f->name);
103 if (!strchr(f->name, '/')) {
108 } else if (strstarts(f->name, "test/")) {
110 if (strstarts(f->name, "test/api"))
111 dest = &m->api_tests;
112 else if (strstarts(f->name, "test/run"))
113 dest = &m->run_tests;
114 else if (strstarts(f->name, "test/compile_ok"))
115 dest = &m->compile_ok_tests;
116 else if (strstarts(f->name, "test/compile_fail"))
117 dest = &m->compile_fail_tests;
119 dest = &m->other_test_c_files;
121 dest = &m->other_test_files;
123 dest = &m->other_files;
125 list_add(dest, &f->list);
130 char *report_on_lines(struct list_head *files,
131 char *(*report)(const char *),
136 list_for_each(files, f, list) {
138 char **lines = get_ccan_file_lines(f);
140 for (i = 0; i < f->num_lines; i++) {
141 char *r = report(lines[i]);
145 sofar = talloc_asprintf_append(sofar,
154 struct manifest *get_manifest(const void *ctx, const char *dir)
156 struct manifest *m = talloc(ctx, struct manifest);
161 list_head_init(&m->c_files);
162 list_head_init(&m->h_files);
163 list_head_init(&m->api_tests);
164 list_head_init(&m->run_tests);
165 list_head_init(&m->compile_ok_tests);
166 list_head_init(&m->compile_fail_tests);
167 list_head_init(&m->other_test_c_files);
168 list_head_init(&m->other_test_files);
169 list_head_init(&m->other_files);
170 list_head_init(&m->dep_dirs);
171 list_head_init(&m->dep_objs);
173 olddir = talloc_getcwd(NULL);
175 err(1, "Getting current directory");
178 err(1, "Failed to chdir to %s", dir);
180 m->basename = talloc_getcwd(m);
182 err(1, "Getting current directory");
183 len = strlen(m->basename);
184 while (len && m->basename[len-1] == '/')
185 m->basename[--len] = '\0';
187 m->basename = strrchr(m->basename, '/');
189 errx(1, "I don't expect to be run from the root directory");
194 if (chdir(olddir) != 0)
195 err(1, "Returning to original directory '%s'", olddir);
203 * remove_comments - strip comments from a line, return copy.
204 * @line: line to copy
205 * @in_comment: are we already within a comment (from prev line).
206 * @unterminated: are we still in a comment for next line.
208 static char *remove_comments(const char *line, bool in_comment,
211 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
216 /* Find first comment. */
217 const char *old_comment = strstr(line, "/*");
218 const char *new_comment = strstr(line, "//");
221 if (new_comment && old_comment)
222 comment = new_comment < old_comment
223 ? new_comment : old_comment;
224 else if (old_comment)
225 comment = old_comment;
226 else if (new_comment)
227 comment = new_comment;
231 *unterminated = false;
235 /* Copy up to comment. */
236 memcpy(p, line, comment - line);
238 line += comment - line + 2;
240 if (comment == new_comment) {
241 /* We're done: goes to EOL. */
243 *unterminated = false;
250 const char *end = strstr(line, "*/");
252 *unterminated = true;
263 static bool is_empty(const char *line)
265 return strspn(line, " \t") == strlen(line);
268 static bool continues(const char *line)
270 /* Technically, any odd number of these. But who cares? */
271 return strends(line, "\\");
274 /* Get token if it's equal to token. */
275 bool get_token(const char **line, const char *token)
279 *line += strspn(*line, " \t");
280 if (isalnum(token[0]) || token[0] == '_')
281 toklen = strspn(*line, IDENT_CHARS);
283 /* FIXME: real tokenizer handles ++ and other multi-chars. */
284 toklen = strlen(token);
287 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
294 char *get_symbol_token(void *ctx, const char **line)
299 *line += strspn(*line, " \t");
300 toklen = strspn(*line, IDENT_CHARS);
303 ret = talloc_strndup(ctx, *line, toklen);
308 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
310 bool brackets, defined;
312 cond->inverse = get_token(line, "!");
313 defined = get_token(line, "defined");
314 brackets = get_token(line, "(");
315 cond->symbol = get_symbol_token(cond, line);
318 if (brackets && !get_token(line, ")"))
321 cond->type = PP_COND_IF;
325 /* FIXME: Get serious! */
326 static struct pp_conditions *analyze_directive(struct ccan_file *f,
328 struct pp_conditions *parent)
330 struct pp_conditions *cond = talloc(f, struct pp_conditions);
333 line = remove_comments(line, false, &unused);
335 cond->parent = parent;
336 cond->type = PP_COND_IFDEF;
338 if (!get_token(&line, "#"))
341 if (get_token(&line, "if")) {
342 if (!parse_hash_if(cond, &line))
344 } else if (get_token(&line, "elif")) {
348 cond->parent = parent->parent;
349 /* FIXME: Not quite true. This implies !parent, but we don't
350 * do multiple conditionals yet. */
351 if (!parse_hash_if(cond, &line))
353 } else if (get_token(&line, "ifdef")) {
355 cond->inverse = false;
356 brackets = get_token(&line, "(");
357 cond->symbol = get_symbol_token(cond, &line);
360 if (brackets && !get_token(&line, ")"))
362 } else if (get_token(&line, "ifndef")) {
364 cond->inverse = true;
365 brackets = get_token(&line, "(");
366 cond->symbol = get_symbol_token(cond, &line);
369 if (brackets && !get_token(&line, ")"))
371 } else if (get_token(&line, "else")) {
377 cond->inverse = !cond->inverse;
379 } else if (get_token(&line, "endif")) {
385 return parent->parent;
387 /* Not a conditional. */
397 cond->type = PP_COND_UNKNOWN;
401 /* This parser is rough, but OK if code is reasonably neat. */
402 struct line_info *get_ccan_line_info(struct ccan_file *f)
404 bool continued = false, in_comment = false;
405 struct pp_conditions *cond = NULL;
411 get_ccan_file_lines(f);
412 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
414 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
418 /* Current conditions apply to this line. */
419 f->line_info[i].cond = cond;
420 f->line_info[i].continued = continued;
423 /* Same as last line. */
424 f->line_info[i].type = f->line_info[i-1].type;
425 /* Update in_comment. */
426 remove_comments(f->lines[i], in_comment, &in_comment);
430 /* Preprocessor directive? */
432 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
433 f->line_info[i].type = PREPROC_LINE;
434 cond = analyze_directive(f, f->lines[i], cond);
438 still_doc_line = (in_comment
439 && f->line_info[i-1].type == DOC_LINE);
441 p = remove_comments(f->lines[i], in_comment, &in_comment);
443 if (strstarts(f->lines[i], "/**") || still_doc_line)
444 f->line_info[i].type = DOC_LINE;
446 f->line_info[i].type = COMMENT_LINE;
448 f->line_info[i].type = CODE_LINE;
455 struct list_node list;
457 const unsigned int *value;
460 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
464 list_for_each(syms, i, list)
465 if (streq(sym, i->name))
470 static enum line_compiled get_pp(struct pp_conditions *cond,
471 struct list_head *syms)
475 enum line_compiled parent, ret;
477 /* No conditions? Easy. */
481 /* Check we get here at all. */
482 parent = get_pp(cond->parent, syms);
483 if (parent == NOT_COMPILED)
486 if (cond->type == PP_COND_UNKNOWN)
487 return MAYBE_COMPILED;
489 sym = find_symbol(syms, cond->symbol);
491 return MAYBE_COMPILED;
493 switch (cond->type) {
495 /* Undefined is 0. */
496 val = sym->value ? *sym->value : 0;
497 if (!val == cond->inverse)
504 if (cond->inverse == !sym->value)
514 /* If parent didn't know, NO == NO, but YES == MAYBE. */
515 if (parent == MAYBE_COMPILED && ret == COMPILED)
516 ret = MAYBE_COMPILED;
520 static void add_symbol(struct list_head *head,
521 const char *symbol, const unsigned int *value)
523 struct symbol *sym = talloc(head, struct symbol);
526 list_add(head, &sym->list);
529 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
531 const unsigned int *value,
534 enum line_compiled ret;
535 struct list_head *head;
538 head = talloc(NULL, struct list_head);
539 list_head_init(head);
542 add_symbol(head, symbol, value);
544 while ((symbol = va_arg(ap, const char *)) != NULL) {
545 value = va_arg(ap, const unsigned int *);
546 add_symbol(head, symbol, value);
548 ret = get_pp(cond, head);