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 char *example_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[COMPILE_NORMAL])
100 list = talloc_asprintf_append(list, " %s",
107 /* FIXME: Test with reduced features! */
108 static char *lib_list(const struct manifest *m)
112 char *ret = talloc_strdup(m, "");
114 libs = get_libs(m, m->dir, &num,
115 &m->info_file->compiled[COMPILE_NORMAL]);
116 for (i = 0; i < num; i++)
117 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
121 static bool compile(const void *ctx,
123 struct ccan_file *file,
126 file->compiled[COMPILE_NORMAL] = temp_file(ctx, "", file->fullname);
127 if (!compile_and_link(ctx, file->fullname, ccan_dir,
128 example_obj_list(m, file),
130 lib_list(m), file->compiled[COMPILE_NORMAL],
132 /* Don't keep failures, even with --keep */
133 unlink(file->compiled[COMPILE_NORMAL]);
134 file->compiled[COMPILE_NORMAL] = NULL;
140 static char *start_main(char *ret, const char *why)
142 return talloc_asprintf_append(ret,
143 "/* The example %s, so fake function wrapper inserted */\n"
144 "int main(int argc, char *argv[])\n"
148 /* We only handle simple function definitions here. */
149 static char *add_func(char *others, const char *line)
151 const char *p, *end = strchr(line, '(') - 1;
152 while (cisspace(*end)) {
158 for (p = end; cisalnum(*p) || *p == '_'; p--) {
163 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
164 (unsigned)(end - p), p+1);
167 static void strip_leading_whitespace(char **lines)
169 unsigned int i, min_span = -1U;
171 for (i = 0; lines[i]; i++) {
172 unsigned int span = strspn(lines[i], " \t");
173 /* All whitespace? Ignore */
180 for (i = 0; lines[i]; i++)
181 if (strlen(lines[i]) >= min_span)
182 lines[i] += min_span;
185 static bool looks_internal(char **lines, char **why)
188 bool last_ended = true; /* Did last line finish a statement? */
190 for (i = 0; lines[i]; i++) {
191 /* Skip leading whitespace. */
192 const char *line = lines[i] + strspn(lines[i], " \t");
193 unsigned len = strspn(line, IDENT_CHARS);
195 if (!line[0] || cisspace(line[0]) || strstarts(line, "//"))
198 assert(line[strlen(line)-1] != '\n');
201 if (strstarts(line, "if") && len == 2) {
202 *why = cast_const(char *, "starts with if");
205 if (strstarts(line, "for") && len == 3) {
206 *why = cast_const(char *, "starts with for");
209 if (strstarts(line, "while") && len == 5) {
210 *why = cast_const(char *, "starts with while");
213 if (strstarts(line, "do") && len == 2) {
214 *why = cast_const(char *, "starts with do");
219 if (strstarts(line, "#include")) {
220 *why = cast_const(char *, "starts with #include");
224 if (last_ended && strchr(line, '(')) {
225 if (strstarts(line, "static")) {
226 *why = cast_const(char *,
231 if (strends(line, ")")) {
232 *why = cast_const(char *,
233 "contains ( and ends with )");
238 /* Previously prepended. */
239 if (strstr(line, "didn't seem to belong inside a function")) {
240 *why = cast_const(char *, "Comment said so");
244 /* Single identifier then operator == inside function. */
245 if (last_ended && len
246 && cispunct(line[len+strspn(line+len, " ")])) {
247 *why = cast_const(char *, "starts with identifier"
248 " then punctuation");
252 last_ended = (strends(line, "}")
253 || strends(line, ";")
254 || strends(line, "*/")
255 || streq(line, "..."));
258 /* No idea... Say yes? */
259 *why = cast_const(char *, "gave no clues");
263 /* Examples will often build on prior ones. Try combining them. */
264 static char **combine(const void *ctx, char **lines, char **prev)
266 unsigned int i, lines_total, prev_total, count;
268 const char *reasoning;
274 /* If it looks internal, put prev at start. */
275 if (looks_internal(lines, &why)) {
277 reasoning = "seemed to belong inside a function";
279 /* Try inserting in first elided position */
280 for (count = 0; lines[count]; count++) {
281 if (strcmp(lines[count], "...") == 0)
285 /* Try at start anyway? */
287 reasoning = "didn't seem to belong inside"
288 " a function, so we prepended the previous"
291 reasoning = "didn't seem to belong inside"
292 " a function, so we put the previous example"
299 for (i = 0; lines[i]; i++);
302 for (i = 0; prev[i]; i++);
305 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
306 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */",
308 memcpy(ret+1, lines, count * sizeof(ret[0]));
309 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
310 memcpy(ret+1 + count + prev_total, lines + count,
311 (lines_total - count + 1) * sizeof(ret[0]));
315 /* Only handles very simple comments. */
316 static char *strip_comment(const void *ctx, const char *orig_line)
318 char *p, *ret = talloc_strdup(ctx, orig_line);
320 p = strstr(ret, "/*");
322 p = strstr(ret, "//");
328 static char *mangle(struct manifest *m, char **lines)
330 char *ret, *use_funcs = NULL, *why;
331 bool in_function = false, fake_function = false, has_main = false;
334 ret = talloc_asprintf(m,
335 "/* Include header from module. */\n"
336 "#include <ccan/%s/%s.h>\n"
337 "/* Prepend a heap of headers. */\n"
338 "#include <assert.h>\n"
340 "#include <errno.h>\n"
341 "#include <fcntl.h>\n"
342 "#include <limits.h>\n"
343 "#include <stdbool.h>\n"
344 "#include <stdint.h>\n"
345 "#include <stdio.h>\n"
346 "#include <stdlib.h>\n"
347 "#include <string.h>\n"
348 "#include <sys/stat.h>\n"
349 "#include <sys/types.h>\n"
350 "#include <unistd.h>\n",
351 m->basename, m->basename);
353 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
354 "extern int somefunc(void);\n"
355 "int somefunc(void) { return 0; }\n"
356 "extern char somestring[];\n"
357 "char somestring[] = \"hello world\";\n");
359 if (looks_internal(lines, &why)) {
360 /* Wrap it all in main(). */
361 ret = start_main(ret, why);
362 fake_function = true;
366 ret = talloc_asprintf_append(ret,
367 "/* The example %s, so didn't wrap in main() */\n",
370 /* Primitive, very primitive. */
371 for (i = 0; lines[i]; i++) {
372 char *line = strip_comment(ret, lines[i]);
374 /* } at start of line ends a function. */
379 /* Character at start of line, with ( and no ;
380 * == function start. Ignore comments. */
381 if (!cisspace(line[0])
383 && !strchr(line, ';')
384 && !strstr(line, "//")) {
386 if (strncmp(line, "int main", 8) == 0)
388 if (strncmp(line, "static", 6) == 0) {
389 use_funcs = add_func(use_funcs,
394 /* ... means elided code. */
395 if (strcmp(line, "...") == 0) {
396 if (!in_function && !has_main
397 && looks_internal(lines + i + 1, &why)) {
398 /* This implies we start a function here. */
399 ret = start_main(ret, why);
401 fake_function = true;
404 ret = talloc_asprintf_append(ret,
405 "/* ... removed */\n");
408 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
412 ret = talloc_asprintf_append(ret,
413 "/* Need a main to link successfully. */\n"
414 "int main(void)\n{\n");
415 fake_function = true;
419 ret = talloc_asprintf_append(ret,
420 "/* Get rid of unused warnings"
421 " by printing addresses of"
422 " static funcs. */\n");
423 if (!fake_function) {
424 ret = talloc_asprintf_append(ret,
425 "int use_funcs(void);\n"
426 "int use_funcs(void) {\n");
427 fake_function = true;
429 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
433 ret = talloc_asprintf_append(ret, "return 0;\n"
438 static struct ccan_file *mangle_example(struct manifest *m,
439 struct ccan_file *example,
442 char *name, *contents;
446 name = temp_file(example, ".c",
447 talloc_asprintf(m, "%s/mangled-%s",
448 m->dir, example->name));
449 f = new_ccan_file(example,
450 talloc_dirname(example, name),
451 talloc_basename(example, name));
452 talloc_steal(f, name);
454 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
458 contents = mangle(m, lines);
459 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
464 f->contents = talloc_steal(f, contents);
465 list_add(&m->mangled_examples, &f->list);
469 /* If an example has expected output, it's complete and should not be
470 * included in future examples. */
471 static bool has_expected_output(char **lines)
475 for (i = 0; lines[i]; i++) {
476 char *p = lines[i] + strspn(lines[i], " \t");
477 if (!strstarts(p, "//"))
479 p += strspn(p, "/ ");
480 if (strncasecmp(p, "given", strlen("given")) == 0)
486 static unsigned int try_compiling(struct manifest *m,
489 struct ccan_file *mangled[3],
496 /* Try standalone. */
498 res[0] = compile(i, m, mangled[0], &err[0]);
499 lines[0] = get_ccan_file_lines(i);
500 if (res[0] && streq(err[0], ""))
504 lines[1] = combine(i, get_ccan_file_lines(i), prev);
506 mangled[1] = mangle_example(m, i, lines[1]);
507 res[1] = compile(i, m, mangled[1], &err[1]);
508 if (res[1] && streq(err[1], "")) {
515 /* Try standalone. */
516 lines[num] = get_ccan_file_lines(i);
517 mangled[num] = mangle_example(m, i, lines[num]);
518 res[num] = compile(i, m, mangled[num], &err[num]);
523 static void build_examples(struct manifest *m,
524 unsigned int *timeleft, struct score *score)
528 bool warnings = false;
533 list_for_each(&m->examples, i, list) {
535 struct ccan_file *file[3] = { NULL, NULL, NULL };
543 /* Simplify our dumb parsing. */
544 strip_leading_whitespace(get_ccan_file_lines(i));
546 num = try_compiling(m, i, prev, file, res, err, lines);
548 /* First look for a compile without any warnings. */
549 for (j = 0; j < num; j++) {
550 if (res[j] && streq(err[j], "")) {
551 if (!has_expected_output(lines[j]))
558 /* Now accept anything which succeeded. */
559 for (j = 0; j < num; j++) {
561 if (!has_expected_output(lines[j]))
565 score_file_error(score, file[j], 0,
566 "Compiling extracted example"
572 get_ccan_file_contents(file[j]),
581 error = "Compiling standalone, adding headers, "
582 "and including previous "
583 "example all failed";
585 error = "Standalone compile and"
586 " adding headers both failed";
589 error = talloc_asprintf(score,
590 "Standalone example:\n"
593 "Combining with previous example:\n"
596 "Adding headers, wrappers:\n"
599 get_ccan_file_contents(file[0]),
601 get_ccan_file_contents(file[1]),
603 get_ccan_file_contents(file[2]),
606 error = talloc_asprintf(score,
607 "Standalone example:\n"
610 "Adding headers, wrappers:\n"
613 get_ccan_file_contents(file[0]),
615 get_ccan_file_contents(file[1]),
619 score_file_error(score, i, 0, "%s", error);
620 /* This didn't work, so not a candidate for combining. */
627 /* An extra point if they all compiled without warnings. */
628 if (!list_empty(&m->examples)) {
635 struct ccanlint examples_compile = {
636 .key = "examples_compile",
637 .name = "Module examples compile",
638 .check = build_examples,
640 .needs = "examples_exist module_builds"
643 REGISTER_TEST(examples_compile);