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,
66 &m->info_file->compiled[COMPILE_NORMAL]);
68 for (i = 0; infodeps[i]; i++) {
69 if (strstarts(infodeps[i], "ccan/"))
70 add_dep(deps, infodeps[i] + strlen("ccan/"));
75 static char *example_obj_list(struct manifest *m, struct ccan_file *f)
77 struct manifest **deps = talloc_array(f, struct manifest *, 0);
81 /* This one for a start. */
82 add_dep(&deps, m->basename);
84 /* Other modules implied by includes. */
85 for (lines = get_ccan_file_lines(f); *lines; lines++) {
86 unsigned preflen = strspn(*lines, " \t");
87 if (strstarts(*lines + preflen, "#include <ccan/")) {
90 modname = talloc_strdup(f, *lines + preflen
91 + strlen("#include <ccan/"));
92 modname[strcspn(modname, "/")] = '\0';
93 if (!have_mod(deps, modname))
94 add_dep(&deps, modname);
98 list = talloc_strdup(f, "");
99 for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++) {
100 if (deps[i]->compiled[COMPILE_NORMAL])
101 list = talloc_asprintf_append(list, " %s",
108 /* FIXME: Test with reduced features! */
109 static char *lib_list(const struct manifest *m)
113 char *ret = talloc_strdup(m, "");
115 libs = get_libs(m, m->dir, &num,
116 &m->info_file->compiled[COMPILE_NORMAL]);
117 for (i = 0; i < num; i++)
118 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
122 static bool compile(const void *ctx,
124 struct ccan_file *file,
127 file->compiled[COMPILE_NORMAL] = temp_file(ctx, "", file->fullname);
128 if (!compile_and_link(ctx, file->fullname, ccan_dir,
129 example_obj_list(m, file),
131 lib_list(m), file->compiled[COMPILE_NORMAL],
133 /* Don't keep failures, even with --keep */
134 unlink(file->compiled[COMPILE_NORMAL]);
135 file->compiled[COMPILE_NORMAL] = NULL;
141 static char *start_main(char *ret, const char *why)
143 return talloc_asprintf_append(ret,
144 "/* The example %s, so fake function wrapper inserted */\n"
145 "int main(int argc, char *argv[])\n"
149 /* We only handle simple function definitions here. */
150 static char *add_func(char *others, const char *line)
152 const char *p, *end = strchr(line, '(') - 1;
153 while (cisspace(*end)) {
159 for (p = end; cisalnum(*p) || *p == '_'; p--) {
164 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
165 (unsigned)(end - p), p+1);
168 static void strip_leading_whitespace(char **lines)
170 unsigned int i, min_span = -1U;
172 for (i = 0; lines[i]; i++) {
173 unsigned int span = strspn(lines[i], " \t");
174 /* All whitespace? Ignore */
181 for (i = 0; lines[i]; i++)
182 if (strlen(lines[i]) >= min_span)
183 lines[i] += min_span;
186 static bool looks_internal(char **lines, char **why)
189 bool last_ended = true; /* Did last line finish a statement? */
191 for (i = 0; lines[i]; i++) {
192 /* Skip leading whitespace. */
193 const char *line = lines[i] + strspn(lines[i], " \t");
194 unsigned len = strspn(line, IDENT_CHARS);
196 if (!line[0] || cisspace(line[0]) || strstarts(line, "//"))
199 assert(line[strlen(line)-1] != '\n');
202 if (strstarts(line, "if") && len == 2) {
203 *why = cast_const(char *, "starts with if");
206 if (strstarts(line, "for") && len == 3) {
207 *why = cast_const(char *, "starts with for");
210 if (strstarts(line, "while") && len == 5) {
211 *why = cast_const(char *, "starts with while");
214 if (strstarts(line, "do") && len == 2) {
215 *why = cast_const(char *, "starts with do");
220 if (strstarts(line, "#include")) {
221 *why = cast_const(char *, "starts with #include");
225 if (last_ended && strchr(line, '(')) {
226 if (strstarts(line, "static")) {
227 *why = cast_const(char *,
232 if (strends(line, ")")) {
233 *why = cast_const(char *,
234 "contains ( and ends with )");
239 /* Previously prepended. */
240 if (strstr(line, "didn't seem to belong inside a function")) {
241 *why = cast_const(char *, "Comment said so");
245 /* Single identifier then operator == inside function. */
246 if (last_ended && len
247 && cispunct(line[len+strspn(line+len, " ")])) {
248 *why = cast_const(char *, "starts with identifier"
249 " then punctuation");
253 last_ended = (strends(line, "}")
254 || strends(line, ";")
255 || strends(line, "*/")
256 || streq(line, "..."));
259 /* No idea... Say yes? */
260 *why = cast_const(char *, "gave no clues");
264 /* Examples will often build on prior ones. Try combining them. */
265 static char **combine(const void *ctx, char **lines, char **prev)
267 unsigned int i, lines_total, prev_total, count;
269 const char *reasoning;
275 /* If it looks internal, put prev at start. */
276 if (looks_internal(lines, &why)) {
278 reasoning = "seemed to belong inside a function";
280 /* Try inserting in first elided position */
281 for (count = 0; lines[count]; count++) {
282 if (strcmp(lines[count], "...") == 0)
286 /* Try at start anyway? */
288 reasoning = "didn't seem to belong inside"
289 " a function, so we prepended the previous"
292 reasoning = "didn't seem to belong inside"
293 " a function, so we put the previous example"
300 for (i = 0; lines[i]; i++);
303 for (i = 0; prev[i]; i++);
306 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
307 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */",
309 memcpy(ret+1, lines, count * sizeof(ret[0]));
310 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
311 memcpy(ret+1 + count + prev_total, lines + count,
312 (lines_total - count + 1) * sizeof(ret[0]));
316 /* Only handles very simple comments. */
317 static char *strip_comment(const void *ctx, const char *orig_line)
319 char *p, *ret = talloc_strdup(ctx, orig_line);
321 p = strstr(ret, "/*");
323 p = strstr(ret, "//");
329 static char *mangle(struct manifest *m, char **lines)
331 char *ret, *use_funcs = NULL, *why;
332 bool in_function = false, fake_function = false, has_main = false;
335 ret = talloc_asprintf(m,
336 "/* Include header from module. */\n"
337 "#include <ccan/%s/%s.h>\n"
338 "/* Prepend a heap of headers. */\n"
339 "#include <assert.h>\n"
341 "#include <errno.h>\n"
342 "#include <fcntl.h>\n"
343 "#include <limits.h>\n"
344 "#include <stdbool.h>\n"
345 "#include <stdint.h>\n"
346 "#include <stdio.h>\n"
347 "#include <stdlib.h>\n"
348 "#include <string.h>\n"
349 "#include <sys/stat.h>\n"
350 "#include <sys/types.h>\n"
351 "#include <unistd.h>\n",
352 m->basename, m->basename);
354 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
355 "extern int somefunc(void);\n"
356 "int somefunc(void) { return 0; }\n"
357 "extern char somestring[];\n"
358 "char somestring[] = \"hello world\";\n");
360 if (looks_internal(lines, &why)) {
361 /* Wrap it all in main(). */
362 ret = start_main(ret, why);
363 fake_function = true;
367 ret = talloc_asprintf_append(ret,
368 "/* The example %s, so didn't wrap in main() */\n",
371 /* Primitive, very primitive. */
372 for (i = 0; lines[i]; i++) {
373 char *line = strip_comment(ret, lines[i]);
375 /* } at start of line ends a function. */
380 /* Character at start of line, with ( and no ;
381 * == function start. Ignore comments. */
382 if (!cisspace(line[0])
384 && !strchr(line, ';')
385 && !strstr(line, "//")) {
387 if (strncmp(line, "int main", 8) == 0)
389 if (strncmp(line, "static", 6) == 0) {
390 use_funcs = add_func(use_funcs,
395 /* ... means elided code. */
396 if (strcmp(line, "...") == 0) {
397 if (!in_function && !has_main
398 && looks_internal(lines + i + 1, &why)) {
399 /* This implies we start a function here. */
400 ret = start_main(ret, why);
402 fake_function = true;
405 ret = talloc_asprintf_append(ret,
406 "/* ... removed */\n");
409 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
413 ret = talloc_asprintf_append(ret,
414 "/* Need a main to link successfully. */\n"
415 "int main(void)\n{\n");
416 fake_function = true;
420 ret = talloc_asprintf_append(ret,
421 "/* Get rid of unused warnings"
422 " by printing addresses of"
423 " static funcs. */\n");
424 if (!fake_function) {
425 ret = talloc_asprintf_append(ret,
426 "int use_funcs(void);\n"
427 "int use_funcs(void) {\n");
428 fake_function = true;
430 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
434 ret = talloc_asprintf_append(ret, "return 0;\n"
439 static struct ccan_file *mangle_example(struct manifest *m,
440 struct ccan_file *example,
443 char *name, *contents;
447 name = temp_file(example, ".c",
448 talloc_asprintf(m, "%s/mangled-%s",
449 m->dir, example->name));
450 f = new_ccan_file(example,
451 talloc_dirname(example, name),
452 talloc_basename(example, name));
453 talloc_steal(f, name);
455 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
459 contents = mangle(m, lines);
460 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
465 f->contents = talloc_steal(f, contents);
466 list_add(&m->mangled_examples, &f->list);
470 /* If an example has expected output, it's complete and should not be
471 * included in future examples. */
472 static bool has_expected_output(char **lines)
476 for (i = 0; lines[i]; i++) {
477 char *p = lines[i] + strspn(lines[i], " \t");
478 if (!strstarts(p, "//"))
480 p += strspn(p, "/ ");
481 if (strncasecmp(p, "given", strlen("given")) == 0)
487 static unsigned int try_compiling(struct manifest *m,
490 struct ccan_file *mangled[3],
497 /* Try standalone. */
499 res[0] = compile(i, m, mangled[0], &err[0]);
500 lines[0] = get_ccan_file_lines(i);
501 if (res[0] && streq(err[0], ""))
505 lines[1] = combine(i, get_ccan_file_lines(i), prev);
507 mangled[1] = mangle_example(m, i, lines[1]);
508 res[1] = compile(i, m, mangled[1], &err[1]);
509 if (res[1] && streq(err[1], "")) {
516 /* Try standalone. */
517 lines[num] = get_ccan_file_lines(i);
518 mangled[num] = mangle_example(m, i, lines[num]);
519 res[num] = compile(i, m, mangled[num], &err[num]);
524 static void build_examples(struct manifest *m,
525 unsigned int *timeleft, struct score *score)
529 bool warnings = false;
534 list_for_each(&m->examples, i, list) {
536 struct ccan_file *file[3] = { NULL, NULL, NULL };
544 /* Simplify our dumb parsing. */
545 strip_leading_whitespace(get_ccan_file_lines(i));
547 num = try_compiling(m, i, prev, file, res, err, lines);
549 /* First look for a compile without any warnings. */
550 for (j = 0; j < num; j++) {
551 if (res[j] && streq(err[j], "")) {
552 if (!has_expected_output(lines[j]))
559 /* Now accept anything which succeeded. */
560 for (j = 0; j < num; j++) {
562 if (!has_expected_output(lines[j]))
566 score_file_error(score, file[j], 0,
567 "Compiling extracted example"
573 get_ccan_file_contents(file[j]),
582 error = "Compiling standalone, adding headers, "
583 "and including previous "
584 "example all failed";
586 error = "Standalone compile and"
587 " adding headers both failed";
590 error = talloc_asprintf(score,
591 "Standalone example:\n"
594 "Combining with previous example:\n"
597 "Adding headers, wrappers:\n"
600 get_ccan_file_contents(file[0]),
602 get_ccan_file_contents(file[1]),
604 get_ccan_file_contents(file[2]),
607 error = talloc_asprintf(score,
608 "Standalone example:\n"
611 "Adding headers, wrappers:\n"
614 get_ccan_file_contents(file[0]),
616 get_ccan_file_contents(file[1]),
620 score_file_error(score, i, 0, "%s", error);
621 /* This didn't work, so not a candidate for combining. */
628 /* An extra point if they all compiled without warnings. */
629 if (!list_empty(&m->examples)) {
636 struct ccanlint examples_compile = {
637 .key = "examples_compile",
638 .name = "Module examples compile",
639 .check = build_examples,
641 .needs = "examples_exist module_builds"
644 REGISTER_TEST(examples_compile);