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>
20 static bool find_source_file(const struct manifest *m, const char *filename)
22 const struct ccan_file *i;
24 list_for_each(&m->c_files, i, list) {
25 if (streq(i->fullname, filename))
28 list_for_each(&m->h_files, i, list) {
29 if (streq(i->fullname, filename))
35 /* 1 point for 50%, 2 points for 75%, 3 points for 87.5%... */
36 static unsigned int score_coverage(float covered, unsigned total)
38 float thresh, uncovered = 1.0 - covered;
41 for (i = 0, thresh = 0.5; i < total; i++, thresh /= 2) {
42 if (uncovered > thresh)
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)
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;
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'
64 File '../../../ccan/tdb2/tdb.c'
65 Lines executed:0.00% of 450
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))
76 } else if (lines_matter
77 && strstarts(lines[i], "Lines executed:")) {
80 if (sscanf(lines[i], "Lines executed:%f%% of %u",
82 errx(1, "Could not parse line '%s'", lines[i]);
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, '\'');
92 file = grab_file(score, filename, NULL);
94 score->error = talloc_asprintf(score,
102 printf("Unlinking %s", filename);
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;
116 score->score = score_coverage(covered_lines / total_lines,
121 static void do_run_coverage_tests(struct manifest *m,
123 unsigned int *timeleft, struct score *score)
128 bool full_gcov = (verbose > 1);
129 struct list_head *list;
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));
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",
144 score->error = "Running test with coverage";
145 score_file_error(score, i, 0, cmdout);
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",
158 analyze_coverage(m, full_gcov, cmdout, score);
161 struct ccanlint run_coverage_tests = {
162 .key = "test-coverage",
163 .name = "Code coverage of module tests",
164 .check = do_run_coverage_tests,
167 REGISTER_TEST(run_coverage_tests, &compile_coverage_tests, &run_tests, NULL);