]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/run-coverage.c
ccanlint: use valgrind options when debugging too
[ccan] / tools / ccanlint / tests / run-coverage.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str_talloc/str_talloc.h>
5 #include <ccan/grab_file/grab_file.h>
6 #include <ccan/str/str.h>
7 #include <ccan/foreach/foreach.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <limits.h>
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <err.h>
17 #include <string.h>
18 #include <ctype.h>
19
20 static bool find_source_file(const struct manifest *m, const char *filename)
21 {
22         const struct ccan_file *i;
23
24         list_for_each(&m->c_files, i, list) {
25                 if (streq(i->fullname, filename))
26                         return true;
27         }
28         list_for_each(&m->h_files, i, list) {
29                 if (streq(i->fullname, filename))
30                         return true;
31         }
32         return false;
33 }
34
35 /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... */
36 static unsigned int score_coverage(float covered, unsigned total)
37 {
38         float thresh, uncovered = 1.0 - covered;
39         unsigned int i;
40
41         for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
42                 if (uncovered > thresh)
43                         break;
44         }
45         return i;
46 }
47
48
49 /* FIXME: Don't know how stable this is.  Read cov files directly? */
50 static void analyze_coverage(struct manifest *m, bool full_gcov,
51                              const char *output, struct score *score)
52 {
53         char **lines = strsplit(score, output, "\n", NULL);
54         float covered_lines = 0.0;
55         unsigned int i, total_lines = 0;
56         bool lines_matter = false;
57
58         /*
59           Output looks like:
60            File '../../../ccan/tdb2/private.h'
61            Lines executed:0.00% of 8
62            /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
63
64            File '../../../ccan/tdb2/tdb.c'
65            Lines executed:0.00% of 450
66         */
67
68         for (i = 0; lines[i]; i++) {
69                 if (strstarts(lines[i], "File '")) {
70                         char *file = lines[i] + strlen("File '");
71                         file[strcspn(file, "'")] = '\0';
72                         if (find_source_file(m, file))
73                                 lines_matter = true;
74                         else
75                                 lines_matter = false;
76                 } else if (lines_matter
77                            && strstarts(lines[i], "Lines executed:")) {
78                         float ex;
79                         unsigned of;
80                         if (sscanf(lines[i], "Lines executed:%f%% of %u",
81                                    &ex, &of) != 2)
82                                 errx(1, "Could not parse line '%s'", lines[i]);
83                         total_lines += of;
84                         covered_lines += ex / 100.0 * of;
85                 } else if (full_gcov && strstr(lines[i], ":creating '")) {
86                         char *file, *filename, *apostrophe;
87                         apostrophe = strchr(lines[i], '\'');
88                         filename = apostrophe + 1;
89                         apostrophe = strchr(filename, '\'');
90                         *apostrophe = '\0';
91                         if (lines_matter) {
92                                 file = grab_file(score, filename, NULL);
93                                 if (!file) {
94                                         score->error = talloc_asprintf(score,
95                                                             "Reading %s",
96                                                             filename);
97                                         return;
98                                 }
99                                 printf("%s", file);
100                         }
101                         if (tools_verbose)
102                                 printf("Unlinking %s", filename);
103                         unlink(filename);
104                 }
105         }
106
107         score->pass = true;
108
109         /* Nothing covered?  We can't tell if there's a source file which
110          * was never executed, or there really is no code to execute, so
111          * assume the latter: this test deserves no score. */
112         if (total_lines == 0)
113                 score->total = score->score = 0;
114         else {
115                 score->total = 5;
116                 score->score = score_coverage(covered_lines / total_lines,
117                                               score->total);
118         }
119 }
120
121 static void do_run_coverage_tests(struct manifest *m,
122                                   bool keep,
123                                   unsigned int *timeleft, struct score *score)
124 {
125         struct ccan_file *i;
126         char *cmdout;
127         char *covcmd;
128         bool full_gcov = (verbose > 1);
129         struct list_head *list;
130
131         /* This tells gcov where we put those .gcno files. */
132         covcmd = talloc_asprintf(m, "gcov %s -o %s",
133                                  full_gcov ? "" : "-n",
134                                  talloc_dirname(score, m->info_file->compiled));
135
136         /* Run them all. */
137         foreach_ptr(list, &m->run_tests, &m->api_tests) {
138                 list_for_each(list, i, list) {
139                         if (run_command(score, timeleft, &cmdout,
140                                         "%s", i->cov_compiled)) {
141                                 covcmd = talloc_asprintf_append(covcmd, " %s",
142                                                                 i->fullname);
143                         } else {
144                                 score->error = "Running test with coverage";
145                                 score_file_error(score, i, 0, cmdout);
146                                 return;
147                         }
148                 }
149         }
150
151         /* Now run gcov: we want output even if it succeeds. */
152         if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
153                 score->error = talloc_asprintf(score, "Running gcov: %s",
154                                                cmdout);
155                 return;
156         }
157
158         analyze_coverage(m, full_gcov, cmdout, score);
159 }
160
161 struct ccanlint run_coverage_tests = {
162         .key = "test-coverage",
163         .name = "Code coverage of module tests",
164         .check = do_run_coverage_tests,
165 };
166
167 REGISTER_TEST(run_coverage_tests, &compile_coverage_tests, &run_tests, NULL);