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 char *buffer = grab_file(f, f->name, NULL);
23 err(1, "Getting file %s", f->name);
24 f->lines = strsplit(f, buffer, "\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 static void add_files(struct manifest *m, const char *dir)
48 err(1, "Opening directory %s", dir[0] ? dir : ".");
50 while ((ent = readdir(d)) != NULL) {
53 struct list_head *dest;
56 if (ent->d_name[0] == '.')
59 f = talloc(m, struct ccan_file);
62 f->doc_sections = NULL;
63 f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
64 if (lstat(f->name, &st) != 0)
65 err(1, "lstat %s", f->name);
67 if (S_ISDIR(st.st_mode)) {
68 f->name = talloc_append_string(f->name, "/");
69 add_files(m, f->name);
72 if (!S_ISREG(st.st_mode)) {
77 if (streq(f->name, "_info.c")) {
82 is_c_src = strends(f->name, ".c");
83 if (!is_c_src && !strends(f->name, ".h"))
84 dest = &m->other_files;
85 else if (!strchr(f->name, '/')) {
90 } else if (strstarts(f->name, "test/")) {
92 if (strstarts(f->name, "test/api"))
94 else if (strstarts(f->name, "test/run"))
96 else if (strstarts(f->name, "test/compile_ok"))
97 dest = &m->compile_ok_tests;
98 else if (strstarts(f->name, "test/compile_fail"))
99 dest = &m->compile_fail_tests;
101 dest = &m->other_test_files;
103 dest = &m->other_test_files;
105 dest = &m->other_files;
107 list_add(dest, &f->list);
112 char *report_on_lines(struct list_head *files,
113 char *(*report)(const char *),
118 list_for_each(files, f, list) {
120 char **lines = get_ccan_file_lines(f);
122 for (i = 0; i < f->num_lines; i++) {
123 char *r = report(lines[i]);
127 sofar = talloc_asprintf_append(sofar,
136 struct manifest *get_manifest(void)
138 struct manifest *m = talloc(NULL, struct manifest);
142 list_head_init(&m->c_files);
143 list_head_init(&m->h_files);
144 list_head_init(&m->api_tests);
145 list_head_init(&m->run_tests);
146 list_head_init(&m->compile_ok_tests);
147 list_head_init(&m->compile_fail_tests);
148 list_head_init(&m->other_test_files);
149 list_head_init(&m->other_files);
151 /* *This* is why people hate C. */
153 m->basename = talloc_array(m, char, len);
154 while (!getcwd(m->basename, len)) {
156 err(1, "Getting current directory");
157 m->basename = talloc_realloc(m, m->basename, char, len *= 2);
160 len = strlen(m->basename);
161 while (len && m->basename[len-1] == '/')
162 m->basename[--len] = '\0';
164 m->basename = strrchr(m->basename, '/');
166 errx(1, "I don't expect to be run from the root directory");
175 * remove_comments - strip comments from a line, return copy.
176 * @line: line to copy
177 * @in_comment: are we already within a comment (from prev line).
178 * @unterminated: are we still in a comment for next line.
180 static char *remove_comments(const char *line, bool in_comment,
183 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
188 /* Find first comment. */
189 const char *old_comment = strstr(line, "/*");
190 const char *new_comment = strstr(line, "//");
193 if (new_comment && old_comment)
194 comment = new_comment < old_comment
195 ? new_comment : old_comment;
196 else if (old_comment)
197 comment = old_comment;
198 else if (new_comment)
199 comment = new_comment;
203 *unterminated = false;
207 /* Copy up to comment. */
208 memcpy(p, line, comment - line);
210 line += comment - line + 2;
212 if (comment == new_comment) {
213 /* We're done: goes to EOL. */
215 *unterminated = false;
222 const char *end = strstr(line, "*/");
224 *unterminated = true;
235 static bool is_empty(const char *line)
237 return strspn(line, " \t") == strlen(line);
240 static bool continues(const char *line)
242 /* Technically, any odd number of these. But who cares? */
243 return strends(line, "\\");
246 /* Get token if it's equal to token. */
247 bool get_token(const char **line, const char *token)
251 *line += strspn(*line, " \t");
252 if (isalnum(token[0]) || token[0] == '_')
253 toklen = strspn(*line, IDENT_CHARS);
255 /* FIXME: real tokenizer handles ++ and other multi-chars. */
256 toklen = strlen(token);
259 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
266 char *get_symbol_token(void *ctx, const char **line)
271 *line += strspn(*line, " \t");
272 toklen = strspn(*line, IDENT_CHARS);
275 ret = talloc_strndup(ctx, *line, toklen);
280 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
282 bool brackets, defined;
284 cond->inverse = get_token(line, "!");
285 defined = get_token(line, "defined");
286 brackets = get_token(line, "(");
287 cond->symbol = get_symbol_token(cond, line);
290 if (brackets && !get_token(line, ")"))
293 cond->type = PP_COND_IF;
297 /* FIXME: Get serious! */
298 static struct pp_conditions *analyze_directive(struct ccan_file *f,
300 struct pp_conditions *parent)
302 struct pp_conditions *cond = talloc(f, struct pp_conditions);
305 line = remove_comments(line, false, &unused);
307 cond->parent = parent;
308 cond->type = PP_COND_IFDEF;
310 if (!get_token(&line, "#"))
313 if (get_token(&line, "if")) {
314 if (!parse_hash_if(cond, &line))
316 } else if (get_token(&line, "elif")) {
320 cond->parent = parent->parent;
321 /* FIXME: Not quite true. This implies !parent, but we don't
322 * do multiple conditionals yet. */
323 if (!parse_hash_if(cond, &line))
325 } else if (get_token(&line, "ifdef")) {
327 cond->inverse = false;
328 brackets = get_token(&line, "(");
329 cond->symbol = get_symbol_token(cond, &line);
332 if (brackets && !get_token(&line, ")"))
334 } else if (get_token(&line, "ifndef")) {
336 cond->inverse = true;
337 brackets = get_token(&line, "(");
338 cond->symbol = get_symbol_token(cond, &line);
341 if (brackets && !get_token(&line, ")"))
343 } else if (get_token(&line, "else")) {
349 cond->inverse = !cond->inverse;
351 } else if (get_token(&line, "endif")) {
357 return parent->parent;
359 /* Not a conditional. */
369 cond->type = PP_COND_UNKNOWN;
373 /* This parser is rough, but OK if code is reasonably neat. */
374 struct line_info *get_ccan_line_info(struct ccan_file *f)
376 bool continued = false, in_comment = false;
377 struct pp_conditions *cond = NULL;
383 get_ccan_file_lines(f);
384 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
386 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
390 /* Current conditions apply to this line. */
391 f->line_info[i].cond = cond;
392 f->line_info[i].continued = continued;
395 /* Same as last line. */
396 f->line_info[i].type = f->line_info[i-1].type;
397 /* Update in_comment. */
398 remove_comments(f->lines[i], in_comment, &in_comment);
402 /* Preprocessor directive? */
404 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
405 f->line_info[i].type = PREPROC_LINE;
406 cond = analyze_directive(f, f->lines[i], cond);
410 still_doc_line = (in_comment
411 && f->line_info[i-1].type == DOC_LINE);
413 p = remove_comments(f->lines[i], in_comment, &in_comment);
415 if (strstarts(f->lines[i], "/**") || still_doc_line)
416 f->line_info[i].type = DOC_LINE;
418 f->line_info[i].type = COMMENT_LINE;
420 f->line_info[i].type = CODE_LINE;
427 struct list_node list;
429 const unsigned int *value;
432 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
436 list_for_each(syms, i, list)
437 if (streq(sym, i->name))
442 static enum line_compiled get_pp(struct pp_conditions *cond,
443 struct list_head *syms)
447 enum line_compiled parent, ret;
449 /* No conditions? Easy. */
453 /* Check we get here at all. */
454 parent = get_pp(cond->parent, syms);
455 if (parent == NOT_COMPILED)
458 if (cond->type == PP_COND_UNKNOWN)
459 return MAYBE_COMPILED;
461 sym = find_symbol(syms, cond->symbol);
463 return MAYBE_COMPILED;
465 switch (cond->type) {
467 /* Undefined is 0. */
468 val = sym->value ? *sym->value : 0;
469 if (!val == cond->inverse)
476 if (cond->inverse == !sym->value)
486 /* If parent didn't know, NO == NO, but YES == MAYBE. */
487 if (parent == MAYBE_COMPILED && ret == COMPILED)
488 ret = MAYBE_COMPILED;
492 static void add_symbol(struct list_head *head,
493 const char *symbol, const unsigned int *value)
495 struct symbol *sym = talloc(head, struct symbol);
498 list_add(head, &sym->list);
501 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
503 const unsigned int *value,
506 enum line_compiled ret;
507 struct list_head *head;
510 head = talloc(NULL, struct list_head);
511 list_head_init(head);
514 add_symbol(head, symbol, value);
516 while ((symbol = va_arg(ap, const char *)) != NULL) {
517 value = va_arg(ap, const unsigned int *);
518 add_symbol(head, symbol, value);
520 ret = get_pp(cond, head);