2 # Simple script for updating the prototypes in a C header file
4 # Copyright (C) 2006 Jelmer Vernooij <jelmer@samba.org>
5 # Published under the GNU GPL
13 update-proto - automatically update prototypes in header files
17 update-proto [OPTIONS] <HEADER> <C-FILE>...
19 update-proto [OPTIONS] <HEADER>
23 Update-proto makes sure the prototypes in a C header file are current
24 by comparing the existing prototypes in a header with the function definition
25 in the source file. It aims to keep the diff between the original header
26 and generated one as small as possible.
28 New prototypes are inserted before any line that contains the following comment:
30 /* New prototypes are inserted above this line */
32 It will automatically parse C files after it encounters a line that contains:
34 /* The following definitions come from FILE */
36 When two or more prototypes exist for a function, only the first one
45 Increase verbosity. Currently only two levels of verbosity are used.
55 Strange complex functions are not recognized. In particular those
56 created by macros or returning (without typedef) function pointers.
60 update-proto is licensed under the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
64 update-proto was written by Jelmer Vernooij L<jelmer@samba.org>.
70 print "Usage: update-proto.pl [OPTIONS] <HEADER> <C-FILE>...\n";
76 print "Usage: update-proto.pl [OPTIONS] <HEADER> <C-FILE>...\n";
78 print " --help Show this help message\n";
79 print " --verbose Write changes made to standard error\n\n";
89 'v|verbose' => sub { $verbose += 1; }
96 while($s =~ s/^(.)//) { $count++ if $1 eq $t; }
100 my $header = shift @ARGV;
106 while (my $line = <IN
>) {
110 next if /^\/|[;]|^#|}|^\s*static/;
112 my $public = s/_PUBLIC_//g;
113 s/_PRINTF_ATTRIBUTE\([^)]+\)//g;
114 next unless /^(struct\s+\w+|union\s+\w+|\w+)\s+\**\s*(\w+)\s*\((.*)$/;
118 next if ($name eq "main");
120 # Read continuation lines if any
121 my $prn = 1 + count
("(", $3) - count
(")", $3);
125 $l or die("EOF while parsing function prototype");
127 $prn += count
("(", $l) - count
(")", $l);
132 # Strip off possible start of function
135 $new_protos{$name} = "$line;";
140 process_file
($_) foreach (@ARGV);
147 sub insert_new_protos
()
149 foreach (keys %new_protos) {
150 print "$new_protos{$_}\n";
151 print STDERR
"Inserted prototype for `$_'\n" if ($verbose);
157 my $blankline_due = 0;
159 open (HDR
, "<$header");
160 while (my $line = <HDR
>) {
166 # Recognize C files that prototypes came from
167 if ($line =~ /\/\
* The following definitions come from
(.*) \
*\
//) {
169 if ($blankline_due) {
178 if ($blankline_due) {
183 # Insert prototypes that weren't in the header before
184 if ($line =~ /\/\
* New prototypes are inserted above this line
.*\
*\
/\s*/) {
190 if ($line =~ /^\s*typedef |^\#|^\s*static/) {
197 my $public = s/_PUBLIC_//g;
198 s/_PRINTF_ATTRIBUTE\([^)]+\)//g;
199 unless (/^(struct\s+\w+|union\s+\w+|\w+)\s+\**\s*(\w+)\s*\((.*)$/) {
204 # Read continuation lines if any
205 my $prn = 1 + count
("(", $3) - count
(")", $3);
209 $l or die("EOF while parsing function prototype");
211 $prn += count
("(", $l) - count
(")", $l);
216 # This prototype is for a function that was removed
217 unless (defined($new_protos{$name})) {
219 print STDERR
"Removed prototype for `$name'\n" if ($verbose);
226 if ($new_protos{$name} ne $nline) {
228 print STDERR
"Updated prototype for `$name'\n" if ($verbose);
229 print "$new_protos{$name}\n";
232 print STDERR
"Prototype for `$name' didn't change\n" if ($verbose > 1);
236 delete $new_protos{$name};
240 print STDERR
"$added added, $modified modified, $deleted deleted, $kept unchanged.\n";