]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/examples_compile.c
7e1f14f3088ab55ab605e1a1262d8d7d8f58875d
[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 static char *mangle(struct manifest *m, struct ccan_file *example)
79 {
80         char **lines = get_ccan_file_lines(example);
81         char *ret;
82         bool in_function = false, fake_function = false;
83         unsigned int i;
84
85         ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
86                             "#include <assert.h>\n"
87                             "#include <err.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);
100
101         ret = talloc_asprintf_append(ret, "/* Useful dummmy functions. */\n"
102                                      "int somefunc(void);\n"
103                                      "int somefunc(void) { return 0; }\n");
104
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;
109                 in_function = true;
110         }
111
112         /* Primitive, very primitive. */
113         for (i = 0; lines[i]; i++) {
114                 /* } at start of line ends a function. */
115                 if (in_function) {
116                         if (lines[i][0] == '}')
117                                 in_function = false;
118                 } else {
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], ';'))
124                                 in_function = true;
125                 }
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) {
129                         if (!in_function
130                             && lines[i+1]
131                             && isblank(lines[i+1][0])) {
132                                 /* This implies we start a function here. */
133                                 ret = start_main(ret);
134                                 fake_function = true;
135                                 in_function = true;
136                         }
137                         ret = talloc_asprintf_append(ret,
138                                                      "/* ... removed */\n");
139                         continue;
140                 }
141                 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
142         }
143
144         if (fake_function)
145                 ret = talloc_asprintf_append(ret, "return 0;\n"
146                                              "}\n");
147         return ret;
148 }
149
150 static struct ccan_file *mangle_example(struct manifest *m,
151                                         struct ccan_file *example, bool keep)
152 {
153         char *name, *contents;
154         int fd;
155         struct ccan_file *f;
156
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);
164
165         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
166         if (fd < 0)
167                 return NULL;
168
169         contents = mangle(m, example);
170         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
171                 close(fd);
172                 return NULL;
173         }
174         close(fd);
175         return f;
176 }
177
178 static void *build_examples(struct manifest *m, bool keep,
179                             unsigned int *timeleft)
180 {
181         struct ccan_file *i;
182         struct score *score = talloc(m, struct score);
183
184         score->score = 0;
185         score->errors = NULL;
186
187         list_for_each(&m->examples, i, list) {
188                 char *ret;
189
190                 examples_compile.total_score++;
191                 ret = compile(score, m, i, keep);
192                 if (!ret)
193                         score->score++;
194                 else {
195                         struct ccan_file *mangle = mangle_example(m, i, keep);
196
197                         talloc_free(ret);
198                         ret = compile(score, m, mangle, keep);
199                         if (!ret)
200                                 score->score++;
201                         else {
202                                 if (!score->errors)
203                                         score->errors = ret;
204                                 else {
205                                         score->errors
206                                         = talloc_append_string(score->errors,
207                                                                ret);
208                                         talloc_free(ret);
209                                 }
210                         }
211                 }
212         }
213         return score;
214 }
215
216 static unsigned int score_examples(struct manifest *m, void *check_result)
217 {
218         struct score *score = check_result;
219         return score->score;
220 }
221
222 static const char *describe(struct manifest *m, void *check_result)
223 {
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);
228         return NULL;
229 }
230
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,
237         .can_run = can_run,
238 };
239
240 REGISTER_TEST(examples_compile, &has_examples, NULL);