2014-10-31 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / contrib / mklog
blob6ed4c6e8f51ebbc182cfb9b6bbe59281d1c8f4d9
1 #!/usr/bin/perl
2 # Copyright (C) 2012-2014 Free Software Foundation, Inc.
4 # This file is part of GCC.
6 # GCC is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
11 # GCC is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GCC; see the file COPYING. If not, write to
18 # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 # Boston, MA 02110-1301, USA.
21 # This script parses a .diff file generated with 'diff -up' or 'diff -cp'
22 # and adds a skeleton ChangeLog file to the file. It does not try to be
23 # very smart when parsing function names, but it produces a reasonable
24 # approximation.
26 # Author: Diego Novillo <dnovillo@google.com> and
27 # Cary Coutant <ccoutant@google.com>
29 use File::Temp;
30 use File::Copy qw(cp mv);
32 # Change these settings to reflect your profile.
33 $username = $ENV{'USER'};
34 $name = `finger $username | grep -o 'Name: .*'`;
35 @n = split(/: /, $name);
36 $name = $n[1]; chop($name);
37 $addr = $username . "\@my.domain.org";
38 $date = `date +%Y-%m-%d`; chop ($date);
40 $gcc_root = $0;
41 $gcc_root =~ s/[^\\\/]+$/../;
43 # if this is a git tree then take name and email from the git configuration
44 if (-d "$gcc_root/.git") {
45 $gitname = `git config user.name`;
46 chomp($gitname);
47 if ($gitname) {
48 $name = $gitname;
51 $gitaddr = `git config user.email`;
52 chomp($gitaddr);
53 if ($gitaddr) {
54 $addr = $gitaddr;
58 #-----------------------------------------------------------------------------
59 # Program starts here. You should not need to edit anything below this
60 # line.
61 #-----------------------------------------------------------------------------
62 $inline = 0;
63 if ($#ARGV == 1 && ("$ARGV[0]" eq "-i" || "$ARGV[0]" eq "--inline")) {
64 shift;
65 $inline = 1;
66 } elsif ($#ARGV != 0) {
67 $prog = `basename $0`; chop ($prog);
68 print <<EOF;
69 usage: $prog [ -i | --inline ] file.diff
71 Generate ChangeLog template for file.diff.
72 It assumes that patch has been created with -up or -cp.
73 When -i is used, the ChangeLog template is followed by the contents of
74 file.diff.
75 When file.diff is -, read standard input.
76 When -i is used and file.diff is not -, it writes to file.diff, otherwise it
77 writes to stdout.
78 EOF
79 exit 1;
82 $diff = $ARGV[0];
83 $dir = `dirname $diff`; chop ($dir);
84 $basename = `basename $diff`; chop ($basename);
85 $hdrline = "$date $name <$addr>";
87 sub get_clname ($) {
88 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
90 my $dirname = $_[0];
91 while ($dirname) {
92 my $clname = "$dirname/ChangeLog";
93 if (-f "$gcc_root/$clname") {
94 my $relname = substr ($_[0], length ($dirname) + 1);
95 return ($clname, $relname);
96 } else {
97 $dirname =~ s/[\/\\]?[^\/\\]*$//;
101 return ('Unknown ChangeLog', $_[0]);
104 sub remove_suffixes ($) {
105 my $filename = $_[0];
106 $filename =~ s/^[ab]\///;
107 $filename =~ s/\.jj$//;
108 return $filename;
111 # Check if line is a top-level declaration.
112 # TODO: ignore preprocessor directives except maybe #define ?
113 sub is_top_level {
114 my ($function, $is_context_diff) = (@_);
115 if ($is_context_diff) {
116 $function =~ s/^..//;
117 } else {
118 $function =~ s/^.//;
120 return $function && $function !~ /^[\s{}]/;
123 # For every file in the .diff print all the function names in ChangeLog
124 # format.
125 %cl_entries = ();
126 $change_msg = undef;
127 $look_for_funs = 0;
128 $clname = get_clname('');
129 open (DFILE, $diff) or die "Could not open file $diff for reading";
130 chomp (my @diff_lines = <DFILE>);
131 close (DFILE);
132 $line_idx = 0;
133 foreach (@diff_lines) {
134 # Stop processing functions if we found a new file.
135 # Remember both left and right names because one may be /dev/null.
136 # Don't be fooled by line markers in case of context diff.
137 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
138 $left = remove_suffixes ($1);
139 $look_for_funs = 0;
141 if (!/---$/ && /^--- +(\S+)?/) {
142 $right = remove_suffixes ($1);
143 $look_for_funs = 0;
146 # Check if the body of diff started.
147 # We should now have both left and right name,
148 # so we can decide filename.
150 if ($left && (/^\*{15}/ || /^@@ /)) {
151 # If we have not seen any function names in the previous file (ie,
152 # $change_msg is empty), we just write out a ':' before starting the next
153 # file.
154 if ($clname) {
155 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
158 if ($left eq $right) {
159 $filename = $left;
160 } elsif($left eq '/dev/null') {
161 $filename = $right;
162 } elsif($right eq '/dev/null') {
163 $filename = $left;
164 } else {
165 print STDERR "Error: failed to parse diff for $left and $right\n";
166 exit 1;
168 $left = $right = undef;
169 ($clname, $relname) = get_clname ($filename);
170 $cl_entries{$clname} .= "\t* $relname";
171 $change_msg = '';
172 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
175 # Context diffs have extra whitespace after first char;
176 # remove it to make matching easier.
177 if ($is_context_diff) {
178 s/^([-+! ]) /\1/;
181 # Remember the last line in a diff block that might start
182 # a new function.
183 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
184 $save_fn = $1;
187 # Check if file is newly added.
188 # Two patterns: for context and unified diff.
189 if (/^\*\*\* 0 \*\*\*\*/
190 || /^@@ -0,0 \+1.* @@/) {
191 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ? ": New test.\n" : ": New file.\n";
192 $look_for_funs = 0;
195 # Check if file was removed.
196 # Two patterns: for context and unified diff.
197 if (/^--- 0 ----/
198 || /^@@ -1.* \+0,0 @@/) {
199 $change_msg = ": Remove.\n";
200 $look_for_funs = 0;
203 # Mark if we met doubtfully changed function.
204 $doubtfunc = 0;
205 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
206 $doubtfunc = 1;
207 $is_context_diff = 0;
209 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
210 $doubtfunc = 1;
211 $is_context_diff = 1;
214 # If we find a new function, print it in brackets. Special case if
215 # this is the first function in a file.
217 # Note that we don't try too hard to find good matches. This should
218 # return a superset of the actual set of functions in the .diff file.
220 # The first pattern works with context diff files (diff -c). The
221 # second pattern works with unified diff files (diff -u).
223 # The third pattern looks for the starts of functions or classes
224 # within a diff block both for context and unified diff files.
226 if ($look_for_funs
227 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
228 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
229 || /^[-+! ](\{)/))
231 $_ = $1;
232 my $fn;
233 if (/^\{/) {
234 # Beginning of a new function.
235 $_ = $save_fn;
236 } else {
237 $save_fn = "";
239 if (/;$/) {
240 # No usable function name found.
241 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
242 # Discard stuff after the class/struct/etc. tag.
243 $fn = $1;
244 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
245 # Discard template and function parameters.
246 $fn = $1;
247 1 while ($fn =~ s/<[^<>]*>//);
248 $fn =~ s/[ \t]*$//;
250 # Check is function really modified
251 $no_real_change = 0;
252 if ($doubtfunc) {
253 $idx = $line_idx;
254 # Skip line info in context diffs.
255 while ($is_context_diff && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
256 ++$idx;
258 # Check all lines till the first change
259 # for the presence of really changed function
260 do {
261 ++$idx;
262 $no_real_change = is_top_level ($diff_lines[$idx], $is_context_diff);
263 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/))
265 if ($fn && !$seen_names{$fn} && !$no_real_change) {
266 # If this is the first function in the file, we display it next
267 # to the filename, so we need an extra space before the opening
268 # brace.
269 if (!$change_msg) {
270 $change_msg .= " ";
271 } else {
272 $change_msg .= "\t";
275 $change_msg .= "($fn):\n";
276 $seen_names{$fn} = 1;
279 $line_idx++;
282 # If we have not seen any function names (ie, $change_msg is empty), we just
283 # write out a ':'. This happens when there is only one file with no
284 # functions.
285 $cl_entries{$clname} .= $change_msg ? "$change_msg\n" : ":\n";
287 if ($inline && $diff ne "-") {
288 # Get a temp filename, rather than an open filehandle, because we use
289 # the open to truncate.
290 $tmp = mktemp("tmp.XXXXXXXX") or die "Could not create temp file: $!";
292 # Copy the permissions to the temp file (in File::Copy module version
293 # 2.15 and later).
294 cp $diff, $tmp or die "Could not copy patch file to temp file: $!";
296 # Open the temp file, clearing contents.
297 open (OUTPUTFILE, '>', $tmp) or die "Could not open temp file: $!";
298 } else {
299 *OUTPUTFILE = STDOUT;
302 # Print the log
303 foreach my $clname (keys %cl_entries) {
304 print OUTPUTFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
307 if ($inline) {
308 # Append the patch to the log
309 foreach (@diff_lines) {
310 print OUTPUTFILE "$_\n";
314 if ($inline && $diff ne "-") {
315 # Close $tmp
316 close(OUTPUTFILE);
318 # Write new contents to $diff atomically
319 mv $tmp, $diff or die "Could not move temp file to patch file: $!";
322 exit 0;