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)
40 /* Not if there's no object file for that module */
41 if (!expect_obj_file(talloc_asprintf(list, "%s/ccan/%s", ccan_dir,mod)))
44 obj = talloc_asprintf(list, "%s/ccan/%s.o", ccan_dir, mod);
46 /* Not anyone we've already included. */
47 if (strstr(list, obj))
50 list = talloc_asprintf_append(list, " %s", obj);
52 /* Get that modules depends as well... */
54 deps = get_deps(m, talloc_asprintf(list, "%s/ccan/%s", ccan_dir, mod),
57 for (i = 0; deps[i]; i++) {
58 if (strstarts(deps[i], "ccan/"))
59 list = add_dep(m, list, deps[i] + strlen("ccan/"));
64 static char *obj_list(const struct manifest *m, struct ccan_file *f)
66 char *list = talloc_strdup(m, "");
70 /* Object files for this module. */
71 list_for_each(&m->c_files, i, list)
72 list = talloc_asprintf_append(list, " %s", i->compiled);
74 /* Other ccan modules we depend on. */
75 list_for_each(&m->dep_dirs, i, list) {
77 list = talloc_asprintf_append(list, " %s", i->compiled);
80 /* Other modules implied by includes. */
81 for (lines = get_ccan_file_lines(f); *lines; lines++) {
82 unsigned preflen = strspn(*lines, " \t");
83 if (strstarts(*lines + preflen, "#include <ccan/")) {
87 mod = *lines + preflen + strlen("#include <ccan/");
88 modlen = strcspn(mod, "/");
89 mod = talloc_strndup(f, mod, modlen);
91 if (streq(m->basename, mod))
93 list = add_dep(m, list, mod);
100 static char *lib_list(const struct manifest *m)
103 char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
104 char *ret = talloc_strdup(m, "");
106 for (i = 0; i < num; i++)
107 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
111 static char *compile(const void *ctx,
113 struct ccan_file *file,
118 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
119 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
121 "", lib_list(m), file->compiled);
123 talloc_free(file->compiled);
134 static char *start_main(char *ret)
136 return talloc_asprintf_append(ret,
137 "/* Fake function wrapper inserted */\n"
138 "int main(int argc, char *argv[])\n"
142 /* We only handle simple function definitions here. */
143 static char *add_func(char *others, const char *line)
145 const char *p, *end = strchr(line, '(') - 1;
146 while (isblank(*end)) {
152 for (p = end; isalnum(*p) || *p == '_'; p--) {
157 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
161 static void strip_leading_whitespace(char **lines)
163 unsigned int i, min_span = -1U;
165 for (i = 0; lines[i]; i++) {
166 unsigned int span = strspn(lines[i], " \t");
167 /* All whitespace? Ignore */
174 for (i = 0; lines[i]; i++)
175 if (strlen(lines[i]) >= min_span)
176 lines[i] += min_span;
179 static bool looks_internal(char **lines)
183 for (i = 0; lines[i]; i++) {
184 unsigned len = strspn(lines[i], IDENT_CHARS);
187 if (strstarts(lines[i], "if") && len == 2)
189 if (strstarts(lines[i], "for") && len == 3)
191 if (strstarts(lines[i], "while") && len == 5)
193 if (strstarts(lines[i], "do") && len == 2)
197 if (strchr(lines[i], '(')) {
198 if (strstarts(lines[i], "static"))
200 if (strends(lines[i], ")"))
205 /* No idea... Say no? */
209 /* Examples will often build on prior ones. Try combining them. */
210 static char **combine(char **lines, char **prev)
212 unsigned int i, lines_total, prev_total, count;
218 strip_leading_whitespace(lines);
220 /* If it looks internal, put prev at start. */
221 if (looks_internal(lines)) {
224 /* Try inserting in first elided position */
225 for (count = 0; lines[count]; count++) {
226 if (strcmp(lines[count], "...") == 0)
230 /* Try at start anyway? */
236 for (i = 0; lines[i]; i++);
239 for (i = 0; prev[i]; i++);
242 ret = talloc_array(lines, char *, lines_total + prev_total + 1);
243 memcpy(ret, lines, count * sizeof(ret[0]));
244 memcpy(ret + count, prev, prev_total * sizeof(ret[0]));
245 memcpy(ret + count + prev_total, lines + count,
246 (lines_total - count + 1) * sizeof(ret[0]));
250 static char *mangle(struct manifest *m, char **lines)
252 char *ret, *use_funcs = NULL;
253 bool in_function = false, fake_function = false, has_main = false;
256 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
257 "#include <assert.h>\n"
259 "#include <errno.h>\n"
260 "#include <fcntl.h>\n"
261 "#include <limits.h>\n"
262 "#include <stdbool.h>\n"
263 "#include <stdint.h>\n"
264 "#include <stdio.h>\n"
265 "#include <stdlib.h>\n"
266 "#include <string.h>\n"
267 "#include <sys/stat.h>\n"
268 "#include <sys/types.h>\n"
269 "#include <unistd.h>\n");
270 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
271 "#include <ccan/%s/%s.h>\n",
272 m->basename, m->basename);
274 ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
275 "int somefunc(void);\n"
276 "int somefunc(void) { return 0; }\n");
278 strip_leading_whitespace(lines);
279 if (looks_internal(lines)) {
280 /* Wrap it all in main(). */
281 ret = start_main(ret);
282 fake_function = true;
287 /* Primitive, very primitive. */
288 for (i = 0; lines[i]; i++) {
289 /* } at start of line ends a function. */
291 if (lines[i][0] == '}')
294 /* Character at start of line, with ( and no ;
295 * == function start. Ignore comments. */
296 if (!isblank(lines[i][0])
297 && strchr(lines[i], '(')
298 && !strchr(lines[i], ';')
299 && !strstr(lines[i], "//")) {
301 if (strncmp(lines[i], "int main", 8) == 0)
303 if (strncmp(lines[i], "static", 6) == 0) {
304 use_funcs = add_func(use_funcs,
309 /* ... means elided code. If followed by spaced line, means
310 * next part is supposed to be inside a function. */
311 if (strcmp(lines[i], "...") == 0) {
314 && isblank(lines[i+1][0])) {
315 /* This implies we start a function here. */
316 ret = start_main(ret);
318 fake_function = true;
321 ret = talloc_asprintf_append(ret,
322 "/* ... removed */\n");
325 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
328 /* Need a main to link successfully. */
330 ret = talloc_asprintf_append(ret, "int main(void)\n{\n");
331 fake_function = true;
334 /* Get rid of unused warnings by printing addresses of static funcs. */
336 if (!fake_function) {
337 ret = talloc_asprintf_append(ret,
338 "int use_funcs(void);\n"
339 "int use_funcs(void) {\n");
340 fake_function = true;
342 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
346 ret = talloc_asprintf_append(ret, "return 0;\n"
351 static struct ccan_file *mangle_example(struct manifest *m,
352 struct ccan_file *example,
356 char *name, *contents;
360 name = maybe_temp_file(example, ".c", keep,
361 talloc_asprintf(m, "%s/mangled-%s",
362 m->dir, example->name));
363 f = new_ccan_file(example,
364 talloc_dirname(example, name),
365 talloc_basename(example, name));
366 talloc_steal(f, name);
368 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
372 contents = mangle(m, lines);
373 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
378 f->contents = talloc_steal(f, contents);
382 static void *build_examples(struct manifest *m, bool keep,
383 unsigned int *timeleft)
386 struct score *score = talloc(m, struct score);
387 struct ccan_file *mangle;
391 score->errors = NULL;
393 list_for_each(&m->examples, i, list) {
396 examples_compile.total_score++;
397 ret = compile(score, m, i, keep);
399 prev = get_ccan_file_lines(i);
405 mangle = mangle_example(m, i, get_ccan_file_lines(i), keep);
406 ret = compile(score, m, mangle, keep);
408 prev = get_ccan_file_lines(i);
413 /* Try combining with previous (successful) example... */
415 prev = combine(get_ccan_file_lines(i), prev);
418 /* We're going to replace this failure. */
420 unlink(mangle->fullname);
423 mangle = mangle_example(m, i, prev, keep);
424 ret = compile(score, m, mangle, keep);
434 score->errors = talloc_append_string(score->errors,
438 /* This didn't work, so not a candidate for combining. */
445 static unsigned int score_examples(struct manifest *m, void *check_result)
447 struct score *score = check_result;
451 static const char *describe(struct manifest *m, void *check_result)
453 struct score *score = check_result;
454 if (verbose >= 2 && score->errors)
455 return talloc_asprintf(m, "Compile errors building examples:\n"
456 "%s", score->errors);
460 struct ccanlint examples_compile = {
461 .key = "examples-compile",
462 .name = "Module examples compile",
463 .score = score_examples,
464 .check = build_examples,
465 .describe = describe,
469 REGISTER_TEST(examples_compile, &has_examples, NULL);