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)
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
26 # Author: Diego Novillo <dnovillo@google.com> and
27 # Cary Coutant <ccoutant@google.com>
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);
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`;
51 $gitaddr = `git config user.email`;
58 #-----------------------------------------------------------------------------
59 # Program starts here. You should not need to edit anything below this
61 #-----------------------------------------------------------------------------
63 if ($#ARGV == 1 && ("$ARGV[0]" eq "-i" || "$ARGV[0]" eq "--inline")) {
66 } elsif ($#ARGV != 0) {
67 $prog = `basename $0`; chop ($prog);
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
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
83 $dir = `dirname $diff`; chop ($dir);
84 $basename = `basename $diff`; chop ($basename);
85 $hdrline = "$date $name <$addr>";
88 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
92 my $clname = "$dirname/ChangeLog";
93 if (-f
"$gcc_root/$clname") {
94 my $relname = substr ($_[0], length ($dirname) + 1);
95 return ($clname, $relname);
97 $dirname =~ s/[\/\\]?[^\/\\]*$//;
101 return ('Unknown ChangeLog', $_[0]);
104 sub remove_suffixes
($) {
105 my $filename = $_[0];
106 $filename =~ s/^[ab]\///;
107 $filename =~ s/\.jj$//;
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 ?
122 my ($function, $is_context_diff) = (@_);
123 if (is_unified_hunk_start
($function)
124 || is_context_hunk_start
($function)) {
127 if ($is_context_diff) {
128 $function =~ s/^..//;
132 return $function && $function !~ /^[\s{}]/;
135 # For every file in the .diff print all the function names in ChangeLog
140 $clname = get_clname
('');
141 open (DFILE
, $diff) or die "Could not open file $diff for reading";
142 chomp (my @diff_lines = <DFILE
>);
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);
153 if (!/---$/ && /^--- +(\S+)?/) {
154 $right = remove_suffixes
($1);
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
167 $cl_entries{$clname} .= $change_msg ?
"$change_msg" : ":\n";
170 if ($left eq $right) {
172 } elsif($left eq '/dev/null') {
174 } elsif($right eq '/dev/null') {
177 print STDERR
"Error: failed to parse diff for $left and $right\n";
180 $left = $right = undef;
181 ($clname, $relname) = get_clname
($filename);
182 $cl_entries{$clname} .= "\t* $relname";
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) {
193 # Remember the last line in a diff block that might start
195 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
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";
207 # Check if file was removed.
208 # Two patterns: for context and unified diff.
210 || /^@@ -1.* \+0,0 @@/) {
211 $change_msg = ": Remove.\n";
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.
234 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
235 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
241 # Beginning of a new function.
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.
251 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
252 # Discard template and function parameters.
254 1 while ($fn =~ s/<[^<>]*>//);
257 # Check is function really modified
260 # Skip line info in context diffs.
261 while ($idx <= $#diff_lines && $is_context_diff
262 && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
265 # Check all lines till the first change
266 # for the presence of really changed function
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
282 $change_msg .= "($fn):\n";
283 $seen_names{$fn} = 1;
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
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
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: $!";
306 *OUTPUTFILE
= STDOUT
;
310 foreach my $clname (keys %cl_entries) {
311 print OUTPUTFILE
"$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
315 # Append the patch to the log
316 foreach (@diff_lines) {
317 print OUTPUTFILE
"$_\n";
321 if ($inline && $diff ne "-") {
325 # Write new contents to $diff atomically
326 mv
$tmp, $diff or die "Could not move temp file to patch file: $!";