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>
17 char **get_ccan_file_lines(struct ccan_file *f)
20 char *buffer = grab_file(f, f->name, NULL);
22 err(1, "Getting file %s", f->name);
23 f->lines = strsplit(f, buffer, "\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 static void add_files(struct manifest *m, const char *dir)
47 err(1, "Opening directory %s", dir[0] ? dir : ".");
49 while ((ent = readdir(d)) != NULL) {
52 struct list_head *dest;
55 if (ent->d_name[0] == '.')
58 f = talloc(m, struct ccan_file);
60 f->doc_sections = NULL;
61 f->name = talloc_asprintf(f, "%s%s", dir, ent->d_name);
62 if (lstat(f->name, &st) != 0)
63 err(1, "lstat %s", f->name);
65 if (S_ISDIR(st.st_mode)) {
66 f->name = talloc_append_string(f->name, "/");
67 add_files(m, f->name);
70 if (!S_ISREG(st.st_mode)) {
75 if (streq(f->name, "_info.c")) {
80 is_c_src = strends(f->name, ".c");
81 if (!is_c_src && !strends(f->name, ".h"))
82 dest = &m->other_files;
83 else if (!strchr(f->name, '/')) {
88 } else if (strstarts(f->name, "test/")) {
90 if (strstarts(f->name, "test/api"))
92 else if (strstarts(f->name, "test/run"))
94 else if (strstarts(f->name, "test/compile_ok"))
95 dest = &m->compile_ok_tests;
96 else if (strstarts(f->name, "test/compile_fail"))
97 dest = &m->compile_fail_tests;
99 dest = &m->other_test_files;
101 dest = &m->other_test_files;
103 dest = &m->other_files;
105 list_add(dest, &f->list);
110 char *report_on_lines(struct list_head *files,
111 char *(*report)(const char *),
116 list_for_each(files, f, list) {
118 char **lines = get_ccan_file_lines(f);
120 for (i = 0; i < f->num_lines; i++) {
121 char *r = report(lines[i]);
125 sofar = talloc_asprintf_append(sofar,
134 struct manifest *get_manifest(void)
136 struct manifest *m = talloc(NULL, struct manifest);
140 list_head_init(&m->c_files);
141 list_head_init(&m->h_files);
142 list_head_init(&m->api_tests);
143 list_head_init(&m->run_tests);
144 list_head_init(&m->compile_ok_tests);
145 list_head_init(&m->compile_fail_tests);
146 list_head_init(&m->other_test_files);
147 list_head_init(&m->other_files);
149 /* *This* is why people hate C. */
151 m->basename = talloc_array(m, char, len);
152 while (!getcwd(m->basename, len)) {
154 err(1, "Getting current directory");
155 m->basename = talloc_realloc(m, m->basename, char, len *= 2);
158 len = strlen(m->basename);
159 while (len && m->basename[len-1] == '/')
160 m->basename[--len] = '\0';
162 m->basename = strrchr(m->basename, '/');
164 errx(1, "I don't expect to be run from the root directory");
173 * remove_comments - strip comments from a line, return copy.
174 * @line: line to copy
175 * @in_comment: are we already within a comment (from prev line).
176 * @unterminated: are we still in a comment for next line.
178 static char *remove_comments(const char *line, bool in_comment,
181 char *p, *ret = talloc_array(line, char, strlen(line) + 1);
186 /* Find first comment. */
187 const char *old_comment = strstr(line, "/*");
188 const char *new_comment = strstr(line, "//");
191 if (new_comment && old_comment)
192 comment = new_comment < old_comment
193 ? new_comment : old_comment;
194 else if (old_comment)
195 comment = old_comment;
196 else if (new_comment)
197 comment = new_comment;
201 *unterminated = false;
205 /* Copy up to comment. */
206 memcpy(p, line, comment - line);
208 line += comment - line + 2;
210 if (comment == new_comment) {
211 /* We're done: goes to EOL. */
213 *unterminated = false;
220 const char *end = strstr(line, "*/");
222 *unterminated = true;
233 static bool is_empty(const char *line)
235 return strspn(line, " \t") == strlen(line);
238 static bool continues(const char *line)
240 /* Technically, any odd number of these. But who cares? */
241 return strends(line, "\\");
244 /* Get token if it's equal to token. */
245 static bool get_token(const char **line, const char *token)
249 *line += strspn(*line, " \t");
250 if (isalnum(token[0]) || token[0] == '_')
251 toklen = strspn(*line, IDENT_CHARS);
253 /* FIXME: real tokenizer handles ++ and other multi-chars. */
254 toklen = strlen(token);
257 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
264 static char *get_symbol_token(void *ctx, const char **line)
269 *line += strspn(*line, " \t");
270 toklen = strspn(*line, IDENT_CHARS);
273 ret = talloc_strndup(ctx, *line, toklen);
278 static bool parse_hash_if(struct pp_conditions *cond, const char **line)
280 bool brackets, defined;
282 cond->inverse = get_token(line, "!");
283 defined = get_token(line, "defined");
284 brackets = get_token(line, "(");
285 cond->symbol = get_symbol_token(cond, line);
288 if (brackets && !get_token(line, ")"))
291 cond->type = PP_COND_IF;
295 /* FIXME: Get serious! */
296 static struct pp_conditions *analyze_directive(struct ccan_file *f,
298 struct pp_conditions *parent)
300 struct pp_conditions *cond = talloc(f, struct pp_conditions);
303 line = remove_comments(line, false, &unused);
305 cond->parent = parent;
306 cond->type = PP_COND_IFDEF;
308 if (!get_token(&line, "#"))
311 if (get_token(&line, "if")) {
312 if (!parse_hash_if(cond, &line))
314 } else if (get_token(&line, "elif")) {
318 cond->parent = parent->parent;
319 /* FIXME: Not quite true. This implies !parent, but we don't
320 * do multiple conditionals yet. */
321 if (!parse_hash_if(cond, &line))
323 } else if (get_token(&line, "ifdef")) {
325 cond->inverse = false;
326 brackets = get_token(&line, "(");
327 cond->symbol = get_symbol_token(cond, &line);
330 if (brackets && !get_token(&line, ")"))
332 } else if (get_token(&line, "ifndef")) {
334 cond->inverse = true;
335 brackets = get_token(&line, "(");
336 cond->symbol = get_symbol_token(cond, &line);
339 if (brackets && !get_token(&line, ")"))
341 } else if (get_token(&line, "else")) {
347 cond->inverse = !cond->inverse;
349 } else if (get_token(&line, "endif")) {
355 return parent->parent;
357 /* Not a conditional. */
367 cond->type = PP_COND_UNKNOWN;
371 /* This parser is rough, but OK if code is reasonably neat. */
372 struct line_info *get_ccan_line_info(struct ccan_file *f)
374 bool continued = false, in_comment = false;
375 struct pp_conditions *cond = NULL;
381 get_ccan_file_lines(f);
382 f->line_info = talloc_array(f->lines, struct line_info, f->num_lines);
384 for (i = 0; i < f->num_lines; continued = continues(f->lines[i++])) {
388 /* Current conditions apply to this line. */
389 f->line_info[i].cond = cond;
390 f->line_info[i].continued = continued;
393 /* Same as last line. */
394 f->line_info[i].type = f->line_info[i-1].type;
395 /* Update in_comment. */
396 remove_comments(f->lines[i], in_comment, &in_comment);
400 /* Preprocessor directive? */
402 && f->lines[i][strspn(f->lines[i], " \t")] == '#') {
403 f->line_info[i].type = PREPROC_LINE;
404 cond = analyze_directive(f, f->lines[i], cond);
408 still_doc_line = (in_comment
409 && f->line_info[i-1].type == DOC_LINE);
411 p = remove_comments(f->lines[i], in_comment, &in_comment);
413 if (strstarts(f->lines[i], "/**") || still_doc_line)
414 f->line_info[i].type = DOC_LINE;
416 f->line_info[i].type = COMMENT_LINE;
418 f->line_info[i].type = CODE_LINE;
424 enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
428 enum line_compiled ret;
430 /* No conditions? Easy. */
434 /* Check we get here at all. */
435 ret = get_ccan_line_pp(cond->parent, symbol, value);
439 switch (cond->type) {
441 if (streq(cond->symbol, symbol)) {
442 if (!value == cond->inverse)
447 /* Unknown symbol, will be 0. */
453 if (streq(cond->symbol, symbol)) {
459 /* Unknown symbol, assume undefined. */
464 default: /* Unknown. */
465 return MAYBE_COMPILED;