ccanlint: actually try to compile examples.
[ccan] / tools / ccanlint / tests / examples_compile.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <ctype.h>
11
12 static const char *can_run(struct manifest *m)
13 {
14         if (safe_mode)
15                 return "Safe mode enabled";
16         return NULL;
17 }
18
19 static char *obj_list(const struct manifest *m)
20 {
21         char *list;
22         struct ccan_file *i;
23
24         /* This CCAN module. */
25         list = talloc_asprintf(m, " %s.o", m->dir);
26
27         /* Other ccan modules we depend on. */
28         list_for_each(&m->dep_dirs, i, list) {
29                 if (i->compiled)
30                         list = talloc_asprintf_append(list, " %s", i->compiled);
31         }
32
33         return list;
34 }
35
36 static char *lib_list(const struct manifest *m)
37 {
38         unsigned int i, num;
39         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
40         char *ret = talloc_strdup(m, "");
41
42         for (i = 0; i < num; i++)
43                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
44         return ret;
45 }
46
47 static char *compile(const void *ctx,
48                      struct manifest *m,
49                      struct ccan_file *file,
50                      bool keep)
51 {
52         char *errmsg;
53
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);
57         if (errmsg) {
58                 talloc_free(file->compiled);
59                 return errmsg;
60         }
61         return NULL;
62 }
63
64 struct score {
65         unsigned int score;
66         char *errors;
67 };
68
69 static char *start_main(char *ret)
70 {
71         return talloc_asprintf_append(ret,
72                                       "/* Fake function wrapper inserted */\n"
73                                       "int main(int argc, char *argv[])\n"
74                                       "{\n");
75 }
76
77 static char *mangle(struct manifest *m, struct ccan_file *example)
78 {
79         char **lines = get_ccan_file_lines(example);
80         char *ret;
81         bool in_function = false, fake_function = false;
82         unsigned int i;
83
84         ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
85                             "#include <assert.h>\n"
86                             "#include <err.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);
99
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;
104                 in_function = true;
105         }
106
107         /* Primitive, very primitive. */
108         for (i = 0; lines[i]; i++) {
109                 /* } at start of line ends a function. */
110                 if (in_function) {
111                         if (lines[i][0] == '}')
112                                 in_function = false;
113                 } else {
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], ';'))
119                                 in_function = true;
120                 }
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) {
124                         if (!in_function
125                             && lines[i+1]
126                             && isblank(lines[i+1][0])) {
127                                 /* This implies we start a function here. */
128                                 ret = start_main(ret);
129                                 fake_function = true;
130                                 in_function = true;
131                         }
132                         ret = talloc_asprintf_append(ret,
133                                                      "/* ... removed */\n");
134                         continue;
135                 }
136                 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
137         }
138
139         if (fake_function)
140                 ret = talloc_asprintf_append(ret, "return 0;\n"
141                                              "}\n");
142         return ret;
143 }
144
145 static struct ccan_file *mangle_example(struct manifest *m,
146                                         struct ccan_file *example, bool keep)
147 {
148         char *name, *contents;
149         int fd;
150         struct ccan_file *f;
151
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);
159
160         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
161         if (fd < 0)
162                 return NULL;
163
164         contents = mangle(m, example);
165         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
166                 close(fd);
167                 return NULL;
168         }
169         close(fd);
170         return f;
171 }
172
173 static void *build_examples(struct manifest *m, bool keep,
174                             unsigned int *timeleft)
175 {
176         struct ccan_file *i;
177         struct score *score = talloc(m, struct score);
178
179         score->score = 0;
180         score->errors = NULL;
181
182         list_for_each(&m->examples, i, list) {
183                 char *ret;
184
185                 examples_compile.total_score++;
186                 ret = compile(score, m, i, keep);
187                 if (!ret)
188                         score->score++;
189                 else {
190                         struct ccan_file *mangle = mangle_example(m, i, keep);
191
192                         talloc_free(ret);
193                         ret = compile(score, m, mangle, keep);
194                         if (!ret)
195                                 score->score++;
196                         else {
197                                 if (!score->errors)
198                                         score->errors = ret;
199                                 else {
200                                         score->errors
201                                         = talloc_append_string(score->errors,
202                                                                ret);
203                                         talloc_free(ret);
204                                 }
205                         }
206                 }
207         }
208         return score;
209 }
210
211 static unsigned int score_examples(struct manifest *m, void *check_result)
212 {
213         struct score *score = check_result;
214         return score->score;
215 }
216
217 static const char *describe(struct manifest *m, void *check_result)
218 {
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);
223         return NULL;
224 }
225
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,
232         .can_run = can_run,
233 };
234
235 REGISTER_TEST(examples_compile, &has_examples, NULL);