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);
168 m->basename = talloc_getcwd(m);
170 err(1, "Getting current directory");
171 len = strlen(m->basename);
172 while (len && m->basename[len-1] == '/')
173 m->basename[--len] = '\0';
175 m->basename = strrchr(m->basename, '/');
177 errx(1, "I don't expect to be run from the root directory");
186 * remove_comments - strip comments from a line, return copy.
187 * @line: line to copy
188 * @in_comment: are we already within a comment (from prev line).
189 * @unterminated: are we still in a comment for next line.
191 static char *remove_comments(const char *line, bool in_comment,
194 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
199 /* Find first comment. */
200 const char *old_comment = strstr(line, "/*");
201 const char *new_comment = strstr(line, "//");
204 if (new_comment && old_comment)
205 comment = new_comment < old_comment
206 ? new_comment : old_comment;
207 else if (old_comment)
208 comment = old_comment;
209 else if (new_comment)
210 comment = new_comment;
214 *unterminated = false;
218 /* Copy up to comment. */
219 memcpy(p, line, comment - line);
221 line += comment - line + 2;
223 if (comment == new_comment) {
224 /* We're done: goes to EOL. */
226 *unterminated = false;
233 const char *end = strstr(line, "*/");
235 *unterminated = true;
246 static bool is_empty(const char *line)
248 return strspn(line, " \t") == strlen(line);
251 static bool continues(const char *line)
253 /* Technically, any odd number of these. But who cares? */
254 return strends(line, "\\");
257 /* Get token if it's equal to token. */
258 bool get_token(const char **line, const char *token)
262 *line += strspn(*line, " \t");
263 if (isalnum(token[0]) || token[0] == '_')
264 toklen = strspn(*line, IDENT_CHARS);
266 /* FIXME: real tokenizer handles ++ and other multi-chars. */
267 toklen = strlen(token);
270 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
277 char *get_symbol_token(void *ctx, const char **line)
282 *line += strspn(*line, " \t");
283 toklen = strspn(*line, IDENT_CHARS);
286 ret = talloc_strndup(ctx, *line, toklen);
291 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
293 bool brackets, defined;
295 cond->inverse = get_token(line, "!");
296 defined = get_token(line, "defined");
297 brackets = get_token(line, "(");
298 cond->symbol = get_symbol_token(cond, line);
301 if (brackets && !get_token(line, ")"))
304 cond->type = PP_COND_IF;
308 /* FIXME: Get serious! */
309 static struct pp_conditions *analyze_directive(struct ccan_file *f,
311 struct pp_conditions *parent)
313 struct pp_conditions *cond = talloc(f, struct pp_conditions);
316 line = remove_comments(line, false, &unused);
318 cond->parent = parent;
319 cond->type = PP_COND_IFDEF;
321 if (!get_token(&line, "#"))
324 if (get_token(&line, "if")) {
325 if (!parse_hash_if(cond, &line))
327 } else if (get_token(&line, "elif")) {
331 cond->parent = parent->parent;
332 /* FIXME: Not quite true. This implies !parent, but we don't
333 * do multiple conditionals yet. */
334 if (!parse_hash_if(cond, &line))
336 } else if (get_token(&line, "ifdef")) {
338 cond->inverse = false;
339 brackets = get_token(&line, "(");
340 cond->symbol = get_symbol_token(cond, &line);
343 if (brackets && !get_token(&line, ")"))
345 } else if (get_token(&line, "ifndef")) {
347 cond->inverse = true;
348 brackets = get_token(&line, "(");
349 cond->symbol = get_symbol_token(cond, &line);
352 if (brackets && !get_token(&line, ")"))
354 } else if (get_token(&line, "else")) {
360 cond->inverse = !cond->inverse;
362 } else if (get_token(&line, "endif")) {
368 return parent->parent;
370 /* Not a conditional. */
380 cond->type = PP_COND_UNKNOWN;
384 /* This parser is rough, but OK if code is reasonably neat. */
385 struct line_info *get_ccan_line_info(struct ccan_file *f)
387 bool continued = false, in_comment = false;
388 struct pp_conditions *cond = NULL;
394 get_ccan_file_lines(f);
395 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
397 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
401 /* Current conditions apply to this line. */
402 f->line_info[i].cond = cond;
403 f->line_info[i].continued = continued;
406 /* Same as last line. */
407 f->line_info[i].type = f->line_info[i-1].type;
408 /* Update in_comment. */
409 remove_comments(f->lines[i], in_comment, &in_comment);
413 /* Preprocessor directive? */
415 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
416 f->line_info[i].type = PREPROC_LINE;
417 cond = analyze_directive(f, f->lines[i], cond);
421 still_doc_line = (in_comment
422 && f->line_info[i-1].type == DOC_LINE);
424 p = remove_comments(f->lines[i], in_comment, &in_comment);
426 if (strstarts(f->lines[i], "/**") || still_doc_line)
427 f->line_info[i].type = DOC_LINE;
429 f->line_info[i].type = COMMENT_LINE;
431 f->line_info[i].type = CODE_LINE;
438 struct list_node list;
440 const unsigned int *value;
443 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
447 list_for_each(syms, i, list)
448 if (streq(sym, i->name))
453 static enum line_compiled get_pp(struct pp_conditions *cond,
454 struct list_head *syms)
458 enum line_compiled parent, ret;
460 /* No conditions? Easy. */
464 /* Check we get here at all. */
465 parent = get_pp(cond->parent, syms);
466 if (parent == NOT_COMPILED)
469 if (cond->type == PP_COND_UNKNOWN)
470 return MAYBE_COMPILED;
472 sym = find_symbol(syms, cond->symbol);
474 return MAYBE_COMPILED;
476 switch (cond->type) {
478 /* Undefined is 0. */
479 val = sym->value ? *sym->value : 0;
480 if (!val == cond->inverse)
487 if (cond->inverse == !sym->value)
497 /* If parent didn't know, NO == NO, but YES == MAYBE. */
498 if (parent == MAYBE_COMPILED && ret == COMPILED)
499 ret = MAYBE_COMPILED;
503 static void add_symbol(struct list_head *head,
504 const char *symbol, const unsigned int *value)
506 struct symbol *sym = talloc(head, struct symbol);
509 list_add(head, &sym->list);
512 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
514 const unsigned int *value,
517 enum line_compiled ret;
518 struct list_head *head;
521 head = talloc(NULL, struct list_head);
522 list_head_init(head);
525 add_symbol(head, symbol, value);
527 while ((symbol = va_arg(ap, const char *)) != NULL) {
528 value = va_arg(ap, const unsigned int *);
529 add_symbol(head, symbol, value);
531 ret = get_pp(cond, head);