ccanlint: more heuristics for compiling 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         /* Object files for this module. */
25         list_for_each(&m->c_files, i, list)
26                 list = talloc_asprintf_append(list, " %s", i->compiled);
27
28         /* Other ccan modules we depend on. */
29         list_for_each(&m->dep_dirs, i, list) {
30                 if (i->compiled)
31                         list = talloc_asprintf_append(list, " %s", i->compiled);
32         }
33
34         return list;
35 }
36
37 static char *lib_list(const struct manifest *m)
38 {
39         unsigned int i, num;
40         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
41         char *ret = talloc_strdup(m, "");
42
43         for (i = 0; i < num; i++)
44                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
45         return ret;
46 }
47
48 static char *compile(const void *ctx,
49                      struct manifest *m,
50                      struct ccan_file *file,
51                      bool keep)
52 {
53         char *errmsg;
54
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);
58         if (errmsg) {
59                 talloc_free(file->compiled);
60                 return errmsg;
61         }
62         return NULL;
63 }
64
65 struct score {
66         unsigned int score;
67         char *errors;
68 };
69
70 static char *start_main(char *ret)
71 {
72         return talloc_asprintf_append(ret,
73                                       "/* Fake function wrapper inserted */\n"
74                                       "int main(int argc, char *argv[])\n"
75                                       "{\n");
76 }
77
78 /* We only handle simple function definitions here. */
79 static char *add_func(char *others, const char *line)
80 {
81         const char *p, *end = strchr(line, '(') - 1;
82         while (isblank(*end)) {
83                 end--;
84                 if (end == line)
85                         return others;
86         }
87
88         for (p = end; isalnum(*p) || *p == '_'; p--) {
89                 if (p == line)
90                         return others;
91         }
92
93         return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
94                                       end - p + 1, p);
95 }
96
97 static bool looks_internal(const char *p)
98 {
99         return (strncmp(p, "#", 1) != 0
100                 && strncmp(p, "static", 6) != 0
101                 && strncmp(p, "struct", 6) != 0
102                 && strncmp(p, "union", 5) != 0);
103 }
104
105 static void strip_leading_whitespace(char **lines, unsigned prefix_len)
106 {
107         unsigned int i;
108
109         for (i = 0; lines[i]; i++)
110                 lines[i] += prefix_len;
111 }
112
113 static char *mangle(struct manifest *m, struct ccan_file *example)
114 {
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;
118         unsigned int i;
119
120         ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
121                             "#include <assert.h>\n"
122                             "#include <err.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);
135
136         ret = talloc_asprintf_append(ret, "/* Useful dummmy functions. */\n"
137                                      "int somefunc(void);\n"
138                                      "int somefunc(void) { return 0; }\n");
139
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;
147                         in_function = true;
148                         has_main = true;
149                 } else
150                         strip_leading_whitespace(lines, prefix);
151         }
152
153         /* Primitive, very primitive. */
154         for (i = 0; lines[i]; i++) {
155                 /* } at start of line ends a function. */
156                 if (in_function) {
157                         if (lines[i][0] == '}')
158                                 in_function = false;
159                 } else {
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], ';')) {
165                                 in_function = true;
166                                 if (strncmp(lines[i], "int main", 8) == 0)
167                                         has_main = true;
168                                 if (strncmp(lines[i], "static", 6) == 0) {
169                                         use_funcs = add_func(use_funcs,
170                                                              lines[i]);
171                                 }
172                         }
173                 }
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) {
177                         if (!in_function
178                             && lines[i+1]
179                             && isblank(lines[i+1][0])) {
180                                 /* This implies we start a function here. */
181                                 ret = start_main(ret);
182                                 fake_function = true;
183                                 in_function = true;
184                         }
185                         ret = talloc_asprintf_append(ret,
186                                                      "/* ... removed */\n");
187                         continue;
188                 }
189                 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
190         }
191
192         /* Need a main to link successfully. */
193         if (!has_main) {
194                 ret = talloc_asprintf_append(ret, "int main(void)\n{\n");
195                 fake_function = true;
196         }
197
198         /* Get rid of unused warnings by printing addresses of static funcs. */
199         if (use_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;
205                 }
206                 ret = talloc_asprintf_append(ret, "     %s\n", use_funcs);
207         }
208
209         if (fake_function)
210                 ret = talloc_asprintf_append(ret, "return 0;\n"
211                                              "}\n");
212         return ret;
213 }
214
215 static struct ccan_file *mangle_example(struct manifest *m,
216                                         struct ccan_file *example, bool keep)
217 {
218         char *name, *contents;
219         int fd;
220         struct ccan_file *f;
221
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);
229
230         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
231         if (fd < 0)
232                 return NULL;
233
234         contents = mangle(m, example);
235         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
236                 close(fd);
237                 return NULL;
238         }
239         close(fd);
240         return f;
241 }
242
243 static void *build_examples(struct manifest *m, bool keep,
244                             unsigned int *timeleft)
245 {
246         struct ccan_file *i;
247         struct score *score = talloc(m, struct score);
248
249         score->score = 0;
250         score->errors = NULL;
251
252         list_for_each(&m->examples, i, list) {
253                 char *ret;
254
255                 examples_compile.total_score++;
256                 ret = compile(score, m, i, keep);
257                 if (!ret)
258                         score->score++;
259                 else {
260                         struct ccan_file *mangle = mangle_example(m, i, keep);
261
262                         talloc_free(ret);
263                         ret = compile(score, m, mangle, keep);
264                         if (!ret)
265                                 score->score++;
266                         else {
267                                 if (!score->errors)
268                                         score->errors = ret;
269                                 else {
270                                         score->errors
271                                         = talloc_append_string(score->errors,
272                                                                ret);
273                                         talloc_free(ret);
274                                 }
275                         }
276                 }
277         }
278         return score;
279 }
280
281 static unsigned int score_examples(struct manifest *m, void *check_result)
282 {
283         struct score *score = check_result;
284         return score->score;
285 }
286
287 static const char *describe(struct manifest *m, void *check_result)
288 {
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);
293         return NULL;
294 }
295
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,
302         .can_run = can_run,
303 };
304
305 REGISTER_TEST(examples_compile, &has_examples, NULL);