Debian Codegraph @

commonTools/test/coverage/gcovmerge

raw
1 #!/usr/bin/perl
2 #==========================================================================
3 # gcovmerge
4 #
5 # A script for automatically extracting and merging coverage information
6 # from C++ programs compiled using 'gcc -fprofile-arcs -ftest-coverage'.
7 # This script requires gcc 3.4.3 or above.
8 #
9 # Usage:
10 #
11 # gcovmerge [--with-files=min-max] \
12 # [--with-directories] \
13 # [--without-files] \
14 # [--with-total] \
15 # [--exclude=word] \
16 # [--source-dir=path] \
17 # [--with-gcov=GCOV] \
18 # [--old-school] \
19 # [--with-count] \
20 # [--with-percent] \
21 # [--with-report=REP] \
22 # [--with-full-paths] \
23 # [--help] \
24 # build-dir
25 #
26 # Arguments:
27 #
28 # build-dir
29 # The path to search for .gcda files (which are the
30 # markers for programs compiled with the appropriate
31 # arguments and later executed).
32 #
33 # --with-files=min-max [0-100]
34 # When specified, no coverage information is displayed for
35 # any individual files with a coverage percent outside of
36 # the range given.
37 #
38 # --with-directories
39 # When specified, coverage percentages are displayed for
40 # each directory examined.
41 #
42 # --without-files
43 # No coverage information will be given for individual files.
44 #
45 # --exclude=word []
46 # No files with word in their name or path will be examined.
47 # This option may be specified more than once, in order to
48 # exclude a variety of files.
49 #
50 # --with-total
51 # The total coverage for all files examined will be displayed.
52 #
53 # --source-dir=path
54 # The path to look for source files if the source and build
55 # directories are seperate. This directory should have an identical
56 # hierarchy to that of build_dir, or else this script will not be
57 # able to reconcile the data in source_dir with that in build_dir.
58 # If not specified, the script will assume that the source files
59 # are in build-dir.
60 #
61 # --with-gcov=GCOV
62 # Execute GCOV instead of "gcov". This is useful for using a
63 # different version of gcov than is located in the user path,
64 # or one with a different name (e.g., gcov-4.0)
65 #
66 # --old-school
67 # Revert to previous (ver 1.5) behavior of gcovmerge. Coverage
68 # information will be generated from a single executable per
69 # source file. This methodology results in lower and less accurate
70 # coverage numbers.
71 #
72 # --with-count
73 # Specifies that coverage should be reported as "n of t", instead of
74 # the percentage.
75 #
76 # --with-percent
77 # Specifies that coverage should be reported as a percentage.
78 # If neither --with-count nor --with-percent are specified,
79 # then --with-percent is the default.
80 #
81 # --with-report
82 # --with-report=REP
83 # Specifies that a report should be generated.
84 # This report will produce a listing of the
85 # all source files whose coverage was analyzed,
86 # wherein the lines of code are displayed as either
87 # covered, not-covered, or not compiled.
88 # If --with-report is given without the REP
89 # option, then the filename for the report
90 # will be coverage.html.
91 #
92 # --with-full-paths
93 # Outputs full pathnames instead of abbrieviated file names.
94 #
95 #
96 # Written by Joe Outzen, for the Trilinos project.
97 # Modified by Chris Baker ([email protected]) 2005-07-31
98 #
99 #==========================================================================
100
101 # base options
102 use Cwd;
103 chomp($base = cwd);
104 $maximum_per_file = 100;
105 $minimum_per_file = 0;
106 $directories = 0;
107 $total = 0;
108 @exclude_list = ();
109 @file_types = ("cpp", "c++", "c", "hpp", "h++", "h");
110 %source_files = ();
111 %file_exec = ();
112 %file_comp = ();
113 %path_exec = ();
114 %path_comp = ();
115 $total_exec = 0;
116 $total_comp = 0;
117 $show_count = 0;
118 $show_percent = 0;
119 @not_executed = ();
120 $gen_report = 0;
121 $full_paths = 0;
122
123 # parse command-line options
124 if (@ARGV == 0){
125 die "Incorrect syntax. Run 'gcovmerge --help' for help.\n";
126 }
127 $build_directory = pop @ARGV;
128 if ($build_directory =~ /--/){
129 # it was a command line argument. go ahead and process them, before
130 # failing. this gives "gcovmerge --help" a chance to do its thing.
131 push @ARGV, $build_directory;
132 undef $build_directory;
133 }
134 $source_directory = $build_directory;
135 $which_gcov = "gcov";
136 $oldschool = 0;
137 foreach (@ARGV){
138 if(/--with-files=(\d+)-(\d+)/){
139 $minimum_per_file = $1;
140 $maximum_per_file = $2;
141 }
142 elsif(/--with-directories/){
143 $directories = 1;
144 }
145 elsif(/--without-files/){
146 $minimum_per_file = 100;
147 $maximum_per_file = 0;
148 }
149 elsif(/--with-count/){
150 $with_count = 1;
151 }
152 elsif(/--with-percent/){
153 $with_percent = 1;
154 }
155 elsif(/--exclude=(.*)/){
156 push @exclude_list, $1;
157 }
158 elsif(/--with-total/){
159 $total = 1;
160 }
161 elsif(/--with-gcov=(.*)/){
162 $which_gcov = $1;
163 }
164 elsif(/--old-school/){
165 $oldschool = 1;
166 }
167 elsif(/--source-dir=(.*)/){
168 $source_directory = $1;
169 }
170 elsif(/--with-report=(.*)/){
171 $gen_report = 1;
172 $report_fn = $1;
173 }
174 elsif(/--with-report/){
175 $gen_report = 1;
176 $report_fn = "coverage.html";
177 }
178 elsif(/--with-full-paths/){
179 $full_paths = 1;
180 }
181 elsif(/--help/){
182 print "gcovmerge\n";
183 print "\n";
184 print "A script for automatically extracting and merging coverage information\n";
185 print "from C++ programs compiled using 'gcc -fprofile-arcs -ftest-coverage'.\n";
186 print "This script requires gcc 3.4.3 or above.\n";
187 print "\n";
188 print "Usage: \n";
189 print "\n";
190 print " gcovmerge [--with-files=min-max] \\\n";
191 print " [--with-directories] \\\n";
192 print " [--without-files] \\\n";
193 print " [--with-total] \\\n";
194 print " [--exclude=word] \\\n";
195 print " [--source-dir=path] \\\n";
196 print " [--with-gcov=GCOV] \\\n";
197 print " [--old-school] \\\n";
198 print " [--with-count] \\\n";
199 print " [--with-percent] \\\n";
200 print " [--with-report=REP] \\\n";
201 print " [--help] \\\n";
202 print " build-dir\n";
203 print "\n";
204 print "Arguments:\n";
205 print "\n";
206 print " build-dir\n";
207 print " The path to search for .gcda files (which are the\n";
208 print " markers for programs compiled with the appropriate\n";
209 print " arguments and later executed).\n";
210 print "\n";
211 print " --with-files=min-max [0-100]\n";
212 print " When specified, no coverage information is displayed for\n";
213 print " any individual files with a coverage percent outside of \n";
214 print " the range given.\n";
215 print "\n";
216 print " --with-directories\n";
217 print " When specified, coverage percentages are displayed for\n";
218 print " each directory examined.\n";
219 print "\n";
220 print " --without-files\n";
221 print " No coverage information will be given for individual files.\n";
222 print "\n";
223 print " --exclude=word []\n";
224 print " No files with word in their name or path will be examined.\n";
225 print " This option may be specified more than once, in order to\n";
226 print " exclude a variety of files.\n";
227 print "\n";
228 print " --with-total \n";
229 print " The total coverage for all files examined will be displayed.\n";
230 print "\n";
231 print " --source-dir=path\n";
232 print " The path to look for source files if the source and build\n";
233 print " directories are seperate. This directory should have an identical\n";
234 print " hierarchy to that of build_dir, or else this script will not be\n";
235 print " able to reconcile the data in source_dir with that in build_dir.\n";
236 print " If not specified, the script will assume that the source files\n";
237 print " are in build-dir.\n";
238 print "\n";
239 print " --with-gcov=GCOV\n";
240 print " Execute GCOV instead of \"gcov\". This is useful for using a\n";
241 print " different version of gcov than is located in the user path,\n";
242 print " or one with a different name (e.g., gcov-4.0)\n";
243 print "\n";
244 print " --old-school\n";
245 print " Revert to previous (ver 1.5) behavior of gcovmerge. Coverage\n";
246 print " information will be generated from a single executable per \n";
247 print " source file. This methodology results in lower and less accurate\n";
248 print " coverage numbers.\n";
249 print "\n";
250 print " --with-count\n";
251 print " Specifies that coverage should be reported as \"n of t\", instead of\n";
252 print " the percentage.\n";
253 print "\n";
254 print " --with-percent\n";
255 print " Specifies that coverage should be reported as a percentage.\n";
256 print " If neither --with-count nor --with-percent are specified,\n";
257 print " then --with-percent is the default.\n";
258 print "\n";
259 print " --with-report\n";
260 print " --with-report=REP\n";
261 print " Specifies that a report should be generated.\n";
262 print " This report will produce a listing of the\n";
263 print " all source files whose coverage was analyzed,\n";
264 print " wherein the lines of code are displayed as either\n";
265 print " covered, not-covered, or not compiled.\n";
266 print " If --with-report is given without the REP\n";
267 print " option, then the filename for the report\n";
268 print " will be coverage.html.\n";
269 print "\n";
270 print " --with-full-paths\n";
271 print " Outputs full pathnames instead of abbrieviated file names.\n";
272 print "\n";
273 print " --help\n";
274 print " Print this help output and exit.\n";
275 print "\n";
276 print " See README for more help.\n";
277 print "\n";
278
279 exit;
280 }
281 else{
282 die "96: Invalid paramater: $_";
283 }
284 }
285
286 if (!defined($build_directory)) {
287 die "Incorrect syntax. Run 'gcovmerge --help' for help.\n";
288 }
289
290 if ($with_percent==0 && $with_count==0) {
291 $with_percent = 1;
292 }
293
294 # find all source files of a type listed in @filetypes
295 $find_string = "find $source_directory";
296 foreach(@file_types){
297 $find_string .= " -name \"*.$_\" -or";
298 }
299 chop $find_string;
300 chop $find_string;
301 chop $find_string;
302 chop $find_string;
303
304 foreach(`$find_string`){
305 chomp;
306 $current_file = $_;
307 # skip files that match names in @exclude_list
308 $skip = 0;
309 foreach (@exclude_list){
310 if($current_file =~ /$_/){
311 $skip = 1;
312 last;
313 }
314 }
315 if($skip){
316 next;
317 }
318
319 # add this source file and its path to %source_files
320 s/^((.*\/)*)[^\/]+$/$1/ or die "128: Path-only regex failed for $_\n";
321 $current_file =~ s/^(.*\/)*([^\/]+)$/$2/ or die "Name-only regex failed for $_\n";
322 $source_files{$current_file} = $_;
323
324 # if we are keeping track of directory-based coverage, then put 0s into
325 # %path_exec and %path_comp
326 if($directories){
327 $path_exec{$_} = 0;
328 $path_comp{$_} = 0;
329 }
330 }
331
332 # find all .gcda files (which indicate a successful execution of code)
333 $last = "!";
334 foreach(`find $build_directory -name \"*.gcda\"`){
335 chomp;
336 s/^((.*\/)*)[^\/]+$/$1/ or die "145: Path-only regex failed for $_\n";
337
338 #skip this directory if we've already seen it
339 if($_ eq $last){
340 next;
341 }
342 $last = $_;
343
344 # run gcov on all the .gcda files in this directory
345 chdir $_ or die "could not cd to $_\n";
346 foreach(<*.gcda>){
347 system "$which_gcov $_ > /dev/null" and die "156: $which_gcov failed for $_\n";
348 }
349 chdir $base or die "158: could not cd to $base\n"
350 }
351
352 # start the report
353 if ($gen_report) {
354 open RPORT, "> $report_fn";
355 print RPORT "<html>\n";
356 print RPORT "<head>\n";
357 print RPORT "<style type=\"text/css\">\n";
358 print RPORT ".notcomp {color: grey; font-style: italic;}\n";
359 print RPORT ".cov {color: blue; font-style: normal;}\n";
360 print RPORT ".notcov {color: red; font-style: normal;}\n";
361 print RPORT ".file_header {background: gray; align: left; font-weight: bold;}\n";
362 print RPORT "</style>\n";
363 print RPORT "</head>\n";
364 print RPORT "<body>\n";
365 print RPORT "<h3>Source files:</h3>\n";
366 print RPORT "<ul>\n";
367 # list the source files
368 foreach(keys(%source_files)){
369 print RPORT "<li><a href=\"#$_\">$_</a>\n";
370 }
371 print RPORT "</ul>\n";
372 print RPORT "\n";
373 print RPORT "<strong>Color key:</strong>\n";
374 print RPORT "<ul>\n";
375 print RPORT "<li><span class=cov>Code was covered</span>\n";
376 print RPORT "<li><span class=notcov>Code was not covered</span>\n";
377 print RPORT "<li><span class=notcomp>Code was not compiled</span>\n";
378 print RPORT "</ul>\n";
379 print RPORT "\n";
380 print RPORT "<table align=center width=\"80%\">\n";
381 }
382
383 # for every source file, find all of the gcov files for the source file.
384 # then determine, for each executable line, whether it was executed or not.
385 foreach(keys(%source_files)){
386 undef %lines_exec;
387 undef %lines_comp;
388 $found = 0;
389 $compest = 0;
390 # find .gcov files for this source file
391 foreach(`find $build_dir -name \"$_.gcov\"`){
392 chomp;
393 open GCOV, "< $_";
394 foreach(<GCOV>){
395 # if a line in the .gcov file starts with either numbers or #s,
396 # then that line was compiled into executable form.
397 if(/^\s*([\d\#]+):\s*([\d]+):/){
398 $lineno = $2;
399 # if a line in the .gcov file starts with numbers, then that
400 # line was executed.
401 $lines_comp{$lineno} = 1;
402 if (!defined($lines_exec{$lineno})) {
403 $lines_exec{$lineno} = 0;
404 $compest++;
405 }
406 if($1 =~ /\d+/){
407 # mark the line as executed
408 $lines_exec{$lineno} = 1;
409 }
410 }
411 elsif(/^\s*-:\s*([\d]+):/){
412 $lines_comp{$1} = 0;
413 }
414 # we shouldn't need to look at more than one .gcov file per source
415 # file, so we can skip all the rest of the find.
416 $found = 1;
417 }
418 close GCOV;
419 if ($oldschool==1 && $found==1){
420 last;
421 }
422 }
423 if(!$found){
424 # deal with an unexecuted source file?
425 push @not_executed, $_;
426 }
427 else{
428 $exec = 0;
429 $comp = 0;
430 foreach (keys(%lines_exec)) {
431 $comp++;
432 $exec += $lines_exec{$_};
433 }
434 if ($comp != $compest){
435 print "Logic error in mygovmerge: coverage estimate may be incorrect.\n";
436 }
437 # record the results for this source file
438 $cov = $comp ? 100 * $exec / $comp : 0;
439 if($cov >= $minimum_per_file && $cov <= $maximum_per_file){
440 $file_exec{$source_files{$_}.$_} = $exec;
441 $file_comp{$source_files{$_}.$_} = $comp;
442 }
443 if($directories){
444 $path_exec{$source_files{$_}} += $exec;
445 $path_comp{$source_files{$_}} += $comp;
446 }
447 if($total){
448 $total_exec += $exec;
449 $total_comp += $comp;
450 }
451
452 # print the coverage report, if appropriate
453 if ($gen_report){
454 $cursrc = $source_files{$_}.$_;
455 print RPORT "<tr class=file_header><td class=file_header><a name=\"$_\"><span class=file_header>$cursrc</span></th></tr></span>\n";
456 print RPORT "<tr align=left><td><pre>\n";
457 open SRC, "< $cursrc";
458 $curln=0;
459 foreach(<SRC>){
460 chomp;
461 $_ =~ s/</&lt;/g;
462 $_ =~ s/>/&gt;/g;
463 $_ =~ s/\r//g;
464 $curln += 1;
465 if ($lines_comp{$curln}){
466 if ($lines_exec{$curln}){
467 print RPORT "<span class=\"cov\">$_</span>\n";
468 }
469 else {
470 print RPORT "<span class=\"notcov\">$_</span>\n";
471 }
472 }
473 else {
474 print RPORT "<span class=\"notcomp\">$_</span>\n";
475 }
476 }
477 close SRC;
478 print RPORT "</pre>\n";
479 print RPORT "</td></tr>\n\n";
480 print RPORT "<tr height=40><td>&nbsp;</td></tr>\n\n";
481 }
482 }
483 }
484
485 # output the results for files
486 foreach(sort(keys(%source_files))){
487 $fn = $_;
488 if($full_paths){
489 $fn = $source_files{$fn}.$fn;
490 }
491 $exec = $file_exec{$source_files{$_}.$_};
492 $comp = $file_comp{$source_files{$_}.$_};
493 if ($exec ne undef){
494 $perc = $comp ? 100 * $exec / $comp : 0;
495 if ($with_percent==1 && $with_count==0) {
496 printf "%-50s %8.4f%%\n", $fn, $perc;
497 }
498 elsif ($with_percent==0 && $with_count==1) {
499 printf "%-50s %5d / %5d\n", $fn, $exec, $comp;
500 }
501 else {
502 printf "%-50s %5d / %5d (%8.4f%%)\n", $fn, $exec, $comp, $perc;
503 }
504 }
505 }
506
507 if ($gen_report){
508 print RPORT "</table>\n";
509 if (@not_executed != 0) {
510 print RPORT "<h3>Files not executed:</h3>\n";
511 print RPORT "<ul>\n";
512 foreach(sort(@not_executed)){
513 $cursrc = $source_files{$_}.$_;
514 print RPORT "<li><a name=\"$_\">$cursrc\n";
515 }
516 print RPORT "</ul>\n";
517 }
518 }
519
520 # output the results for directories
521 foreach(sort(keys(%path_exec))){
522 $fn = $_;
523 $exec = $path_exec{$_};
524 $comp = $path_comp{$_};
525 $perc = $comp ? 100 * $exec / $comp : 0;
526 if ($with_percent==1 && $with_count==0) {
527 printf "%50s %8.4f%%\n", $fn, $perc;
528 }
529 elsif ($with_percent==0 && $with_count==1) {
530 printf "%50s %5d / %5d\n", $fn, $exec, $comp;
531 }
532 else {
533 printf "%50s %5d / %5d (%8.4f%%)\n", $fn, $exec, $comp, $perc;
534 }
535 }
536
537 # output the total results
538 if($total){
539 $exec = $total_exec{$_};
540 $comp = $total_comp{$_};
541 $perc = $comp ? 100 * $exec / $comp : 0;
542 if ($with_percent==1 && $with_count==0) {
543 printf "%50s %8.4f%%\n", $fn, $perc;
544 }
545 elsif ($with_percent==0 && $with_count==1) {
546 printf "%50s %5d / %5d\n", $fn, $exec, $comp;
547 }
548 else {
549 printf "%50s %5d / %5d (%8.4f%%)\n", $fn, $exec, $comp, $perc;
550 }
551 }
552
553 # finish the report
554 if ($gen_report) {
555 print RPORT "</body></html>\n";
556 close RPORT;
557 }