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,
124 "", lib_list(m), file->compiled, output)) {
125 /* Don't keep failures. */
127 unlink(file->compiled);
128 talloc_free(file->compiled);
129 file->compiled = NULL;
135 static char *start_main(char *ret, const char *why)
137 return talloc_asprintf_append(ret,
138 "/* The example %s, so fake function wrapper inserted */\n"
139 "int main(int argc, char *argv[])\n"
143 /* We only handle simple function definitions here. */
144 static char *add_func(char *others, const char *line)
146 const char *p, *end = strchr(line, '(') - 1;
147 while (isspace(*end)) {
153 for (p = end; isalnum(*p) || *p == '_'; p--) {
158 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
159 (unsigned)(end - p), p+1);
162 static void strip_leading_whitespace(char **lines)
164 unsigned int i, min_span = -1U;
166 for (i = 0; lines[i]; i++) {
167 unsigned int span = strspn(lines[i], " \t");
168 /* All whitespace? Ignore */
175 for (i = 0; lines[i]; i++)
176 if (strlen(lines[i]) >= min_span)
177 lines[i] += min_span;
180 static bool looks_internal(char **lines, char **why)
183 bool last_ended = true; /* Did last line finish a statement? */
185 for (i = 0; lines[i]; i++) {
186 /* Skip leading whitespace. */
187 const char *line = lines[i] + strspn(lines[i], " \t");
188 unsigned len = strspn(line, IDENT_CHARS);
190 if (!line[0] || isspace(line[0]) || strstarts(line, "//"))
194 if (strstarts(line, "if") && len == 2) {
195 *why = "starts with if";
198 if (strstarts(line, "for") && len == 3) {
199 *why = "starts with for";
202 if (strstarts(line, "while") && len == 5) {
203 *why = "starts with while";
206 if (strstarts(line, "do") && len == 2) {
207 *why = "starts with do";
212 if (strstarts(line, "#include")) {
213 *why = "starts with #include";
217 if (last_ended && strchr(line, '(')) {
218 if (strstarts(line, "static")) {
219 *why = "starts with static and contains (";
222 if (strends(line, ")")) {
223 *why = "contains ( and ends with )";
228 /* Single identifier then operator == inside function. */
229 if (last_ended && len
230 && ispunct(line[len+strspn(line+len, " ")])) {
231 *why = "starts with identifier then punctuation";
235 last_ended = (strends(line, "}")
236 || strends(line, ";")
237 || streq(line, "..."));
240 /* No idea... Say yes? */
241 *why = "gave no clues";
245 /* Examples will often build on prior ones. Try combining them. */
246 static char **combine(const void *ctx, char **lines, char **prev)
248 unsigned int i, lines_total, prev_total, count;
250 const char *reasoning;
256 /* If it looks internal, put prev at start. */
257 if (looks_internal(lines, &why)) {
259 reasoning = "seemed to belong inside a function";
261 /* Try inserting in first elided position */
262 for (count = 0; lines[count]; count++) {
263 if (strcmp(lines[count], "...") == 0)
267 /* Try at start anyway? */
269 reasoning = "didn't seem to belong inside"
270 " a function, so we prepended the previous"
273 reasoning = "didn't seem to belong inside"
274 " a function, so we put the previous example"
281 for (i = 0; lines[i]; i++);
284 for (i = 0; prev[i]; i++);
287 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
288 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
290 memcpy(ret+1, lines, count * sizeof(ret[0]));
291 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
292 memcpy(ret+1 + count + prev_total, lines + count,
293 (lines_total - count + 1) * sizeof(ret[0]));
297 /* Only handles very simple comments. */
298 static char *strip_comment(const void *ctx, const char *orig_line)
300 char *p, *ret = talloc_strdup(ctx, orig_line);
302 p = strstr(ret, "/*");
304 p = strstr(ret, "//");
310 static char *mangle(struct manifest *m, char **lines)
312 char *ret, *use_funcs = NULL, *why;
313 bool in_function = false, fake_function = false, has_main = false;
316 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
317 "#include <assert.h>\n"
319 "#include <errno.h>\n"
320 "#include <fcntl.h>\n"
321 "#include <limits.h>\n"
322 "#include <stdbool.h>\n"
323 "#include <stdint.h>\n"
324 "#include <stdio.h>\n"
325 "#include <stdlib.h>\n"
326 "#include <string.h>\n"
327 "#include <sys/stat.h>\n"
328 "#include <sys/types.h>\n"
329 "#include <unistd.h>\n");
330 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
331 "#include <ccan/%s/%s.h>\n",
332 m->basename, m->basename);
334 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
335 "extern int somefunc(void);\n"
336 "int somefunc(void) { return 0; }\n"
337 "extern char somestring[];\n"
338 "char somestring[] = \"hello world\";\n");
340 if (looks_internal(lines, &why)) {
341 /* Wrap it all in main(). */
342 ret = start_main(ret, why);
343 fake_function = true;
347 ret = talloc_asprintf_append(ret,
348 "/* The example %s, so didn't wrap in main() */\n",
351 /* Primitive, very primitive. */
352 for (i = 0; lines[i]; i++) {
353 char *line = strip_comment(ret, lines[i]);
355 /* } at start of line ends a function. */
360 /* Character at start of line, with ( and no ;
361 * == function start. Ignore comments. */
362 if (!isspace(line[0])
364 && !strchr(line, ';')
365 && !strstr(line, "//")) {
367 if (strncmp(line, "int main", 8) == 0)
369 if (strncmp(line, "static", 6) == 0) {
370 use_funcs = add_func(use_funcs,
375 /* ... means elided code. */
376 if (strcmp(line, "...") == 0) {
377 if (!in_function && !has_main
378 && looks_internal(lines + i + 1, &why)) {
379 /* This implies we start a function here. */
380 ret = start_main(ret, why);
382 fake_function = true;
385 ret = talloc_asprintf_append(ret,
386 "/* ... removed */\n");
389 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
393 ret = talloc_asprintf_append(ret,
394 "/* Need a main to link successfully. */\n"
395 "int main(void)\n{\n");
396 fake_function = true;
400 ret = talloc_asprintf_append(ret,
401 "/* Get rid of unused warnings"
402 " by printing addresses of"
403 " static funcs. */\n");
404 if (!fake_function) {
405 ret = talloc_asprintf_append(ret,
406 "int use_funcs(void);\n"
407 "int use_funcs(void) {\n");
408 fake_function = true;
410 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
414 ret = talloc_asprintf_append(ret, "return 0;\n"
419 static struct ccan_file *mangle_example(struct manifest *m,
420 struct ccan_file *example,
424 char *name, *contents;
428 name = maybe_temp_file(example, ".c", keep,
429 talloc_asprintf(m, "%s/mangled-%s",
430 m->dir, example->name));
431 f = new_ccan_file(example,
432 talloc_dirname(example, name),
433 talloc_basename(example, name));
434 talloc_steal(f, name);
436 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
440 contents = mangle(m, lines);
441 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
446 f->contents = talloc_steal(f, contents);
447 list_add(&m->mangled_examples, &f->list);
451 /* If an example has expected output, it's complete and should not be
452 * included in future examples. */
453 static bool has_expected_output(char **lines)
457 for (i = 0; lines[i]; i++) {
458 char *p = lines[i] + strspn(lines[i], " \t");
459 if (!strstarts(p, "//"))
461 p += strspn(p, "/ ");
462 if (strncasecmp(p, "given", strlen("given")) == 0)
468 static unsigned int try_compiling(struct manifest *m,
472 struct ccan_file *mangled[3],
479 /* Try standalone. */
481 res[0] = compile(i, m, mangled[0], keep, &err[0]);
482 lines[0] = get_ccan_file_lines(i);
483 if (res[0] && streq(err[0], ""))
487 lines[1] = combine(i, get_ccan_file_lines(i), prev);
489 mangled[1] = mangle_example(m, i, lines[1], keep);
490 res[1] = compile(i, m, mangled[1], keep, &err[1]);
491 if (res[1] && streq(err[1], "")) {
498 /* Try standalone. */
499 lines[num] = get_ccan_file_lines(i);
500 mangled[num] = mangle_example(m, i, lines[num], keep);
501 res[num] = compile(i, m, mangled[num], keep, &err[num]);
506 static void build_examples(struct manifest *m, bool keep,
507 unsigned int *timeleft, struct score *score)
511 bool warnings = false;
516 list_for_each(&m->examples, i, list) {
518 struct ccan_file *file[3] = { NULL, NULL, NULL };
526 /* Simplify our dumb parsing. */
527 strip_leading_whitespace(get_ccan_file_lines(i));
529 num = try_compiling(m, i, prev, keep, file, res, err, lines);
531 /* First look for a compile without any warnings. */
532 for (j = 0; j < num; j++) {
533 if (res[j] && streq(err[j], "")) {
534 if (!has_expected_output(lines[j]))
541 /* Now accept anything which succeeded. */
542 for (j = 0; j < num; j++) {
544 if (!has_expected_output(lines[j]))
548 score_file_error(score, file[j], 0,
549 "Compiling extracted example"
555 get_ccan_file_contents(file[j]),
564 error = "Compiling standalone, adding headers, "
565 "and including previous "
566 "example all failed";
568 error = "Standalone compile and"
569 " adding headers both failed";
572 error = talloc_asprintf(score,
573 "Standalone example:\n"
576 "Combining with previous example:\n"
579 "Adding headers, wrappers:\n"
582 get_ccan_file_contents(file[0]),
584 get_ccan_file_contents(file[1]),
586 get_ccan_file_contents(file[2]),
589 error = talloc_asprintf(score,
590 "Standalone example:\n"
593 "Adding headers, wrappers:\n"
596 get_ccan_file_contents(file[0]),
598 get_ccan_file_contents(file[1]),
602 score_file_error(score, i, 0, error);
603 /* This didn't work, so not a candidate for combining. */
610 /* An extra point if they all compiled without warnings. */
611 if (!list_empty(&m->examples)) {
618 struct ccanlint examples_compile = {
619 .key = "examples_compile",
620 .name = "Module examples compile",
621 .check = build_examples,
623 .needs = "examples_exist module_builds"
626 REGISTER_TEST(examples_compile);