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 static char *obj_list(const struct manifest *m, struct ccan_file *f)
72 char *list = talloc_strdup(m, "");
76 /* Object files for this module. */
77 list_for_each(&m->c_files, i, list)
78 list = talloc_asprintf_append(list, " %s", i->compiled);
80 /* Other ccan modules we depend on. */
81 list_for_each(&m->dep_dirs, i, list) {
83 list = talloc_asprintf_append(list, " %s", i->compiled);
86 /* Other modules implied by includes. */
87 for (lines = get_ccan_file_lines(f); *lines; lines++) {
88 unsigned preflen = strspn(*lines, " \t");
89 if (strstarts(*lines + preflen, "#include <ccan/")) {
93 mod = *lines + preflen + strlen("#include <ccan/");
94 modlen = strcspn(mod, "/");
95 mod = talloc_strndup(f, mod, modlen);
96 list = add_dep(m, list, mod);
103 static char *lib_list(const struct manifest *m)
106 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
107 char *ret = talloc_strdup(m, "");
109 for (i = 0; i < num; i++)
110 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
114 static bool compile(const void *ctx,
116 struct ccan_file *file,
117 bool keep, char **output)
119 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
120 if (!compile_and_link(ctx, file->fullname, ccan_dir,
122 "", lib_list(m), file->compiled, output)) {
123 talloc_free(file->compiled);
124 file->compiled = NULL;
130 static char *start_main(char *ret, const char *why)
132 return talloc_asprintf_append(ret,
133 "/* The example %s, so fake function wrapper inserted */\n"
134 "int main(int argc, char *argv[])\n"
138 /* We only handle simple function definitions here. */
139 static char *add_func(char *others, const char *line)
141 const char *p, *end = strchr(line, '(') - 1;
142 while (isspace(*end)) {
148 for (p = end; isalnum(*p) || *p == '_'; p--) {
153 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
154 (unsigned)(end - p + 1), p);
157 static void strip_leading_whitespace(char **lines)
159 unsigned int i, min_span = -1U;
161 for (i = 0; lines[i]; i++) {
162 unsigned int span = strspn(lines[i], " \t");
163 /* All whitespace? Ignore */
170 for (i = 0; lines[i]; i++)
171 if (strlen(lines[i]) >= min_span)
172 lines[i] += min_span;
175 static bool looks_internal(char **lines, char **why)
178 bool last_ended = true; /* Did last line finish a statement? */
180 for (i = 0; lines[i]; i++) {
181 /* Skip leading whitespace. */
182 const char *line = lines[i] + strspn(lines[i], " \t");
183 unsigned len = strspn(line, IDENT_CHARS);
185 if (!line[0] || isspace(line[0]) || strstarts(line, "//"))
189 if (strstarts(line, "if") && len == 2) {
190 *why = "starts with if";
193 if (strstarts(line, "for") && len == 3) {
194 *why = "starts with for";
197 if (strstarts(line, "while") && len == 5) {
198 *why = "starts with while";
201 if (strstarts(line, "do") && len == 2) {
202 *why = "starts with do";
207 if (strstarts(line, "#include")) {
208 *why = "starts with #include";
212 if (last_ended && strchr(line, '(')) {
213 if (strstarts(line, "static")) {
214 *why = "starts with static and contains (";
217 if (strends(line, ")")) {
218 *why = "contains ( and ends with )";
223 /* Single identifier then operator == inside function. */
224 if (last_ended && len
225 && ispunct(line[len+strspn(line+len, " ")])) {
226 *why = "starts with identifier then punctuation";
230 last_ended = (strends(line, "}")
231 || strends(line, ";")
232 || streq(line, "..."));
235 /* No idea... Say yes? */
236 *why = "gave no clues";
240 /* Examples will often build on prior ones. Try combining them. */
241 static char **combine(const void *ctx, char **lines, char **prev)
243 unsigned int i, lines_total, prev_total, count;
245 const char *reasoning;
251 /* If it looks internal, put prev at start. */
252 if (looks_internal(lines, &why)) {
254 reasoning = "seemed to belong inside a function";
256 /* Try inserting in first elided position */
257 for (count = 0; lines[count]; count++) {
258 if (strcmp(lines[count], "...") == 0)
262 /* Try at start anyway? */
264 reasoning = "didn't seem to belong inside"
265 " a function, so we prepended the previous"
268 reasoning = "didn't seem to belong inside"
269 " a function, so we put the previous example"
276 for (i = 0; lines[i]; i++);
279 for (i = 0; prev[i]; i++);
282 ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
283 ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
285 memcpy(ret+1, lines, count * sizeof(ret[0]));
286 memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
287 memcpy(ret+1 + count + prev_total, lines + count,
288 (lines_total - count + 1) * sizeof(ret[0]));
292 static char *mangle(struct manifest *m, char **lines)
294 char *ret, *use_funcs = NULL, *why;
295 bool in_function = false, fake_function = false, has_main = false;
298 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
299 "#include <assert.h>\n"
301 "#include <errno.h>\n"
302 "#include <fcntl.h>\n"
303 "#include <limits.h>\n"
304 "#include <stdbool.h>\n"
305 "#include <stdint.h>\n"
306 "#include <stdio.h>\n"
307 "#include <stdlib.h>\n"
308 "#include <string.h>\n"
309 "#include <sys/stat.h>\n"
310 "#include <sys/types.h>\n"
311 "#include <unistd.h>\n");
312 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
313 "#include <ccan/%s/%s.h>\n",
314 m->basename, m->basename);
316 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
317 "extern int somefunc(void);\n"
318 "int somefunc(void) { return 0; }\n"
319 "extern char somestring[];\n"
320 "char somestring[] = \"hello world\";\n");
322 if (looks_internal(lines, &why)) {
323 /* Wrap it all in main(). */
324 ret = start_main(ret, why);
325 fake_function = true;
329 ret = talloc_asprintf_append(ret,
330 "/* The example %s, so didn't wrap in main() */\n",
333 /* Primitive, very primitive. */
334 for (i = 0; lines[i]; i++) {
335 /* } at start of line ends a function. */
337 if (lines[i][0] == '}')
340 /* Character at start of line, with ( and no ;
341 * == function start. Ignore comments. */
342 if (!isspace(lines[i][0])
343 && strchr(lines[i], '(')
344 && !strchr(lines[i], ';')
345 && !strstr(lines[i], "//")) {
347 if (strncmp(lines[i], "int main", 8) == 0)
349 if (strncmp(lines[i], "static", 6) == 0) {
350 use_funcs = add_func(use_funcs,
355 /* ... means elided code. */
356 if (strcmp(lines[i], "...") == 0) {
357 if (!in_function && !has_main
358 && looks_internal(lines + i + 1, &why)) {
359 /* This implies we start a function here. */
360 ret = start_main(ret, why);
362 fake_function = true;
365 ret = talloc_asprintf_append(ret,
366 "/* ... removed */\n");
369 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
373 ret = talloc_asprintf_append(ret,
374 "/* Need a main to link successfully. */\n"
375 "int main(void)\n{\n");
376 fake_function = true;
380 ret = talloc_asprintf_append(ret,
381 "/* Get rid of unused warnings"
382 " by printing addresses of"
383 " static funcs. */\n");
384 if (!fake_function) {
385 ret = talloc_asprintf_append(ret,
386 "int use_funcs(void);\n"
387 "int use_funcs(void) {\n");
388 fake_function = true;
390 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
394 ret = talloc_asprintf_append(ret, "return 0;\n"
399 static struct ccan_file *mangle_example(struct manifest *m,
400 struct ccan_file *example,
404 char *name, *contents;
408 name = maybe_temp_file(example, ".c", keep,
409 talloc_asprintf(m, "%s/mangled-%s",
410 m->dir, example->name));
411 f = new_ccan_file(example,
412 talloc_dirname(example, name),
413 talloc_basename(example, name));
414 talloc_steal(f, name);
416 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
420 contents = mangle(m, lines);
421 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
426 f->contents = talloc_steal(f, contents);
427 list_add(&m->mangled_examples, &f->list);
431 /* If an example has expected output, it's complete and should not be
432 * included in future examples. */
433 static bool has_expected_output(char **lines)
437 for (i = 0; lines[i]; i++) {
438 char *p = lines[i] + strspn(lines[i], " \t");
439 if (!strstarts(p, "//"))
441 p += strspn(p, "/ ");
442 if (strncasecmp(p, "given", strlen("given")) == 0)
448 static unsigned int try_compiling(struct manifest *m,
452 struct ccan_file *mangled[3],
459 /* Try standalone. */
461 res[0] = compile(i, m, mangled[0], keep, &err[0]);
462 lines[0] = get_ccan_file_lines(i);
463 if (res[0] && streq(err[0], ""))
467 lines[1] = combine(i, get_ccan_file_lines(i), prev);
469 mangled[1] = mangle_example(m, i, lines[1], keep);
470 res[1] = compile(i, m, mangled[1], keep, &err[1]);
471 if (res[1] && streq(err[1], "")) {
478 /* Try standalone. */
479 lines[num] = get_ccan_file_lines(i);
480 mangled[num] = mangle_example(m, i, lines[num], keep);
481 res[num] = compile(i, m, mangled[num], keep, &err[num]);
486 static void build_examples(struct manifest *m, bool keep,
487 unsigned int *timeleft, struct score *score)
491 bool warnings = false;
496 list_for_each(&m->examples, i, list) {
498 struct ccan_file *file[3] = { NULL, NULL, NULL };
506 /* Simplify our dumb parsing. */
507 strip_leading_whitespace(get_ccan_file_lines(i));
509 num = try_compiling(m, i, prev, keep, file, res, err, lines);
511 /* First look for a compile without any warnings. */
512 for (j = 0; j < num; j++) {
513 if (res[j] && streq(err[j], "")) {
514 if (!has_expected_output(lines[j]))
521 /* Now accept anything which succeeded. */
522 for (j = 0; j < num; j++) {
524 if (!has_expected_output(lines[j]))
528 score->error = "Compiling extracted example"
530 score_file_error(score, file[j], 0, err[j]);
536 score->error = "Compiling extracted examples failed";
539 error = "Standalone, adding headers, "
540 "and including previous "
541 "example all failed";
543 error = "Standalone compile and"
544 " adding headers both failed";
547 error = talloc_asprintf(score,
548 "Standalone example:\n"
551 "Combining with previous example:\n"
554 "Adding headers, wrappers:\n"
557 get_ccan_file_contents(file[0]),
559 get_ccan_file_contents(file[1]),
561 get_ccan_file_contents(file[2]),
564 error = talloc_asprintf(score,
565 "Standalone example:\n"
568 "Adding headers, wrappers:\n"
571 get_ccan_file_contents(file[0]),
573 get_ccan_file_contents(file[1]),
577 score_file_error(score, i, 0, error);
578 /* This didn't work, so not a candidate for combining. */
585 /* An extra point if they all compiled without warnings. */
586 if (!list_empty(&m->examples)) {
593 struct ccanlint examples_compile = {
594 .key = "examples-compile",
595 .name = "Module examples compile",
596 .check = build_examples,
600 REGISTER_TEST(examples_compile, &has_examples, &build_objs, NULL);