1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/cast/cast.h>
5 #include <ccan/str/str.h>
17 static const char *can_run(struct manifest *m)
20 return "Safe mode enabled";
21 if (list_empty(&m->examples))
22 return "No examples to compile";
26 static void add_mod(struct manifest ***deps, struct manifest *m)
28 unsigned int num = talloc_get_size(*deps) / sizeof(*deps);
29 *deps = talloc_realloc(NULL, *deps, struct manifest *, num + 1);
33 static bool have_mod(struct manifest *deps[], const char *basename)
37 for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++)
38 if (strcmp(deps[i]->basename, basename) == 0)
43 static void add_dep(struct manifest ***deps, const char *basename)
49 if (have_mod(*deps, basename))
52 m = get_manifest(*deps, talloc_asprintf(*deps, "%s/ccan/%s",
54 errstr = build_submodule(m, cflags, COMPILE_NORMAL);
56 errx(1, "%s", errstr);
60 /* Get that modules depends as well... */
65 infodeps = get_deps(m, m->dir, "depends", false,
68 for (i = 0; infodeps[i]; i++) {
69 if (strstarts(infodeps[i], "ccan/"))
70 add_dep(deps, infodeps[i] + strlen("ccan/"));
75 static struct manifest **get_example_deps(struct manifest *m,
79 struct manifest **deps = talloc_array(f, struct manifest *, 0);
81 /* This one for a start. */
82 add_dep(&deps, m->basename);
84 /* Other modules implied by includes. */
85 for (lines = get_ccan_file_lines(f); *lines; lines++) {
86 unsigned preflen = strspn(*lines, " \t");
87 if (strstarts(*lines + preflen, "#include <ccan/")) {
90 modname = talloc_strdup(f, *lines + preflen
91 + strlen("#include <ccan/"));
92 modname[strcspn(modname, "/")] = '\0';
93 if (!have_mod(deps, modname))
94 add_dep(&deps, modname);
101 static char *example_obj_list(const void *ctx, struct manifest **deps)
103 char *list = talloc_strdup(ctx, "");
106 for (i = 0; i < talloc_array_length(deps); i++) {
107 if (deps[i]->compiled[COMPILE_NORMAL])
108 list = talloc_asprintf_append(list, " %s",
115 static char *example_lib_list(const void *ctx, struct manifest **deps)
117 char *list = talloc_strdup(ctx, "");
121 /* FIXME: This doesn't uniquify. */
122 for (i = 0; i < talloc_array_length(deps); i++) {
123 libs = get_libs(ctx, deps[i]->dir, NULL, get_or_compile_info);
124 for (j = 0; libs[j]; j++)
125 list = talloc_asprintf_append(list, "-l%s ", libs[j]);
130 /* FIXME: Test with reduced features! */
131 static bool compile(const void *ctx,
133 struct ccan_file *file,
136 struct manifest **deps = get_example_deps(m, file);
138 file->compiled[COMPILE_NORMAL] = temp_file(ctx, "", file->fullname);
139 if (!compile_and_link(ctx, file->fullname, ccan_dir,
140 example_obj_list(file, deps),
142 example_lib_list(file, deps),
143 file->compiled[COMPILE_NORMAL],
145 /* Don't keep failures, even with --keep */
146 unlink(file->compiled[COMPILE_NORMAL]);
147 file->compiled[COMPILE_NORMAL] = NULL;
153 static char *start_main(char *ret, const char *why)
155 return talloc_asprintf_append(ret,
156 "/* The example %s, so fake function wrapper inserted */\n"
157 "int main(int argc, char *argv[])\n"
161 /* We only handle simple function definitions here. */
162 static char *add_func(char *others, const char *line)
164 const char *p, *end = strchr(line, '(') - 1;
165 while (cisspace(*end)) {
171 for (p = end; cisalnum(*p) || *p == '_'; p--) {
176 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
177 (unsigned)(end - p), p+1);
180 static void strip_leading_whitespace(char **lines)
182 unsigned int i, min_span = -1U;
184 for (i = 0; lines[i]; i++) {
185 unsigned int span = strspn(lines[i], " \t");
186 /* All whitespace? Ignore */
193 for (i = 0; lines[i]; i++)
194 if (strlen(lines[i]) >= min_span)
195 lines[i] += min_span;
198 static bool looks_internal(char **lines, char **why)
201 bool last_ended = true; /* Did last line finish a statement? */
203 for (i = 0; lines[i]; i++) {
204 /* Skip leading whitespace. */
205 const char *line = lines[i] + strspn(lines[i], " \t");
206 unsigned len = strspn(line, IDENT_CHARS);
208 if (!line[0] || cisspace(line[0]) || strstarts(line, "//")
209 || strstarts(line, "#line"))
212 assert(line[strlen(line)-1] != '\n');
215 if (strstarts(line, "if") && len == 2) {
216 *why = cast_const(char *, "starts with if");
219 if (strstarts(line, "for") && len == 3) {
220 *why = cast_const(char *, "starts with for");
223 if (strstarts(line, "while") && len == 5) {
224 *why = cast_const(char *, "starts with while");
227 if (strstarts(line, "do") && len == 2) {
228 *why = cast_const(char *, "starts with do");
233 if (strstarts(line, "#include")) {
234 *why = cast_const(char *, "starts with #include");
238 if (last_ended && strchr(line, '(')) {
239 if (strstarts(line, "static")) {
240 *why = cast_const(char *,
245 if (strends(line, ")")) {
246 *why = cast_const(char *,
247 "contains ( and ends with )");
252 /* Previously prepended. */
253 if (strstr(line, "didn't seem to belong inside a function")) {
254 *why = cast_const(char *, "Comment said so");
258 /* Single identifier then operator == inside function. */
259 if (last_ended && len
260 && cispunct(line[len+strspn(line+len, " ")])) {
261 *why = cast_const(char *, "starts with identifier"
262 " then punctuation");
266 last_ended = (strends(line, "}")
267 || strends(line, ";")
268 || strends(line, "*/")
269 || streq(line, "..."));
272 /* No idea... Say yes? */
273 *why = cast_const(char *, "gave no clues");
277 /* Examples will often build on prior ones. Try combining them. */
278 static char **combine(const void *ctx, char **lines, char **prev)
280 unsigned int i, lines_total, prev_total, count;
282 const char *reasoning;
288 /* If it looks internal, put prev at start. */
289 if (looks_internal(lines, &why)) {
291 reasoning = "seemed to belong inside a function";
293 /* Try inserting in first elided position */
294 for (count = 0; lines[count]; count++) {
295 if (strcmp(lines[count], "...") == 0)
299 /* Try at start anyway? */
301 reasoning = "didn't seem to belong inside"
302 " a function, so we prepended the previous"
305 reasoning = "didn't seem to belong inside"
306 " a function, so we put the previous example"
313 for (i = 0; lines[i]; i++);
316 for (i = 0; prev[i]; i++);
319 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
320 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */",
322 memcpy(ret+1, lines, count * sizeof(ret[0]));
323 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
324 memcpy(ret+1 + count + prev_total, lines + count,
325 (lines_total - count + 1) * sizeof(ret[0]));
329 /* Only handles very simple comments. */
330 static char *strip_comment(const void *ctx, const char *orig_line)
332 char *p, *ret = talloc_strdup(ctx, orig_line);
334 p = strstr(ret, "/*");
336 p = strstr(ret, "//");
342 static char *mangle(struct manifest *m, char **lines)
344 char *ret, *use_funcs = NULL, *why;
345 bool in_function = false, fake_function = false, has_main = false;
348 ret = talloc_asprintf(m,
349 "/* Include header from module. */\n"
350 "#include <ccan/%s/%s.h>\n"
351 "/* Prepend a heap of headers. */\n"
352 "#include <assert.h>\n"
354 "#include <errno.h>\n"
355 "#include <fcntl.h>\n"
356 "#include <limits.h>\n"
357 "#include <stdbool.h>\n"
358 "#include <stdint.h>\n"
359 "#include <stdio.h>\n"
360 "#include <stdlib.h>\n"
361 "#include <string.h>\n"
362 "#include <sys/stat.h>\n"
363 "#include <sys/types.h>\n"
364 "#include <unistd.h>\n",
365 m->basename, m->basename);
367 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
368 "extern int somefunc(void);\n"
369 "int somefunc(void) { return 0; }\n"
370 "extern char somestring[];\n"
371 "char somestring[] = \"hello world\";\n");
373 if (looks_internal(lines, &why)) {
374 /* Wrap it all in main(). */
375 ret = start_main(ret, why);
376 fake_function = true;
380 ret = talloc_asprintf_append(ret,
381 "/* The example %s, so didn't wrap in main() */\n",
384 /* Primitive, very primitive. */
385 for (i = 0; lines[i]; i++) {
386 char *line = strip_comment(ret, lines[i]);
388 /* } at start of line ends a function. */
393 /* Character at start of line, with ( and no ;
394 * == function start. Ignore comments. */
395 if (!cisspace(line[0])
397 && !strchr(line, ';')
398 && !strstr(line, "//")) {
400 if (strncmp(line, "int main", 8) == 0)
402 if (strncmp(line, "static", 6) == 0) {
403 use_funcs = add_func(use_funcs,
408 /* ... means elided code. */
409 if (strcmp(line, "...") == 0) {
410 if (!in_function && !has_main
411 && looks_internal(lines + i + 1, &why)) {
412 /* This implies we start a function here. */
413 ret = start_main(ret, why);
415 fake_function = true;
418 ret = talloc_asprintf_append(ret,
419 "/* ... removed */\n");
422 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
426 ret = talloc_asprintf_append(ret,
427 "/* Need a main to link successfully. */\n"
428 "int main(void)\n{\n");
429 fake_function = true;
433 ret = talloc_asprintf_append(ret,
434 "/* Get rid of unused warnings"
435 " by printing addresses of"
436 " static funcs. */\n");
437 if (!fake_function) {
438 ret = talloc_asprintf_append(ret,
439 "int use_funcs(void);\n"
440 "int use_funcs(void) {\n");
441 fake_function = true;
443 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
447 ret = talloc_asprintf_append(ret, "return 0;\n"
452 static struct ccan_file *mangle_example(struct manifest *m,
453 struct ccan_file *example,
456 char *name, *contents;
460 name = temp_file(example, ".c",
461 talloc_asprintf(m, "%s/mangled-%s",
462 m->dir, example->name));
463 f = new_ccan_file(example,
464 talloc_dirname(example, name),
465 talloc_basename(example, name));
466 talloc_steal(f, name);
468 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
472 contents = mangle(m, lines);
473 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
478 f->contents = talloc_steal(f, contents);
479 list_add(&m->mangled_examples, &f->list);
483 /* If an example has expected output, it's complete and should not be
484 * included in future examples. */
485 static bool has_expected_output(char **lines)
489 for (i = 0; lines[i]; i++) {
490 char *p = lines[i] + strspn(lines[i], " \t");
491 if (!strstarts(p, "//"))
493 p += strspn(p, "/ ");
494 if (strncasecmp(p, "given", strlen("given")) == 0)
500 static unsigned int try_compiling(struct manifest *m,
503 struct ccan_file *mangled[3],
510 /* Try standalone. */
512 res[0] = compile(i, m, mangled[0], &err[0]);
513 lines[0] = get_ccan_file_lines(i);
514 if (res[0] && streq(err[0], ""))
518 lines[1] = combine(i, get_ccan_file_lines(i), prev);
520 mangled[1] = mangle_example(m, i, lines[1]);
521 res[1] = compile(i, m, mangled[1], &err[1]);
522 if (res[1] && streq(err[1], "")) {
529 /* Try standalone. */
530 lines[num] = get_ccan_file_lines(i);
531 mangled[num] = mangle_example(m, i, lines[num]);
532 res[num] = compile(i, m, mangled[num], &err[num]);
537 static void build_examples(struct manifest *m,
538 unsigned int *timeleft, struct score *score)
542 bool warnings = false;
547 list_for_each(&m->examples, i, list) {
549 struct ccan_file *file[3] = { NULL, NULL, NULL };
557 /* Simplify our dumb parsing. */
558 strip_leading_whitespace(get_ccan_file_lines(i));
560 num = try_compiling(m, i, prev, file, res, err, lines);
562 /* First look for a compile without any warnings. */
563 for (j = 0; j < num; j++) {
564 if (res[j] && streq(err[j], "")) {
565 if (!has_expected_output(lines[j]))
572 /* Now accept anything which succeeded. */
573 for (j = 0; j < num; j++) {
575 if (!has_expected_output(lines[j]))
579 score_file_error(score, file[j], 0,
580 "Compiling extracted example"
586 get_ccan_file_contents(file[j]),
595 error = "Compiling standalone, adding headers, "
596 "and including previous "
597 "example all failed";
599 error = "Standalone compile and"
600 " adding headers both failed";
603 error = talloc_asprintf(score,
604 "Standalone example:\n"
607 "Combining with previous example:\n"
610 "Adding headers, wrappers:\n"
613 get_ccan_file_contents(file[0]),
615 get_ccan_file_contents(file[1]),
617 get_ccan_file_contents(file[2]),
620 error = talloc_asprintf(score,
621 "Standalone example:\n"
624 "Adding headers, wrappers:\n"
627 get_ccan_file_contents(file[0]),
629 get_ccan_file_contents(file[1]),
633 score_file_error(score, i, 0, "%s", error);
634 /* This didn't work, so not a candidate for combining. */
641 /* An extra point if they all compiled without warnings. */
642 if (!list_empty(&m->examples)) {
649 struct ccanlint examples_compile = {
650 .key = "examples_compile",
651 .name = "Module examples compile",
652 .check = build_examples,
654 .needs = "examples_exist module_builds"
657 REGISTER_TEST(examples_compile);