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