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>
20 char **get_ccan_file_lines(struct ccan_file *f)
23 f->lines = strsplit(f, f->contents, "\n", &f->num_lines);
28 struct list_head *get_ccan_file_docs(struct ccan_file *f)
30 if (!f->doc_sections) {
31 get_ccan_file_lines(f);
32 f->doc_sections = extract_doc_sections(f->lines, f->num_lines);
34 return f->doc_sections;
37 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
41 f = talloc(ctx, struct ccan_file);
44 f->doc_sections = NULL;
46 f->name = talloc_steal(f, name);
47 f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
51 static void add_files(struct manifest *m, const char *dir)
61 err(1, "Opening directory %s", dir[0] ? dir : ".");
63 while ((ent = readdir(d)) != NULL) {
66 struct list_head *dest;
69 if (ent->d_name[0] == '.')
72 f = new_ccan_file(m, m->dir,
73 talloc_asprintf(m, "%s%s",
75 if (lstat(f->name, &st) != 0)
76 err(1, "lstat %s", f->name);
78 if (S_ISDIR(st.st_mode)) {
79 f->name = talloc_append_string(f->name, "/");
80 add_files(m, f->name);
83 if (!S_ISREG(st.st_mode)) {
88 if (streq(f->name, "_info")) {
90 f->contents = grab_file(f, f->name, &f->contents_size);
92 err(1, "Reading file %s", f->name);
96 is_c_src = strends(f->name, ".c");
97 if (!is_c_src && !strends(f->name, ".h")) {
98 /* We don't pull in contents of non-source files */
99 dest = &m->other_files;
103 f->contents = grab_file(f, f->name, &f->contents_size);
105 err(1, "Reading file %s", f->name);
107 if (!strchr(f->name, '/')) {
112 } else if (strstarts(f->name, "test/")) {
114 if (strstarts(f->name, "test/api"))
115 dest = &m->api_tests;
116 else if (strstarts(f->name, "test/run"))
117 dest = &m->run_tests;
118 else if (strstarts(f->name, "test/compile_ok"))
119 dest = &m->compile_ok_tests;
120 else if (strstarts(f->name, "test/compile_fail"))
121 dest = &m->compile_fail_tests;
123 dest = &m->other_test_c_files;
125 dest = &m->other_test_files;
127 dest = &m->other_files;
129 list_add(dest, &f->list);
134 char *report_on_lines(struct list_head *files,
135 char *(*report)(const char *),
140 list_for_each(files, f, list) {
142 char **lines = get_ccan_file_lines(f);
144 for (i = 0; i < f->num_lines; i++) {
145 char *r = report(lines[i]);
149 sofar = talloc_asprintf_append(sofar,
158 struct manifest *get_manifest(const void *ctx, const char *dir)
160 struct manifest *m = talloc(ctx, struct manifest);
165 list_head_init(&m->c_files);
166 list_head_init(&m->h_files);
167 list_head_init(&m->api_tests);
168 list_head_init(&m->run_tests);
169 list_head_init(&m->compile_ok_tests);
170 list_head_init(&m->compile_fail_tests);
171 list_head_init(&m->other_test_c_files);
172 list_head_init(&m->other_test_files);
173 list_head_init(&m->other_files);
174 list_head_init(&m->dep_dirs);
175 list_head_init(&m->dep_objs);
177 olddir = talloc_getcwd(NULL);
179 err(1, "Getting current directory");
182 err(1, "Failed to chdir to %s", dir);
184 m->dir = talloc_getcwd(m);
186 err(1, "Getting current directory");
188 len = strlen(m->dir);
189 while (len && m->dir[len-1] == '/')
190 m->dir[--len] = '\0';
192 m->basename = strrchr(m->dir, '/');
194 errx(1, "I don't expect to be run from the root directory");
197 /* We expect the ccan dir to be two levels above module dir. */
200 ccan_dir = talloc_strdup(NULL, m->dir);
201 p = strrchr(ccan_dir, '/');
203 p = strrchr(ccan_dir, '/');
209 if (chdir(olddir) != 0)
210 err(1, "Returning to original directory '%s'", olddir);
218 * remove_comments - strip comments from a line, return copy.
219 * @line: line to copy
220 * @in_comment: are we already within a comment (from prev line).
221 * @unterminated: are we still in a comment for next line.
223 static char *remove_comments(const char *line, bool in_comment,
226 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
231 /* Find first comment. */
232 const char *old_comment = strstr(line, "/*");
233 const char *new_comment = strstr(line, "//");
236 if (new_comment && old_comment)
237 comment = new_comment < old_comment
238 ? new_comment : old_comment;
239 else if (old_comment)
240 comment = old_comment;
241 else if (new_comment)
242 comment = new_comment;
246 *unterminated = false;
250 /* Copy up to comment. */
251 memcpy(p, line, comment - line);
253 line += comment - line + 2;
255 if (comment == new_comment) {
256 /* We're done: goes to EOL. */
258 *unterminated = false;
265 const char *end = strstr(line, "*/");
267 *unterminated = true;
278 static bool is_empty(const char *line)
280 return strspn(line, " \t") == strlen(line);
283 static bool continues(const char *line)
285 /* Technically, any odd number of these. But who cares? */
286 return strends(line, "\\");
289 /* Get token if it's equal to token. */
290 bool get_token(const char **line, const char *token)
294 *line += strspn(*line, " \t");
295 if (isalnum(token[0]) || token[0] == '_')
296 toklen = strspn(*line, IDENT_CHARS);
298 /* FIXME: real tokenizer handles ++ and other multi-chars. */
299 toklen = strlen(token);
302 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
309 char *get_symbol_token(void *ctx, const char **line)
314 *line += strspn(*line, " \t");
315 toklen = strspn(*line, IDENT_CHARS);
318 ret = talloc_strndup(ctx, *line, toklen);
323 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
325 bool brackets, defined;
327 cond->inverse = get_token(line, "!");
328 defined = get_token(line, "defined");
329 brackets = get_token(line, "(");
330 cond->symbol = get_symbol_token(cond, line);
333 if (brackets && !get_token(line, ")"))
336 cond->type = PP_COND_IF;
340 /* FIXME: Get serious! */
341 static struct pp_conditions *analyze_directive(struct ccan_file *f,
343 struct pp_conditions *parent)
345 struct pp_conditions *cond = talloc(f, struct pp_conditions);
348 line = remove_comments(line, false, &unused);
350 cond->parent = parent;
351 cond->type = PP_COND_IFDEF;
353 if (!get_token(&line, "#"))
356 if (get_token(&line, "if")) {
357 if (!parse_hash_if(cond, &line))
359 } else if (get_token(&line, "elif")) {
363 cond->parent = parent->parent;
364 /* FIXME: Not quite true. This implies !parent, but we don't
365 * do multiple conditionals yet. */
366 if (!parse_hash_if(cond, &line))
368 } else if (get_token(&line, "ifdef")) {
370 cond->inverse = false;
371 brackets = get_token(&line, "(");
372 cond->symbol = get_symbol_token(cond, &line);
375 if (brackets && !get_token(&line, ")"))
377 } else if (get_token(&line, "ifndef")) {
379 cond->inverse = true;
380 brackets = get_token(&line, "(");
381 cond->symbol = get_symbol_token(cond, &line);
384 if (brackets && !get_token(&line, ")"))
386 } else if (get_token(&line, "else")) {
392 cond->inverse = !cond->inverse;
394 } else if (get_token(&line, "endif")) {
400 return parent->parent;
402 /* Not a conditional. */
412 cond->type = PP_COND_UNKNOWN;
416 /* This parser is rough, but OK if code is reasonably neat. */
417 struct line_info *get_ccan_line_info(struct ccan_file *f)
419 bool continued = false, in_comment = false;
420 struct pp_conditions *cond = NULL;
426 get_ccan_file_lines(f);
427 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
429 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
433 /* Current conditions apply to this line. */
434 f->line_info[i].cond = cond;
435 f->line_info[i].continued = continued;
438 /* Same as last line. */
439 f->line_info[i].type = f->line_info[i-1].type;
440 /* Update in_comment. */
441 remove_comments(f->lines[i], in_comment, &in_comment);
445 /* Preprocessor directive? */
447 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
448 f->line_info[i].type = PREPROC_LINE;
449 cond = analyze_directive(f, f->lines[i], cond);
453 still_doc_line = (in_comment
454 && f->line_info[i-1].type == DOC_LINE);
456 p = remove_comments(f->lines[i], in_comment, &in_comment);
458 if (strstarts(f->lines[i], "/**") || still_doc_line)
459 f->line_info[i].type = DOC_LINE;
461 f->line_info[i].type = COMMENT_LINE;
463 f->line_info[i].type = CODE_LINE;
470 struct list_node list;
472 const unsigned int *value;
475 static struct symbol *find_symbol(struct list_head *syms, const char *sym)
479 list_for_each(syms, i, list)
480 if (streq(sym, i->name))
485 static enum line_compiled get_pp(struct pp_conditions *cond,
486 struct list_head *syms)
490 enum line_compiled parent, ret;
492 /* No conditions? Easy. */
496 /* Check we get here at all. */
497 parent = get_pp(cond->parent, syms);
498 if (parent == NOT_COMPILED)
501 if (cond->type == PP_COND_UNKNOWN)
502 return MAYBE_COMPILED;
504 sym = find_symbol(syms, cond->symbol);
506 return MAYBE_COMPILED;
508 switch (cond->type) {
510 /* Undefined is 0. */
511 val = sym->value ? *sym->value : 0;
512 if (!val == cond->inverse)
519 if (cond->inverse == !sym->value)
529 /* If parent didn't know, NO == NO, but YES == MAYBE. */
530 if (parent == MAYBE_COMPILED && ret == COMPILED)
531 ret = MAYBE_COMPILED;
535 static void add_symbol(struct list_head *head,
536 const char *symbol, const unsigned int *value)
538 struct symbol *sym = talloc(head, struct symbol);
541 list_add(head, &sym->list);
544 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
546 const unsigned int *value,
549 enum line_compiled ret;
550 struct list_head *head;
553 head = talloc(NULL, struct list_head);
554 list_head_init(head);
557 add_symbol(head, symbol, value);
559 while ((symbol = va_arg(ap, const char *)) != NULL) {
560 value = va_arg(ap, const unsigned int *);
561 add_symbol(head, symbol, value);
563 ret = get_pp(cond, head);