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