1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
14 #include "../compulsory_tests/build.h"
16 static const char *can_run(struct manifest *m)
19 return "Safe mode enabled";
20 if (list_empty(&m->examples))
21 return "No examples to compile";
25 static void add_mod(struct manifest ***deps, struct manifest *m)
27 unsigned int num = talloc_get_size(*deps) / sizeof(*deps);
28 *deps = talloc_realloc(NULL, *deps, struct manifest *, num + 1);
32 static bool have_mod(struct manifest *deps[], const char *basename)
36 for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++)
37 if (strcmp(deps[i]->basename, basename) == 0)
42 static void add_dep(struct manifest ***deps, const char *basename)
48 if (have_mod(*deps, basename))
51 m = get_manifest(*deps, talloc_asprintf(*deps, "%s/ccan/%s",
53 errstr = build_submodule(m);
55 errx(1, "%s", errstr);
59 /* Get that modules depends as well... */
64 infodeps = get_deps(m, m->dir, false, &m->info_file->compiled);
66 for (i = 0; infodeps[i]; i++) {
67 if (strstarts(infodeps[i], "ccan/"))
68 add_dep(deps, infodeps[i] + strlen("ccan/"));
73 static char *obj_list(struct manifest *m, struct ccan_file *f)
75 struct manifest **deps = talloc_array(f, struct manifest *, 0);
79 /* This one for a start. */
80 add_dep(&deps, m->basename);
82 /* Other modules implied by includes. */
83 for (lines = get_ccan_file_lines(f); *lines; lines++) {
84 unsigned preflen = strspn(*lines, " \t");
85 if (strstarts(*lines + preflen, "#include <ccan/")) {
88 modname = talloc_strdup(f, *lines + preflen
89 + strlen("#include <ccan/"));
90 modname[strcspn(modname, "/")] = '\0';
91 if (!have_mod(deps, modname))
92 add_dep(&deps, modname);
96 list = talloc_strdup(f, "");
97 for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++) {
98 if (deps[i]->compiled)
99 list = talloc_asprintf_append(list, " %s",
105 static char *lib_list(const struct manifest *m)
108 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
109 char *ret = talloc_strdup(m, "");
111 for (i = 0; i < num; i++)
112 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
116 static bool compile(const void *ctx,
118 struct ccan_file *file,
119 bool keep, char **output)
121 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
122 if (!compile_and_link(ctx, file->fullname, ccan_dir,
125 lib_list(m), file->compiled, output)) {
126 /* Don't keep failures. */
128 unlink(file->compiled);
129 talloc_free(file->compiled);
130 file->compiled = NULL;
136 static char *start_main(char *ret, const char *why)
138 return talloc_asprintf_append(ret,
139 "/* The example %s, so fake function wrapper inserted */\n"
140 "int main(int argc, char *argv[])\n"
144 /* We only handle simple function definitions here. */
145 static char *add_func(char *others, const char *line)
147 const char *p, *end = strchr(line, '(') - 1;
148 while (isspace(*end)) {
154 for (p = end; isalnum(*p) || *p == '_'; p--) {
159 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
160 (unsigned)(end - p), p+1);
163 static void strip_leading_whitespace(char **lines)
165 unsigned int i, min_span = -1U;
167 for (i = 0; lines[i]; i++) {
168 unsigned int span = strspn(lines[i], " \t");
169 /* All whitespace? Ignore */
176 for (i = 0; lines[i]; i++)
177 if (strlen(lines[i]) >= min_span)
178 lines[i] += min_span;
181 static bool looks_internal(char **lines, char **why)
184 bool last_ended = true; /* Did last line finish a statement? */
186 for (i = 0; lines[i]; i++) {
187 /* Skip leading whitespace. */
188 const char *line = lines[i] + strspn(lines[i], " \t");
189 unsigned len = strspn(line, IDENT_CHARS);
191 if (!line[0] || isspace(line[0]) || strstarts(line, "//"))
195 if (strstarts(line, "if") && len == 2) {
196 *why = "starts with if";
199 if (strstarts(line, "for") && len == 3) {
200 *why = "starts with for";
203 if (strstarts(line, "while") && len == 5) {
204 *why = "starts with while";
207 if (strstarts(line, "do") && len == 2) {
208 *why = "starts with do";
213 if (strstarts(line, "#include")) {
214 *why = "starts with #include";
218 if (last_ended && strchr(line, '(')) {
219 if (strstarts(line, "static")) {
220 *why = "starts with static and contains (";
223 if (strends(line, ")")) {
224 *why = "contains ( and ends with )";
229 /* Single identifier then operator == inside function. */
230 if (last_ended && len
231 && ispunct(line[len+strspn(line+len, " ")])) {
232 *why = "starts with identifier then punctuation";
236 last_ended = (strends(line, "}")
237 || strends(line, ";")
238 || streq(line, "..."));
241 /* No idea... Say yes? */
242 *why = "gave no clues";
246 /* Examples will often build on prior ones. Try combining them. */
247 static char **combine(const void *ctx, char **lines, char **prev)
249 unsigned int i, lines_total, prev_total, count;
251 const char *reasoning;
257 /* If it looks internal, put prev at start. */
258 if (looks_internal(lines, &why)) {
260 reasoning = "seemed to belong inside a function";
262 /* Try inserting in first elided position */
263 for (count = 0; lines[count]; count++) {
264 if (strcmp(lines[count], "...") == 0)
268 /* Try at start anyway? */
270 reasoning = "didn't seem to belong inside"
271 " a function, so we prepended the previous"
274 reasoning = "didn't seem to belong inside"
275 " a function, so we put the previous example"
282 for (i = 0; lines[i]; i++);
285 for (i = 0; prev[i]; i++);
288 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
289 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
291 memcpy(ret+1, lines, count * sizeof(ret[0]));
292 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
293 memcpy(ret+1 + count + prev_total, lines + count,
294 (lines_total - count + 1) * sizeof(ret[0]));
298 /* Only handles very simple comments. */
299 static char *strip_comment(const void *ctx, const char *orig_line)
301 char *p, *ret = talloc_strdup(ctx, orig_line);
303 p = strstr(ret, "/*");
305 p = strstr(ret, "//");
311 static char *mangle(struct manifest *m, char **lines)
313 char *ret, *use_funcs = NULL, *why;
314 bool in_function = false, fake_function = false, has_main = false;
317 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
318 "#include <assert.h>\n"
320 "#include <errno.h>\n"
321 "#include <fcntl.h>\n"
322 "#include <limits.h>\n"
323 "#include <stdbool.h>\n"
324 "#include <stdint.h>\n"
325 "#include <stdio.h>\n"
326 "#include <stdlib.h>\n"
327 "#include <string.h>\n"
328 "#include <sys/stat.h>\n"
329 "#include <sys/types.h>\n"
330 "#include <unistd.h>\n");
331 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
332 "#include <ccan/%s/%s.h>\n",
333 m->basename, m->basename);
335 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
336 "extern int somefunc(void);\n"
337 "int somefunc(void) { return 0; }\n"
338 "extern char somestring[];\n"
339 "char somestring[] = \"hello world\";\n");
341 if (looks_internal(lines, &why)) {
342 /* Wrap it all in main(). */
343 ret = start_main(ret, why);
344 fake_function = true;
348 ret = talloc_asprintf_append(ret,
349 "/* The example %s, so didn't wrap in main() */\n",
352 /* Primitive, very primitive. */
353 for (i = 0; lines[i]; i++) {
354 char *line = strip_comment(ret, lines[i]);
356 /* } at start of line ends a function. */
361 /* Character at start of line, with ( and no ;
362 * == function start. Ignore comments. */
363 if (!isspace(line[0])
365 && !strchr(line, ';')
366 && !strstr(line, "//")) {
368 if (strncmp(line, "int main", 8) == 0)
370 if (strncmp(line, "static", 6) == 0) {
371 use_funcs = add_func(use_funcs,
376 /* ... means elided code. */
377 if (strcmp(line, "...") == 0) {
378 if (!in_function && !has_main
379 && looks_internal(lines + i + 1, &why)) {
380 /* This implies we start a function here. */
381 ret = start_main(ret, why);
383 fake_function = true;
386 ret = talloc_asprintf_append(ret,
387 "/* ... removed */\n");
390 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
394 ret = talloc_asprintf_append(ret,
395 "/* Need a main to link successfully. */\n"
396 "int main(void)\n{\n");
397 fake_function = true;
401 ret = talloc_asprintf_append(ret,
402 "/* Get rid of unused warnings"
403 " by printing addresses of"
404 " static funcs. */\n");
405 if (!fake_function) {
406 ret = talloc_asprintf_append(ret,
407 "int use_funcs(void);\n"
408 "int use_funcs(void) {\n");
409 fake_function = true;
411 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
415 ret = talloc_asprintf_append(ret, "return 0;\n"
420 static struct ccan_file *mangle_example(struct manifest *m,
421 struct ccan_file *example,
425 char *name, *contents;
429 name = maybe_temp_file(example, ".c", keep,
430 talloc_asprintf(m, "%s/mangled-%s",
431 m->dir, example->name));
432 f = new_ccan_file(example,
433 talloc_dirname(example, name),
434 talloc_basename(example, name));
435 talloc_steal(f, name);
437 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
441 contents = mangle(m, lines);
442 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
447 f->contents = talloc_steal(f, contents);
448 list_add(&m->mangled_examples, &f->list);
452 /* If an example has expected output, it's complete and should not be
453 * included in future examples. */
454 static bool has_expected_output(char **lines)
458 for (i = 0; lines[i]; i++) {
459 char *p = lines[i] + strspn(lines[i], " \t");
460 if (!strstarts(p, "//"))
462 p += strspn(p, "/ ");
463 if (strncasecmp(p, "given", strlen("given")) == 0)
469 static unsigned int try_compiling(struct manifest *m,
473 struct ccan_file *mangled[3],
480 /* Try standalone. */
482 res[0] = compile(i, m, mangled[0], keep, &err[0]);
483 lines[0] = get_ccan_file_lines(i);
484 if (res[0] && streq(err[0], ""))
488 lines[1] = combine(i, get_ccan_file_lines(i), prev);
490 mangled[1] = mangle_example(m, i, lines[1], keep);
491 res[1] = compile(i, m, mangled[1], keep, &err[1]);
492 if (res[1] && streq(err[1], "")) {
499 /* Try standalone. */
500 lines[num] = get_ccan_file_lines(i);
501 mangled[num] = mangle_example(m, i, lines[num], keep);
502 res[num] = compile(i, m, mangled[num], keep, &err[num]);
507 static void build_examples(struct manifest *m, bool keep,
508 unsigned int *timeleft, struct score *score)
512 bool warnings = false;
517 list_for_each(&m->examples, i, list) {
519 struct ccan_file *file[3] = { NULL, NULL, NULL };
527 /* Simplify our dumb parsing. */
528 strip_leading_whitespace(get_ccan_file_lines(i));
530 num = try_compiling(m, i, prev, keep, file, res, err, lines);
532 /* First look for a compile without any warnings. */
533 for (j = 0; j < num; j++) {
534 if (res[j] && streq(err[j], "")) {
535 if (!has_expected_output(lines[j]))
542 /* Now accept anything which succeeded. */
543 for (j = 0; j < num; j++) {
545 if (!has_expected_output(lines[j]))
549 score_file_error(score, file[j], 0,
550 "Compiling extracted example"
556 get_ccan_file_contents(file[j]),
565 error = "Compiling standalone, adding headers, "
566 "and including previous "
567 "example all failed";
569 error = "Standalone compile and"
570 " adding headers both failed";
573 error = talloc_asprintf(score,
574 "Standalone example:\n"
577 "Combining with previous example:\n"
580 "Adding headers, wrappers:\n"
583 get_ccan_file_contents(file[0]),
585 get_ccan_file_contents(file[1]),
587 get_ccan_file_contents(file[2]),
590 error = talloc_asprintf(score,
591 "Standalone example:\n"
594 "Adding headers, wrappers:\n"
597 get_ccan_file_contents(file[0]),
599 get_ccan_file_contents(file[1]),
603 score_file_error(score, i, 0, "%s", error);
604 /* This didn't work, so not a candidate for combining. */
611 /* An extra point if they all compiled without warnings. */
612 if (!list_empty(&m->examples)) {
619 struct ccanlint examples_compile = {
620 .key = "examples_compile",
621 .name = "Module examples compile",
622 .check = build_examples,
624 .needs = "examples_exist module_builds"
627 REGISTER_TEST(examples_compile);