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, ".", &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 char *compile(const void *ctx,
116 struct ccan_file *file,
121 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
122 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
124 "", lib_list(m), file->compiled);
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 (isblank(*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] || isblank(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 (!isblank(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. */");
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 void build_examples(struct manifest *m, bool keep,
452 unsigned int *timeleft, struct score *score)
460 list_for_each(&m->examples, i, list) {
461 char *ret, *ret1, *ret2 = NULL;
462 struct ccan_file *mangle1, *mangle2 = NULL;
466 /* Simplify our dumb parsing. */
467 strip_leading_whitespace(get_ccan_file_lines(i));
468 ret = compile(i, m, i, keep);
470 char **lines = get_ccan_file_lines(i);
471 if (!has_expected_output(lines))
478 char **new = combine(i, get_ccan_file_lines(i), prev);
480 mangle2 = mangle_example(m, i, new, keep);
481 ret2 = compile(i, m, mangle2, keep);
483 if (!has_expected_output(new))
490 /* Try standalone. */
491 mangle1 = mangle_example(m, i, get_ccan_file_lines(i), keep);
492 ret1 = compile(i, m, mangle1, keep);
494 char **lines = get_ccan_file_lines(i);
495 if (!has_expected_output(lines))
502 score->error = "Compiling extracted examples failed";
505 err = "Standalone, adding headers, "
506 "and including previous "
507 "example all failed";
509 err = "Standalone compile and"
510 " adding headers both failed";
512 err = talloc_asprintf(score,
513 "Standalone example:\n"
516 "Adding headers, wrappers:\n"
519 get_ccan_file_contents(i),
521 get_ccan_file_contents(mangle1),
525 err = talloc_asprintf_append(err,
526 "Combining with previous example:\n"
529 get_ccan_file_contents(mangle2),
532 score_file_error(score, i, 0, err);
533 /* This didn't work, so not a candidate for combining. */
538 struct ccanlint examples_compile = {
539 .key = "examples-compile",
540 .name = "Module examples compile",
541 .check = build_examples,
545 REGISTER_TEST(examples_compile, &has_examples, NULL);