ccanlint: fix abort when -vv used and examples don't compile.
[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 <ccan/str/str.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <ctype.h>
12 #include <assert.h>
13
14 static const char *can_run(struct manifest *m)
15 {
16         if (safe_mode)
17                 return "Safe mode enabled";
18         if (list_empty(&m->examples))
19                 return "No examples to compile";
20         return NULL;
21 }
22
23 /* FIXME: We should build if it doesn't exist... */
24 static bool expect_obj_file(const char *dir)
25 {
26         struct manifest *dep_man;
27         bool has_c_files;
28
29         dep_man = get_manifest(dir, dir);
30
31         /* If it has C files, we expect an object file built from them. */
32         has_c_files = !list_empty(&dep_man->c_files);
33         talloc_free(dep_man);
34         return has_c_files;
35 }
36
37 static char *add_dep(const struct manifest *m, char *list, const char *mod)
38 {
39         char **deps, *obj;
40         unsigned int i;
41
42         /* Not ourselves. */
43         if (streq(m->basename, mod))
44                 return list;
45
46         /* Not if there's no object file for that module */
47         if (!expect_obj_file(talloc_asprintf(list, "%s/ccan/%s", ccan_dir,mod)))
48                 return list;
49
50         obj = talloc_asprintf(list, "%s/ccan/%s.o", ccan_dir, mod);
51
52         /* Not anyone we've already included. */
53         if (strstr(list, obj))
54                 return list;
55
56         list = talloc_asprintf_append(list, " %s", obj);
57
58         /* Get that modules depends as well... */
59         assert(!safe_mode);
60         deps = get_deps(m, talloc_asprintf(list, "%s/ccan/%s", ccan_dir, mod),
61                         false, NULL);
62
63         for (i = 0; deps[i]; i++) {
64                 if (strstarts(deps[i], "ccan/"))
65                         list = add_dep(m, list, deps[i] + strlen("ccan/"));
66         }
67         return list;
68 }
69
70 static char *obj_list(const struct manifest *m, struct ccan_file *f)
71 {
72         char *list = talloc_strdup(m, "");
73         struct ccan_file *i;
74         char **lines;
75
76         /* Object files for this module. */
77         list_for_each(&m->c_files, i, list)
78                 list = talloc_asprintf_append(list, " %s", i->compiled);
79
80         /* Other ccan modules we depend on. */
81         list_for_each(&m->dep_dirs, i, list) {
82                 if (i->compiled)
83                         list = talloc_asprintf_append(list, " %s", i->compiled);
84         }
85
86         /* Other modules implied by includes. */
87         for (lines = get_ccan_file_lines(f); *lines; lines++) {
88                 unsigned preflen = strspn(*lines, " \t");
89                 if (strstarts(*lines + preflen, "#include <ccan/")) {
90                         const char *mod;
91                         unsigned modlen;
92
93                         mod = *lines + preflen + strlen("#include <ccan/");
94                         modlen = strcspn(mod, "/");
95                         mod = talloc_strndup(f, mod, modlen);
96                         list = add_dep(m, list, mod);
97                 }
98         }
99
100         return list;
101 }
102
103 static char *lib_list(const struct manifest *m)
104 {
105         unsigned int i, num;
106         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
107         char *ret = talloc_strdup(m, "");
108
109         for (i = 0; i < num; i++)
110                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
111         return ret;
112 }
113
114 static char *compile(const void *ctx,
115                      struct manifest *m,
116                      struct ccan_file *file,
117                      bool keep)
118 {
119         char *errmsg;
120
121         file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
122         errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
123                                   obj_list(m, file),
124                                   "", lib_list(m), file->compiled);
125         if (errmsg) {
126                 talloc_free(file->compiled);
127                 file->compiled = NULL;
128                 return errmsg;
129         }
130         return NULL;
131 }
132
133 static char *start_main(char *ret, const char *why)
134 {
135         return talloc_asprintf_append(ret,
136               "/* The example %s, so fake function wrapper inserted */\n"
137               "int main(int argc, char *argv[])\n"
138               "{\n", why);
139 }
140
141 /* We only handle simple function definitions here. */
142 static char *add_func(char *others, const char *line)
143 {
144         const char *p, *end = strchr(line, '(') - 1;
145         while (isblank(*end)) {
146                 end--;
147                 if (end == line)
148                         return others;
149         }
150
151         for (p = end; isalnum(*p) || *p == '_'; p--) {
152                 if (p == line)
153                         return others;
154         }
155
156         return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
157                                       (unsigned)(end - p + 1), p);
158 }
159
160 static void strip_leading_whitespace(char **lines)
161 {
162         unsigned int i, min_span = -1U;
163
164         for (i = 0; lines[i]; i++) {
165                 unsigned int span = strspn(lines[i], " \t");
166                 /* All whitespace?  Ignore */
167                 if (!lines[i][span])
168                         continue;
169                 if (span < min_span)
170                         min_span = span;
171         }
172
173         for (i = 0; lines[i]; i++)
174                 if (strlen(lines[i]) >= min_span)
175                         lines[i] += min_span;
176 }
177
178 static bool looks_internal(char **lines, char **why)
179 {
180         unsigned int i;
181         bool last_ended = true; /* Did last line finish a statement? */
182
183         for (i = 0; lines[i]; i++) {
184                 /* Skip leading whitespace. */
185                 const char *line = lines[i] + strspn(lines[i], " \t");
186                 unsigned len = strspn(line, IDENT_CHARS);
187
188                 if (!line[0] || isblank(line[0]) || strstarts(line, "//"))
189                         continue;
190
191                 /* The winners. */
192                 if (strstarts(line, "if") && len == 2) {
193                         *why = "starts with if";
194                         return true;
195                 }
196                 if (strstarts(line, "for") && len == 3) {
197                         *why = "starts with for";
198                         return true;
199                 }
200                 if (strstarts(line, "while") && len == 5) {
201                         *why = "starts with while";
202                         return true;
203                 }
204                 if (strstarts(line, "do") && len == 2) {
205                         *why = "starts with do";
206                         return true;
207                 }
208
209                 /* The losers. */
210                 if (strstarts(line, "#include")) {
211                         *why = "starts with #include";
212                         return false;
213                 }
214
215                 if (last_ended && strchr(line, '(')) {
216                         if (strstarts(line, "static")) {
217                                 *why = "starts with static and contains (";
218                                 return false;
219                         }
220                         if (strends(line, ")")) {
221                                 *why = "contains ( and ends with )";
222                                 return false;
223                         }
224                 }
225
226                 /* Single identifier then operator == inside function. */
227                 if (last_ended && len
228                     && ispunct(line[len+strspn(line+len, " ")])) {
229                         *why = "starts with identifier then punctuation";
230                         return true;
231                 }
232
233                 last_ended = (strends(line, "}")
234                               || strends(line, ";")
235                               || streq(line, "..."));
236         }
237
238         /* No idea... Say yes? */
239         *why = "gave no clues";
240         return true;
241 }
242
243 /* Examples will often build on prior ones.  Try combining them. */
244 static char **combine(const void *ctx, char **lines, char **prev)
245 {
246         unsigned int i, lines_total, prev_total, count;
247         char **ret;
248         const char *reasoning;
249         char *why = NULL;
250
251         if (!prev)
252                 return NULL;
253
254         /* If it looks internal, put prev at start. */
255         if (looks_internal(lines, &why)) {
256                 count = 0;
257                 reasoning = "seemed to belong inside a function";
258         } else {
259                 /* Try inserting in first elided position */
260                 for (count = 0; lines[count]; count++) {
261                         if (strcmp(lines[count], "...") == 0)
262                                 break;
263                 }
264                 if (!lines[count]) {
265                         /* Try at start anyway? */
266                         count = 0;
267                         reasoning = "didn't seem to belong inside"
268                                 " a function, so we prepended the previous"
269                                 " example";
270                 } else {
271                         reasoning = "didn't seem to belong inside"
272                                 " a function, so we put the previous example"
273                                 " at the first ...";
274
275                         count++;
276                 }
277         }
278
279         for (i = 0; lines[i]; i++);
280         lines_total = i;
281
282         for (i = 0; prev[i]; i++);
283         prev_total = i;
284
285         ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
286         ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
287                                  why, reasoning);
288         memcpy(ret+1, lines, count * sizeof(ret[0]));
289         memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
290         memcpy(ret+1 + count + prev_total, lines + count,
291                (lines_total - count + 1) * sizeof(ret[0]));
292         return ret;
293 }
294
295 static char *mangle(struct manifest *m, char **lines)
296 {
297         char *ret, *use_funcs = NULL, *why;
298         bool in_function = false, fake_function = false, has_main = false;
299         unsigned int i;
300
301         ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
302                             "#include <assert.h>\n"
303                             "#include <err.h>\n"
304                             "#include <errno.h>\n"
305                             "#include <fcntl.h>\n"
306                             "#include <limits.h>\n"
307                             "#include <stdbool.h>\n"
308                             "#include <stdint.h>\n"
309                             "#include <stdio.h>\n"
310                             "#include <stdlib.h>\n"
311                             "#include <string.h>\n"
312                             "#include <sys/stat.h>\n"
313                             "#include <sys/types.h>\n"
314                             "#include <unistd.h>\n");
315         ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
316                                      "#include <ccan/%s/%s.h>\n",
317                                      m->basename, m->basename);
318
319         ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
320                                      "extern int somefunc(void);\n"
321                                      "int somefunc(void) { return 0; }\n"
322                                      "extern char somestring[];\n"
323                                      "char somestring[] = \"hello world\";\n");
324
325         if (looks_internal(lines, &why)) {
326                 /* Wrap it all in main(). */
327                 ret = start_main(ret, why);
328                 fake_function = true;
329                 in_function = true;
330                 has_main = true;
331         } else
332                 ret = talloc_asprintf_append(ret,
333                              "/* The example %s, so didn't wrap in main() */\n",
334                                      why);
335
336         /* Primitive, very primitive. */
337         for (i = 0; lines[i]; i++) {
338                 /* } at start of line ends a function. */
339                 if (in_function) {
340                         if (lines[i][0] == '}')
341                                 in_function = false;
342                 } else {
343                         /* Character at start of line, with ( and no ;
344                          * == function start.  Ignore comments. */
345                         if (!isblank(lines[i][0])
346                             && strchr(lines[i], '(')
347                             && !strchr(lines[i], ';')
348                             && !strstr(lines[i], "//")) {
349                                 in_function = true;
350                                 if (strncmp(lines[i], "int main", 8) == 0)
351                                         has_main = true;
352                                 if (strncmp(lines[i], "static", 6) == 0) {
353                                         use_funcs = add_func(use_funcs,
354                                                              lines[i]);
355                                 }
356                         }
357                 }
358                 /* ... means elided code. */
359                 if (strcmp(lines[i], "...") == 0) {
360                         if (!in_function && !has_main
361                             && looks_internal(lines + i + 1, &why)) {
362                                 /* This implies we start a function here. */
363                                 ret = start_main(ret, why);
364                                 has_main = true;
365                                 fake_function = true;
366                                 in_function = true;
367                         }
368                         ret = talloc_asprintf_append(ret,
369                                                      "/* ... removed */\n");
370                         continue;
371                 }
372                 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
373         }
374
375         if (!has_main) {
376                 ret = talloc_asprintf_append(ret,
377                              "/* Need a main to link successfully. */\n"
378                              "int main(void)\n{\n");
379                 fake_function = true;
380         }
381
382         if (use_funcs) {
383                 ret = talloc_asprintf_append(ret,
384                                              "/* Get rid of unused warnings"
385                                              " by printing addresses of"
386                                              " static funcs. */");
387                 if (!fake_function) {
388                         ret = talloc_asprintf_append(ret,
389                                                      "int use_funcs(void);\n"
390                                                      "int use_funcs(void) {\n");
391                         fake_function = true;
392                 }
393                 ret = talloc_asprintf_append(ret, "     %s\n", use_funcs);
394         }
395
396         if (fake_function)
397                 ret = talloc_asprintf_append(ret, "return 0;\n"
398                                              "}\n");
399         return ret;
400 }
401
402 static struct ccan_file *mangle_example(struct manifest *m,
403                                         struct ccan_file *example,
404                                         char **lines,
405                                         bool keep)
406 {
407         char *name, *contents;
408         int fd;
409         struct ccan_file *f;
410
411         name = maybe_temp_file(example, ".c", keep, 
412                                talloc_asprintf(m, "%s/mangled-%s",
413                                                m->dir, example->name));
414         f = new_ccan_file(example,
415                           talloc_dirname(example, name),
416                           talloc_basename(example, name));
417         talloc_steal(f, name);
418
419         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
420         if (fd < 0)
421                 return NULL;
422
423         contents = mangle(m, lines);
424         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
425                 close(fd);
426                 return NULL;
427         }
428         close(fd);
429         f->contents = talloc_steal(f, contents);
430         list_add(&m->mangled_examples, &f->list);
431         return f;
432 }
433
434 static void build_examples(struct manifest *m, bool keep,
435                            unsigned int *timeleft, struct score *score)
436 {
437         struct ccan_file *i;
438         char **prev = NULL;
439
440         score->total = 0;
441         score->pass = true;
442
443         list_for_each(&m->examples, i, list) {
444                 char *ret, *ret1, *ret2 = NULL;
445                 struct ccan_file *mangle1, *mangle2 = NULL;
446                 char *err;
447
448                 score->total++;
449                 /* Simplify our dumb parsing. */
450                 strip_leading_whitespace(get_ccan_file_lines(i));
451                 ret = compile(i, m, i, keep);
452                 if (!ret) {
453                         prev = get_ccan_file_lines(i);
454                         score->score++;
455                         continue;
456                 }
457
458                 /* Try standalone. */
459                 mangle1 = mangle_example(m, i, get_ccan_file_lines(i), keep);
460                 ret1 = compile(i, m, mangle1, keep);
461                 if (!ret1) {
462                         prev = get_ccan_file_lines(i);
463                         score->score++;
464                         continue;
465                 }
466
467                 /* Try combining with previous (successful) example... */
468                 if (prev) {
469                         char **new = combine(i, get_ccan_file_lines(i), prev);
470
471                         mangle2 = mangle_example(m, i, new, keep);
472                         ret2 = compile(i, m, mangle2, keep);
473                         if (!ret2) {
474                                 prev = new;
475                                 score->score++;
476                                 continue;
477                         }
478                 }
479
480                 score->pass = false;
481                 score->error = "Compiling extracted examples failed";
482                 if (!verbose) {
483                         if (mangle2) 
484                                 err = "Standalone, adding headers, "
485                                         "and including previous "
486                                         "example all failed";
487                         else
488                                 err = "Standalone compile and"
489                                         " adding headers both failed";
490                 } else {
491                         err = talloc_asprintf(score,
492                                               "Standalone example:\n"
493                                               "%s\n"
494                                               "Errors: %s\n\n"
495                                               "Adding headers, wrappers:\n"
496                                               "%s\n"
497                                               "Errors: %s\n\n",
498                                               get_ccan_file_contents(i),
499                                               ret,
500                                               get_ccan_file_contents(mangle1),
501                                               ret1);
502
503                         if (mangle2)
504                                 err = talloc_asprintf_append(err, 
505                                        "Combining with previous example:\n"
506                                        "%s\n"
507                                        "Errors: %s\n\n",
508                                        get_ccan_file_contents(mangle2),
509                                        ret2);
510                 }
511                 score_file_error(score, i, 0, err);
512                 /* This didn't work, so not a candidate for combining. */
513                 prev = NULL;
514         }
515 }
516
517 struct ccanlint examples_compile = {
518         .key = "examples-compile",
519         .name = "Module examples compile",
520         .check = build_examples,
521         .can_run = can_run,
522 };
523
524 REGISTER_TEST(examples_compile, &has_examples, NULL);