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, false, get_or_compile_info);
67 for (i = 0; infodeps[i]; i++) {
68 if (strstarts(infodeps[i], "ccan/"))
69 add_dep(deps, infodeps[i] + strlen("ccan/"));
74 static char *example_obj_list(struct manifest *m, struct ccan_file *f)
76 struct manifest **deps = talloc_array(f, struct manifest *, 0);
80 /* This one for a start. */
81 add_dep(&deps, m->basename);
83 /* Other modules implied by includes. */
84 for (lines = get_ccan_file_lines(f); *lines; lines++) {
85 unsigned preflen = strspn(*lines, " \t");
86 if (strstarts(*lines + preflen, "#include <ccan/")) {
89 modname = talloc_strdup(f, *lines + preflen
90 + strlen("#include <ccan/"));
91 modname[strcspn(modname, "/")] = '\0';
92 if (!have_mod(deps, modname))
93 add_dep(&deps, modname);
97 list = talloc_strdup(f, "");
98 for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++) {
99 if (deps[i]->compiled[COMPILE_NORMAL])
100 list = talloc_asprintf_append(list, " %s",
107 /* FIXME: Test with reduced features! */
108 static char *lib_list(const struct manifest *m)
112 char *ret = talloc_strdup(m, "");
114 libs = get_libs(m, m->dir, true, get_or_compile_info);
115 for (i = 0; libs[i]; i++)
116 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
120 static bool compile(const void *ctx,
122 struct ccan_file *file,
125 file->compiled[COMPILE_NORMAL] = temp_file(ctx, "", file->fullname);
126 if (!compile_and_link(ctx, file->fullname, ccan_dir,
127 example_obj_list(m, file),
129 lib_list(m), file->compiled[COMPILE_NORMAL],
131 /* Don't keep failures, even with --keep */
132 unlink(file->compiled[COMPILE_NORMAL]);
133 file->compiled[COMPILE_NORMAL] = NULL;
139 static char *start_main(char *ret, const char *why)
141 return talloc_asprintf_append(ret,
142 "/* The example %s, so fake function wrapper inserted */\n"
143 "int main(int argc, char *argv[])\n"
147 /* We only handle simple function definitions here. */
148 static char *add_func(char *others, const char *line)
150 const char *p, *end = strchr(line, '(') - 1;
151 while (cisspace(*end)) {
157 for (p = end; cisalnum(*p) || *p == '_'; p--) {
162 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
163 (unsigned)(end - p), p+1);
166 static void strip_leading_whitespace(char **lines)
168 unsigned int i, min_span = -1U;
170 for (i = 0; lines[i]; i++) {
171 unsigned int span = strspn(lines[i], " \t");
172 /* All whitespace? Ignore */
179 for (i = 0; lines[i]; i++)
180 if (strlen(lines[i]) >= min_span)
181 lines[i] += min_span;
184 static bool looks_internal(char **lines, char **why)
187 bool last_ended = true; /* Did last line finish a statement? */
189 for (i = 0; lines[i]; i++) {
190 /* Skip leading whitespace. */
191 const char *line = lines[i] + strspn(lines[i], " \t");
192 unsigned len = strspn(line, IDENT_CHARS);
194 if (!line[0] || cisspace(line[0]) || strstarts(line, "//"))
197 assert(line[strlen(line)-1] != '\n');
200 if (strstarts(line, "if") && len == 2) {
201 *why = cast_const(char *, "starts with if");
204 if (strstarts(line, "for") && len == 3) {
205 *why = cast_const(char *, "starts with for");
208 if (strstarts(line, "while") && len == 5) {
209 *why = cast_const(char *, "starts with while");
212 if (strstarts(line, "do") && len == 2) {
213 *why = cast_const(char *, "starts with do");
218 if (strstarts(line, "#include")) {
219 *why = cast_const(char *, "starts with #include");
223 if (last_ended && strchr(line, '(')) {
224 if (strstarts(line, "static")) {
225 *why = cast_const(char *,
230 if (strends(line, ")")) {
231 *why = cast_const(char *,
232 "contains ( and ends with )");
237 /* Previously prepended. */
238 if (strstr(line, "didn't seem to belong inside a function")) {
239 *why = cast_const(char *, "Comment said so");
243 /* Single identifier then operator == inside function. */
244 if (last_ended && len
245 && cispunct(line[len+strspn(line+len, " ")])) {
246 *why = cast_const(char *, "starts with identifier"
247 " then punctuation");
251 last_ended = (strends(line, "}")
252 || strends(line, ";")
253 || strends(line, "*/")
254 || streq(line, "..."));
257 /* No idea... Say yes? */
258 *why = cast_const(char *, "gave no clues");
262 /* Examples will often build on prior ones. Try combining them. */
263 static char **combine(const void *ctx, char **lines, char **prev)
265 unsigned int i, lines_total, prev_total, count;
267 const char *reasoning;
273 /* If it looks internal, put prev at start. */
274 if (looks_internal(lines, &why)) {
276 reasoning = "seemed to belong inside a function";
278 /* Try inserting in first elided position */
279 for (count = 0; lines[count]; count++) {
280 if (strcmp(lines[count], "...") == 0)
284 /* Try at start anyway? */
286 reasoning = "didn't seem to belong inside"
287 " a function, so we prepended the previous"
290 reasoning = "didn't seem to belong inside"
291 " a function, so we put the previous example"
298 for (i = 0; lines[i]; i++);
301 for (i = 0; prev[i]; i++);
304 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
305 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */",
307 memcpy(ret+1, lines, count * sizeof(ret[0]));
308 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
309 memcpy(ret+1 + count + prev_total, lines + count,
310 (lines_total - count + 1) * sizeof(ret[0]));
314 /* Only handles very simple comments. */
315 static char *strip_comment(const void *ctx, const char *orig_line)
317 char *p, *ret = talloc_strdup(ctx, orig_line);
319 p = strstr(ret, "/*");
321 p = strstr(ret, "//");
327 static char *mangle(struct manifest *m, char **lines)
329 char *ret, *use_funcs = NULL, *why;
330 bool in_function = false, fake_function = false, has_main = false;
333 ret = talloc_asprintf(m,
334 "/* Include header from module. */\n"
335 "#include <ccan/%s/%s.h>\n"
336 "/* Prepend a heap of headers. */\n"
337 "#include <assert.h>\n"
339 "#include <errno.h>\n"
340 "#include <fcntl.h>\n"
341 "#include <limits.h>\n"
342 "#include <stdbool.h>\n"
343 "#include <stdint.h>\n"
344 "#include <stdio.h>\n"
345 "#include <stdlib.h>\n"
346 "#include <string.h>\n"
347 "#include <sys/stat.h>\n"
348 "#include <sys/types.h>\n"
349 "#include <unistd.h>\n",
350 m->basename, m->basename);
352 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
353 "extern int somefunc(void);\n"
354 "int somefunc(void) { return 0; }\n"
355 "extern char somestring[];\n"
356 "char somestring[] = \"hello world\";\n");
358 if (looks_internal(lines, &why)) {
359 /* Wrap it all in main(). */
360 ret = start_main(ret, why);
361 fake_function = true;
365 ret = talloc_asprintf_append(ret,
366 "/* The example %s, so didn't wrap in main() */\n",
369 /* Primitive, very primitive. */
370 for (i = 0; lines[i]; i++) {
371 char *line = strip_comment(ret, lines[i]);
373 /* } at start of line ends a function. */
378 /* Character at start of line, with ( and no ;
379 * == function start. Ignore comments. */
380 if (!cisspace(line[0])
382 && !strchr(line, ';')
383 && !strstr(line, "//")) {
385 if (strncmp(line, "int main", 8) == 0)
387 if (strncmp(line, "static", 6) == 0) {
388 use_funcs = add_func(use_funcs,
393 /* ... means elided code. */
394 if (strcmp(line, "...") == 0) {
395 if (!in_function && !has_main
396 && looks_internal(lines + i + 1, &why)) {
397 /* This implies we start a function here. */
398 ret = start_main(ret, why);
400 fake_function = true;
403 ret = talloc_asprintf_append(ret,
404 "/* ... removed */\n");
407 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
411 ret = talloc_asprintf_append(ret,
412 "/* Need a main to link successfully. */\n"
413 "int main(void)\n{\n");
414 fake_function = true;
418 ret = talloc_asprintf_append(ret,
419 "/* Get rid of unused warnings"
420 " by printing addresses of"
421 " static funcs. */\n");
422 if (!fake_function) {
423 ret = talloc_asprintf_append(ret,
424 "int use_funcs(void);\n"
425 "int use_funcs(void) {\n");
426 fake_function = true;
428 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
432 ret = talloc_asprintf_append(ret, "return 0;\n"
437 static struct ccan_file *mangle_example(struct manifest *m,
438 struct ccan_file *example,
441 char *name, *contents;
445 name = temp_file(example, ".c",
446 talloc_asprintf(m, "%s/mangled-%s",
447 m->dir, example->name));
448 f = new_ccan_file(example,
449 talloc_dirname(example, name),
450 talloc_basename(example, name));
451 talloc_steal(f, name);
453 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
457 contents = mangle(m, lines);
458 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
463 f->contents = talloc_steal(f, contents);
464 list_add(&m->mangled_examples, &f->list);
468 /* If an example has expected output, it's complete and should not be
469 * included in future examples. */
470 static bool has_expected_output(char **lines)
474 for (i = 0; lines[i]; i++) {
475 char *p = lines[i] + strspn(lines[i], " \t");
476 if (!strstarts(p, "//"))
478 p += strspn(p, "/ ");
479 if (strncasecmp(p, "given", strlen("given")) == 0)
485 static unsigned int try_compiling(struct manifest *m,
488 struct ccan_file *mangled[3],
495 /* Try standalone. */
497 res[0] = compile(i, m, mangled[0], &err[0]);
498 lines[0] = get_ccan_file_lines(i);
499 if (res[0] && streq(err[0], ""))
503 lines[1] = combine(i, get_ccan_file_lines(i), prev);
505 mangled[1] = mangle_example(m, i, lines[1]);
506 res[1] = compile(i, m, mangled[1], &err[1]);
507 if (res[1] && streq(err[1], "")) {
514 /* Try standalone. */
515 lines[num] = get_ccan_file_lines(i);
516 mangled[num] = mangle_example(m, i, lines[num]);
517 res[num] = compile(i, m, mangled[num], &err[num]);
522 static void build_examples(struct manifest *m,
523 unsigned int *timeleft, struct score *score)
527 bool warnings = false;
532 list_for_each(&m->examples, i, list) {
534 struct ccan_file *file[3] = { NULL, NULL, NULL };
542 /* Simplify our dumb parsing. */
543 strip_leading_whitespace(get_ccan_file_lines(i));
545 num = try_compiling(m, i, prev, file, res, err, lines);
547 /* First look for a compile without any warnings. */
548 for (j = 0; j < num; j++) {
549 if (res[j] && streq(err[j], "")) {
550 if (!has_expected_output(lines[j]))
557 /* Now accept anything which succeeded. */
558 for (j = 0; j < num; j++) {
560 if (!has_expected_output(lines[j]))
564 score_file_error(score, file[j], 0,
565 "Compiling extracted example"
571 get_ccan_file_contents(file[j]),
580 error = "Compiling standalone, adding headers, "
581 "and including previous "
582 "example all failed";
584 error = "Standalone compile and"
585 " adding headers both failed";
588 error = talloc_asprintf(score,
589 "Standalone example:\n"
592 "Combining with previous example:\n"
595 "Adding headers, wrappers:\n"
598 get_ccan_file_contents(file[0]),
600 get_ccan_file_contents(file[1]),
602 get_ccan_file_contents(file[2]),
605 error = talloc_asprintf(score,
606 "Standalone example:\n"
609 "Adding headers, wrappers:\n"
612 get_ccan_file_contents(file[0]),
614 get_ccan_file_contents(file[1]),
618 score_file_error(score, i, 0, "%s", error);
619 /* This didn't work, so not a candidate for combining. */
626 /* An extra point if they all compiled without warnings. */
627 if (!list_empty(&m->examples)) {
634 struct ccanlint examples_compile = {
635 .key = "examples_compile",
636 .name = "Module examples compile",
637 .check = build_examples,
639 .needs = "examples_exist module_builds"
642 REGISTER_TEST(examples_compile);