std/c/README.md: Add section skeletons and don't indent lists
[sunny256-utils.git] / gcov-cmt
blobd4abf2dc09966781ea3b139d85c4ca7c9a11edb2
1 #!/usr/bin/env perl
3 #==============================================================================
4 # gcov-cmt
5 # File ID: a6fde67c-bd88-11e7-93f2-f74d993421b0
7 # Add "/* gcov */" markers to lines in .c files reported by gcov(1) as unused.
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.1';
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 =~ /\#\#\#\#\#/
96 && !($line =~ m'/\* gncov \*/'
97 && !$Opt{'gncov'}
99 ) {
100 $c3 .= " /* gcov */";
102 print(to_fp "$c3\n");
106 close(to_fp);
107 close(from_fp);
108 rename("$f.tmp", $f) or return 1;
109 return 0;
112 sub print_version {
113 # Print program version
114 print("$progname $VERSION\n");
115 return;
118 sub usage {
119 # Send the help message to stdout
120 my $Retval = shift;
122 if ($Opt{'verbose'}) {
123 print("\n");
124 print_version();
126 print(<<"END");
128 Usage: $progname [options] C_FILES [...]
130 Add "/* gcov */" markers to lines in .c files reported by gcov(1) as
131 unused.
133 \$ make clean test
134 \$ gcov *.c
135 \$ $progname *.c
137 Options:
139 -d, --delete
140 Delete existing markers.
141 -g, --gncov
142 Mark lines even when they contain the gncov marker.
143 -h, --help
144 Show this help.
145 -q, --quiet
146 Be more quiet. Can be repeated to increase silence.
147 -v, --verbose
148 Increase level of verbosity. Can be repeated.
149 --version
150 Print version information.
152 To suppress the gcov marker for some lines, add the string "/* gncov */"
153 to the relevant lines. This is for lines that are difficult to test
154 (full disk, memory full, long paths, etc).
157 exit($Retval);
160 sub msg {
161 # Print a status message to stderr based on verbosity level
162 my ($verbose_level, $Txt) = @_;
164 if ($Opt{'verbose'} >= $verbose_level) {
165 print(STDERR "$progname: $Txt\n");
167 return;
170 __END__
172 # This program is free software; you can redistribute it and/or modify it under
173 # the terms of the GNU General Public License as published by the Free Software
174 # Foundation; either version 2 of the License, or (at your option) any later
175 # version.
177 # This program is distributed in the hope that it will be useful, but WITHOUT
178 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
179 # FOR A PARTICULAR PURPOSE.
180 # See the GNU General Public License for more details.
182 # You should have received a copy of the GNU General Public License along with
183 # this program.
184 # If not, see L<http://www.gnu.org/licenses/>.
186 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :