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, "");
74 struct manifest *subm;
79 list = talloc_asprintf_append(list, " %s", m->compiled);
81 /* Other ccan modules we depend on. */
82 list_for_each(&m->deps, subm, list) {
84 list = talloc_asprintf_append(list, " %s",
88 /* Other modules implied by includes. */
89 for (lines = get_ccan_file_lines(f); *lines; lines++) {
90 unsigned preflen = strspn(*lines, " \t");
91 if (strstarts(*lines + preflen, "#include <ccan/")) {
95 mod = *lines + preflen + strlen("#include <ccan/");
96 modlen = strcspn(mod, "/");
97 mod = talloc_strndup(f, mod, modlen);
98 list = add_dep(m, list, mod);
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 talloc_free(file->compiled);
126 file->compiled = NULL;
132 static char *start_main(char *ret, const char *why)
134 return talloc_asprintf_append(ret,
135 "/* The example %s, so fake function wrapper inserted */\n"
136 "int main(int argc, char *argv[])\n"
140 /* We only handle simple function definitions here. */
141 static char *add_func(char *others, const char *line)
143 const char *p, *end = strchr(line, '(') - 1;
144 while (isspace(*end)) {
150 for (p = end; isalnum(*p) || *p == '_'; p--) {
155 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
156 (unsigned)(end - p), p+1);
159 static void strip_leading_whitespace(char **lines)
161 unsigned int i, min_span = -1U;
163 for (i = 0; lines[i]; i++) {
164 unsigned int span = strspn(lines[i], " \t");
165 /* All whitespace? Ignore */
172 for (i = 0; lines[i]; i++)
173 if (strlen(lines[i]) >= min_span)
174 lines[i] += min_span;
177 static bool looks_internal(char **lines, char **why)
180 bool last_ended = true; /* Did last line finish a statement? */
182 for (i = 0; lines[i]; i++) {
183 /* Skip leading whitespace. */
184 const char *line = lines[i] + strspn(lines[i], " \t");
185 unsigned len = strspn(line, IDENT_CHARS);
187 if (!line[0] || isspace(line[0]) || strstarts(line, "//"))
191 if (strstarts(line, "if") && len == 2) {
192 *why = "starts with if";
195 if (strstarts(line, "for") && len == 3) {
196 *why = "starts with for";
199 if (strstarts(line, "while") && len == 5) {
200 *why = "starts with while";
203 if (strstarts(line, "do") && len == 2) {
204 *why = "starts with do";
209 if (strstarts(line, "#include")) {
210 *why = "starts with #include";
214 if (last_ended && strchr(line, '(')) {
215 if (strstarts(line, "static")) {
216 *why = "starts with static and contains (";
219 if (strends(line, ")")) {
220 *why = "contains ( and ends with )";
225 /* Single identifier then operator == inside function. */
226 if (last_ended && len
227 && ispunct(line[len+strspn(line+len, " ")])) {
228 *why = "starts with identifier then punctuation";
232 last_ended = (strends(line, "}")
233 || strends(line, ";")
234 || streq(line, "..."));
237 /* No idea... Say yes? */
238 *why = "gave no clues";
242 /* Examples will often build on prior ones. Try combining them. */
243 static char **combine(const void *ctx, char **lines, char **prev)
245 unsigned int i, lines_total, prev_total, count;
247 const char *reasoning;
253 /* If it looks internal, put prev at start. */
254 if (looks_internal(lines, &why)) {
256 reasoning = "seemed to belong inside a function";
258 /* Try inserting in first elided position */
259 for (count = 0; lines[count]; count++) {
260 if (strcmp(lines[count], "...") == 0)
264 /* Try at start anyway? */
266 reasoning = "didn't seem to belong inside"
267 " a function, so we prepended the previous"
270 reasoning = "didn't seem to belong inside"
271 " a function, so we put the previous example"
278 for (i = 0; lines[i]; i++);
281 for (i = 0; prev[i]; i++);
284 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
285 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
287 memcpy(ret+1, lines, count * sizeof(ret[0]));
288 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
289 memcpy(ret+1 + count + prev_total, lines + count,
290 (lines_total - count + 1) * sizeof(ret[0]));
294 static char *mangle(struct manifest *m, char **lines)
296 char *ret, *use_funcs = NULL, *why;
297 bool in_function = false, fake_function = false, has_main = false;
300 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
301 "#include <assert.h>\n"
303 "#include <errno.h>\n"
304 "#include <fcntl.h>\n"
305 "#include <limits.h>\n"
306 "#include <stdbool.h>\n"
307 "#include <stdint.h>\n"
308 "#include <stdio.h>\n"
309 "#include <stdlib.h>\n"
310 "#include <string.h>\n"
311 "#include <sys/stat.h>\n"
312 "#include <sys/types.h>\n"
313 "#include <unistd.h>\n");
314 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
315 "#include <ccan/%s/%s.h>\n",
316 m->basename, m->basename);
318 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
319 "extern int somefunc(void);\n"
320 "int somefunc(void) { return 0; }\n"
321 "extern char somestring[];\n"
322 "char somestring[] = \"hello world\";\n");
324 if (looks_internal(lines, &why)) {
325 /* Wrap it all in main(). */
326 ret = start_main(ret, why);
327 fake_function = true;
331 ret = talloc_asprintf_append(ret,
332 "/* The example %s, so didn't wrap in main() */\n",
335 /* Primitive, very primitive. */
336 for (i = 0; lines[i]; i++) {
337 /* } at start of line ends a function. */
339 if (lines[i][0] == '}')
342 /* Character at start of line, with ( and no ;
343 * == function start. Ignore comments. */
344 if (!isspace(lines[i][0])
345 && strchr(lines[i], '(')
346 && !strchr(lines[i], ';')
347 && !strstr(lines[i], "//")) {
349 if (strncmp(lines[i], "int main", 8) == 0)
351 if (strncmp(lines[i], "static", 6) == 0) {
352 use_funcs = add_func(use_funcs,
357 /* ... means elided code. */
358 if (strcmp(lines[i], "...") == 0) {
359 if (!in_function && !has_main
360 && looks_internal(lines + i + 1, &why)) {
361 /* This implies we start a function here. */
362 ret = start_main(ret, why);
364 fake_function = true;
367 ret = talloc_asprintf_append(ret,
368 "/* ... removed */\n");
371 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
375 ret = talloc_asprintf_append(ret,
376 "/* Need a main to link successfully. */\n"
377 "int main(void)\n{\n");
378 fake_function = true;
382 ret = talloc_asprintf_append(ret,
383 "/* Get rid of unused warnings"
384 " by printing addresses of"
385 " static funcs. */\n");
386 if (!fake_function) {
387 ret = talloc_asprintf_append(ret,
388 "int use_funcs(void);\n"
389 "int use_funcs(void) {\n");
390 fake_function = true;
392 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
396 ret = talloc_asprintf_append(ret, "return 0;\n"
401 static struct ccan_file *mangle_example(struct manifest *m,
402 struct ccan_file *example,
406 char *name, *contents;
410 name = maybe_temp_file(example, ".c", keep,
411 talloc_asprintf(m, "%s/mangled-%s",
412 m->dir, example->name));
413 f = new_ccan_file(example,
414 talloc_dirname(example, name),
415 talloc_basename(example, name));
416 talloc_steal(f, name);
418 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
422 contents = mangle(m, lines);
423 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
428 f->contents = talloc_steal(f, contents);
429 list_add(&m->mangled_examples, &f->list);
433 /* If an example has expected output, it's complete and should not be
434 * included in future examples. */
435 static bool has_expected_output(char **lines)
439 for (i = 0; lines[i]; i++) {
440 char *p = lines[i] + strspn(lines[i], " \t");
441 if (!strstarts(p, "//"))
443 p += strspn(p, "/ ");
444 if (strncasecmp(p, "given", strlen("given")) == 0)
450 static unsigned int try_compiling(struct manifest *m,
454 struct ccan_file *mangled[3],
461 /* Try standalone. */
463 res[0] = compile(i, m, mangled[0], keep, &err[0]);
464 lines[0] = get_ccan_file_lines(i);
465 if (res[0] && streq(err[0], ""))
469 lines[1] = combine(i, get_ccan_file_lines(i), prev);
471 mangled[1] = mangle_example(m, i, lines[1], keep);
472 res[1] = compile(i, m, mangled[1], keep, &err[1]);
473 if (res[1] && streq(err[1], "")) {
480 /* Try standalone. */
481 lines[num] = get_ccan_file_lines(i);
482 mangled[num] = mangle_example(m, i, lines[num], keep);
483 res[num] = compile(i, m, mangled[num], keep, &err[num]);
488 static void build_examples(struct manifest *m, bool keep,
489 unsigned int *timeleft, struct score *score)
493 bool warnings = false;
498 list_for_each(&m->examples, i, list) {
500 struct ccan_file *file[3] = { NULL, NULL, NULL };
508 /* Simplify our dumb parsing. */
509 strip_leading_whitespace(get_ccan_file_lines(i));
511 num = try_compiling(m, i, prev, keep, file, res, err, lines);
513 /* First look for a compile without any warnings. */
514 for (j = 0; j < num; j++) {
515 if (res[j] && streq(err[j], "")) {
516 if (!has_expected_output(lines[j]))
523 /* Now accept anything which succeeded. */
524 for (j = 0; j < num; j++) {
526 if (!has_expected_output(lines[j]))
530 score->error = "Compiling extracted example"
532 score_file_error(score, file[j], 0, err[j]);
538 score->error = "Compiling extracted examples failed";
541 error = "Standalone, adding headers, "
542 "and including previous "
543 "example all failed";
545 error = "Standalone compile and"
546 " adding headers both failed";
549 error = talloc_asprintf(score,
550 "Standalone example:\n"
553 "Combining with previous example:\n"
556 "Adding headers, wrappers:\n"
559 get_ccan_file_contents(file[0]),
561 get_ccan_file_contents(file[1]),
563 get_ccan_file_contents(file[2]),
566 error = talloc_asprintf(score,
567 "Standalone example:\n"
570 "Adding headers, wrappers:\n"
573 get_ccan_file_contents(file[0]),
575 get_ccan_file_contents(file[1]),
579 score_file_error(score, i, 0, error);
580 /* This didn't work, so not a candidate for combining. */
587 /* An extra point if they all compiled without warnings. */
588 if (!list_empty(&m->examples)) {
595 struct ccanlint examples_compile = {
596 .key = "examples-compile",
597 .name = "Module examples compile",
598 .check = build_examples,
602 REGISTER_TEST(examples_compile, &has_examples, &build, NULL);