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 struct manifest **get_example_deps(struct manifest *m,
78 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);
100 static char *example_obj_list(const void *ctx, struct manifest **deps)
102 char *list = talloc_strdup(ctx, "");
105 for (i = 0; i < talloc_array_length(deps); i++) {
106 if (deps[i]->compiled[COMPILE_NORMAL])
107 list = talloc_asprintf_append(list, " %s",
114 static char *example_lib_list(const void *ctx, struct manifest **deps)
116 char *list = talloc_strdup(ctx, "");
120 /* FIXME: This doesn't uniquify. */
121 for (i = 0; i < talloc_array_length(deps); i++) {
122 libs = get_libs(ctx, deps[i]->dir, false, get_or_compile_info);
123 for (j = 0; libs[j]; j++)
124 list = talloc_asprintf_append(list, "-l%s ", libs[j]);
129 /* FIXME: Test with reduced features! */
130 static bool compile(const void *ctx,
132 struct ccan_file *file,
135 struct manifest **deps = get_example_deps(m, file);
137 file->compiled[COMPILE_NORMAL] = temp_file(ctx, "", file->fullname);
138 if (!compile_and_link(ctx, file->fullname, ccan_dir,
139 example_obj_list(file, deps),
141 example_lib_list(file, deps),
142 file->compiled[COMPILE_NORMAL],
144 /* Don't keep failures, even with --keep */
145 unlink(file->compiled[COMPILE_NORMAL]);
146 file->compiled[COMPILE_NORMAL] = NULL;
152 static char *start_main(char *ret, const char *why)
154 return talloc_asprintf_append(ret,
155 "/* The example %s, so fake function wrapper inserted */\n"
156 "int main(int argc, char *argv[])\n"
160 /* We only handle simple function definitions here. */
161 static char *add_func(char *others, const char *line)
163 const char *p, *end = strchr(line, '(') - 1;
164 while (cisspace(*end)) {
170 for (p = end; cisalnum(*p) || *p == '_'; p--) {
175 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
176 (unsigned)(end - p), p+1);
179 static void strip_leading_whitespace(char **lines)
181 unsigned int i, min_span = -1U;
183 for (i = 0; lines[i]; i++) {
184 unsigned int span = strspn(lines[i], " \t");
185 /* All whitespace? Ignore */
192 for (i = 0; lines[i]; i++)
193 if (strlen(lines[i]) >= min_span)
194 lines[i] += min_span;
197 static bool looks_internal(char **lines, char **why)
200 bool last_ended = true; /* Did last line finish a statement? */
202 for (i = 0; lines[i]; i++) {
203 /* Skip leading whitespace. */
204 const char *line = lines[i] + strspn(lines[i], " \t");
205 unsigned len = strspn(line, IDENT_CHARS);
207 if (!line[0] || cisspace(line[0]) || strstarts(line, "//")
208 || strstarts(line, "#line"))
211 assert(line[strlen(line)-1] != '\n');
214 if (strstarts(line, "if") && len == 2) {
215 *why = cast_const(char *, "starts with if");
218 if (strstarts(line, "for") && len == 3) {
219 *why = cast_const(char *, "starts with for");
222 if (strstarts(line, "while") && len == 5) {
223 *why = cast_const(char *, "starts with while");
226 if (strstarts(line, "do") && len == 2) {
227 *why = cast_const(char *, "starts with do");
232 if (strstarts(line, "#include")) {
233 *why = cast_const(char *, "starts with #include");
237 if (last_ended && strchr(line, '(')) {
238 if (strstarts(line, "static")) {
239 *why = cast_const(char *,
244 if (strends(line, ")")) {
245 *why = cast_const(char *,
246 "contains ( and ends with )");
251 /* Previously prepended. */
252 if (strstr(line, "didn't seem to belong inside a function")) {
253 *why = cast_const(char *, "Comment said so");
257 /* Single identifier then operator == inside function. */
258 if (last_ended && len
259 && cispunct(line[len+strspn(line+len, " ")])) {
260 *why = cast_const(char *, "starts with identifier"
261 " then punctuation");
265 last_ended = (strends(line, "}")
266 || strends(line, ";")
267 || strends(line, "*/")
268 || streq(line, "..."));
271 /* No idea... Say yes? */
272 *why = cast_const(char *, "gave no clues");
276 /* Examples will often build on prior ones. Try combining them. */
277 static char **combine(const void *ctx, char **lines, char **prev)
279 unsigned int i, lines_total, prev_total, count;
281 const char *reasoning;
287 /* If it looks internal, put prev at start. */
288 if (looks_internal(lines, &why)) {
290 reasoning = "seemed to belong inside a function";
292 /* Try inserting in first elided position */
293 for (count = 0; lines[count]; count++) {
294 if (strcmp(lines[count], "...") == 0)
298 /* Try at start anyway? */
300 reasoning = "didn't seem to belong inside"
301 " a function, so we prepended the previous"
304 reasoning = "didn't seem to belong inside"
305 " a function, so we put the previous example"
312 for (i = 0; lines[i]; i++);
315 for (i = 0; prev[i]; i++);
318 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
319 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */",
321 memcpy(ret+1, lines, count * sizeof(ret[0]));
322 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
323 memcpy(ret+1 + count + prev_total, lines + count,
324 (lines_total - count + 1) * sizeof(ret[0]));
328 /* Only handles very simple comments. */
329 static char *strip_comment(const void *ctx, const char *orig_line)
331 char *p, *ret = talloc_strdup(ctx, orig_line);
333 p = strstr(ret, "/*");
335 p = strstr(ret, "//");
341 static char *mangle(struct manifest *m, char **lines)
343 char *ret, *use_funcs = NULL, *why;
344 bool in_function = false, fake_function = false, has_main = false;
347 ret = talloc_asprintf(m,
348 "/* Include header from module. */\n"
349 "#include <ccan/%s/%s.h>\n"
350 "/* Prepend a heap of headers. */\n"
351 "#include <assert.h>\n"
353 "#include <errno.h>\n"
354 "#include <fcntl.h>\n"
355 "#include <limits.h>\n"
356 "#include <stdbool.h>\n"
357 "#include <stdint.h>\n"
358 "#include <stdio.h>\n"
359 "#include <stdlib.h>\n"
360 "#include <string.h>\n"
361 "#include <sys/stat.h>\n"
362 "#include <sys/types.h>\n"
363 "#include <unistd.h>\n",
364 m->basename, m->basename);
366 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
367 "extern int somefunc(void);\n"
368 "int somefunc(void) { return 0; }\n"
369 "extern char somestring[];\n"
370 "char somestring[] = \"hello world\";\n");
372 if (looks_internal(lines, &why)) {
373 /* Wrap it all in main(). */
374 ret = start_main(ret, why);
375 fake_function = true;
379 ret = talloc_asprintf_append(ret,
380 "/* The example %s, so didn't wrap in main() */\n",
383 /* Primitive, very primitive. */
384 for (i = 0; lines[i]; i++) {
385 char *line = strip_comment(ret, lines[i]);
387 /* } at start of line ends a function. */
392 /* Character at start of line, with ( and no ;
393 * == function start. Ignore comments. */
394 if (!cisspace(line[0])
396 && !strchr(line, ';')
397 && !strstr(line, "//")) {
399 if (strncmp(line, "int main", 8) == 0)
401 if (strncmp(line, "static", 6) == 0) {
402 use_funcs = add_func(use_funcs,
407 /* ... means elided code. */
408 if (strcmp(line, "...") == 0) {
409 if (!in_function && !has_main
410 && looks_internal(lines + i + 1, &why)) {
411 /* This implies we start a function here. */
412 ret = start_main(ret, why);
414 fake_function = true;
417 ret = talloc_asprintf_append(ret,
418 "/* ... removed */\n");
421 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
425 ret = talloc_asprintf_append(ret,
426 "/* Need a main to link successfully. */\n"
427 "int main(void)\n{\n");
428 fake_function = true;
432 ret = talloc_asprintf_append(ret,
433 "/* Get rid of unused warnings"
434 " by printing addresses of"
435 " static funcs. */\n");
436 if (!fake_function) {
437 ret = talloc_asprintf_append(ret,
438 "int use_funcs(void);\n"
439 "int use_funcs(void) {\n");
440 fake_function = true;
442 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
446 ret = talloc_asprintf_append(ret, "return 0;\n"
451 static struct ccan_file *mangle_example(struct manifest *m,
452 struct ccan_file *example,
455 char *name, *contents;
459 name = temp_file(example, ".c",
460 talloc_asprintf(m, "%s/mangled-%s",
461 m->dir, example->name));
462 f = new_ccan_file(example,
463 talloc_dirname(example, name),
464 talloc_basename(example, name));
465 talloc_steal(f, name);
467 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
471 contents = mangle(m, lines);
472 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
477 f->contents = talloc_steal(f, contents);
478 list_add(&m->mangled_examples, &f->list);
482 /* If an example has expected output, it's complete and should not be
483 * included in future examples. */
484 static bool has_expected_output(char **lines)
488 for (i = 0; lines[i]; i++) {
489 char *p = lines[i] + strspn(lines[i], " \t");
490 if (!strstarts(p, "//"))
492 p += strspn(p, "/ ");
493 if (strncasecmp(p, "given", strlen("given")) == 0)
499 static unsigned int try_compiling(struct manifest *m,
502 struct ccan_file *mangled[3],
509 /* Try standalone. */
511 res[0] = compile(i, m, mangled[0], &err[0]);
512 lines[0] = get_ccan_file_lines(i);
513 if (res[0] && streq(err[0], ""))
517 lines[1] = combine(i, get_ccan_file_lines(i), prev);
519 mangled[1] = mangle_example(m, i, lines[1]);
520 res[1] = compile(i, m, mangled[1], &err[1]);
521 if (res[1] && streq(err[1], "")) {
528 /* Try standalone. */
529 lines[num] = get_ccan_file_lines(i);
530 mangled[num] = mangle_example(m, i, lines[num]);
531 res[num] = compile(i, m, mangled[num], &err[num]);
536 static void build_examples(struct manifest *m,
537 unsigned int *timeleft, struct score *score)
541 bool warnings = false;
546 list_for_each(&m->examples, i, list) {
548 struct ccan_file *file[3] = { NULL, NULL, NULL };
556 /* Simplify our dumb parsing. */
557 strip_leading_whitespace(get_ccan_file_lines(i));
559 num = try_compiling(m, i, prev, file, res, err, lines);
561 /* First look for a compile without any warnings. */
562 for (j = 0; j < num; j++) {
563 if (res[j] && streq(err[j], "")) {
564 if (!has_expected_output(lines[j]))
571 /* Now accept anything which succeeded. */
572 for (j = 0; j < num; j++) {
574 if (!has_expected_output(lines[j]))
578 score_file_error(score, file[j], 0,
579 "Compiling extracted example"
585 get_ccan_file_contents(file[j]),
594 error = "Compiling standalone, adding headers, "
595 "and including previous "
596 "example all failed";
598 error = "Standalone compile and"
599 " adding headers both failed";
602 error = talloc_asprintf(score,
603 "Standalone example:\n"
606 "Combining with previous example:\n"
609 "Adding headers, wrappers:\n"
612 get_ccan_file_contents(file[0]),
614 get_ccan_file_contents(file[1]),
616 get_ccan_file_contents(file[2]),
619 error = talloc_asprintf(score,
620 "Standalone example:\n"
623 "Adding headers, wrappers:\n"
626 get_ccan_file_contents(file[0]),
628 get_ccan_file_contents(file[1]),
632 score_file_error(score, i, 0, "%s", error);
633 /* This didn't work, so not a candidate for combining. */
640 /* An extra point if they all compiled without warnings. */
641 if (!list_empty(&m->examples)) {
648 struct ccanlint examples_compile = {
649 .key = "examples_compile",
650 .name = "Module examples compile",
651 .check = build_examples,
653 .needs = "examples_exist module_builds"
656 REGISTER_TEST(examples_compile);