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);
56 errx(1, "%s", errstr);
60 /* Get that modules depends as well... */
65 infodeps = get_deps(m, m->dir, false, &m->info_file->compiled);
67 for (i = 0; infodeps[i]; i++) {
68 if (strstarts(infodeps[i], "ccan/"))
69 add_dep(deps, infodeps[i] + strlen("ccan/"));
74 static char *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)
100 list = talloc_asprintf_append(list, " %s",
106 static char *lib_list(const struct manifest *m)
109 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
110 char *ret = talloc_strdup(m, "");
112 for (i = 0; i < num; i++)
113 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
117 static bool compile(const void *ctx,
119 struct ccan_file *file,
120 bool keep, char **output)
122 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
123 if (!compile_and_link(ctx, file->fullname, ccan_dir,
126 lib_list(m), file->compiled, output)) {
127 /* Don't keep failures. */
129 unlink(file->compiled);
130 talloc_free(file->compiled);
131 file->compiled = NULL;
137 static char *start_main(char *ret, const char *why)
139 return talloc_asprintf_append(ret,
140 "/* The example %s, so fake function wrapper inserted */\n"
141 "int main(int argc, char *argv[])\n"
145 /* We only handle simple function definitions here. */
146 static char *add_func(char *others, const char *line)
148 const char *p, *end = strchr(line, '(') - 1;
149 while (cisspace(*end)) {
155 for (p = end; cisalnum(*p) || *p == '_'; p--) {
160 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
161 (unsigned)(end - p), p+1);
164 static void strip_leading_whitespace(char **lines)
166 unsigned int i, min_span = -1U;
168 for (i = 0; lines[i]; i++) {
169 unsigned int span = strspn(lines[i], " \t");
170 /* All whitespace? Ignore */
177 for (i = 0; lines[i]; i++)
178 if (strlen(lines[i]) >= min_span)
179 lines[i] += min_span;
182 static bool looks_internal(char **lines, char **why)
185 bool last_ended = true; /* Did last line finish a statement? */
187 for (i = 0; lines[i]; i++) {
188 /* Skip leading whitespace. */
189 const char *line = lines[i] + strspn(lines[i], " \t");
190 unsigned len = strspn(line, IDENT_CHARS);
192 if (!line[0] || cisspace(line[0]) || strstarts(line, "//"))
196 if (strstarts(line, "if") && len == 2) {
197 *why = cast_const(char *, "starts with if");
200 if (strstarts(line, "for") && len == 3) {
201 *why = cast_const(char *, "starts with for");
204 if (strstarts(line, "while") && len == 5) {
205 *why = cast_const(char *, "starts with while");
208 if (strstarts(line, "do") && len == 2) {
209 *why = cast_const(char *, "starts with do");
214 if (strstarts(line, "#include")) {
215 *why = cast_const(char *, "starts with #include");
219 if (last_ended && strchr(line, '(')) {
220 if (strstarts(line, "static")) {
221 *why = cast_const(char *,
226 if (strends(line, ")")) {
227 *why = cast_const(char *,
228 "contains ( and ends with )");
233 /* Single identifier then operator == inside function. */
234 if (last_ended && len
235 && cispunct(line[len+strspn(line+len, " ")])) {
236 *why = cast_const(char *, "starts with identifier"
237 " then punctuation");
241 last_ended = (strends(line, "}")
242 || strends(line, ";")
243 || streq(line, "..."));
246 /* No idea... Say yes? */
247 *why = cast_const(char *, "gave no clues");
251 /* Examples will often build on prior ones. Try combining them. */
252 static char **combine(const void *ctx, char **lines, char **prev)
254 unsigned int i, lines_total, prev_total, count;
256 const char *reasoning;
262 /* If it looks internal, put prev at start. */
263 if (looks_internal(lines, &why)) {
265 reasoning = "seemed to belong inside a function";
267 /* Try inserting in first elided position */
268 for (count = 0; lines[count]; count++) {
269 if (strcmp(lines[count], "...") == 0)
273 /* Try at start anyway? */
275 reasoning = "didn't seem to belong inside"
276 " a function, so we prepended the previous"
279 reasoning = "didn't seem to belong inside"
280 " a function, so we put the previous example"
287 for (i = 0; lines[i]; i++);
290 for (i = 0; prev[i]; i++);
293 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
294 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
296 memcpy(ret+1, lines, count * sizeof(ret[0]));
297 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
298 memcpy(ret+1 + count + prev_total, lines + count,
299 (lines_total - count + 1) * sizeof(ret[0]));
303 /* Only handles very simple comments. */
304 static char *strip_comment(const void *ctx, const char *orig_line)
306 char *p, *ret = talloc_strdup(ctx, orig_line);
308 p = strstr(ret, "/*");
310 p = strstr(ret, "//");
316 static char *mangle(struct manifest *m, char **lines)
318 char *ret, *use_funcs = NULL, *why;
319 bool in_function = false, fake_function = false, has_main = false;
322 ret = talloc_asprintf(m,
323 "/* Include header from module. */\n"
324 "#include <ccan/%s/%s.h>\n"
325 "/* Prepend a heap of headers. */\n"
326 "#include <assert.h>\n"
328 "#include <errno.h>\n"
329 "#include <fcntl.h>\n"
330 "#include <limits.h>\n"
331 "#include <stdbool.h>\n"
332 "#include <stdint.h>\n"
333 "#include <stdio.h>\n"
334 "#include <stdlib.h>\n"
335 "#include <string.h>\n"
336 "#include <sys/stat.h>\n"
337 "#include <sys/types.h>\n"
338 "#include <unistd.h>\n",
339 m->basename, m->basename);
341 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
342 "extern int somefunc(void);\n"
343 "int somefunc(void) { return 0; }\n"
344 "extern char somestring[];\n"
345 "char somestring[] = \"hello world\";\n");
347 if (looks_internal(lines, &why)) {
348 /* Wrap it all in main(). */
349 ret = start_main(ret, why);
350 fake_function = true;
354 ret = talloc_asprintf_append(ret,
355 "/* The example %s, so didn't wrap in main() */\n",
358 /* Primitive, very primitive. */
359 for (i = 0; lines[i]; i++) {
360 char *line = strip_comment(ret, lines[i]);
362 /* } at start of line ends a function. */
367 /* Character at start of line, with ( and no ;
368 * == function start. Ignore comments. */
369 if (!cisspace(line[0])
371 && !strchr(line, ';')
372 && !strstr(line, "//")) {
374 if (strncmp(line, "int main", 8) == 0)
376 if (strncmp(line, "static", 6) == 0) {
377 use_funcs = add_func(use_funcs,
382 /* ... means elided code. */
383 if (strcmp(line, "...") == 0) {
384 if (!in_function && !has_main
385 && looks_internal(lines + i + 1, &why)) {
386 /* This implies we start a function here. */
387 ret = start_main(ret, why);
389 fake_function = true;
392 ret = talloc_asprintf_append(ret,
393 "/* ... removed */\n");
396 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
400 ret = talloc_asprintf_append(ret,
401 "/* Need a main to link successfully. */\n"
402 "int main(void)\n{\n");
403 fake_function = true;
407 ret = talloc_asprintf_append(ret,
408 "/* Get rid of unused warnings"
409 " by printing addresses of"
410 " static funcs. */\n");
411 if (!fake_function) {
412 ret = talloc_asprintf_append(ret,
413 "int use_funcs(void);\n"
414 "int use_funcs(void) {\n");
415 fake_function = true;
417 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
421 ret = talloc_asprintf_append(ret, "return 0;\n"
426 static struct ccan_file *mangle_example(struct manifest *m,
427 struct ccan_file *example,
431 char *name, *contents;
435 name = maybe_temp_file(example, ".c", keep,
436 talloc_asprintf(m, "%s/mangled-%s",
437 m->dir, example->name));
438 f = new_ccan_file(example,
439 talloc_dirname(example, name),
440 talloc_basename(example, name));
441 talloc_steal(f, name);
443 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
447 contents = mangle(m, lines);
448 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
453 f->contents = talloc_steal(f, contents);
454 list_add(&m->mangled_examples, &f->list);
458 /* If an example has expected output, it's complete and should not be
459 * included in future examples. */
460 static bool has_expected_output(char **lines)
464 for (i = 0; lines[i]; i++) {
465 char *p = lines[i] + strspn(lines[i], " \t");
466 if (!strstarts(p, "//"))
468 p += strspn(p, "/ ");
469 if (strncasecmp(p, "given", strlen("given")) == 0)
475 static unsigned int try_compiling(struct manifest *m,
479 struct ccan_file *mangled[3],
486 /* Try standalone. */
488 res[0] = compile(i, m, mangled[0], keep, &err[0]);
489 lines[0] = get_ccan_file_lines(i);
490 if (res[0] && streq(err[0], ""))
494 lines[1] = combine(i, get_ccan_file_lines(i), prev);
496 mangled[1] = mangle_example(m, i, lines[1], keep);
497 res[1] = compile(i, m, mangled[1], keep, &err[1]);
498 if (res[1] && streq(err[1], "")) {
505 /* Try standalone. */
506 lines[num] = get_ccan_file_lines(i);
507 mangled[num] = mangle_example(m, i, lines[num], keep);
508 res[num] = compile(i, m, mangled[num], keep, &err[num]);
513 static void build_examples(struct manifest *m, bool keep,
514 unsigned int *timeleft, struct score *score)
518 bool warnings = false;
523 list_for_each(&m->examples, i, list) {
525 struct ccan_file *file[3] = { NULL, NULL, NULL };
533 /* Simplify our dumb parsing. */
534 strip_leading_whitespace(get_ccan_file_lines(i));
536 num = try_compiling(m, i, prev, keep, file, res, err, lines);
538 /* First look for a compile without any warnings. */
539 for (j = 0; j < num; j++) {
540 if (res[j] && streq(err[j], "")) {
541 if (!has_expected_output(lines[j]))
548 /* Now accept anything which succeeded. */
549 for (j = 0; j < num; j++) {
551 if (!has_expected_output(lines[j]))
555 score_file_error(score, file[j], 0,
556 "Compiling extracted example"
562 get_ccan_file_contents(file[j]),
571 error = "Compiling standalone, adding headers, "
572 "and including previous "
573 "example all failed";
575 error = "Standalone compile and"
576 " adding headers both failed";
579 error = talloc_asprintf(score,
580 "Standalone example:\n"
583 "Combining with previous example:\n"
586 "Adding headers, wrappers:\n"
589 get_ccan_file_contents(file[0]),
591 get_ccan_file_contents(file[1]),
593 get_ccan_file_contents(file[2]),
596 error = talloc_asprintf(score,
597 "Standalone example:\n"
600 "Adding headers, wrappers:\n"
603 get_ccan_file_contents(file[0]),
605 get_ccan_file_contents(file[1]),
609 score_file_error(score, i, 0, "%s", error);
610 /* This didn't work, so not a candidate for combining. */
617 /* An extra point if they all compiled without warnings. */
618 if (!list_empty(&m->examples)) {
625 struct ccanlint examples_compile = {
626 .key = "examples_compile",
627 .name = "Module examples compile",
628 .check = build_examples,
630 .needs = "examples_exist module_builds"
633 REGISTER_TEST(examples_compile);