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";
21 /* FIXME: We should build if it doesn't exist... */
22 static bool expect_obj_file(const char *dir)
24 struct manifest *dep_man;
27 dep_man = get_manifest(dir, dir);
29 /* If it has C files, we expect an object file built from them. */
30 has_c_files = !list_empty(&dep_man->c_files);
35 static char *add_dep(const struct manifest *m, char *list, const char *mod)
41 if (streq(m->basename, mod))
44 /* Not if there's no object file for that module */
45 if (!expect_obj_file(talloc_asprintf(list, "%s/ccan/%s", ccan_dir,mod)))
48 obj = talloc_asprintf(list, "%s/ccan/%s.o", ccan_dir, mod);
50 /* Not anyone we've already included. */
51 if (strstr(list, obj))
54 list = talloc_asprintf_append(list, " %s", obj);
56 /* Get that modules depends as well... */
58 deps = get_deps(m, talloc_asprintf(list, "%s/ccan/%s", ccan_dir, mod),
61 for (i = 0; deps[i]; i++) {
62 if (strstarts(deps[i], "ccan/"))
63 list = add_dep(m, list, deps[i] + strlen("ccan/"));
68 static char *obj_list(const struct manifest *m, struct ccan_file *f)
70 char *list = talloc_strdup(m, "");
74 /* Object files for this module. */
75 list_for_each(&m->c_files, i, list)
76 list = talloc_asprintf_append(list, " %s", i->compiled);
78 /* Other ccan modules we depend on. */
79 list_for_each(&m->dep_dirs, i, list) {
81 list = talloc_asprintf_append(list, " %s", i->compiled);
84 /* Other modules implied by includes. */
85 for (lines = get_ccan_file_lines(f); *lines; lines++) {
86 unsigned preflen = strspn(*lines, " \t");
87 if (strstarts(*lines + preflen, "#include <ccan/")) {
91 mod = *lines + preflen + strlen("#include <ccan/");
92 modlen = strcspn(mod, "/");
93 mod = talloc_strndup(f, mod, modlen);
94 list = add_dep(m, list, mod);
101 static char *lib_list(const struct manifest *m)
104 char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
105 char *ret = talloc_strdup(m, "");
107 for (i = 0; i < num; i++)
108 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
112 static char *compile(const void *ctx,
114 struct ccan_file *file,
119 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
120 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
122 "", lib_list(m), file->compiled);
124 talloc_free(file->compiled);
135 static char *start_main(char *ret)
137 return talloc_asprintf_append(ret,
138 "/* Fake function wrapper inserted */\n"
139 "int main(int argc, char *argv[])\n"
143 /* We only handle simple function definitions here. */
144 static char *add_func(char *others, const char *line)
146 const char *p, *end = strchr(line, '(') - 1;
147 while (isblank(*end)) {
153 for (p = end; isalnum(*p) || *p == '_'; p--) {
158 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
159 (unsigned)(end - p + 1), p);
162 static void strip_leading_whitespace(char **lines)
164 unsigned int i, min_span = -1U;
166 for (i = 0; lines[i]; i++) {
167 unsigned int span = strspn(lines[i], " \t");
168 /* All whitespace? Ignore */
175 for (i = 0; lines[i]; i++)
176 if (strlen(lines[i]) >= min_span)
177 lines[i] += min_span;
180 static bool looks_internal(char **lines)
183 bool last_ended = true; /* Did last line finish a statement? */
185 for (i = 0; lines[i]; i++) {
186 /* Skip leading whitespace. */
187 const char *line = lines[i] + strspn(lines[i], " \t");
188 unsigned len = strspn(line, IDENT_CHARS);
190 if (!line[0] || isblank(line[0]) || strstarts(line, "//"))
194 if (strstarts(line, "if") && len == 2)
196 if (strstarts(line, "for") && len == 3)
198 if (strstarts(line, "while") && len == 5)
200 if (strstarts(line, "do") && len == 2)
204 if (strstarts(line, "#include"))
207 if (last_ended && strchr(line, '(')) {
208 if (strstarts(line, "static"))
210 if (strends(line, ")"))
214 /* Single identifier then operator == inside function. */
215 if (last_ended && len
216 && ispunct(line[len+strspn(line+len, " ")]))
219 last_ended = (strends(line, "}")
220 || strends(line, ";")
221 || streq(line, "..."));
224 /* No idea... Say yes? */
228 /* Examples will often build on prior ones. Try combining them. */
229 static char **combine(const void *ctx, char **lines, char **prev)
231 unsigned int i, lines_total, prev_total, count;
237 /* If it looks internal, put prev at start. */
238 if (looks_internal(lines)) {
241 /* Try inserting in first elided position */
242 for (count = 0; lines[count]; count++) {
243 if (strcmp(lines[count], "...") == 0)
247 /* Try at start anyway? */
253 for (i = 0; lines[i]; i++);
256 for (i = 0; prev[i]; i++);
259 ret = talloc_array(ctx, char *, lines_total + prev_total + 1);
260 memcpy(ret, lines, count * sizeof(ret[0]));
261 memcpy(ret + count, prev, prev_total * sizeof(ret[0]));
262 memcpy(ret + count + prev_total, lines + count,
263 (lines_total - count + 1) * sizeof(ret[0]));
267 static char *mangle(struct manifest *m, char **lines)
269 char *ret, *use_funcs = NULL;
270 bool in_function = false, fake_function = false, has_main = false;
273 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
274 "#include <assert.h>\n"
276 "#include <errno.h>\n"
277 "#include <fcntl.h>\n"
278 "#include <limits.h>\n"
279 "#include <stdbool.h>\n"
280 "#include <stdint.h>\n"
281 "#include <stdio.h>\n"
282 "#include <stdlib.h>\n"
283 "#include <string.h>\n"
284 "#include <sys/stat.h>\n"
285 "#include <sys/types.h>\n"
286 "#include <unistd.h>\n");
287 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
288 "#include <ccan/%s/%s.h>\n",
289 m->basename, m->basename);
291 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
292 "extern int somefunc(void);\n"
293 "int somefunc(void) { return 0; }\n"
294 "extern char somestring[];\n"
295 "char somestring[] = \"hello world\";\n");
297 if (looks_internal(lines)) {
298 /* Wrap it all in main(). */
299 ret = start_main(ret);
300 fake_function = true;
305 /* Primitive, very primitive. */
306 for (i = 0; lines[i]; i++) {
307 /* } at start of line ends a function. */
309 if (lines[i][0] == '}')
312 /* Character at start of line, with ( and no ;
313 * == function start. Ignore comments. */
314 if (!isblank(lines[i][0])
315 && strchr(lines[i], '(')
316 && !strchr(lines[i], ';')
317 && !strstr(lines[i], "//")) {
319 if (strncmp(lines[i], "int main", 8) == 0)
321 if (strncmp(lines[i], "static", 6) == 0) {
322 use_funcs = add_func(use_funcs,
327 /* ... means elided code. */
328 if (strcmp(lines[i], "...") == 0) {
329 if (!in_function && !has_main
330 && looks_internal(lines + i + 1)) {
331 /* This implies we start a function here. */
332 ret = start_main(ret);
334 fake_function = true;
337 ret = talloc_asprintf_append(ret,
338 "/* ... removed */\n");
341 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
344 /* Need a main to link successfully. */
346 ret = talloc_asprintf_append(ret, "int main(void)\n{\n");
347 fake_function = true;
350 /* Get rid of unused warnings by printing addresses of static funcs. */
352 if (!fake_function) {
353 ret = talloc_asprintf_append(ret,
354 "int use_funcs(void);\n"
355 "int use_funcs(void) {\n");
356 fake_function = true;
358 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
362 ret = talloc_asprintf_append(ret, "return 0;\n"
367 static struct ccan_file *mangle_example(struct manifest *m,
368 struct ccan_file *example,
372 char *name, *contents;
376 name = maybe_temp_file(example, ".c", keep,
377 talloc_asprintf(m, "%s/mangled-%s",
378 m->dir, example->name));
379 f = new_ccan_file(example,
380 talloc_dirname(example, name),
381 talloc_basename(example, name));
382 talloc_steal(f, name);
384 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
388 contents = mangle(m, lines);
389 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
394 f->contents = talloc_steal(f, contents);
398 static void *build_examples(struct manifest *m, bool keep,
399 unsigned int *timeleft)
402 struct score *score = talloc(m, struct score);
403 struct ccan_file *mangle;
407 score->errors = NULL;
409 list_for_each(&m->examples, i, list) {
412 examples_compile.total_score++;
413 /* Simplify our dumb parsing. */
414 strip_leading_whitespace(get_ccan_file_lines(i));
415 ret = compile(score, m, i, keep);
417 prev = get_ccan_file_lines(i);
422 /* Try combining with previous (successful) example... */
424 char **new = combine(i, get_ccan_file_lines(i), prev);
427 mangle = mangle_example(m, i, new, keep);
428 ret = compile(score, m, mangle, keep);
436 /* Try standalone. */
438 mangle = mangle_example(m, i, get_ccan_file_lines(i), keep);
439 ret = compile(score, m, mangle, keep);
441 prev = get_ccan_file_lines(i);
449 score->errors = talloc_append_string(score->errors,
453 /* This didn't work, so not a candidate for combining. */
459 static unsigned int score_examples(struct manifest *m, void *check_result)
461 struct score *score = check_result;
465 static const char *describe(struct manifest *m, void *check_result)
467 struct score *score = check_result;
468 if (verbose >= 2 && score->errors)
469 return talloc_asprintf(m, "Compile errors building examples:\n"
470 "%s", score->errors);
474 struct ccanlint examples_compile = {
475 .key = "examples-compile",
476 .name = "Module examples compile",
477 .score = score_examples,
478 .check = build_examples,
479 .describe = describe,
483 REGISTER_TEST(examples_compile, &has_examples, NULL);