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