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 /* This CCAN module. */
25 list = talloc_asprintf(m, " %s.o", m->dir);
27 /* Other ccan modules we depend on. */
28 list_for_each(&m->dep_dirs, i, list) {
30 list = talloc_asprintf_append(list, " %s", i->compiled);
36 static char *lib_list(const struct manifest *m)
39 char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
40 char *ret = talloc_strdup(m, "");
42 for (i = 0; i < num; i++)
43 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
47 static char *compile(const void *ctx,
49 struct ccan_file *file,
54 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
55 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
56 obj_list(m), "", lib_list(m), file->compiled);
58 talloc_free(file->compiled);
69 static char *start_main(char *ret)
71 return talloc_asprintf_append(ret,
72 "/* Fake function wrapper inserted */\n"
73 "int main(int argc, char *argv[])\n"
77 static char *mangle(struct manifest *m, struct ccan_file *example)
79 char **lines = get_ccan_file_lines(example);
81 bool in_function = false, fake_function = false;
84 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
85 "#include <assert.h>\n"
87 "#include <fcntl.h>\n"
88 "#include <stdbool.h>\n"
89 "#include <stdint.h>\n"
90 "#include <stdio.h>\n"
91 "#include <stdlib.h>\n"
92 "#include <string.h>\n"
93 "#include <sys/stat.h>\n"
94 "#include <sys/types.h>\n"
95 "#include <unistd.h>\n");
96 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
97 "#include <ccan/%s/%s.h>\n",
98 m->basename, m->basename);
100 /* Starts indented? Wrap it in a main() function. */
101 if (lines[0] && isblank(lines[0][0])) {
102 ret = start_main(ret);
103 fake_function = true;
107 /* Primitive, very primitive. */
108 for (i = 0; lines[i]; i++) {
109 /* } at start of line ends a function. */
111 if (lines[i][0] == '}')
114 /* Character at start of line, with ( and no ;
115 * == function start. */
116 if (!isblank(lines[i][0])
117 && strchr(lines[i], '(')
118 && !strchr(lines[i], ';'))
121 /* ... means elided code. If followed by spaced line, means
122 * next part is supposed to be inside a function. */
123 if (strcmp(lines[i], "...") == 0) {
126 && isblank(lines[i+1][0])) {
127 /* This implies we start a function here. */
128 ret = start_main(ret);
129 fake_function = true;
132 ret = talloc_asprintf_append(ret,
133 "/* ... removed */\n");
136 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
140 ret = talloc_asprintf_append(ret, "return 0;\n"
145 static struct ccan_file *mangle_example(struct manifest *m,
146 struct ccan_file *example, bool keep)
148 char *name, *contents;
152 name = maybe_temp_file(example, ".c", keep,
153 talloc_asprintf(m, "%s/mangled-%s",
154 m->dir, example->name));
155 f = new_ccan_file(example,
156 talloc_dirname(example, name),
157 talloc_basename(example, name));
158 talloc_steal(f, name);
160 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
164 contents = mangle(m, example);
165 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
173 static void *build_examples(struct manifest *m, bool keep,
174 unsigned int *timeleft)
177 struct score *score = talloc(m, struct score);
180 score->errors = NULL;
182 list_for_each(&m->examples, i, list) {
185 examples_compile.total_score++;
186 ret = compile(score, m, i, keep);
190 struct ccan_file *mangle = mangle_example(m, i, keep);
193 ret = compile(score, m, mangle, keep);
201 = talloc_append_string(score->errors,
211 static unsigned int score_examples(struct manifest *m, void *check_result)
213 struct score *score = check_result;
217 static const char *describe(struct manifest *m, void *check_result)
219 struct score *score = check_result;
220 if (verbose >= 2 && score->errors)
221 return talloc_asprintf(m, "Compile errors building examples:\n"
222 "%s", score->errors);
226 struct ccanlint examples_compile = {
227 .key = "examples-compile",
228 .name = "Module examples compile",
229 .score = score_examples,
230 .check = build_examples,
231 .describe = describe,
235 REGISTER_TEST(examples_compile, &has_examples, NULL);