]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/examples_compile.c
2479d80a70a7ff9d93dc58f9428eaefb09e80ed8
[ccan] / tools / ccanlint / tests / examples_compile.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/tal/tal.h>
4 #include <ccan/tal/str/str.h>
5 #include <ccan/take/take.h>
6 #include <ccan/cast/cast.h>
7 #include <ccan/tal/path/path.h>
8 #include <ccan/str/str.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <ctype.h>
16 #include <assert.h>
17 #include <err.h>
18 #include "build.h"
19
20 static const char *can_run(struct manifest *m)
21 {
22         if (safe_mode)
23                 return "Safe mode enabled";
24         if (list_empty(&m->examples))
25                 return "No examples to compile";
26         return NULL;
27 }
28
29 static void add_mod(struct manifest ***deps, struct manifest *m)
30 {
31         unsigned int num = tal_count(*deps);
32         tal_resize(deps, num + 1);
33         (*deps)[num] = m;
34 }
35
36 static bool have_mod(struct manifest *deps[], const char *modname)
37 {
38         unsigned int i;
39
40         for (i = 0; i < tal_count(deps); i++)
41                 if (strcmp(deps[i]->modname, modname) == 0)
42                         return true;
43         return false;
44 }
45
46 static void add_dep(struct manifest ***deps, const char *modname)
47 {
48         unsigned int i;
49         struct manifest *m;
50         char *errstr;
51
52         if (have_mod(*deps, modname))
53                 return;
54
55         m = get_manifest(*deps,
56                          tal_fmt(*deps, "%s/ccan/%s", ccan_dir, modname));
57         errstr = build_submodule(m, cflags, COMPILE_NORMAL);
58         if (errstr)
59                 errx(1, "%s", errstr);
60
61         add_mod(deps, m);
62
63         /* Get that modules depends as well... */
64         assert(!safe_mode);
65         if (m->info_file) {
66                 char **infodeps;
67
68                 infodeps = get_deps(m, m->dir, "depends", false,
69                                     get_or_compile_info);
70
71                 for (i = 0; infodeps[i]; i++) {
72                         if (strstarts(infodeps[i], "ccan/"))
73                                 add_dep(deps, infodeps[i] + strlen("ccan/"));
74                 }
75         }
76 }
77
78 static struct manifest **get_example_deps(struct manifest *m,
79                                           struct ccan_file *f)
80 {
81         char **lines;
82         struct manifest **deps = tal_arr(f, struct manifest *, 0);
83
84         /* This one for a start. */
85         add_dep(&deps, m->modname);
86
87         /* Other modules implied by includes. */
88         for (lines = get_ccan_file_lines(f); *lines; lines++) {
89                 char *modname;
90                 if (tal_strreg(f, *lines,
91                                "^[ \t]*#[ \t]*include[ \t]*[<\"]"
92                                "ccan/+(.+)/+[^/]+\\.h", &modname)) {
93                         if (!have_mod(deps, modname))
94                                 add_dep(&deps, modname);
95                 }
96         }
97
98         return deps;
99 }
100
101 static char *example_obj_list(const void *ctx, struct manifest **deps)
102 {
103         char *list = tal_strdup(ctx, "");
104         unsigned int i;
105
106         for (i = 0; i < tal_count(deps); i++) {
107                 if (deps[i]->compiled[COMPILE_NORMAL])
108                         tal_append_fmt(&list, " %s",
109                                        deps[i]->compiled[COMPILE_NORMAL]);
110         }
111         return list;
112 }
113
114 static char *example_lib_list(const void *ctx, struct manifest **deps)
115 {
116         char *list = tal_strdup(ctx, "");
117         char **libs;
118         unsigned int i, j;
119
120         /* FIXME: This doesn't uniquify. */
121         for (i = 0; i < tal_count(deps); i++) {
122                 libs = get_libs(ctx, deps[i]->dir, NULL, get_or_compile_info);
123                 for (j = 0; libs[j]; j++)
124                         tal_append_fmt(&list, "-l%s ", libs[j]);
125         }
126         return list;
127 }
128
129 /* FIXME: Test with reduced features! */
130 static bool compile(const void *ctx,
131                     struct manifest *m,
132                     struct ccan_file *file,
133                     char **output)
134 {
135         struct manifest **deps = get_example_deps(m, file);
136
137         file->compiled[COMPILE_NORMAL] = temp_file(ctx, "", file->fullname);
138         if (!compile_and_link(ctx, file->fullname, ccan_dir,
139                               example_obj_list(file, deps),
140                               compiler, cflags,
141                               example_lib_list(file, deps),
142                               file->compiled[COMPILE_NORMAL],
143                               output)) {
144                 /* Don't keep failures, even with --keep */
145                 unlink(file->compiled[COMPILE_NORMAL]);
146                 file->compiled[COMPILE_NORMAL] = NULL;
147                 return false;
148         }
149         return true;
150 }
151
152 static void start_main(char **ret, const char *why)
153 {
154         tal_append_fmt(ret,
155               "/* The example %s, so fake function wrapper inserted */\n"
156               "int main(int argc, char *argv[])\n"
157                "{\n", why);
158 }
159
160 /* We only handle simple function definitions here. */
161 static char *add_func(const tal_t *ctx, char *others, const char *line)
162 {
163         const char *p, *end = strchr(line, '(') - 1;
164         char *use;
165
166         while (cisspace(*end)) {
167                 end--;
168                 if (end == line)
169                         return others;
170         }
171
172         for (p = end; cisalnum(*p) || *p == '_'; p--) {
173                 if (p == line)
174                         return others;
175         }
176
177         use = tal_fmt(ctx, "printf(\"%%p\", %.*s);\n",
178                       (unsigned)(end - p), p+1);
179         if (others)
180                 use = tal_strcat(ctx, take(others), take(use));
181
182         return use;
183 }
184
185 static void strip_leading_whitespace(char **lines)
186 {
187         unsigned int i, min_span = -1U;
188
189         for (i = 0; lines[i]; i++) {
190                 unsigned int span = strspn(lines[i], " \t");
191                 /* All whitespace?  Ignore */
192                 if (!lines[i][span])
193                         continue;
194                 if (span < min_span)
195                         min_span = span;
196         }
197
198         for (i = 0; lines[i]; i++)
199                 if (strlen(lines[i]) >= min_span)
200                         lines[i] += min_span;
201 }
202
203 static bool looks_internal(char **lines, char **why)
204 {
205         unsigned int i;
206         bool last_ended = true; /* Did last line finish a statement? */
207
208         for (i = 0; lines[i]; i++) {
209                 /* Skip leading whitespace. */
210                 const char *line = lines[i] + strspn(lines[i], " \t");
211                 unsigned len = strspn(line, IDENT_CHARS);
212
213                 if (!line[0] || cisspace(line[0]) || strstarts(line, "//")
214                     || strstarts(line, "#line"))
215                         continue;
216
217                 assert(line[strlen(line)-1] != '\n');
218
219                 /* The winners. */
220                 if (strstarts(line, "if") && len == 2) {
221                         *why = cast_const(char *, "starts with if");
222                         return true;
223                 }
224                 if (strstarts(line, "for") && len == 3) {
225                         *why = cast_const(char *, "starts with for");
226                         return true;
227                 }
228                 if (strstarts(line, "while") && len == 5) {
229                         *why = cast_const(char *, "starts with while");
230                         return true;
231                 }
232                 if (strstarts(line, "do") && len == 2) {
233                         *why = cast_const(char *, "starts with do");
234                         return true;
235                 }
236
237                 /* The losers. */
238                 if (strstarts(line, "#include")) {
239                         *why = cast_const(char *, "starts with #include");
240                         return false;
241                 }
242
243                 if (last_ended && strchr(line, '(')) {
244                         if (strstarts(line, "static")) {
245                                 *why = cast_const(char *,
246                                                   "starts with static"
247                                                   " and contains (");
248                                 return false;
249                         }
250                         if (strends(line, ")")) {
251                                 *why = cast_const(char *,
252                                                   "contains ( and ends with )");
253                                 return false;
254                         }
255                 }
256
257                 /* Previously prepended. */
258                 if (strstr(line, "didn't seem to belong inside a function")) {
259                         *why = cast_const(char *, "Comment said so");
260                         return false;
261                 }
262
263                 /* Single identifier then operator == inside function. */
264                 if (last_ended && len
265                     && cispunct(line[len+strspn(line+len, " ")])) {
266                         *why = cast_const(char *, "starts with identifier"
267                                           " then punctuation");
268                         return true;
269                 }
270
271                 last_ended = (strends(line, "}")
272                               || strends(line, ";")
273                               || strends(line, "*/")
274                               || streq(line, "..."));
275         }
276
277         /* No idea... Say yes? */
278         *why = cast_const(char *, "gave no clues");
279         return true;
280 }
281
282 /* Examples will often build on prior ones.  Try combining them. */
283 static char **combine(const void *ctx, char **lines, char **prev)
284 {
285         unsigned int i, lines_total, prev_total, count;
286         char **ret;
287         const char *reasoning;
288         char *why = NULL;
289
290         if (!prev)
291                 return NULL;
292
293         /* If it looks internal, put prev at start. */
294         if (looks_internal(lines, &why)) {
295                 count = 0;
296                 reasoning = "seemed to belong inside a function";
297         } else {
298                 /* Try inserting in first elided position */
299                 for (count = 0; lines[count]; count++) {
300                         if (strcmp(lines[count], "...") == 0)
301                                 break;
302                 }
303                 if (!lines[count]) {
304                         /* Try at start anyway? */
305                         count = 0;
306                         reasoning = "didn't seem to belong inside"
307                                 " a function, so we prepended the previous"
308                                 " example";
309                 } else {
310                         reasoning = "didn't seem to belong inside"
311                                 " a function, so we put the previous example"
312                                 " at the first ...";
313
314                         count++;
315                 }
316         }
317
318         for (i = 0; lines[i]; i++);
319         lines_total = i;
320
321         for (i = 0; prev[i]; i++);
322         prev_total = i;
323
324         ret = tal_arr(ctx, char *, 1 + lines_total + prev_total + 1);
325         ret[0] = tal_fmt(ret, "/* The example %s, thus %s */",
326                          why, reasoning);
327         memcpy(ret+1, lines, count * sizeof(ret[0]));
328         memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
329         memcpy(ret+1 + count + prev_total, lines + count,
330                (lines_total - count + 1) * sizeof(ret[0]));
331         return ret;
332 }
333
334 /* Only handles very simple comments. */
335 static char *strip_comment(const void *ctx, const char *orig_line)
336 {
337         char *p, *ret = tal_strdup(ctx, orig_line);
338
339         p = strstr(ret, "/*");
340         if (!p)
341                 p = strstr(ret, "//");
342         if (p)
343                 *p = '\0';
344         return ret;
345 }
346
347 static char *mangle(struct manifest *m, char **lines)
348 {
349         char *ret, *use_funcs = NULL, *why;
350         bool in_function = false, fake_function = false, has_main = false;
351         unsigned int i;
352
353         ret = tal_fmt(m,
354                       "/* Include header from module. */\n"
355                       "#include <ccan/%s/%s.h>\n"
356                       "/* Prepend a heap of headers. */\n"
357                       "#include <assert.h>\n"
358                       "#include <err.h>\n"
359                       "#include <errno.h>\n"
360                       "#include <fcntl.h>\n"
361                       "#include <limits.h>\n"
362                       "#include <stdbool.h>\n"
363                       "#include <stdint.h>\n"
364                       "#include <stdio.h>\n"
365                       "#include <stdlib.h>\n"
366                       "#include <string.h>\n"
367                       "#include <sys/stat.h>\n"
368                       "#include <sys/types.h>\n"
369                       "#include <unistd.h>\n",
370                       m->modname, m->basename);
371
372         ret = tal_strcat(m, take(ret), "/* Useful dummy functions. */\n"
373                                      "extern int somefunc(void);\n"
374                                      "int somefunc(void) { return 0; }\n"
375                                      "extern char somestring[];\n"
376                                      "char somestring[] = \"hello world\";\n");
377
378         if (looks_internal(lines, &why)) {
379                 /* Wrap it all in main(). */
380                 start_main(&ret, why);
381                 fake_function = true;
382                 in_function = true;
383                 has_main = true;
384         } else
385                 tal_append_fmt(&ret,
386                              "/* The example %s, so didn't wrap in main() */\n",
387                                why);
388
389         /* Primitive, very primitive. */
390         for (i = 0; lines[i]; i++) {
391                 char *line = strip_comment(ret, lines[i]);
392
393                 /* } at start of line ends a function. */
394                 if (in_function) {
395                         if (line[0] == '}')
396                                 in_function = false;
397                 } else {
398                         /* Character at start of line, with ( and no ;
399                          * == function start.  Ignore comments. */
400                         if (!cisspace(line[0])
401                             && strchr(line, '(')
402                             && !strchr(line, ';')
403                             && !strstr(line, "//")) {
404                                 in_function = true;
405                                 if (strncmp(line, "int main", 8) == 0)
406                                         has_main = true;
407                                 if (strncmp(line, "static", 6) == 0) {
408                                         use_funcs = add_func(m, use_funcs,
409                                                              line);
410                                 }
411                         }
412                 }
413                 /* ... means elided code. */
414                 if (strcmp(line, "...") == 0) {
415                         if (!in_function && !has_main
416                             && looks_internal(lines + i + 1, &why)) {
417                                 /* This implies we start a function here. */
418                                 start_main(&ret, why);
419                                 has_main = true;
420                                 fake_function = true;
421                                 in_function = true;
422                         }
423                         ret = tal_strcat(m, take(ret), "/* ... removed */\n");
424                         continue;
425                 }
426                 ret = tal_strcat(m, take(ret), lines[i]);
427                 ret = tal_strcat(m, take(ret), "\n");
428         }
429
430         if (!has_main) {
431                 ret = tal_strcat(m, take(ret),
432                              "/* Need a main to link successfully. */\n"
433                              "int main(void)\n{\n");
434                 fake_function = true;
435         }
436
437         if (use_funcs) {
438                 ret = tal_strcat(m, take(ret),
439                                  "/* Get rid of unused warnings"
440                                  " by printing addresses of"
441                                  " static funcs. */\n");
442                 if (!fake_function) {
443                         ret = tal_strcat(m, take(ret),
444                                          "int use_funcs(void);\n"
445                                          "int use_funcs(void) {\n");
446                         fake_function = true;
447                 }
448                 tal_append_fmt(&ret, "  %s\n", use_funcs);
449         }
450
451         if (fake_function)
452                 ret = tal_strcat(m, take(ret), "return 0;\n}\n");
453         return ret;
454 }
455
456 static struct ccan_file *mangle_example(struct manifest *m,
457                                         struct ccan_file *example,
458                                         char **lines)
459 {
460         char *name, *contents;
461         int fd;
462         struct ccan_file *f;
463
464         name = temp_file(example, ".c",
465                          take(tal_fmt(NULL, "mangled-%s", example->name)));
466         f = new_ccan_file(example,
467                           path_dirname(example, name),
468                           path_basename(example, name));
469         tal_steal(f, name);
470
471         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
472         if (fd < 0)
473                 return NULL;
474
475         contents = mangle(m, lines);
476         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
477                 close(fd);
478                 return NULL;
479         }
480         close(fd);
481         f->contents = tal_steal(f, contents);
482         list_add(&m->mangled_examples, &f->list);
483         return f;
484 }
485
486 /* If an example has expected output, it's complete and should not be
487  * included in future examples. */
488 static bool has_expected_output(char **lines)
489 {
490         unsigned int i;
491
492         for (i = 0; lines[i]; i++) {
493                 char *p = lines[i] + strspn(lines[i], " \t");
494                 if (!strstarts(p, "//"))
495                         continue;
496                 p += strspn(p, "/ ");
497                 if (strncasecmp(p, "given", strlen("given")) == 0)
498                         return true;
499         }
500         return false;
501 }
502
503 static unsigned int try_compiling(struct manifest *m,
504                                   struct ccan_file *i,
505                                   char **prev,
506                                   struct ccan_file *mangled[3],
507                                   bool res[3],
508                                   char *err[3],
509                                   char **lines[3])
510 {
511         unsigned int num;
512
513         /* Try standalone. */
514         mangled[0] = i;
515         res[0] = compile(i, m, mangled[0], &err[0]);
516         lines[0] = get_ccan_file_lines(i);
517         if (res[0] && streq(err[0], ""))
518                 return 1;
519
520         if (prev) {
521                 lines[1] = combine(i, get_ccan_file_lines(i), prev);
522
523                 mangled[1] = mangle_example(m, i, lines[1]);
524                 res[1] = compile(i, m, mangled[1], &err[1]);
525                 if (res[1] && streq(err[1], "")) {
526                         return 2;
527                 }
528                 num = 2;
529         } else
530                 num = 1;
531
532         /* Try standalone. */
533         lines[num] = get_ccan_file_lines(i);
534         mangled[num] = mangle_example(m, i, lines[num]);
535         res[num] = compile(i, m, mangled[num], &err[num]);
536
537         return num+1;
538 }
539
540 static void build_examples(struct manifest *m,
541                            unsigned int *timeleft, struct score *score)
542 {
543         struct ccan_file *i;
544         char **prev = NULL;
545         bool warnings = false;
546
547         score->total = 0;
548         score->pass = true;
549
550         list_for_each(&m->examples, i, list) {
551                 char *err[3];
552                 struct ccan_file *file[3] = { NULL, NULL, NULL };
553                 bool res[3];
554                 unsigned num, j;
555                 char **lines[3];
556                 const char *error;
557
558                 score->total++;
559
560                 /* Simplify our dumb parsing. */
561                 strip_leading_whitespace(get_ccan_file_lines(i));
562
563                 num = try_compiling(m, i, prev, file, res, err, lines);
564
565                 /* First look for a compile without any warnings. */
566                 for (j = 0; j < num; j++) {
567                         if (res[j] && streq(err[j], "")) {
568                                 if (!has_expected_output(lines[j]))
569                                         prev = lines[j];
570                                 score->score++;
571                                 goto next;
572                         }
573                 }
574
575                 /* Now accept anything which succeeded. */
576                 for (j = 0; j < num; j++) {
577                         if (res[j]) {
578                                 if (!has_expected_output(lines[j]))
579                                         prev = lines[j];
580                                 score->score++;
581                                 warnings = true;
582                                 score_file_error(score, file[j], 0,
583                                                  "Compiling extracted example"
584                                                  " gave warnings:\n"
585                                                  "Example:\n"
586                                                  "%s\n"
587                                                  "Compiler:\n"
588                                                  "%s",
589                                                  get_ccan_file_contents(file[j]),
590                                                  err[j]);
591                                 goto next;
592                         }
593                 }
594
595                 score->pass = false;
596                 if (!verbose) {
597                         if (num == 3)
598                                 error = "Compiling standalone, adding headers, "
599                                         "and including previous "
600                                         "example all failed";
601                         else
602                                 error = "Standalone compile and"
603                                         " adding headers both failed";
604                 } else {
605                         if (num == 3) {
606                                 error = tal_fmt(score,
607                                       "Standalone example:\n"
608                                       "%s\n"
609                                       "Errors: %s\n\n"
610                                       "Combining with previous example:\n"
611                                       "%s\n"
612                                       "Errors: %s\n\n"
613                                       "Adding headers, wrappers:\n"
614                                       "%s\n"
615                                       "Errors: %s\n\n",
616                                       get_ccan_file_contents(file[0]),
617                                       err[0],
618                                       get_ccan_file_contents(file[1]),
619                                       err[1],
620                                       get_ccan_file_contents(file[2]),
621                                       err[2]);
622                         } else {
623                                 error = tal_fmt(score,
624                                       "Standalone example:\n"
625                                       "%s\n"
626                                       "Errors: %s\n\n"
627                                       "Adding headers, wrappers:\n"
628                                       "%s\n"
629                                       "Errors: %s\n\n",
630                                       get_ccan_file_contents(file[0]),
631                                       err[0],
632                                       get_ccan_file_contents(file[1]),
633                                       err[1]);
634                         }
635                 }
636                 score_file_error(score, i, 0, "%s", error);
637                 /* This didn't work, so not a candidate for combining. */
638                 prev = NULL;
639
640         next:
641                 ;
642         }
643
644         /* An extra point if they all compiled without warnings. */
645         if (!list_empty(&m->examples)) {
646                 score->total++;
647                 if (!warnings)
648                         score->score++;
649         }
650 }
651
652 struct ccanlint examples_compile = {
653         .key = "examples_compile",
654         .name = "Module examples compile",
655         .check = build_examples,
656         .can_run = can_run,
657         .needs = "examples_exist module_builds"
658 };
659
660 REGISTER_TEST(examples_compile);