s3: client - rename 'struct smb2_create_returns' to 'struct smb_create_returns' so...
[Samba.git] / source4 / script / update-proto.pl
blobc130650fb827473d72cae54939e62ea21f370714
1 #!/usr/bin/perl
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
7 use strict;
8 use warnings;
9 use Getopt::Long;
11 =head1 NAME
13 update-proto - automatically update prototypes in header files
15 =head1 SYNOPSIS
17 update-proto [OPTIONS] <HEADER> <C-FILE>...
19 update-proto [OPTIONS] <HEADER>
21 =head1 DESCRIPTION
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
37 will be kept.
39 =head1 OPTIONS
41 =over 4
43 =item I<--verbose|-v>
45 Increase verbosity. Currently only two levels of verbosity are used.
47 =item I<--help>
49 Show list of options
51 =back
53 =head1 BUGS
55 Strange complex functions are not recognized. In particular those
56 created by macros or returning (without typedef) function pointers.
58 =head1 LICENSE
60 update-proto is licensed under the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
62 =head1 AUTHOR
64 update-proto was written by Jelmer Vernooij L<jelmer@samba.org>.
66 =cut
68 sub Usage()
70 print "Usage: update-proto.pl [OPTIONS] <HEADER> <C-FILE>...\n";
71 exit 1;
74 sub Help()
76 print "Usage: update-proto.pl [OPTIONS] <HEADER> <C-FILE>...\n";
77 print "Options:\n";
78 print " --help Show this help message\n";
79 print " --verbose Write changes made to standard error\n\n";
80 exit 0;
83 my %new_protos = ();
85 my $verbose = 0;
87 GetOptions(
88 'help|h' => \&Help,
89 'v|verbose' => sub { $verbose += 1; }
90 ) or Usage();
92 sub count($$)
94 my ($t, $s) = @_;
95 my $count = 0;
96 while($s =~ s/^(.)//) { $count++ if $1 eq $t; }
97 return $count;
100 my $header = shift @ARGV;
102 sub process_file($)
104 my $file = shift;
105 open (IN, "<$file");
106 while (my $line = <IN>) {
107 $_ = $line;
108 next if /^\s/;
109 next unless /\(/;
110 next if /^\/|[;]|^#|}|^\s*static/;
111 s/\/\*(.*?)\*\///g;
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*\((.*)$/;
116 my $name = $2;
118 next if ($name eq "main");
120 # Read continuation lines if any
121 my $prn = 1 + count("(", $3) - count(")", $3);
123 while ($prn) {
124 my $l = <IN>;
125 $l or die("EOF while parsing function prototype");
126 $line .= $l;
127 $prn += count("(", $l) - count(")", $l);
130 $line =~ s/\n$//;
132 # Strip off possible start of function
133 $line =~ s/{\s*$//g;
135 $new_protos{$name} = "$line;";
137 close(IN);
140 process_file($_) foreach (@ARGV);
142 my $added = 0;
143 my $modified = 0;
144 my $deleted = 0;
145 my $kept = 0;
147 sub insert_new_protos()
149 foreach (keys %new_protos) {
150 print "$new_protos{$_}\n";
151 print STDERR "Inserted prototype for `$_'\n" if ($verbose);
152 $added+=1;
154 %new_protos = ();
157 my $blankline_due = 0;
159 open (HDR, "<$header");
160 while (my $line = <HDR>) {
161 if ($line eq "\n") {
162 $blankline_due = 1;
163 $line = <HDR>;
166 # Recognize C files that prototypes came from
167 if ($line =~ /\/\* The following definitions come from (.*) \*\//) {
168 insert_new_protos();
169 if ($blankline_due) {
170 print "\n";
171 $blankline_due = 0;
173 process_file($1);
174 print "$line";
175 next;
178 if ($blankline_due) {
179 print "\n";
180 $blankline_due = 0;
183 # Insert prototypes that weren't in the header before
184 if ($line =~ /\/\* New prototypes are inserted above this line.*\*\/\s*/) {
185 insert_new_protos();
186 print "$line\n";
187 next;
190 if ($line =~ /^\s*typedef |^\#|^\s*static/) {
191 print "$line";
192 next;
195 $_ = $line;
196 s/\/\*(.*?)\*\///g;
197 my $public = s/_PUBLIC_//g;
198 s/_PRINTF_ATTRIBUTE\([^)]+\)//g;
199 unless (/^(struct\s+\w+|union\s+\w+|\w+)\s+\**\s*(\w+)\s*\((.*)$/) {
200 print "$line";
201 next;
204 # Read continuation lines if any
205 my $prn = 1 + count("(", $3) - count(")", $3);
207 while ($prn) {
208 my $l = <HDR>;
209 $l or die("EOF while parsing function prototype");
210 $line .= $l;
211 $prn += count("(", $l) - count(")", $l);
214 my $name = $2;
216 # This prototype is for a function that was removed
217 unless (defined($new_protos{$name})) {
218 $deleted+=1;
219 print STDERR "Removed prototype for `$name'\n" if ($verbose);
220 next;
223 my $nline = $line;
224 chop($nline);
226 if ($new_protos{$name} ne $nline) {
227 $modified+=1;
228 print STDERR "Updated prototype for `$name'\n" if ($verbose);
229 print "$new_protos{$name}\n";
230 } else {
231 $kept+=1;
232 print STDERR "Prototype for `$name' didn't change\n" if ($verbose > 1);
233 print "$line";
236 delete $new_protos{$name};
238 close(HDR);
240 print STDERR "$added added, $modified modified, $deleted deleted, $kept unchanged.\n";