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