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 $date = `date +%Y-%m-%d`; chop ($date);
34 $dot_mklog_format_msg =
35 "The .mklog format is:\n"
39 # Create a .mklog to reflect your profile, if necessary.
40 my $conf = "$ENV{HOME}/.mklog";
43 or die "Could not open file '$conf' for reading: $!\n";
45 if (m/^\s*NAME\s*=\s*(.*?)\s*$/) {
47 } elsif (m/^\s*EMAIL\s*=\s*(.*?)\s*$/) {
51 if (!($name && $addr)) {
52 die "Could not read .mklog settings.\n"
53 . $dot_mklog_format_msg;
56 $name = `git config user.name`;
58 $addr = `git config user.email`;
61 if (!($name && $addr)) {
62 die "Could not read git user.name and user.email settings.\n"
63 . "Please add missing git settings, or create a .mklog file in"
65 . $dot_mklog_format_msg;
70 $gcc_root =~ s/[^\\\/]+$/../;
72 #-----------------------------------------------------------------------------
73 # Program starts here. You should not need to edit anything below this
75 #-----------------------------------------------------------------------------
77 if ($#ARGV == 1 && ("$ARGV[0]" eq "-i" || "$ARGV[0]" eq "--inline")) {
80 } elsif ($#ARGV != 0) {
81 $prog = `basename $0`; chop ($prog);
83 usage: $prog [ -i | --inline ] file.diff
85 Generate ChangeLog template for file.diff.
86 It assumes that patch has been created with -up or -cp.
87 When -i is used, the ChangeLog template is followed by the contents of
89 When file.diff is -, read standard input.
90 When -i is used and file.diff is not -, it writes to file.diff, otherwise it
97 $dir = `dirname $diff`; chop ($dir);
98 $basename = `basename $diff`; chop ($basename);
99 $hdrline = "$date $name <$addr>";
102 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
106 my $clname = "$dirname/ChangeLog";
107 if (-f
"$gcc_root/$clname") {
108 my $relname = substr ($_[0], length ($dirname) + 1);
109 return ($clname, $relname);
111 $dirname =~ s/[\/\\]?[^\/\\]*$//;
115 return ('Unknown ChangeLog', $_[0]);
118 sub remove_suffixes
($) {
119 my $filename = $_[0];
120 $filename =~ s/^[ab]\///;
121 $filename =~ s/\.jj$//;
125 sub is_context_hunk_start
{
126 return @_[0] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/;
129 sub is_unified_hunk_start
{
130 return @_[0] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/;
133 # Check if line is a top-level declaration.
134 # TODO: ignore preprocessor directives except maybe #define ?
136 my ($function, $is_context_diff) = (@_);
137 if (is_unified_hunk_start
($function)
138 || is_context_hunk_start
($function)) {
141 if ($is_context_diff) {
142 $function =~ s/^..//;
146 return $function && $function !~ /^[\s{]/;
149 # Read contents of .diff file
150 open (DFILE
, $diff) or die "Could not open file $diff for reading";
151 chomp (my @diff_lines = <DFILE
>);
154 # Array diff_lines is modified by the log generation, so save a copy in
155 # orig_diff_lines if needed.
157 @orig_diff_lines = @diff_lines;
160 # For every file in the .diff print all the function names in ChangeLog
165 $clname = get_clname
('');
167 foreach (@diff_lines) {
168 # Stop processing functions if we found a new file.
169 # Remember both left and right names because one may be /dev/null.
170 # Don't be fooled by line markers in case of context diff.
171 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
172 $left = remove_suffixes
($1);
175 if (!/---$/ && /^--- +(\S+)?/) {
176 $right = remove_suffixes
($1);
180 # Check if the body of diff started.
181 # We should now have both left and right name,
182 # so we can decide filename.
184 if ($left && (/^\*{15}/ || /^@@ /)) {
185 # If we have not seen any function names in the previous file (ie,
186 # $change_msg is empty), we just write out a ':' before starting the next
189 $cl_entries{$clname} .= $change_msg ?
"$change_msg" : ":\n";
192 if ($left eq $right) {
194 } elsif($left eq '/dev/null') {
196 } elsif($right eq '/dev/null') {
199 print STDERR
"Error: failed to parse diff for $left and $right\n";
202 $left = $right = undef;
203 ($clname, $relname) = get_clname
($filename);
204 $cl_entries{$clname} .= "\t* $relname";
206 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
209 # Context diffs have extra whitespace after first char;
210 # remove it to make matching easier.
211 if ($is_context_diff) {
215 # Remember the last line in a diff block that might start
217 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
221 # Check if file is newly added.
222 # Two patterns: for context and unified diff.
223 if (/^\*\*\* 0 \*\*\*\*/
224 || /^@@ -0,0 \+1.* @@/) {
225 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ?
": New test.\n" : ": New file.\n";
229 # Check if file was removed.
230 # Two patterns: for context and unified diff.
232 || /^@@ -1.* \+0,0 @@/) {
233 $change_msg = ": Remove.\n";
237 if (is_unified_hunk_start
($diff_lines[$line_idx])) {
238 $is_context_diff = 0;
240 elsif (is_context_hunk_start
($diff_lines[$line_idx])) {
241 $is_context_diff = 1;
244 # If we find a new function, print it in brackets. Special case if
245 # this is the first function in a file.
247 # Note that we don't try too hard to find good matches. This should
248 # return a superset of the actual set of functions in the .diff file.
250 # The first pattern works with context diff files (diff -c). The
251 # second pattern works with unified diff files (diff -u).
253 # The third pattern looks for the starts of functions or classes
254 # within a diff block both for context and unified diff files.
256 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
257 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
263 # Beginning of a new function.
269 # No usable function name found.
270 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
271 # Discard stuff after the class/struct/etc. tag.
273 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
274 # Discard template and function parameters.
276 1 while ($fn =~ s/<[^<>]*>//);
279 # Check is function really modified
282 # Skip line info in context diffs.
283 while ($idx <= $#diff_lines && $is_context_diff
284 && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
287 # Check all lines till the first change
288 # for the presence of really changed function
291 $no_real_change = $idx > $#diff_lines
292 || is_top_level
($diff_lines[$idx], $is_context_diff);
293 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/));
294 if ($fn && !$seen_names{$fn} && !$no_real_change) {
295 # If this is the first function in the file, we display it next
296 # to the filename, so we need an extra space before the opening
304 $change_msg .= "($fn):\n";
305 $seen_names{$fn} = 1;
311 # If we have not seen any function names (ie, $change_msg is empty), we just
312 # write out a ':'. This happens when there is only one file with no
314 $cl_entries{$clname} .= $change_msg ?
"$change_msg\n" : ":\n";
316 if ($inline && $diff ne "-") {
317 # Get a temp filename, rather than an open filehandle, because we use
318 # the open to truncate.
319 $tmp = mktemp
("tmp.XXXXXXXX") or die "Could not create temp file: $!";
321 # Copy the permissions to the temp file (in File::Copy module version
323 cp
$diff, $tmp or die "Could not copy patch file to temp file: $!";
325 # Open the temp file, clearing contents.
326 open (OUTPUTFILE
, '>', $tmp) or die "Could not open temp file: $!";
328 *OUTPUTFILE
= STDOUT
;
332 foreach my $clname (keys %cl_entries) {
333 print OUTPUTFILE
"$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
337 # Append the patch to the log
338 foreach (@orig_diff_lines) {
339 print OUTPUTFILE
"$_\n";
343 if ($inline && $diff ne "-") {
347 # Write new contents to $diff atomically
348 mv
$tmp, $diff or die "Could not move temp file to patch file: $!";