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/[^\\\/]+$/../;
40 # if this is a git tree then take name and email from the git configuration
41 if (-d
"$gcc_root/.git") {
42 $gitname = `git config user.name`;
48 $gitaddr = `git config user.email`;
55 #-----------------------------------------------------------------------------
56 # Program starts here. You should not need to edit anything below this
58 #-----------------------------------------------------------------------------
60 $prog = `basename $0`; chop ($prog);
62 usage: $prog file.diff
64 Generate ChangeLog template for file.diff.
65 It assumes that patch has been created with -up or -cp.
66 When file.diff is -, read standard input.
72 $dir = `dirname $diff`; chop ($dir);
73 $basename = `basename $diff`; chop ($basename);
74 $hdrline = "$date $name <$addr>";
77 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
81 my $clname = "$dirname/ChangeLog";
82 if (-f
"$gcc_root/$clname") {
83 my $relname = substr ($_[0], length ($dirname) + 1);
84 return ($clname, $relname);
86 $dirname =~ s/[\/\\]?[^\/\\]*$//;
90 return ('Unknown ChangeLog', $_[0]);
93 sub remove_suffixes
($) {
95 $filename =~ s/^[ab]\///;
96 $filename =~ s/\.jj$//;
100 # Check if line is a top-level declaration.
101 # TODO: ignore preprocessor directives except maybe #define ?
103 my ($function, $is_context_diff) = (@_);
104 if ($is_context_diff) {
105 $function =~ s/^..//;
109 return $function && $function !~ /^[\s{}]/;
112 # For every file in the .diff print all the function names in ChangeLog
117 $clname = get_clname
('');
118 open (DFILE
, $diff) or die "Could not open file $diff for reading";
119 chomp (my @diff_lines = <DFILE
>);
122 foreach (@diff_lines) {
123 # Stop processing functions if we found a new file.
124 # Remember both left and right names because one may be /dev/null.
125 # Don't be fooled by line markers in case of context diff.
126 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
127 $left = remove_suffixes
($1);
130 if (!/---$/ && /^--- +(\S+)?/) {
131 $right = remove_suffixes
($1);
135 # Check if the body of diff started.
136 # We should now have both left and right name,
137 # so we can decide filename.
139 if ($left && (/^\*{15}/ || /^@@ /)) {
140 # If we have not seen any function names in the previous file (ie,
141 # $change_msg is empty), we just write out a ':' before starting the next
144 $cl_entries{$clname} .= $change_msg ?
"$change_msg" : ":\n";
147 if ($left eq $right) {
149 } elsif($left eq '/dev/null') {
151 } elsif($right eq '/dev/null') {
154 print STDERR
"Error: failed to parse diff for $left and $right\n";
157 $left = $right = undef;
158 ($clname, $relname) = get_clname
($filename);
159 $cl_entries{$clname} .= "\t* $relname";
161 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
164 # Context diffs have extra whitespace after first char;
165 # remove it to make matching easier.
166 if ($is_context_diff) {
170 # Remember the last line in a diff block that might start
172 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
176 # Check if file is newly added.
177 # Two patterns: for context and unified diff.
178 if (/^\*\*\* 0 \*\*\*\*/
179 || /^@@ -0,0 \+1.* @@/) {
180 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ?
": New test.\n" : ": New file.\n";
184 # Check if file was removed.
185 # Two patterns: for context and unified diff.
187 || /^@@ -1.* \+0,0 @@/) {
188 $change_msg = ": Remove.\n";
192 # Mark if we met doubtfully changed function.
194 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
196 $is_context_diff = 0;
198 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
200 $is_context_diff = 1;
203 # If we find a new function, print it in brackets. Special case if
204 # this is the first function in a file.
206 # Note that we don't try too hard to find good matches. This should
207 # return a superset of the actual set of functions in the .diff file.
209 # The first pattern works with context diff files (diff -c). The
210 # second pattern works with unified diff files (diff -u).
212 # The third pattern looks for the starts of functions or classes
213 # within a diff block both for context and unified diff files.
216 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
217 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
223 # Beginning of a new function.
229 # No usable function name found.
230 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
231 # Discard stuff after the class/struct/etc. tag.
233 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
234 # Discard template and function parameters.
236 1 while ($fn =~ s/<[^<>]*>//);
239 # Check is function really modified
243 # Skip line info in context diffs.
244 while ($is_context_diff && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
247 # Check all lines till the first change
248 # for the presence of really changed function
251 $no_real_change = is_top_level
($diff_lines[$idx], $is_context_diff);
252 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/))
254 if ($fn && !$seen_names{$fn} && !$no_real_change) {
255 # If this is the first function in the file, we display it next
256 # to the filename, so we need an extra space before the opening
264 $change_msg .= "($fn):\n";
265 $seen_names{$fn} = 1;
271 # If we have not seen any function names (ie, $change_msg is empty), we just
272 # write out a ':'. This happens when there is only one file with no
274 $cl_entries{$clname} .= $change_msg ?
"$change_msg\n" : ":\n";
276 foreach my $clname (keys %cl_entries) {
277 print "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";