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;
43 f->name = talloc_steal(f, name);
47 static void add_files(struct manifest *m, const char *dir)
57 err(1, "Opening directory %s", dir[0] ? dir : ".");
59 while ((ent = readdir(d)) != NULL) {
62 struct list_head *dest;
65 if (ent->d_name[0] == '.')
68 f = new_ccan_file(m, talloc_asprintf(m, "%s%s",
70 if (lstat(f->name, &st) != 0)
71 err(1, "lstat %s", f->name);
73 if (S_ISDIR(st.st_mode)) {
74 f->name = talloc_append_string(f->name, "/");
75 add_files(m, f->name);
78 if (!S_ISREG(st.st_mode)) {
83 if (streq(f->name, "_info")) {
85 f->contents = grab_file(f, f->name, &f->contents_size);
87 err(1, "Reading file %s", f->name);
91 is_c_src = strends(f->name, ".c");
92 if (!is_c_src && !strends(f->name, ".h")) {
93 /* We don't pull in contents of non-source files */
94 dest = &m->other_files;
98 f->contents = grab_file(f, f->name, &f->contents_size);
100 err(1, "Reading file %s", f->name);
102 if (!strchr(f->name, '/')) {
107 } else if (strstarts(f->name, "test/")) {
109 if (strstarts(f->name, "test/api"))
110 dest = &m->api_tests;
111 else if (strstarts(f->name, "test/run"))
112 dest = &m->run_tests;
113 else if (strstarts(f->name, "test/compile_ok"))
114 dest = &m->compile_ok_tests;
115 else if (strstarts(f->name, "test/compile_fail"))
116 dest = &m->compile_fail_tests;
118 dest = &m->other_test_files;
120 dest = &m->other_test_files;
122 dest = &m->other_files;
124 list_add(dest, &f->list);
129 char *report_on_lines(struct list_head *files,
130 char *(*report)(const char *),
135 list_for_each(files, f, list) {
137 char **lines = get_ccan_file_lines(f);
139 for (i = 0; i < f->num_lines; i++) {
140 char *r = report(lines[i]);
144 sofar = talloc_asprintf_append(sofar,
153 struct manifest *get_manifest(const void *ctx)
155 struct manifest *m = talloc(ctx, struct manifest);
159 list_head_init(&m->c_files);
160 list_head_init(&m->h_files);
161 list_head_init(&m->api_tests);
162 list_head_init(&m->run_tests);
163 list_head_init(&m->compile_ok_tests);
164 list_head_init(&m->compile_fail_tests);
165 list_head_init(&m->other_test_files);
166 list_head_init(&m->other_files);
167 list_head_init(&m->dep_dirs);
168 list_head_init(&m->dep_objs);
170 m->basename = talloc_getcwd(m);
172 err(1, "Getting current directory");
173 len = strlen(m->basename);
174 while (len && m->basename[len-1] == '/')
175 m->basename[--len] = '\0';
177 m->basename = strrchr(m->basename, '/');
179 errx(1, "I don't expect to be run from the root directory");
188 * remove_comments - strip comments from a line, return copy.
189 * @line: line to copy
190 * @in_comment: are we already within a comment (from prev line).
191 * @unterminated: are we still in a comment for next line.
193 static char *remove_comments(const char *line, bool in_comment,
196 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
201 /* Find first comment. */
202 const char *old_comment = strstr(line, "/*");
203 const char *new_comment = strstr(line, "//");
206 if (new_comment && old_comment)
207 comment = new_comment < old_comment
208 ? new_comment : old_comment;
209 else if (old_comment)
210 comment = old_comment;
211 else if (new_comment)
212 comment = new_comment;
216 *unterminated = false;
220 /* Copy up to comment. */
221 memcpy(p, line, comment - line);
223 line += comment - line + 2;
225 if (comment == new_comment) {
226 /* We're done: goes to EOL. */
228 *unterminated = false;
235 const char *end = strstr(line, "*/");
237 *unterminated = true;
248 static bool is_empty(const char *line)
250 return strspn(line, " \t") == strlen(line);
253 static bool continues(const char *line)
255 /* Technically, any odd number of these. But who cares? */
256 return strends(line, "\\");
259 /* Get token if it's equal to token. */
260 bool get_token(const char **line, const char *token)
264 *line += strspn(*line, " \t");
265 if (isalnum(token[0]) || token[0] == '_')
266 toklen = strspn(*line, IDENT_CHARS);
268 /* FIXME: real tokenizer handles ++ and other multi-chars. */
269 toklen = strlen(token);
272 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
279 char *get_symbol_token(void *ctx, const char **line)
284 *line += strspn(*line, " \t");
285 toklen = strspn(*line, IDENT_CHARS);
288 ret = talloc_strndup(ctx, *line, toklen);
293 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
295 bool brackets, defined;
297 cond->inverse = get_token(line, "!");
298 defined = get_token(line, "defined");
299 brackets = get_token(line, "(");
300 cond->symbol = get_symbol_token(cond, line);
303 if (brackets && !get_token(line, ")"))
306 cond->type = PP_COND_IF;
310 /* FIXME: Get serious! */
311 static struct pp_conditions *analyze_directive(struct ccan_file *f,
313 struct pp_conditions *parent)
315 struct pp_conditions *cond = talloc(f, struct pp_conditions);
318 line = remove_comments(line, false, &unused);
320 cond->parent = parent;
321 cond->type = PP_COND_IFDEF;
323 if (!get_token(&line, "#"))
326 if (get_token(&line, "if")) {
327 if (!parse_hash_if(cond, &line))
329 } else if (get_token(&line, "elif")) {
333 cond->parent = parent->parent;
334 /* FIXME: Not quite true. This implies !parent, but we don't
335 * do multiple conditionals yet. */
336 if (!parse_hash_if(cond, &line))
338 } else if (get_token(&line, "ifdef")) {
340 cond->inverse = false;
341 brackets = get_token(&line, "(");
342 cond->symbol = get_symbol_token(cond, &line);
345 if (brackets && !get_token(&line, ")"))
347 } else if (get_token(&line, "ifndef")) {
349 cond->inverse = true;
350 brackets = get_token(&line, "(");
351 cond->symbol = get_symbol_token(cond, &line);
354 if (brackets && !get_token(&line, ")"))
356 } else if (get_token(&line, "else")) {
362 cond->inverse = !cond->inverse;
364 } else if (get_token(&line, "endif")) {
370 return parent->parent;
372 /* Not a conditional. */
382 cond->type = PP_COND_UNKNOWN;
386 /* This parser is rough, but OK if code is reasonably neat. */
387 struct line_info *get_ccan_line_info(struct ccan_file *f)
389 bool continued = false, in_comment = false;
390 struct pp_conditions *cond = NULL;
396 get_ccan_file_lines(f);
397 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
399 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
403 /* Current conditions apply to this line. */
404 f->line_info[i].cond = cond;
405 f->line_info[i].continued = continued;
408 /* Same as last line. */
409 f->line_info[i].type = f->line_info[i-1].type;
410 /* Update in_comment. */
411 remove_comments(f->lines[i], in_comment, &in_comment);
415 /* Preprocessor directive? */
417 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
418 f->line_info[i].type = PREPROC_LINE;
419 cond = analyze_directive(f, f->lines[i], cond);
423 still_doc_line = (in_comment
424 && f->line_info[i-1].type == DOC_LINE);
426 p = remove_comments(f->lines[i], in_comment, &in_comment);
428 if (strstarts(f->lines[i], "/**") || still_doc_line)
429 f->line_info[i].type = DOC_LINE;
431 f->line_info[i].type = COMMENT_LINE;
433 f->line_info[i].type = CODE_LINE;
440 struct list_node list;
442 const unsigned int *value;
445 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
449 list_for_each(syms, i, list)
450 if (streq(sym, i->name))
455 static enum line_compiled get_pp(struct pp_conditions *cond,
456 struct list_head *syms)
460 enum line_compiled parent, ret;
462 /* No conditions? Easy. */
466 /* Check we get here at all. */
467 parent = get_pp(cond->parent, syms);
468 if (parent == NOT_COMPILED)
471 if (cond->type == PP_COND_UNKNOWN)
472 return MAYBE_COMPILED;
474 sym = find_symbol(syms, cond->symbol);
476 return MAYBE_COMPILED;
478 switch (cond->type) {
480 /* Undefined is 0. */
481 val = sym->value ? *sym->value : 0;
482 if (!val == cond->inverse)
489 if (cond->inverse == !sym->value)
499 /* If parent didn't know, NO == NO, but YES == MAYBE. */
500 if (parent == MAYBE_COMPILED && ret == COMPILED)
501 ret = MAYBE_COMPILED;
505 static void add_symbol(struct list_head *head,
506 const char *symbol, const unsigned int *value)
508 struct symbol *sym = talloc(head, struct symbol);
511 list_add(head, &sym->list);
514 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
516 const unsigned int *value,
519 enum line_compiled ret;
520 struct list_head *head;
523 head = talloc(NULL, struct list_head);
524 list_head_init(head);
527 add_symbol(head, symbol, value);
529 while ((symbol = va_arg(ap, const char *)) != NULL) {
530 value = va_arg(ap, const unsigned int *);
531 add_symbol(head, symbol, value);
533 ret = get_pp(cond, head);