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>
15 #include "../compulsory_tests/build.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,
66 &m->info_file->compiled[COMPILE_NORMAL]);
68 for (i = 0; infodeps[i]; i++) {
69 if (strstarts(infodeps[i], "ccan/"))
70 add_dep(deps, infodeps[i] + strlen("ccan/"));
75 static char *example_obj_list(struct manifest *m, struct ccan_file *f)
77 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);
98 list = talloc_strdup(f, "");
99 for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++) {
100 if (deps[i]->compiled[COMPILE_NORMAL])
101 list = talloc_asprintf_append(list, " %s",
108 /* FIXME: Test with reduced features! */
109 static char *lib_list(const struct manifest *m)
113 char *ret = talloc_strdup(m, "");
115 libs = get_libs(m, m->dir, &num,
116 &m->info_file->compiled[COMPILE_NORMAL]);
117 for (i = 0; i < num; i++)
118 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
122 static bool compile(const void *ctx,
124 struct ccan_file *file,
125 bool keep, char **output)
127 file->compiled[COMPILE_NORMAL]
128 = maybe_temp_file(ctx, "", keep, file->fullname);
129 if (!compile_and_link(ctx, file->fullname, ccan_dir,
130 example_obj_list(m, file),
132 lib_list(m), file->compiled[COMPILE_NORMAL],
134 /* Don't keep failures. */
136 unlink(file->compiled[COMPILE_NORMAL]);
137 talloc_free(file->compiled[COMPILE_NORMAL]);
138 file->compiled[COMPILE_NORMAL] = NULL;
144 static char *start_main(char *ret, const char *why)
146 return talloc_asprintf_append(ret,
147 "/* The example %s, so fake function wrapper inserted */\n"
148 "int main(int argc, char *argv[])\n"
152 /* We only handle simple function definitions here. */
153 static char *add_func(char *others, const char *line)
155 const char *p, *end = strchr(line, '(') - 1;
156 while (cisspace(*end)) {
162 for (p = end; cisalnum(*p) || *p == '_'; p--) {
167 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
168 (unsigned)(end - p), p+1);
171 static void strip_leading_whitespace(char **lines)
173 unsigned int i, min_span = -1U;
175 for (i = 0; lines[i]; i++) {
176 unsigned int span = strspn(lines[i], " \t");
177 /* All whitespace? Ignore */
184 for (i = 0; lines[i]; i++)
185 if (strlen(lines[i]) >= min_span)
186 lines[i] += min_span;
189 static bool looks_internal(char **lines, char **why)
192 bool last_ended = true; /* Did last line finish a statement? */
194 for (i = 0; lines[i]; i++) {
195 /* Skip leading whitespace. */
196 const char *line = lines[i] + strspn(lines[i], " \t");
197 unsigned len = strspn(line, IDENT_CHARS);
199 if (!line[0] || cisspace(line[0]) || strstarts(line, "//"))
203 if (strstarts(line, "if") && len == 2) {
204 *why = cast_const(char *, "starts with if");
207 if (strstarts(line, "for") && len == 3) {
208 *why = cast_const(char *, "starts with for");
211 if (strstarts(line, "while") && len == 5) {
212 *why = cast_const(char *, "starts with while");
215 if (strstarts(line, "do") && len == 2) {
216 *why = cast_const(char *, "starts with do");
221 if (strstarts(line, "#include")) {
222 *why = cast_const(char *, "starts with #include");
226 if (last_ended && strchr(line, '(')) {
227 if (strstarts(line, "static")) {
228 *why = cast_const(char *,
233 if (strends(line, ")")) {
234 *why = cast_const(char *,
235 "contains ( and ends with )");
240 /* Single identifier then operator == inside function. */
241 if (last_ended && len
242 && cispunct(line[len+strspn(line+len, " ")])) {
243 *why = cast_const(char *, "starts with identifier"
244 " then punctuation");
248 last_ended = (strends(line, "}")
249 || strends(line, ";")
250 || streq(line, "..."));
253 /* No idea... Say yes? */
254 *why = cast_const(char *, "gave no clues");
258 /* Examples will often build on prior ones. Try combining them. */
259 static char **combine(const void *ctx, char **lines, char **prev)
261 unsigned int i, lines_total, prev_total, count;
263 const char *reasoning;
269 /* If it looks internal, put prev at start. */
270 if (looks_internal(lines, &why)) {
272 reasoning = "seemed to belong inside a function";
274 /* Try inserting in first elided position */
275 for (count = 0; lines[count]; count++) {
276 if (strcmp(lines[count], "...") == 0)
280 /* Try at start anyway? */
282 reasoning = "didn't seem to belong inside"
283 " a function, so we prepended the previous"
286 reasoning = "didn't seem to belong inside"
287 " a function, so we put the previous example"
294 for (i = 0; lines[i]; i++);
297 for (i = 0; prev[i]; i++);
300 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
301 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
303 memcpy(ret+1, lines, count * sizeof(ret[0]));
304 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
305 memcpy(ret+1 + count + prev_total, lines + count,
306 (lines_total - count + 1) * sizeof(ret[0]));
310 /* Only handles very simple comments. */
311 static char *strip_comment(const void *ctx, const char *orig_line)
313 char *p, *ret = talloc_strdup(ctx, orig_line);
315 p = strstr(ret, "/*");
317 p = strstr(ret, "//");
323 static char *mangle(struct manifest *m, char **lines)
325 char *ret, *use_funcs = NULL, *why;
326 bool in_function = false, fake_function = false, has_main = false;
329 ret = talloc_asprintf(m,
330 "/* Include header from module. */\n"
331 "#include <ccan/%s/%s.h>\n"
332 "/* Prepend a heap of headers. */\n"
333 "#include <assert.h>\n"
335 "#include <errno.h>\n"
336 "#include <fcntl.h>\n"
337 "#include <limits.h>\n"
338 "#include <stdbool.h>\n"
339 "#include <stdint.h>\n"
340 "#include <stdio.h>\n"
341 "#include <stdlib.h>\n"
342 "#include <string.h>\n"
343 "#include <sys/stat.h>\n"
344 "#include <sys/types.h>\n"
345 "#include <unistd.h>\n",
346 m->basename, m->basename);
348 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
349 "extern int somefunc(void);\n"
350 "int somefunc(void) { return 0; }\n"
351 "extern char somestring[];\n"
352 "char somestring[] = \"hello world\";\n");
354 if (looks_internal(lines, &why)) {
355 /* Wrap it all in main(). */
356 ret = start_main(ret, why);
357 fake_function = true;
361 ret = talloc_asprintf_append(ret,
362 "/* The example %s, so didn't wrap in main() */\n",
365 /* Primitive, very primitive. */
366 for (i = 0; lines[i]; i++) {
367 char *line = strip_comment(ret, lines[i]);
369 /* } at start of line ends a function. */
374 /* Character at start of line, with ( and no ;
375 * == function start. Ignore comments. */
376 if (!cisspace(line[0])
378 && !strchr(line, ';')
379 && !strstr(line, "//")) {
381 if (strncmp(line, "int main", 8) == 0)
383 if (strncmp(line, "static", 6) == 0) {
384 use_funcs = add_func(use_funcs,
389 /* ... means elided code. */
390 if (strcmp(line, "...") == 0) {
391 if (!in_function && !has_main
392 && looks_internal(lines + i + 1, &why)) {
393 /* This implies we start a function here. */
394 ret = start_main(ret, why);
396 fake_function = true;
399 ret = talloc_asprintf_append(ret,
400 "/* ... removed */\n");
403 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
407 ret = talloc_asprintf_append(ret,
408 "/* Need a main to link successfully. */\n"
409 "int main(void)\n{\n");
410 fake_function = true;
414 ret = talloc_asprintf_append(ret,
415 "/* Get rid of unused warnings"
416 " by printing addresses of"
417 " static funcs. */\n");
418 if (!fake_function) {
419 ret = talloc_asprintf_append(ret,
420 "int use_funcs(void);\n"
421 "int use_funcs(void) {\n");
422 fake_function = true;
424 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
428 ret = talloc_asprintf_append(ret, "return 0;\n"
433 static struct ccan_file *mangle_example(struct manifest *m,
434 struct ccan_file *example,
438 char *name, *contents;
442 name = maybe_temp_file(example, ".c", keep,
443 talloc_asprintf(m, "%s/mangled-%s",
444 m->dir, example->name));
445 f = new_ccan_file(example,
446 talloc_dirname(example, name),
447 talloc_basename(example, name));
448 talloc_steal(f, name);
450 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
454 contents = mangle(m, lines);
455 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
460 f->contents = talloc_steal(f, contents);
461 list_add(&m->mangled_examples, &f->list);
465 /* If an example has expected output, it's complete and should not be
466 * included in future examples. */
467 static bool has_expected_output(char **lines)
471 for (i = 0; lines[i]; i++) {
472 char *p = lines[i] + strspn(lines[i], " \t");
473 if (!strstarts(p, "//"))
475 p += strspn(p, "/ ");
476 if (strncasecmp(p, "given", strlen("given")) == 0)
482 static unsigned int try_compiling(struct manifest *m,
486 struct ccan_file *mangled[3],
493 /* Try standalone. */
495 res[0] = compile(i, m, mangled[0], keep, &err[0]);
496 lines[0] = get_ccan_file_lines(i);
497 if (res[0] && streq(err[0], ""))
501 lines[1] = combine(i, get_ccan_file_lines(i), prev);
503 mangled[1] = mangle_example(m, i, lines[1], keep);
504 res[1] = compile(i, m, mangled[1], keep, &err[1]);
505 if (res[1] && streq(err[1], "")) {
512 /* Try standalone. */
513 lines[num] = get_ccan_file_lines(i);
514 mangled[num] = mangle_example(m, i, lines[num], keep);
515 res[num] = compile(i, m, mangled[num], keep, &err[num]);
520 static void build_examples(struct manifest *m, bool keep,
521 unsigned int *timeleft, struct score *score)
525 bool warnings = false;
530 list_for_each(&m->examples, i, list) {
532 struct ccan_file *file[3] = { NULL, NULL, NULL };
540 /* Simplify our dumb parsing. */
541 strip_leading_whitespace(get_ccan_file_lines(i));
543 num = try_compiling(m, i, prev, keep, file, res, err, lines);
545 /* First look for a compile without any warnings. */
546 for (j = 0; j < num; j++) {
547 if (res[j] && streq(err[j], "")) {
548 if (!has_expected_output(lines[j]))
555 /* Now accept anything which succeeded. */
556 for (j = 0; j < num; j++) {
558 if (!has_expected_output(lines[j]))
562 score_file_error(score, file[j], 0,
563 "Compiling extracted example"
569 get_ccan_file_contents(file[j]),
578 error = "Compiling standalone, adding headers, "
579 "and including previous "
580 "example all failed";
582 error = "Standalone compile and"
583 " adding headers both failed";
586 error = talloc_asprintf(score,
587 "Standalone example:\n"
590 "Combining with previous example:\n"
593 "Adding headers, wrappers:\n"
596 get_ccan_file_contents(file[0]),
598 get_ccan_file_contents(file[1]),
600 get_ccan_file_contents(file[2]),
603 error = talloc_asprintf(score,
604 "Standalone example:\n"
607 "Adding headers, wrappers:\n"
610 get_ccan_file_contents(file[0]),
612 get_ccan_file_contents(file[1]),
616 score_file_error(score, i, 0, "%s", error);
617 /* This didn't work, so not a candidate for combining. */
624 /* An extra point if they all compiled without warnings. */
625 if (!list_empty(&m->examples)) {
632 struct ccanlint examples_compile = {
633 .key = "examples_compile",
634 .name = "Module examples compile",
635 .check = build_examples,
637 .needs = "examples_exist module_builds"
640 REGISTER_TEST(examples_compile);