]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/examples_compile.c
ccanlint: examples_compile depends on build, which depends on build_objs
[ccan] / tools / ccanlint / tests / examples_compile.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <ctype.h>
12 #include <assert.h>
13
14 static const char *can_run(struct manifest *m)
15 {
16         if (safe_mode)
17                 return "Safe mode enabled";
18         if (list_empty(&m->examples))
19                 return "No examples to compile";
20         return NULL;
21 }
22
23 /* FIXME: We should build if it doesn't exist... */
24 static bool expect_obj_file(const char *dir)
25 {
26         struct manifest *dep_man;
27         bool has_c_files;
28
29         dep_man = get_manifest(dir, dir);
30
31         /* If it has C files, we expect an object file built from them. */
32         has_c_files = !list_empty(&dep_man->c_files);
33         talloc_free(dep_man);
34         return has_c_files;
35 }
36
37 static char *add_dep(const struct manifest *m, char *list, const char *mod)
38 {
39         char **deps, *obj;
40         unsigned int i;
41
42         /* Not ourselves. */
43         if (streq(m->basename, mod))
44                 return list;
45
46         /* Not if there's no object file for that module */
47         if (!expect_obj_file(talloc_asprintf(list, "%s/ccan/%s", ccan_dir,mod)))
48                 return list;
49
50         obj = talloc_asprintf(list, "%s/ccan/%s.o", ccan_dir, mod);
51
52         /* Not anyone we've already included. */
53         if (strstr(list, obj))
54                 return list;
55
56         list = talloc_asprintf_append(list, " %s", obj);
57
58         /* Get that modules depends as well... */
59         assert(!safe_mode);
60         deps = get_deps(m, talloc_asprintf(list, "%s/ccan/%s", ccan_dir, mod),
61                         false, NULL);
62
63         for (i = 0; deps[i]; i++) {
64                 if (strstarts(deps[i], "ccan/"))
65                         list = add_dep(m, list, deps[i] + strlen("ccan/"));
66         }
67         return list;
68 }
69
70 /* FIXME: Merge this into one place. */
71 static char *obj_list(const struct manifest *m, struct ccan_file *f)
72 {
73         char *list = talloc_strdup(m, "");
74         struct manifest *subm;
75         char **lines;
76
77         /* This module. */
78         if (m->compiled)
79                 list = talloc_asprintf_append(list, " %s", m->compiled);
80
81         /* Other ccan modules we depend on. */
82         list_for_each(&m->deps, subm, list) {
83                 if (subm->compiled)
84                         list = talloc_asprintf_append(list, " %s",
85                                                       subm->compiled);
86         }
87
88         /* Other modules implied by includes. */
89         for (lines = get_ccan_file_lines(f); *lines; lines++) {
90                 unsigned preflen = strspn(*lines, " \t");
91                 if (strstarts(*lines + preflen, "#include <ccan/")) {
92                         const char *mod;
93                         unsigned modlen;
94
95                         mod = *lines + preflen + strlen("#include <ccan/");
96                         modlen = strcspn(mod, "/");
97                         mod = talloc_strndup(f, mod, modlen);
98                         list = add_dep(m, list, mod);
99                 }
100         }
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 static char *mangle(struct manifest *m, char **lines)
295 {
296         char *ret, *use_funcs = NULL, *why;
297         bool in_function = false, fake_function = false, has_main = false;
298         unsigned int i;
299
300         ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
301                             "#include <assert.h>\n"
302                             "#include <err.h>\n"
303                             "#include <errno.h>\n"
304                             "#include <fcntl.h>\n"
305                             "#include <limits.h>\n"
306                             "#include <stdbool.h>\n"
307                             "#include <stdint.h>\n"
308                             "#include <stdio.h>\n"
309                             "#include <stdlib.h>\n"
310                             "#include <string.h>\n"
311                             "#include <sys/stat.h>\n"
312                             "#include <sys/types.h>\n"
313                             "#include <unistd.h>\n");
314         ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
315                                      "#include <ccan/%s/%s.h>\n",
316                                      m->basename, m->basename);
317
318         ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
319                                      "extern int somefunc(void);\n"
320                                      "int somefunc(void) { return 0; }\n"
321                                      "extern char somestring[];\n"
322                                      "char somestring[] = \"hello world\";\n");
323
324         if (looks_internal(lines, &why)) {
325                 /* Wrap it all in main(). */
326                 ret = start_main(ret, why);
327                 fake_function = true;
328                 in_function = true;
329                 has_main = true;
330         } else
331                 ret = talloc_asprintf_append(ret,
332                              "/* The example %s, so didn't wrap in main() */\n",
333                                      why);
334
335         /* Primitive, very primitive. */
336         for (i = 0; lines[i]; i++) {
337                 /* } at start of line ends a function. */
338                 if (in_function) {
339                         if (lines[i][0] == '}')
340                                 in_function = false;
341                 } else {
342                         /* Character at start of line, with ( and no ;
343                          * == function start.  Ignore comments. */
344                         if (!isspace(lines[i][0])
345                             && strchr(lines[i], '(')
346                             && !strchr(lines[i], ';')
347                             && !strstr(lines[i], "//")) {
348                                 in_function = true;
349                                 if (strncmp(lines[i], "int main", 8) == 0)
350                                         has_main = true;
351                                 if (strncmp(lines[i], "static", 6) == 0) {
352                                         use_funcs = add_func(use_funcs,
353                                                              lines[i]);
354                                 }
355                         }
356                 }
357                 /* ... means elided code. */
358                 if (strcmp(lines[i], "...") == 0) {
359                         if (!in_function && !has_main
360                             && looks_internal(lines + i + 1, &why)) {
361                                 /* This implies we start a function here. */
362                                 ret = start_main(ret, why);
363                                 has_main = true;
364                                 fake_function = true;
365                                 in_function = true;
366                         }
367                         ret = talloc_asprintf_append(ret,
368                                                      "/* ... removed */\n");
369                         continue;
370                 }
371                 ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
372         }
373
374         if (!has_main) {
375                 ret = talloc_asprintf_append(ret,
376                              "/* Need a main to link successfully. */\n"
377                              "int main(void)\n{\n");
378                 fake_function = true;
379         }
380
381         if (use_funcs) {
382                 ret = talloc_asprintf_append(ret,
383                                              "/* Get rid of unused warnings"
384                                              " by printing addresses of"
385                                              " static funcs. */\n");
386                 if (!fake_function) {
387                         ret = talloc_asprintf_append(ret,
388                                                      "int use_funcs(void);\n"
389                                                      "int use_funcs(void) {\n");
390                         fake_function = true;
391                 }
392                 ret = talloc_asprintf_append(ret, "     %s\n", use_funcs);
393         }
394
395         if (fake_function)
396                 ret = talloc_asprintf_append(ret, "return 0;\n"
397                                              "}\n");
398         return ret;
399 }
400
401 static struct ccan_file *mangle_example(struct manifest *m,
402                                         struct ccan_file *example,
403                                         char **lines,
404                                         bool keep)
405 {
406         char *name, *contents;
407         int fd;
408         struct ccan_file *f;
409
410         name = maybe_temp_file(example, ".c", keep, 
411                                talloc_asprintf(m, "%s/mangled-%s",
412                                                m->dir, example->name));
413         f = new_ccan_file(example,
414                           talloc_dirname(example, name),
415                           talloc_basename(example, name));
416         talloc_steal(f, name);
417
418         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
419         if (fd < 0)
420                 return NULL;
421
422         contents = mangle(m, lines);
423         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
424                 close(fd);
425                 return NULL;
426         }
427         close(fd);
428         f->contents = talloc_steal(f, contents);
429         list_add(&m->mangled_examples, &f->list);
430         return f;
431 }
432
433 /* If an example has expected output, it's complete and should not be
434  * included in future examples. */
435 static bool has_expected_output(char **lines)
436 {
437         unsigned int i;
438
439         for (i = 0; lines[i]; i++) {
440                 char *p = lines[i] + strspn(lines[i], " \t");
441                 if (!strstarts(p, "//"))
442                         continue;
443                 p += strspn(p, "/ ");
444                 if (strncasecmp(p, "given", strlen("given")) == 0)
445                         return true;
446         }
447         return false;
448 }
449
450 static unsigned int try_compiling(struct manifest *m,
451                                   struct ccan_file *i,
452                                   char **prev,
453                                   bool keep,
454                                   struct ccan_file *mangled[3],
455                                   bool res[3],
456                                   char *err[3],
457                                   char **lines[3])
458 {
459         unsigned int num;
460
461         /* Try standalone. */
462         mangled[0] = i;
463         res[0] = compile(i, m, mangled[0], keep, &err[0]);
464         lines[0] = get_ccan_file_lines(i);
465         if (res[0] && streq(err[0], ""))
466                 return 1;
467
468         if (prev) {
469                 lines[1] = combine(i, get_ccan_file_lines(i), prev);
470
471                 mangled[1] = mangle_example(m, i, lines[1], keep);
472                 res[1] = compile(i, m, mangled[1], keep, &err[1]);
473                 if (res[1] && streq(err[1], "")) {
474                         return 2;
475                 }
476                 num = 2;
477         } else
478                 num = 1;
479
480         /* Try standalone. */
481         lines[num] = get_ccan_file_lines(i);
482         mangled[num] = mangle_example(m, i, lines[num], keep);
483         res[num] = compile(i, m, mangled[num], keep, &err[num]);
484
485         return num+1;
486 }
487
488 static void build_examples(struct manifest *m, bool keep,
489                            unsigned int *timeleft, struct score *score)
490 {
491         struct ccan_file *i;
492         char **prev = NULL;
493         bool warnings = false;
494
495         score->total = 0;
496         score->pass = true;
497
498         list_for_each(&m->examples, i, list) {
499                 char *err[3];
500                 struct ccan_file *file[3] = { NULL, NULL, NULL };
501                 bool res[3];
502                 unsigned num, j;
503                 char **lines[3];
504                 char *error;
505
506                 score->total++;
507
508                 /* Simplify our dumb parsing. */
509                 strip_leading_whitespace(get_ccan_file_lines(i));
510
511                 num = try_compiling(m, i, prev, keep, file, res, err, lines);
512
513                 /* First look for a compile without any warnings. */
514                 for (j = 0; j < num; j++) {
515                         if (res[j] && streq(err[j], "")) {
516                                 if (!has_expected_output(lines[j]))
517                                         prev = lines[j];
518                                 score->score++;
519                                 goto next;
520                         }
521                 }
522
523                 /* Now accept anything which succeeded. */
524                 for (j = 0; j < num; j++) {
525                         if (res[j]) {
526                                 if (!has_expected_output(lines[j]))
527                                         prev = lines[j];
528                                 score->score++;
529                                 warnings = true;
530                                 score->error = "Compiling extracted example"
531                                         " gave warnings";
532                                 score_file_error(score, file[j], 0, err[j]);
533                                 goto next;
534                         }
535                 }
536
537                 score->pass = false;
538                 score->error = "Compiling extracted examples failed";
539                 if (!verbose) {
540                         if (num == 3)
541                                 error = "Standalone, adding headers, "
542                                         "and including previous "
543                                         "example all failed";
544                         else
545                                 error = "Standalone compile and"
546                                         " adding headers both failed";
547                 } else {
548                         if (num == 3) {
549                                 error = talloc_asprintf(score,
550                                       "Standalone example:\n"
551                                       "%s\n"
552                                       "Errors: %s\n\n"
553                                       "Combining with previous example:\n"
554                                       "%s\n"
555                                       "Errors: %s\n\n"
556                                       "Adding headers, wrappers:\n"
557                                       "%s\n"
558                                       "Errors: %s\n\n",
559                                       get_ccan_file_contents(file[0]),
560                                       err[0],
561                                       get_ccan_file_contents(file[1]),
562                                       err[1],
563                                       get_ccan_file_contents(file[2]),
564                                       err[2]);
565                         } else {
566                                 error = talloc_asprintf(score,
567                                       "Standalone example:\n"
568                                       "%s\n"
569                                       "Errors: %s\n\n"
570                                       "Adding headers, wrappers:\n"
571                                       "%s\n"
572                                       "Errors: %s\n\n",
573                                       get_ccan_file_contents(file[0]),
574                                       err[0],
575                                       get_ccan_file_contents(file[1]),
576                                       err[1]);
577                         }
578                 }
579                 score_file_error(score, i, 0, error);
580                 /* This didn't work, so not a candidate for combining. */
581                 prev = NULL;
582
583         next:
584                 ;
585         }
586
587         /* An extra point if they all compiled without warnings. */
588         if (!list_empty(&m->examples)) {
589                 score->total++;
590                 if (!warnings)
591                         score->score++;
592         }
593 }
594
595 struct ccanlint examples_compile = {
596         .key = "examples-compile",
597         .name = "Module examples compile",
598         .check = build_examples,
599         .can_run = can_run,
600 };
601
602 REGISTER_TEST(examples_compile, &has_examples, &build, NULL);