mktar: Use `wc` instead of `du` in summary message
[sunny256-utils.git] / gcov-cmt
blob41c60cae46a4b59516845f5dfafc1bb64e5a53ef
1 #!/usr/bin/env perl
3 #==============================================================================
4 # gcov-cmt
5 # File ID: a6fde67c-bd88-11e7-93f2-f74d993421b0
7 # [Description]
9 # Character set: UTF-8
10 # ©opyleft 2017– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
12 # legal stuff.
13 #==============================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'delete' => 0,
24 'gncov' => 0,
25 'help' => 0,
26 'quiet' => 0,
27 'verbose' => 0,
28 'version' => 0,
32 our $progname = $0;
33 $progname =~ s/^.*\/(.*?)$/$1/;
34 our $VERSION = '0.3.0';
36 Getopt::Long::Configure('bundling');
37 GetOptions(
39 'delete|d' => \$Opt{'delete'},
40 'gncov|g' => \$Opt{'gncov'},
41 'help|h' => \$Opt{'help'},
42 'quiet|q+' => \$Opt{'quiet'},
43 'verbose|v+' => \$Opt{'verbose'},
44 'version' => \$Opt{'version'},
46 ) || die("$progname: Option error. Use -h for help.\n");
48 $Opt{'verbose'} -= $Opt{'quiet'};
49 $Opt{'help'} && usage(0);
50 if ($Opt{'version'}) {
51 print_version();
52 exit(0);
55 exit(main());
57 sub main {
58 my $Retval = 0;
60 for my $f (@ARGV) {
61 msg(1, "Processing $f");
62 if (process_file($f)) {
63 print(STDERR "$progname: $f: " .
64 "Error when processing file: $!\n");
67 return $Retval;
70 sub process_file {
71 my $f = shift;
72 my $line;
74 if ($Opt{'delete'}) {
75 open(from_fp, "<$f") or return 1;
76 open(to_fp, ">$f.tmp") or return 1;
77 while ($line = <from_fp>) {
78 chomp($line);
79 $line =~ s! /\* gcov \*/$!!;
80 print(to_fp "$line\n");
82 } else {
83 if (!-f "$f.gcov") {
84 printf(STDERR "$progname: $f.gcov not found\n");
85 return 0;
87 open(from_fp, "<$f.gcov") or return 1;
88 open(to_fp, ">$f.tmp") or return 1;
89 while ($line = <from_fp>) {
90 chomp($line);
91 if ($line =~ /^(.+?): +(\d+):(.*)$/) {
92 my ($c1, $c2, $c3) = ($1, $2, $3);
93 $c2 eq "0" && next;
94 $c3 =~ s! /\* gcov \*/$!!;
95 if ($c1 =~ /\#\#\#\#\#/ &&
97 $line =~ m'/\* gncov \*/' &&
98 !$Opt{'gncov'}
101 $c3 .= " /* gcov */";
103 print(to_fp "$c3\n");
107 close(to_fp);
108 close(from_fp);
109 rename("$f.tmp", $f) or return 1;
110 return 0;
113 sub print_version {
114 # Print program version
115 print("$progname $VERSION\n");
116 return;
119 sub usage {
120 # Send the help message to stdout
121 my $Retval = shift;
123 if ($Opt{'verbose'}) {
124 print("\n");
125 print_version();
127 print(<<"END");
129 Usage: $progname [options] C_FILES [...]
131 Add "/* gcov */" markers to lines in .c files reported by gcov(1) as
132 unused.
134 \$ make clean test
135 \$ gcov *.c
136 \$ $progname *.c
138 Options:
140 -d, --delete
141 Delete existing markers.
142 -g, --gncov
143 Mark lines even when they contain the gncov marker.
144 -h, --help
145 Show this help.
146 -q, --quiet
147 Be more quiet. Can be repeated to increase silence.
148 -v, --verbose
149 Increase level of verbosity. Can be repeated.
150 --version
151 Print version information.
153 To suppress the gcov marker for some lines, add the string "/* gncov */"
154 to the relevant lines. This is for lines that are difficult to test
155 (full disk, memory full, long paths, etc).
158 exit($Retval);
161 sub msg {
162 # Print a status message to stderr based on verbosity level
163 my ($verbose_level, $Txt) = @_;
165 if ($Opt{'verbose'} >= $verbose_level) {
166 print(STDERR "$progname: $Txt\n");
168 return;
171 __END__
173 # This program is free software; you can redistribute it and/or modify it under
174 # the terms of the GNU General Public License as published by the Free Software
175 # Foundation; either version 2 of the License, or (at your option) any later
176 # version.
178 # This program is distributed in the hope that it will be useful, but WITHOUT
179 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
180 # FOR A PARTICULAR PURPOSE.
181 # See the GNU General Public License for more details.
183 # You should have received a copy of the GNU General Public License along with
184 # this program.
185 # If not, see L<http://www.gnu.org/licenses/>.
187 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :