1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
12 static const char *can_run(struct manifest *m)
15 return "Safe mode enabled";
19 static char *obj_list(const struct manifest *m)
24 /* Object files for this module. */
25 list_for_each(&m->c_files, i, list)
26 list = talloc_asprintf_append(list, " %s", i->compiled);
28 /* Other ccan modules we depend on. */
29 list_for_each(&m->dep_dirs, i, list) {
31 list = talloc_asprintf_append(list, " %s", i->compiled);
37 static char *lib_list(const struct manifest *m)
40 char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
41 char *ret = talloc_strdup(m, "");
43 for (i = 0; i < num; i++)
44 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
48 static char *compile(const void *ctx,
50 struct ccan_file *file,
55 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
56 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
57 obj_list(m), "", lib_list(m), file->compiled);
59 talloc_free(file->compiled);
70 static char *start_main(char *ret)
72 return talloc_asprintf_append(ret,
73 "/* Fake function wrapper inserted */\n"
74 "int main(int argc, char *argv[])\n"
78 static char *mangle(struct manifest *m, struct ccan_file *example)
80 char **lines = get_ccan_file_lines(example);
82 bool in_function = false, fake_function = false;
85 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
86 "#include <assert.h>\n"
88 "#include <fcntl.h>\n"
89 "#include <stdbool.h>\n"
90 "#include <stdint.h>\n"
91 "#include <stdio.h>\n"
92 "#include <stdlib.h>\n"
93 "#include <string.h>\n"
94 "#include <sys/stat.h>\n"
95 "#include <sys/types.h>\n"
96 "#include <unistd.h>\n");
97 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
98 "#include <ccan/%s/%s.h>\n",
99 m->basename, m->basename);
101 ret = talloc_asprintf_append(ret, "/* Useful dummmy functions. */\n"
102 "int somefunc(void);\n"
103 "int somefunc(void) { return 0; }\n");
105 /* Starts indented? Wrap it in a main() function. */
106 if (lines[0] && isblank(lines[0][0])) {
107 ret = start_main(ret);
108 fake_function = true;
112 /* Primitive, very primitive. */
113 for (i = 0; lines[i]; i++) {
114 /* } at start of line ends a function. */
116 if (lines[i][0] == '}')
119 /* Character at start of line, with ( and no ;
120 * == function start. */
121 if (!isblank(lines[i][0])
122 && strchr(lines[i], '(')
123 && !strchr(lines[i], ';'))
126 /* ... means elided code. If followed by spaced line, means
127 * next part is supposed to be inside a function. */
128 if (strcmp(lines[i], "...") == 0) {
131 && isblank(lines[i+1][0])) {
132 /* This implies we start a function here. */
133 ret = start_main(ret);
134 fake_function = true;
137 ret = talloc_asprintf_append(ret,
138 "/* ... removed */\n");
141 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
145 ret = talloc_asprintf_append(ret, "return 0;\n"
150 static struct ccan_file *mangle_example(struct manifest *m,
151 struct ccan_file *example, bool keep)
153 char *name, *contents;
157 name = maybe_temp_file(example, ".c", keep,
158 talloc_asprintf(m, "%s/mangled-%s",
159 m->dir, example->name));
160 f = new_ccan_file(example,
161 talloc_dirname(example, name),
162 talloc_basename(example, name));
163 talloc_steal(f, name);
165 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
169 contents = mangle(m, example);
170 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
178 static void *build_examples(struct manifest *m, bool keep,
179 unsigned int *timeleft)
182 struct score *score = talloc(m, struct score);
185 score->errors = NULL;
187 list_for_each(&m->examples, i, list) {
190 examples_compile.total_score++;
191 ret = compile(score, m, i, keep);
195 struct ccan_file *mangle = mangle_example(m, i, keep);
198 ret = compile(score, m, mangle, keep);
206 = talloc_append_string(score->errors,
216 static unsigned int score_examples(struct manifest *m, void *check_result)
218 struct score *score = check_result;
222 static const char *describe(struct manifest *m, void *check_result)
224 struct score *score = check_result;
225 if (verbose >= 2 && score->errors)
226 return talloc_asprintf(m, "Compile errors building examples:\n"
227 "%s", score->errors);
231 struct ccanlint examples_compile = {
232 .key = "examples-compile",
233 .name = "Module examples compile",
234 .score = score_examples,
235 .check = build_examples,
236 .describe = describe,
240 REGISTER_TEST(examples_compile, &has_examples, NULL);