2014-11-13 Richard Biener <rguenther@suse.de>
[official-gcc.git] / contrib / mklog
blob8412d38fbf678f20c3cb5e42cb3dc364d7f913d3
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 sub is_context_hunk_start {
112 return @_[0] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/;
115 sub is_unified_hunk_start {
116 return @_[0] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/;
119 # Check if line is a top-level declaration.
120 # TODO: ignore preprocessor directives except maybe #define ?
121 sub is_top_level {
122 my ($function, $is_context_diff) = (@_);
123 if (is_unified_hunk_start ($function)
124 || is_context_hunk_start ($function)) {
125 return 1;
127 if ($is_context_diff) {
128 $function =~ s/^..//;
129 } else {
130 $function =~ s/^.//;
132 return $function && $function !~ /^[\s{]/;
135 # For every file in the .diff print all the function names in ChangeLog
136 # format.
137 %cl_entries = ();
138 $change_msg = undef;
139 $look_for_funs = 0;
140 $clname = get_clname('');
141 open (DFILE, $diff) or die "Could not open file $diff for reading";
142 chomp (my @diff_lines = <DFILE>);
143 close (DFILE);
144 $line_idx = 0;
145 foreach (@diff_lines) {
146 # Stop processing functions if we found a new file.
147 # Remember both left and right names because one may be /dev/null.
148 # Don't be fooled by line markers in case of context diff.
149 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
150 $left = remove_suffixes ($1);
151 $look_for_funs = 0;
153 if (!/---$/ && /^--- +(\S+)?/) {
154 $right = remove_suffixes ($1);
155 $look_for_funs = 0;
158 # Check if the body of diff started.
159 # We should now have both left and right name,
160 # so we can decide filename.
162 if ($left && (/^\*{15}/ || /^@@ /)) {
163 # If we have not seen any function names in the previous file (ie,
164 # $change_msg is empty), we just write out a ':' before starting the next
165 # file.
166 if ($clname) {
167 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
170 if ($left eq $right) {
171 $filename = $left;
172 } elsif($left eq '/dev/null') {
173 $filename = $right;
174 } elsif($right eq '/dev/null') {
175 $filename = $left;
176 } else {
177 print STDERR "Error: failed to parse diff for $left and $right\n";
178 exit 1;
180 $left = $right = undef;
181 ($clname, $relname) = get_clname ($filename);
182 $cl_entries{$clname} .= "\t* $relname";
183 $change_msg = '';
184 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
187 # Context diffs have extra whitespace after first char;
188 # remove it to make matching easier.
189 if ($is_context_diff) {
190 s/^([-+! ]) /\1/;
193 # Remember the last line in a diff block that might start
194 # a new function.
195 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
196 $save_fn = $1;
199 # Check if file is newly added.
200 # Two patterns: for context and unified diff.
201 if (/^\*\*\* 0 \*\*\*\*/
202 || /^@@ -0,0 \+1.* @@/) {
203 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ? ": New test.\n" : ": New file.\n";
204 $look_for_funs = 0;
207 # Check if file was removed.
208 # Two patterns: for context and unified diff.
209 if (/^--- 0 ----/
210 || /^@@ -1.* \+0,0 @@/) {
211 $change_msg = ": Remove.\n";
212 $look_for_funs = 0;
215 if (is_unified_hunk_start ($diff_lines[$line_idx])) {
216 $is_context_diff = 0;
218 elsif (is_context_hunk_start ($diff_lines[$line_idx])) {
219 $is_context_diff = 1;
222 # If we find a new function, print it in brackets. Special case if
223 # this is the first function in a file.
225 # Note that we don't try too hard to find good matches. This should
226 # return a superset of the actual set of functions in the .diff file.
228 # The first pattern works with context diff files (diff -c). The
229 # second pattern works with unified diff files (diff -u).
231 # The third pattern looks for the starts of functions or classes
232 # within a diff block both for context and unified diff files.
233 if ($look_for_funs
234 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
235 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
236 || /^[-+! ](\{)/))
238 $_ = $1;
239 my $fn;
240 if (/^\{/) {
241 # Beginning of a new function.
242 $_ = $save_fn;
243 } else {
244 $save_fn = "";
246 if (/;$/) {
247 # No usable function name found.
248 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
249 # Discard stuff after the class/struct/etc. tag.
250 $fn = $1;
251 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
252 # Discard template and function parameters.
253 $fn = $1;
254 1 while ($fn =~ s/<[^<>]*>//);
255 $fn =~ s/[ \t]*$//;
257 # Check is function really modified
258 $no_real_change = 0;
259 $idx = $line_idx;
260 # Skip line info in context diffs.
261 while ($idx <= $#diff_lines && $is_context_diff
262 && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
263 ++$idx;
265 # Check all lines till the first change
266 # for the presence of really changed function
267 do {
268 ++$idx;
269 $no_real_change = $idx > $#diff_lines
270 || is_top_level ($diff_lines[$idx], $is_context_diff);
271 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/));
272 if ($fn && !$seen_names{$fn} && !$no_real_change) {
273 # If this is the first function in the file, we display it next
274 # to the filename, so we need an extra space before the opening
275 # brace.
276 if (!$change_msg) {
277 $change_msg .= " ";
278 } else {
279 $change_msg .= "\t";
282 $change_msg .= "($fn):\n";
283 $seen_names{$fn} = 1;
286 $line_idx++;
289 # If we have not seen any function names (ie, $change_msg is empty), we just
290 # write out a ':'. This happens when there is only one file with no
291 # functions.
292 $cl_entries{$clname} .= $change_msg ? "$change_msg\n" : ":\n";
294 if ($inline && $diff ne "-") {
295 # Get a temp filename, rather than an open filehandle, because we use
296 # the open to truncate.
297 $tmp = mktemp("tmp.XXXXXXXX") or die "Could not create temp file: $!";
299 # Copy the permissions to the temp file (in File::Copy module version
300 # 2.15 and later).
301 cp $diff, $tmp or die "Could not copy patch file to temp file: $!";
303 # Open the temp file, clearing contents.
304 open (OUTPUTFILE, '>', $tmp) or die "Could not open temp file: $!";
305 } else {
306 *OUTPUTFILE = STDOUT;
309 # Print the log
310 foreach my $clname (keys %cl_entries) {
311 print OUTPUTFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
314 if ($inline) {
315 # Append the patch to the log
316 foreach (@diff_lines) {
317 print OUTPUTFILE "$_\n";
321 if ($inline && $diff ne "-") {
322 # Close $tmp
323 close(OUTPUTFILE);
325 # Write new contents to $diff atomically
326 mv $tmp, $diff or die "Could not move temp file to patch file: $!";
329 exit 0;