utf8: don't allow NUL in decoded strings.
[ccan] / tools / ccanlint / tests / tests_compile_coverage.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/foreach/foreach.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include "build.h"
17 #include "tests_compile.h"
18
19 /* Note: we already test safe_mode in run_tests.c */
20 static const char *can_run_coverage(struct manifest *m)
21 {
22         return gcov_unavailable(m);
23 }
24
25 static char *cflags_list(const struct manifest *m)
26 {
27         unsigned int i;
28         char *ret = tal_strdup(m, cflags);
29
30         char **flags = get_cflags(m, m->dir, get_or_compile_info);
31         for (i = 0; flags[i]; i++)
32                 tal_append_fmt(&ret, " %s", flags[i]);
33         return ret;
34 }
35
36 static char *cflags_list_append(const struct manifest *m, char *iflags)
37 {
38         unsigned int i;
39
40         char **flags = get_cflags(m, m->dir, get_or_compile_info);
41         for (i = 0; flags[i]; i++)
42                 tal_append_fmt(&iflags, " %s", flags[i]);
43         return iflags;
44 }
45
46 static void cov_compile(const void *ctx,
47                         unsigned int time_ms,
48                         struct manifest *m,
49                         struct ccan_file *file,
50                         bool link_with_module)
51 {
52         char *flags = tal_fmt(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
53         flags = cflags_list_append(m, flags);
54
55         file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
56         compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
57                                test_obj_list(m, link_with_module,
58                                              COMPILE_NORMAL,
59                                              COMPILE_COVERAGE),
60                                compiler, flags,
61                                test_lib_list(m, COMPILE_NORMAL),
62                                file->compiled[COMPILE_COVERAGE]);
63 }
64
65 /* FIXME: Coverage from testable examples as well. */
66 static void do_compile_coverage_tests(struct manifest *m,
67                                       unsigned int *timeleft,
68                                       struct score *score)
69 {
70         char *cmdout;
71         struct ccan_file *i;
72         struct list_head *h;
73         bool ok;
74         char *f = cflags_list(m);
75         tal_append_fmt(&f, " %s", COVERAGE_CFLAGS);
76
77         /* For API tests, we need coverage version of module. */
78         if (!list_empty(&m->api_tests)) {
79                 build_objects(m, score, f, COMPILE_COVERAGE);
80                 if (!score->pass) {
81                         score->error = tal_strdup(score,
82                                                      "Failed to compile module objects with coverage");
83                         return;
84                 }
85         }
86
87         foreach_ptr(h, &m->run_tests, &m->api_tests) {
88                 list_for_each(h, i, list) {
89                         cov_compile(m, *timeleft, m, i, h == &m->api_tests);
90                 }
91         }
92
93         while ((i = collect_command(&ok, &cmdout)) != NULL) {
94                 if (!ok) {
95                         score_file_error(score, i, 0,
96                                          "Failed to compile test with coverage:"
97                                          " %s", cmdout);
98                 }
99         }
100         if (!score->error) {
101                 score->pass = true;
102                 score->score = score->total;
103         }
104 }
105
106 struct ccanlint tests_compile_coverage = {
107         .key = "tests_compile_coverage",
108         .name = "Module tests compile with " COVERAGE_CFLAGS,
109         .check = do_compile_coverage_tests,
110         .can_run = can_run_coverage,
111         .needs = "tests_compile"
112 };
113
114 REGISTER_TEST(tests_compile_coverage);