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 # Check if line is a top-level declaration.
112 # TODO: ignore preprocessor directives except maybe #define ?
114 my ($function, $is_context_diff) = (@_);
115 if ($is_context_diff) {
116 $function =~ s/^..//;
120 return $function && $function !~ /^[\s{}]/;
123 # For every file in the .diff print all the function names in ChangeLog
128 $clname = get_clname
('');
129 open (DFILE
, $diff) or die "Could not open file $diff for reading";
130 chomp (my @diff_lines = <DFILE
>);
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);
141 if (!/---$/ && /^--- +(\S+)?/) {
142 $right = remove_suffixes
($1);
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
155 $cl_entries{$clname} .= $change_msg ?
"$change_msg" : ":\n";
158 if ($left eq $right) {
160 } elsif($left eq '/dev/null') {
162 } elsif($right eq '/dev/null') {
165 print STDERR
"Error: failed to parse diff for $left and $right\n";
168 $left = $right = undef;
169 ($clname, $relname) = get_clname
($filename);
170 $cl_entries{$clname} .= "\t* $relname";
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) {
181 # Remember the last line in a diff block that might start
183 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
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";
195 # Check if file was removed.
196 # Two patterns: for context and unified diff.
198 || /^@@ -1.* \+0,0 @@/) {
199 $change_msg = ": Remove.\n";
203 # Mark if we met doubtfully changed function.
205 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
207 $is_context_diff = 0;
209 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
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.
227 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
228 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
234 # Beginning of a new function.
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.
244 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
245 # Discard template and function parameters.
247 1 while ($fn =~ s/<[^<>]*>//);
250 # Check is function really modified
254 # Skip line info in context diffs.
255 while ($is_context_diff && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
258 # Check all lines till the first change
259 # for the presence of really changed function
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
275 $change_msg .= "($fn):\n";
276 $seen_names{$fn} = 1;
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
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
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: $!";
299 *OUTPUTFILE
= STDOUT
;
303 foreach my $clname (keys %cl_entries) {
304 print OUTPUTFILE
"$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
308 # Append the patch to the log
309 foreach (@diff_lines) {
310 print OUTPUTFILE
"$_\n";
314 if ($inline && $diff ne "-") {
318 # Write new contents to $diff atomically
319 mv
$tmp, $diff or die "Could not move temp file to patch file: $!";