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 /* Only handles very simple comments. */
295 static char *strip_comment(const void *ctx, const char *orig_line)
297 char *p, *ret = talloc_strdup(ctx, orig_line);
299 p = strstr(ret, "/*");
301 p = strstr(ret, "//");
307 static char *mangle(struct manifest *m, char **lines)
309 char *ret, *use_funcs = NULL, *why;
310 bool in_function = false, fake_function = false, has_main = false;
313 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
314 "#include <assert.h>\n"
316 "#include <errno.h>\n"
317 "#include <fcntl.h>\n"
318 "#include <limits.h>\n"
319 "#include <stdbool.h>\n"
320 "#include <stdint.h>\n"
321 "#include <stdio.h>\n"
322 "#include <stdlib.h>\n"
323 "#include <string.h>\n"
324 "#include <sys/stat.h>\n"
325 "#include <sys/types.h>\n"
326 "#include <unistd.h>\n");
327 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
328 "#include <ccan/%s/%s.h>\n",
329 m->basename, m->basename);
331 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
332 "extern int somefunc(void);\n"
333 "int somefunc(void) { return 0; }\n"
334 "extern char somestring[];\n"
335 "char somestring[] = \"hello world\";\n");
337 if (looks_internal(lines, &why)) {
338 /* Wrap it all in main(). */
339 ret = start_main(ret, why);
340 fake_function = true;
344 ret = talloc_asprintf_append(ret,
345 "/* The example %s, so didn't wrap in main() */\n",
348 /* Primitive, very primitive. */
349 for (i = 0; lines[i]; i++) {
350 char *line = strip_comment(ret, lines[i]);
352 /* } at start of line ends a function. */
357 /* Character at start of line, with ( and no ;
358 * == function start. Ignore comments. */
359 if (!isspace(line[0])
361 && !strchr(line, ';')
362 && !strstr(line, "//")) {
364 if (strncmp(line, "int main", 8) == 0)
366 if (strncmp(line, "static", 6) == 0) {
367 use_funcs = add_func(use_funcs,
372 /* ... means elided code. */
373 if (strcmp(line, "...") == 0) {
374 if (!in_function && !has_main
375 && looks_internal(lines + i + 1, &why)) {
376 /* This implies we start a function here. */
377 ret = start_main(ret, why);
379 fake_function = true;
382 ret = talloc_asprintf_append(ret,
383 "/* ... removed */\n");
386 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
390 ret = talloc_asprintf_append(ret,
391 "/* Need a main to link successfully. */\n"
392 "int main(void)\n{\n");
393 fake_function = true;
397 ret = talloc_asprintf_append(ret,
398 "/* Get rid of unused warnings"
399 " by printing addresses of"
400 " static funcs. */\n");
401 if (!fake_function) {
402 ret = talloc_asprintf_append(ret,
403 "int use_funcs(void);\n"
404 "int use_funcs(void) {\n");
405 fake_function = true;
407 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
411 ret = talloc_asprintf_append(ret, "return 0;\n"
416 static struct ccan_file *mangle_example(struct manifest *m,
417 struct ccan_file *example,
421 char *name, *contents;
425 name = maybe_temp_file(example, ".c", keep,
426 talloc_asprintf(m, "%s/mangled-%s",
427 m->dir, example->name));
428 f = new_ccan_file(example,
429 talloc_dirname(example, name),
430 talloc_basename(example, name));
431 talloc_steal(f, name);
433 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
437 contents = mangle(m, lines);
438 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
443 f->contents = talloc_steal(f, contents);
444 list_add(&m->mangled_examples, &f->list);
448 /* If an example has expected output, it's complete and should not be
449 * included in future examples. */
450 static bool has_expected_output(char **lines)
454 for (i = 0; lines[i]; i++) {
455 char *p = lines[i] + strspn(lines[i], " \t");
456 if (!strstarts(p, "//"))
458 p += strspn(p, "/ ");
459 if (strncasecmp(p, "given", strlen("given")) == 0)
465 static unsigned int try_compiling(struct manifest *m,
469 struct ccan_file *mangled[3],
476 /* Try standalone. */
478 res[0] = compile(i, m, mangled[0], keep, &err[0]);
479 lines[0] = get_ccan_file_lines(i);
480 if (res[0] && streq(err[0], ""))
484 lines[1] = combine(i, get_ccan_file_lines(i), prev);
486 mangled[1] = mangle_example(m, i, lines[1], keep);
487 res[1] = compile(i, m, mangled[1], keep, &err[1]);
488 if (res[1] && streq(err[1], "")) {
495 /* Try standalone. */
496 lines[num] = get_ccan_file_lines(i);
497 mangled[num] = mangle_example(m, i, lines[num], keep);
498 res[num] = compile(i, m, mangled[num], keep, &err[num]);
503 static void build_examples(struct manifest *m, bool keep,
504 unsigned int *timeleft, struct score *score)
508 bool warnings = false;
513 list_for_each(&m->examples, i, list) {
515 struct ccan_file *file[3] = { NULL, NULL, NULL };
523 /* Simplify our dumb parsing. */
524 strip_leading_whitespace(get_ccan_file_lines(i));
526 num = try_compiling(m, i, prev, keep, file, res, err, lines);
528 /* First look for a compile without any warnings. */
529 for (j = 0; j < num; j++) {
530 if (res[j] && streq(err[j], "")) {
531 if (!has_expected_output(lines[j]))
538 /* Now accept anything which succeeded. */
539 for (j = 0; j < num; j++) {
541 if (!has_expected_output(lines[j]))
545 score->error = "Compiling extracted example"
547 error = talloc_asprintf(score,
552 get_ccan_file_contents(file[j]),
554 score_file_error(score, file[j], 0, error);
560 score->error = "Compiling extracted examples failed";
563 error = "Standalone, adding headers, "
564 "and including previous "
565 "example all failed";
567 error = "Standalone compile and"
568 " adding headers both failed";
571 error = talloc_asprintf(score,
572 "Standalone example:\n"
575 "Combining with previous example:\n"
578 "Adding headers, wrappers:\n"
581 get_ccan_file_contents(file[0]),
583 get_ccan_file_contents(file[1]),
585 get_ccan_file_contents(file[2]),
588 error = talloc_asprintf(score,
589 "Standalone example:\n"
592 "Adding headers, wrappers:\n"
595 get_ccan_file_contents(file[0]),
597 get_ccan_file_contents(file[1]),
601 score_file_error(score, i, 0, error);
602 /* This didn't work, so not a candidate for combining. */
609 /* An extra point if they all compiled without warnings. */
610 if (!list_empty(&m->examples)) {
617 struct ccanlint examples_compile = {
618 .key = "examples-compile",
619 .name = "Module examples compile",
620 .check = build_examples,
624 REGISTER_TEST(examples_compile, &has_examples, &build, NULL);