]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_coverage.c
ccanlint: fix coverage display for gcov 4.7
[ccan] / tools / ccanlint / tests / tests_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%...  Bonus for 100%. */
36 static unsigned int score_coverage(float covered, unsigned total)
37 {
38         float thresh, uncovered = 1.0 - covered;
39         unsigned int i;
40
41         if (covered == 1.0)
42                 return total;
43
44         total--;
45         for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
46                 if (uncovered > thresh)
47                         break;
48         }
49         return i;
50 }
51
52
53 /* FIXME: Don't know how stable this is.  Read cov files directly? */
54 static void analyze_coverage(struct manifest *m, bool full_gcov,
55                              const char *output, struct score *score)
56 {
57         char **lines = strsplit(score, output, "\n");
58         float covered_lines = 0.0;
59         unsigned int i, total_lines = 0;
60         bool lines_matter = false;
61
62         /*
63           Output looks like: (gcov 4.6.3)
64            File '../../../ccan/tdb2/private.h'
65            Lines executed:0.00% of 8
66            /home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
67
68            File '../../../ccan/tdb2/tdb.c'
69            Lines executed:0.00% of 450
70
71          For gcov 4.7.2:
72
73            File '/home/dwg/src/ccan/ccan/rfc822/test/run-check-check.c'
74            Lines executed:100.00% of 19
75            Creating 'run-check-check.c.gcov'
76         */
77
78         for (i = 0; lines[i]; i++) {
79                 if (strstarts(lines[i], "File '")) {
80                         char *file = lines[i] + strlen("File '");
81                         file[strcspn(file, "'")] = '\0';
82                         if (find_source_file(m, file))
83                                 lines_matter = true;
84                         else
85                                 lines_matter = false;
86                 } else if (lines_matter
87                            && strstarts(lines[i], "Lines executed:")) {
88                         float ex;
89                         unsigned of;
90                         if (sscanf(lines[i], "Lines executed:%f%% of %u",
91                                    &ex, &of) != 2)
92                                 errx(1, "Could not parse line '%s'", lines[i]);
93                         total_lines += of;
94                         covered_lines += ex / 100.0 * of;
95                 } else if (full_gcov
96                            && (strstr(lines[i], ":creating '")
97                                || strstarts(lines[i], "Creating '"))) {
98                         char *file, *filename, *apostrophe;
99                         apostrophe = strchr(lines[i], '\'');
100                         filename = apostrophe + 1;
101                         apostrophe = strchr(filename, '\'');
102                         *apostrophe = '\0';
103                         if (lines_matter) {
104                                 file = grab_file(score, filename, NULL);
105                                 if (!file) {
106                                         score->error = talloc_asprintf(score,
107                                                             "Reading %s",
108                                                             filename);
109                                         return;
110                                 }
111                                 printf("%s", file);
112                         }
113                         if (tools_verbose)
114                                 printf("Unlinking %s", filename);
115                         unlink(filename);
116                 }
117         }
118
119         score->pass = true;
120
121         if (verbose > 1)
122                 printf("%u of %u lines covered\n",
123                        (unsigned)covered_lines, total_lines);
124
125         /* Nothing covered?  We can't tell if there's a source file which
126          * was never executed, or there really is no code to execute, so
127          * assume the latter: this test deserves no score. */
128         if (total_lines == 0)
129                 score->total = score->score = 0;
130         else {
131                 score->total = 6;
132                 score->score = score_coverage(covered_lines / total_lines,
133                                               score->total);
134         }
135 }
136
137 static void do_run_coverage_tests(struct manifest *m,
138                                   unsigned int *timeleft, struct score *score)
139 {
140         struct ccan_file *i;
141         char *cmdout, *outdir;
142         char *covcmd;
143         bool full_gcov = (verbose > 1);
144         struct list_head *list;
145         bool ran_some = false;
146
147         /* This tells gcov where we put those .gcno files. */
148         outdir = talloc_dirname(score,
149                                 m->info_file->compiled[COMPILE_NORMAL]);
150         covcmd = talloc_asprintf(m, "gcov %s -o %s",
151                                  full_gcov ? "" : "-n",
152                                  outdir);
153
154         /* Run them all. */
155         foreach_ptr(list, &m->run_tests, &m->api_tests) {
156                 list_for_each(list, i, list) {
157                         if (run_command(score, timeleft, &cmdout,
158                                         "%s", i->compiled[COMPILE_COVERAGE])) {
159                                 covcmd = talloc_asprintf_append(covcmd, " %s",
160                                                                 i->fullname);
161                         } else {
162                                 score_file_error(score, i, 0,
163                                                  "Running test with coverage"
164                                                  " failed: %s", cmdout);
165                                 return;
166                         }
167                         ran_some = true;
168                 }
169         }
170
171         /* No tests at all?  0 out of 0 for you... */
172         if (!ran_some) {
173                 score->total = score->score = 0;
174                 score->pass = true;
175                 return;
176         }
177
178         /* Now run gcov: we want output even if it succeeds. */
179         if (!run_command(score, timeleft, &cmdout, "%s", covcmd)) {
180                 score->error = talloc_asprintf(score, "Running gcov: %s",
181                                                cmdout);
182                 return;
183         }
184
185         analyze_coverage(m, full_gcov, cmdout, score);
186 }
187
188 struct ccanlint tests_coverage = {
189         .key = "tests_coverage",
190         .name = "Module's tests cover all the code",
191         .check = do_run_coverage_tests,
192         .needs = "tests_compile_coverage tests_pass"
193 };
194
195 REGISTER_TEST(tests_coverage);