1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
14 static const char *can_run(struct manifest *m)
17 return "Safe mode enabled";
18 if (list_empty(&m->examples))
19 return "No examples to compile";
23 /* FIXME: We should build if it doesn't exist... */
24 static bool expect_obj_file(const char *dir)
26 struct manifest *dep_man;
29 dep_man = get_manifest(dir, dir);
31 /* If it has C files, we expect an object file built from them. */
32 has_c_files = !list_empty(&dep_man->c_files);
37 static char *add_dep(const struct manifest *m, char *list, const char *mod)
43 if (streq(m->basename, mod))
46 /* Not if there's no object file for that module */
47 if (!expect_obj_file(talloc_asprintf(list, "%s/ccan/%s", ccan_dir,mod)))
50 obj = talloc_asprintf(list, "%s/ccan/%s.o", ccan_dir, mod);
52 /* Not anyone we've already included. */
53 if (strstr(list, obj))
56 list = talloc_asprintf_append(list, " %s", obj);
58 /* Get that modules depends as well... */
60 deps = get_deps(m, talloc_asprintf(list, "%s/ccan/%s", ccan_dir, mod),
63 for (i = 0; deps[i]; i++) {
64 if (strstarts(deps[i], "ccan/"))
65 list = add_dep(m, list, deps[i] + strlen("ccan/"));
70 /* FIXME: Merge this into one place. */
71 static char *obj_list(const struct manifest *m, struct ccan_file *f)
73 char *list = talloc_strdup(m, "");
75 struct manifest *subm;
78 /* Object files for this module. */
79 list_for_each(&m->c_files, i, list)
80 list = talloc_asprintf_append(list, " %s", i->compiled);
82 /* Other ccan modules we depend on. */
83 list_for_each(&m->deps, subm, list) {
85 list = talloc_asprintf_append(list, " %s",
89 /* Other modules implied by includes. */
90 for (lines = get_ccan_file_lines(f); *lines; lines++) {
91 unsigned preflen = strspn(*lines, " \t");
92 if (strstarts(*lines + preflen, "#include <ccan/")) {
96 mod = *lines + preflen + strlen("#include <ccan/");
97 modlen = strcspn(mod, "/");
98 mod = talloc_strndup(f, mod, modlen);
99 list = add_dep(m, list, mod);
106 static char *lib_list(const struct manifest *m)
109 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
110 char *ret = talloc_strdup(m, "");
112 for (i = 0; i < num; i++)
113 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
117 static bool compile(const void *ctx,
119 struct ccan_file *file,
120 bool keep, char **output)
122 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
123 if (!compile_and_link(ctx, file->fullname, ccan_dir,
125 "", lib_list(m), file->compiled, output)) {
126 talloc_free(file->compiled);
127 file->compiled = NULL;
133 static char *start_main(char *ret, const char *why)
135 return talloc_asprintf_append(ret,
136 "/* The example %s, so fake function wrapper inserted */\n"
137 "int main(int argc, char *argv[])\n"
141 /* We only handle simple function definitions here. */
142 static char *add_func(char *others, const char *line)
144 const char *p, *end = strchr(line, '(') - 1;
145 while (isspace(*end)) {
151 for (p = end; isalnum(*p) || *p == '_'; p--) {
156 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
157 (unsigned)(end - p + 1), p);
160 static void strip_leading_whitespace(char **lines)
162 unsigned int i, min_span = -1U;
164 for (i = 0; lines[i]; i++) {
165 unsigned int span = strspn(lines[i], " \t");
166 /* All whitespace? Ignore */
173 for (i = 0; lines[i]; i++)
174 if (strlen(lines[i]) >= min_span)
175 lines[i] += min_span;
178 static bool looks_internal(char **lines, char **why)
181 bool last_ended = true; /* Did last line finish a statement? */
183 for (i = 0; lines[i]; i++) {
184 /* Skip leading whitespace. */
185 const char *line = lines[i] + strspn(lines[i], " \t");
186 unsigned len = strspn(line, IDENT_CHARS);
188 if (!line[0] || isspace(line[0]) || strstarts(line, "//"))
192 if (strstarts(line, "if") && len == 2) {
193 *why = "starts with if";
196 if (strstarts(line, "for") && len == 3) {
197 *why = "starts with for";
200 if (strstarts(line, "while") && len == 5) {
201 *why = "starts with while";
204 if (strstarts(line, "do") && len == 2) {
205 *why = "starts with do";
210 if (strstarts(line, "#include")) {
211 *why = "starts with #include";
215 if (last_ended && strchr(line, '(')) {
216 if (strstarts(line, "static")) {
217 *why = "starts with static and contains (";
220 if (strends(line, ")")) {
221 *why = "contains ( and ends with )";
226 /* Single identifier then operator == inside function. */
227 if (last_ended && len
228 && ispunct(line[len+strspn(line+len, " ")])) {
229 *why = "starts with identifier then punctuation";
233 last_ended = (strends(line, "}")
234 || strends(line, ";")
235 || streq(line, "..."));
238 /* No idea... Say yes? */
239 *why = "gave no clues";
243 /* Examples will often build on prior ones. Try combining them. */
244 static char **combine(const void *ctx, char **lines, char **prev)
246 unsigned int i, lines_total, prev_total, count;
248 const char *reasoning;
254 /* If it looks internal, put prev at start. */
255 if (looks_internal(lines, &why)) {
257 reasoning = "seemed to belong inside a function";
259 /* Try inserting in first elided position */
260 for (count = 0; lines[count]; count++) {
261 if (strcmp(lines[count], "...") == 0)
265 /* Try at start anyway? */
267 reasoning = "didn't seem to belong inside"
268 " a function, so we prepended the previous"
271 reasoning = "didn't seem to belong inside"
272 " a function, so we put the previous example"
279 for (i = 0; lines[i]; i++);
282 for (i = 0; prev[i]; i++);
285 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
286 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
288 memcpy(ret+1, lines, count * sizeof(ret[0]));
289 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
290 memcpy(ret+1 + count + prev_total, lines + count,
291 (lines_total - count + 1) * sizeof(ret[0]));
295 static char *mangle(struct manifest *m, char **lines)
297 char *ret, *use_funcs = NULL, *why;
298 bool in_function = false, fake_function = false, has_main = false;
301 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
302 "#include <assert.h>\n"
304 "#include <errno.h>\n"
305 "#include <fcntl.h>\n"
306 "#include <limits.h>\n"
307 "#include <stdbool.h>\n"
308 "#include <stdint.h>\n"
309 "#include <stdio.h>\n"
310 "#include <stdlib.h>\n"
311 "#include <string.h>\n"
312 "#include <sys/stat.h>\n"
313 "#include <sys/types.h>\n"
314 "#include <unistd.h>\n");
315 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
316 "#include <ccan/%s/%s.h>\n",
317 m->basename, m->basename);
319 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
320 "extern int somefunc(void);\n"
321 "int somefunc(void) { return 0; }\n"
322 "extern char somestring[];\n"
323 "char somestring[] = \"hello world\";\n");
325 if (looks_internal(lines, &why)) {
326 /* Wrap it all in main(). */
327 ret = start_main(ret, why);
328 fake_function = true;
332 ret = talloc_asprintf_append(ret,
333 "/* The example %s, so didn't wrap in main() */\n",
336 /* Primitive, very primitive. */
337 for (i = 0; lines[i]; i++) {
338 /* } at start of line ends a function. */
340 if (lines[i][0] == '}')
343 /* Character at start of line, with ( and no ;
344 * == function start. Ignore comments. */
345 if (!isspace(lines[i][0])
346 && strchr(lines[i], '(')
347 && !strchr(lines[i], ';')
348 && !strstr(lines[i], "//")) {
350 if (strncmp(lines[i], "int main", 8) == 0)
352 if (strncmp(lines[i], "static", 6) == 0) {
353 use_funcs = add_func(use_funcs,
358 /* ... means elided code. */
359 if (strcmp(lines[i], "...") == 0) {
360 if (!in_function && !has_main
361 && looks_internal(lines + i + 1, &why)) {
362 /* This implies we start a function here. */
363 ret = start_main(ret, why);
365 fake_function = true;
368 ret = talloc_asprintf_append(ret,
369 "/* ... removed */\n");
372 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
376 ret = talloc_asprintf_append(ret,
377 "/* Need a main to link successfully. */\n"
378 "int main(void)\n{\n");
379 fake_function = true;
383 ret = talloc_asprintf_append(ret,
384 "/* Get rid of unused warnings"
385 " by printing addresses of"
386 " static funcs. */\n");
387 if (!fake_function) {
388 ret = talloc_asprintf_append(ret,
389 "int use_funcs(void);\n"
390 "int use_funcs(void) {\n");
391 fake_function = true;
393 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
397 ret = talloc_asprintf_append(ret, "return 0;\n"
402 static struct ccan_file *mangle_example(struct manifest *m,
403 struct ccan_file *example,
407 char *name, *contents;
411 name = maybe_temp_file(example, ".c", keep,
412 talloc_asprintf(m, "%s/mangled-%s",
413 m->dir, example->name));
414 f = new_ccan_file(example,
415 talloc_dirname(example, name),
416 talloc_basename(example, name));
417 talloc_steal(f, name);
419 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
423 contents = mangle(m, lines);
424 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
429 f->contents = talloc_steal(f, contents);
430 list_add(&m->mangled_examples, &f->list);
434 /* If an example has expected output, it's complete and should not be
435 * included in future examples. */
436 static bool has_expected_output(char **lines)
440 for (i = 0; lines[i]; i++) {
441 char *p = lines[i] + strspn(lines[i], " \t");
442 if (!strstarts(p, "//"))
444 p += strspn(p, "/ ");
445 if (strncasecmp(p, "given", strlen("given")) == 0)
451 static unsigned int try_compiling(struct manifest *m,
455 struct ccan_file *mangled[3],
462 /* Try standalone. */
464 res[0] = compile(i, m, mangled[0], keep, &err[0]);
465 lines[0] = get_ccan_file_lines(i);
466 if (res[0] && streq(err[0], ""))
470 lines[1] = combine(i, get_ccan_file_lines(i), prev);
472 mangled[1] = mangle_example(m, i, lines[1], keep);
473 res[1] = compile(i, m, mangled[1], keep, &err[1]);
474 if (res[1] && streq(err[1], "")) {
481 /* Try standalone. */
482 lines[num] = get_ccan_file_lines(i);
483 mangled[num] = mangle_example(m, i, lines[num], keep);
484 res[num] = compile(i, m, mangled[num], keep, &err[num]);
489 static void build_examples(struct manifest *m, bool keep,
490 unsigned int *timeleft, struct score *score)
494 bool warnings = false;
499 list_for_each(&m->examples, i, list) {
501 struct ccan_file *file[3] = { NULL, NULL, NULL };
509 /* Simplify our dumb parsing. */
510 strip_leading_whitespace(get_ccan_file_lines(i));
512 num = try_compiling(m, i, prev, keep, file, res, err, lines);
514 /* First look for a compile without any warnings. */
515 for (j = 0; j < num; j++) {
516 if (res[j] && streq(err[j], "")) {
517 if (!has_expected_output(lines[j]))
524 /* Now accept anything which succeeded. */
525 for (j = 0; j < num; j++) {
527 if (!has_expected_output(lines[j]))
531 score->error = "Compiling extracted example"
533 score_file_error(score, file[j], 0, err[j]);
539 score->error = "Compiling extracted examples failed";
542 error = "Standalone, adding headers, "
543 "and including previous "
544 "example all failed";
546 error = "Standalone compile and"
547 " adding headers both failed";
550 error = talloc_asprintf(score,
551 "Standalone example:\n"
554 "Combining with previous example:\n"
557 "Adding headers, wrappers:\n"
560 get_ccan_file_contents(file[0]),
562 get_ccan_file_contents(file[1]),
564 get_ccan_file_contents(file[2]),
567 error = talloc_asprintf(score,
568 "Standalone example:\n"
571 "Adding headers, wrappers:\n"
574 get_ccan_file_contents(file[0]),
576 get_ccan_file_contents(file[1]),
580 score_file_error(score, i, 0, error);
581 /* This didn't work, so not a candidate for combining. */
588 /* An extra point if they all compiled without warnings. */
589 if (!list_empty(&m->examples)) {
596 struct ccanlint examples_compile = {
597 .key = "examples-compile",
598 .name = "Module examples compile",
599 .check = build_examples,
603 REGISTER_TEST(examples_compile, &has_examples, &build_objs, NULL);