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 /* We only handle simple function definitions here. */
79 static char *add_func(char *others, const char *line)
81 const char *p, *end = strchr(line, '(') - 1;
82 while (isblank(*end)) {
88 for (p = end; isalnum(*p) || *p == '_'; p--) {
93 return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
97 static bool looks_internal(const char *p)
99 return (strncmp(p, "#", 1) != 0
100 && strncmp(p, "static", 6) != 0
101 && strncmp(p, "struct", 6) != 0
102 && strncmp(p, "union", 5) != 0);
105 static void strip_leading_whitespace(char **lines, unsigned prefix_len)
109 for (i = 0; lines[i]; i++)
110 lines[i] += prefix_len;
113 static char *mangle(struct manifest *m, struct ccan_file *example)
115 char **lines = get_ccan_file_lines(example);
116 char *ret, *use_funcs = NULL;
117 bool in_function = false, fake_function = false, has_main = false;
120 ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
121 "#include <assert.h>\n"
123 "#include <fcntl.h>\n"
124 "#include <stdbool.h>\n"
125 "#include <stdint.h>\n"
126 "#include <stdio.h>\n"
127 "#include <stdlib.h>\n"
128 "#include <string.h>\n"
129 "#include <sys/stat.h>\n"
130 "#include <sys/types.h>\n"
131 "#include <unistd.h>\n");
132 ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
133 "#include <ccan/%s/%s.h>\n",
134 m->basename, m->basename);
136 ret = talloc_asprintf_append(ret, "/* Useful dummmy functions. */\n"
137 "int somefunc(void);\n"
138 "int somefunc(void) { return 0; }\n");
140 /* Starts indented? */
141 if (lines[0] && isblank(lines[0][0])) {
142 unsigned prefix = strspn(lines[0], " \t");
143 if (looks_internal(lines[0] + prefix)) {
144 /* Wrap it all in main(). */
145 ret = start_main(ret);
146 fake_function = true;
150 strip_leading_whitespace(lines, prefix);
153 /* Primitive, very primitive. */
154 for (i = 0; lines[i]; i++) {
155 /* } at start of line ends a function. */
157 if (lines[i][0] == '}')
160 /* Character at start of line, with ( and no ;
161 * == function start. */
162 if (!isblank(lines[i][0])
163 && strchr(lines[i], '(')
164 && !strchr(lines[i], ';')) {
166 if (strncmp(lines[i], "int main", 8) == 0)
168 if (strncmp(lines[i], "static", 6) == 0) {
169 use_funcs = add_func(use_funcs,
174 /* ... means elided code. If followed by spaced line, means
175 * next part is supposed to be inside a function. */
176 if (strcmp(lines[i], "...") == 0) {
179 && isblank(lines[i+1][0])) {
180 /* This implies we start a function here. */
181 ret = start_main(ret);
182 fake_function = true;
185 ret = talloc_asprintf_append(ret,
186 "/* ... removed */\n");
189 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
192 /* Need a main to link successfully. */
194 ret = talloc_asprintf_append(ret, "int main(void)\n{\n");
195 fake_function = true;
198 /* Get rid of unused warnings by printing addresses of static funcs. */
200 if (!fake_function) {
201 ret = talloc_asprintf_append(ret,
202 "int use_funcs(void);\n"
203 "int use_funcs(void) {\n");
204 fake_function = true;
206 ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
210 ret = talloc_asprintf_append(ret, "return 0;\n"
215 static struct ccan_file *mangle_example(struct manifest *m,
216 struct ccan_file *example, bool keep)
218 char *name, *contents;
222 name = maybe_temp_file(example, ".c", keep,
223 talloc_asprintf(m, "%s/mangled-%s",
224 m->dir, example->name));
225 f = new_ccan_file(example,
226 talloc_dirname(example, name),
227 talloc_basename(example, name));
228 talloc_steal(f, name);
230 fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
234 contents = mangle(m, example);
235 if (write(fd, contents, strlen(contents)) != strlen(contents)) {
243 static void *build_examples(struct manifest *m, bool keep,
244 unsigned int *timeleft)
247 struct score *score = talloc(m, struct score);
250 score->errors = NULL;
252 list_for_each(&m->examples, i, list) {
255 examples_compile.total_score++;
256 ret = compile(score, m, i, keep);
260 struct ccan_file *mangle = mangle_example(m, i, keep);
263 ret = compile(score, m, mangle, keep);
271 = talloc_append_string(score->errors,
281 static unsigned int score_examples(struct manifest *m, void *check_result)
283 struct score *score = check_result;
287 static const char *describe(struct manifest *m, void *check_result)
289 struct score *score = check_result;
290 if (verbose >= 2 && score->errors)
291 return talloc_asprintf(m, "Compile errors building examples:\n"
292 "%s", score->errors);
296 struct ccanlint examples_compile = {
297 .key = "examples-compile",
298 .name = "Module examples compile",
299 .score = score_examples,
300 .check = build_examples,
301 .describe = describe,
305 REGISTER_TEST(examples_compile, &has_examples, NULL);