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 static void add_files(struct manifest *m, const char *dir)
45 err(1, "Opening directory %s", dir[0] ? dir : ".");
47 while ((ent = readdir(d)) != NULL) {
50 struct list_head *dest;
53 if (ent->d_name[0] == '.')
56 f = talloc(m, struct ccan_file);
59 f->doc_sections = NULL;
60 f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
61 if (lstat(f->name, &st) != 0)
62 err(1, "lstat %s", f->name);
64 if (S_ISDIR(st.st_mode)) {
65 f->name = talloc_append_string(f->name, "/");
66 add_files(m, f->name);
69 if (!S_ISREG(st.st_mode)) {
74 if (streq(f->name, "_info")) {
76 f->contents = grab_file(f, f->name, &f->contents_size);
78 err(1, "Reading file %s", f->name);
82 is_c_src = strends(f->name, ".c");
83 if (!is_c_src && !strends(f->name, ".h")) {
84 /* We don't pull in contents of non-source files */
85 dest = &m->other_files;
89 f->contents = grab_file(f, f->name, &f->contents_size);
91 err(1, "Reading file %s", f->name);
93 if (!strchr(f->name, '/')) {
98 } else if (strstarts(f->name, "test/")) {
100 if (strstarts(f->name, "test/api"))
101 dest = &m->api_tests;
102 else if (strstarts(f->name, "test/run"))
103 dest = &m->run_tests;
104 else if (strstarts(f->name, "test/compile_ok"))
105 dest = &m->compile_ok_tests;
106 else if (strstarts(f->name, "test/compile_fail"))
107 dest = &m->compile_fail_tests;
109 dest = &m->other_test_files;
111 dest = &m->other_test_files;
113 dest = &m->other_files;
115 list_add(dest, &f->list);
120 char *report_on_lines(struct list_head *files,
121 char *(*report)(const char *),
126 list_for_each(files, f, list) {
128 char **lines = get_ccan_file_lines(f);
130 for (i = 0; i < f->num_lines; i++) {
131 char *r = report(lines[i]);
135 sofar = talloc_asprintf_append(sofar,
144 struct manifest *get_manifest(void)
146 struct manifest *m = talloc(NULL, struct manifest);
150 list_head_init(&m->c_files);
151 list_head_init(&m->h_files);
152 list_head_init(&m->api_tests);
153 list_head_init(&m->run_tests);
154 list_head_init(&m->compile_ok_tests);
155 list_head_init(&m->compile_fail_tests);
156 list_head_init(&m->other_test_files);
157 list_head_init(&m->other_files);
159 /* *This* is why people hate C. */
161 m->basename = talloc_array(m, char, len);
162 while (!getcwd(m->basename, len)) {
164 err(1, "Getting current directory");
165 m->basename = talloc_realloc(m, m->basename, char, len *= 2);
168 len = strlen(m->basename);
169 while (len && m->basename[len-1] == '/')
170 m->basename[--len] = '\0';
172 m->basename = strrchr(m->basename, '/');
174 errx(1, "I don't expect to be run from the root directory");
183 * remove_comments - strip comments from a line, return copy.
184 * @line: line to copy
185 * @in_comment: are we already within a comment (from prev line).
186 * @unterminated: are we still in a comment for next line.
188 static char *remove_comments(const char *line, bool in_comment,
191 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
196 /* Find first comment. */
197 const char *old_comment = strstr(line, "/*");
198 const char *new_comment = strstr(line, "//");
201 if (new_comment && old_comment)
202 comment = new_comment < old_comment
203 ? new_comment : old_comment;
204 else if (old_comment)
205 comment = old_comment;
206 else if (new_comment)
207 comment = new_comment;
211 *unterminated = false;
215 /* Copy up to comment. */
216 memcpy(p, line, comment - line);
218 line += comment - line + 2;
220 if (comment == new_comment) {
221 /* We're done: goes to EOL. */
223 *unterminated = false;
230 const char *end = strstr(line, "*/");
232 *unterminated = true;
243 static bool is_empty(const char *line)
245 return strspn(line, " \t") == strlen(line);
248 static bool continues(const char *line)
250 /* Technically, any odd number of these. But who cares? */
251 return strends(line, "\\");
254 /* Get token if it's equal to token. */
255 bool get_token(const char **line, const char *token)
259 *line += strspn(*line, " \t");
260 if (isalnum(token[0]) || token[0] == '_')
261 toklen = strspn(*line, IDENT_CHARS);
263 /* FIXME: real tokenizer handles ++ and other multi-chars. */
264 toklen = strlen(token);
267 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
274 char *get_symbol_token(void *ctx, const char **line)
279 *line += strspn(*line, " \t");
280 toklen = strspn(*line, IDENT_CHARS);
283 ret = talloc_strndup(ctx, *line, toklen);
288 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
290 bool brackets, defined;
292 cond->inverse = get_token(line, "!");
293 defined = get_token(line, "defined");
294 brackets = get_token(line, "(");
295 cond->symbol = get_symbol_token(cond, line);
298 if (brackets && !get_token(line, ")"))
301 cond->type = PP_COND_IF;
305 /* FIXME: Get serious! */
306 static struct pp_conditions *analyze_directive(struct ccan_file *f,
308 struct pp_conditions *parent)
310 struct pp_conditions *cond = talloc(f, struct pp_conditions);
313 line = remove_comments(line, false, &unused);
315 cond->parent = parent;
316 cond->type = PP_COND_IFDEF;
318 if (!get_token(&line, "#"))
321 if (get_token(&line, "if")) {
322 if (!parse_hash_if(cond, &line))
324 } else if (get_token(&line, "elif")) {
328 cond->parent = parent->parent;
329 /* FIXME: Not quite true. This implies !parent, but we don't
330 * do multiple conditionals yet. */
331 if (!parse_hash_if(cond, &line))
333 } else if (get_token(&line, "ifdef")) {
335 cond->inverse = false;
336 brackets = get_token(&line, "(");
337 cond->symbol = get_symbol_token(cond, &line);
340 if (brackets && !get_token(&line, ")"))
342 } else if (get_token(&line, "ifndef")) {
344 cond->inverse = true;
345 brackets = get_token(&line, "(");
346 cond->symbol = get_symbol_token(cond, &line);
349 if (brackets && !get_token(&line, ")"))
351 } else if (get_token(&line, "else")) {
357 cond->inverse = !cond->inverse;
359 } else if (get_token(&line, "endif")) {
365 return parent->parent;
367 /* Not a conditional. */
377 cond->type = PP_COND_UNKNOWN;
381 /* This parser is rough, but OK if code is reasonably neat. */
382 struct line_info *get_ccan_line_info(struct ccan_file *f)
384 bool continued = false, in_comment = false;
385 struct pp_conditions *cond = NULL;
391 get_ccan_file_lines(f);
392 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
394 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
398 /* Current conditions apply to this line. */
399 f->line_info[i].cond = cond;
400 f->line_info[i].continued = continued;
403 /* Same as last line. */
404 f->line_info[i].type = f->line_info[i-1].type;
405 /* Update in_comment. */
406 remove_comments(f->lines[i], in_comment, &in_comment);
410 /* Preprocessor directive? */
412 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
413 f->line_info[i].type = PREPROC_LINE;
414 cond = analyze_directive(f, f->lines[i], cond);
418 still_doc_line = (in_comment
419 && f->line_info[i-1].type == DOC_LINE);
421 p = remove_comments(f->lines[i], in_comment, &in_comment);
423 if (strstarts(f->lines[i], "/**") || still_doc_line)
424 f->line_info[i].type = DOC_LINE;
426 f->line_info[i].type = COMMENT_LINE;
428 f->line_info[i].type = CODE_LINE;
435 struct list_node list;
437 const unsigned int *value;
440 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
444 list_for_each(syms, i, list)
445 if (streq(sym, i->name))
450 static enum line_compiled get_pp(struct pp_conditions *cond,
451 struct list_head *syms)
455 enum line_compiled parent, ret;
457 /* No conditions? Easy. */
461 /* Check we get here at all. */
462 parent = get_pp(cond->parent, syms);
463 if (parent == NOT_COMPILED)
466 if (cond->type == PP_COND_UNKNOWN)
467 return MAYBE_COMPILED;
469 sym = find_symbol(syms, cond->symbol);
471 return MAYBE_COMPILED;
473 switch (cond->type) {
475 /* Undefined is 0. */
476 val = sym->value ? *sym->value : 0;
477 if (!val == cond->inverse)
484 if (cond->inverse == !sym->value)
494 /* If parent didn't know, NO == NO, but YES == MAYBE. */
495 if (parent == MAYBE_COMPILED && ret == COMPILED)
496 ret = MAYBE_COMPILED;
500 static void add_symbol(struct list_head *head,
501 const char *symbol, const unsigned int *value)
503 struct symbol *sym = talloc(head, struct symbol);
506 list_add(head, &sym->list);
509 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
511 const unsigned int *value,
514 enum line_compiled ret;
515 struct list_head *head;
518 head = talloc(NULL, struct list_head);
519 list_head_init(head);
522 add_symbol(head, symbol, value);
524 while ((symbol = va_arg(ap, const char *)) != NULL) {
525 value = va_arg(ap, const unsigned int *);
526 add_symbol(head, symbol, value);
528 ret = get_pp(cond, head);