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>
29 # Change these settings to reflect your profile.
30 $username = $ENV{'USER'};
31 $name = `finger $username | grep -o 'Name: .*'`;
32 @n = split(/: /, $name);
33 $name = @n[1]; chop($name);
34 $addr = $username . "\@my.domain.org";
35 $date = `date +%Y-%m-%d`; chop ($date);
38 $gcc_root =~ s/[^\\\/]+$/../;
41 # if this is a git tree then take name and email from the git configuration
43 $gitname = `git config user.name`;
49 $gitaddr = `git config user.email`;
56 #-----------------------------------------------------------------------------
57 # Program starts here. You should not need to edit anything below this
59 #-----------------------------------------------------------------------------
61 $prog = `basename $0`; chop ($prog);
63 usage: $prog file.diff
65 Generate ChangeLog template for file.diff.
66 It assumes that patch has been created with -up or -cp.
67 When file.diff is -, read standard input.
73 $dir = `dirname $diff`; chop ($dir);
74 $basename = `basename $diff`; chop ($basename);
75 $hdrline = "$date $name <$addr>";
78 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
82 my $clname = "$dirname/ChangeLog";
84 my $relname = substr ($_[0], length ($dirname) + 1);
85 return ($clname, $relname);
87 $dirname =~ s/[\/\\]?[^\/\\]*$//;
91 return ('Unknown ChangeLog', $_[0]);
94 sub remove_suffixes
($) {
96 $filename =~ s/^[ab]\///;
97 $filename =~ s/\.jj$//;
101 # Check if line is a top-level declaration.
102 # TODO: ignore preprocessor directives except maybe #define ?
104 my ($function, $is_context_diff) = (@_);
105 if ($is_context_diff) {
106 $function =~ s/^..//;
110 return $function && $function !~ /^[\s{}]/;
113 # For every file in the .diff print all the function names in ChangeLog
118 $clname = get_clname
('');
119 open (DFILE
, $diff) or die "Could not open file $diff for reading";
120 chomp (my @diff_lines = <DFILE
>);
123 foreach (@diff_lines) {
124 # Stop processing functions if we found a new file.
125 # Remember both left and right names because one may be /dev/null.
126 # Don't be fooled by line markers in case of context diff.
127 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
128 $left = remove_suffixes
($1);
131 if (!/---$/ && /^--- +(\S+)?/) {
132 $right = remove_suffixes
($1);
136 # Check if the body of diff started.
137 # We should now have both left and right name,
138 # so we can decide filename.
140 if ($left && (/^\*{15}/ || /^@@ /)) {
141 # If we have not seen any function names in the previous file (ie,
142 # $change_msg is empty), we just write out a ':' before starting the next
145 $cl_entries{$clname} .= $change_msg ?
"$change_msg" : ":\n";
148 if ($left eq $right) {
150 } elsif($left eq '/dev/null') {
152 } elsif($right eq '/dev/null') {
155 print STDERR
"Error: failed to parse diff for $left and $right\n";
158 $left = $right = undef;
159 ($clname, $relname) = get_clname
($filename);
160 $cl_entries{$clname} .= "\t* $relname";
162 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
165 # Context diffs have extra whitespace after first char;
166 # remove it to make matching easier.
167 if ($is_context_diff) {
171 # Remember the last line in a diff block that might start
173 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
177 # Check if file is newly added.
178 # Two patterns: for context and unified diff.
179 if (/^\*\*\* 0 \*\*\*\*/
180 || /^@@ -0,0 \+1.* @@/) {
181 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ?
": New test.\n" : ": New file.\n";
185 # Check if file was removed.
186 # Two patterns: for context and unified diff.
188 || /^@@ -1.* \+0,0 @@/) {
189 $change_msg = ": Remove.\n";
193 # Mark if we met doubtfully changed function.
195 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
197 $is_context_diff = 0;
199 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
201 $is_context_diff = 1;
204 # If we find a new function, print it in brackets. Special case if
205 # this is the first function in a file.
207 # Note that we don't try too hard to find good matches. This should
208 # return a superset of the actual set of functions in the .diff file.
210 # The first pattern works with context diff files (diff -c). The
211 # second pattern works with unified diff files (diff -u).
213 # The third pattern looks for the starts of functions or classes
214 # within a diff block both for context and unified diff files.
217 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
218 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
224 # Beginning of a new function.
230 # No usable function name found.
231 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
232 # Discard stuff after the class/struct/etc. tag.
234 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
235 # Discard template and function parameters.
237 1 while ($fn =~ s/<[^<>]*>//);
240 # Check is function really modified
244 # Skip line info in context diffs.
245 while ($is_context_diff && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
248 # Check all lines till the first change
249 # for the presence of really changed function
252 $no_real_change = is_top_level
($diff_lines[$idx], $is_context_diff);
253 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/))
255 if ($fn && !$seen_names{$fn} && !$no_real_change) {
256 # If this is the first function in the file, we display it next
257 # to the filename, so we need an extra space before the opening
265 $change_msg .= "($fn):\n";
266 $seen_names{$fn} = 1;
272 # If we have not seen any function names (ie, $change_msg is empty), we just
273 # write out a ':'. This happens when there is only one file with no
275 $cl_entries{$clname} .= $change_msg ?
"$change_msg\n" : ":\n";
277 foreach my $clname (keys %cl_entries) {
278 print "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";