]> git.ipfire.org Git - thirdparty/gcc.git/blame - contrib/mklog.pl
PR c++/87554 - ICE with extern template and reference member.
[thirdparty/gcc.git] / contrib / mklog.pl
CommitLineData
47e8b7ae 1#!/usr/bin/perl
9d825ec8 2# Copyright (C) 2012-2017 Free Software Foundation, Inc.
47e8b7ae 3#
4# This file is part of GCC.
5#
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)
9# any later version.
10#
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.
15#
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.
20
21# This script parses a .diff file generated with 'diff -up' or 'diff -cp'
867a8daa 22# and adds a skeleton ChangeLog file to the file. It does not try to be
47e8b7ae 23# very smart when parsing function names, but it produces a reasonable
24# approximation.
25#
26# Author: Diego Novillo <dnovillo@google.com> and
27# Cary Coutant <ccoutant@google.com>
28
3979f787 29use File::Temp;
30use File::Copy qw(cp mv);
31
47e8b7ae 32$date = `date +%Y-%m-%d`; chop ($date);
33
4cf91037 34$dot_mklog_format_msg =
35 "The .mklog format is:\n"
36 . "NAME = ...\n"
37 . "EMAIL = ...\n";
a2f6b7ee 38
4cf91037 39# Create a .mklog to reflect your profile, if necessary.
40my $conf = "$ENV{HOME}/.mklog";
41if (-f "$conf") {
42 open (CONF, "$conf")
43 or die "Could not open file '$conf' for reading: $!\n";
44 while (<CONF>) {
45 if (m/^\s*NAME\s*=\s*(.*?)\s*$/) {
46 $name = $1;
47 } elsif (m/^\s*EMAIL\s*=\s*(.*?)\s*$/) {
48 $addr = $1;
49 }
50 }
51 if (!($name && $addr)) {
52 die "Could not read .mklog settings.\n"
53 . $dot_mklog_format_msg;
54 }
55} else {
56 $name = `git config user.name`;
57 chomp($name);
58 $addr = `git config user.email`;
59 chomp($addr);
430c950d 60
4cf91037 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"
64 . " $ENV{HOME}.\n"
65 . $dot_mklog_format_msg;
66 }
430c950d 67}
47e8b7ae 68
4cf91037 69$gcc_root = $0;
70$gcc_root =~ s/[^\\\/]+$/../;
71
47e8b7ae 72#-----------------------------------------------------------------------------
73# Program starts here. You should not need to edit anything below this
74# line.
75#-----------------------------------------------------------------------------
3979f787 76$inline = 0;
77if ($#ARGV == 1 && ("$ARGV[0]" eq "-i" || "$ARGV[0]" eq "--inline")) {
78 shift;
79 $inline = 1;
80} elsif ($#ARGV != 0) {
47e8b7ae 81 $prog = `basename $0`; chop ($prog);
86b6b74f 82 print <<EOF;
3979f787 83usage: $prog [ -i | --inline ] file.diff
86b6b74f 84
85Generate ChangeLog template for file.diff.
86It assumes that patch has been created with -up or -cp.
3979f787 87When -i is used, the ChangeLog template is followed by the contents of
88file.diff.
86b6b74f 89When file.diff is -, read standard input.
3979f787 90When -i is used and file.diff is not -, it writes to file.diff, otherwise it
91writes to stdout.
86b6b74f 92EOF
47e8b7ae 93 exit 1;
94}
95
96$diff = $ARGV[0];
97$dir = `dirname $diff`; chop ($dir);
98$basename = `basename $diff`; chop ($basename);
47e8b7ae 99$hdrline = "$date $name <$addr>";
100
a54caa56 101sub get_clname ($) {
102 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
a2f6b7ee 103
a2f6b7ee 104 my $dirname = $_[0];
105 while ($dirname) {
106 my $clname = "$dirname/ChangeLog";
5dde7819 107 if (-f "$gcc_root/$clname" || -f "$clname") {
a54caa56 108 my $relname = substr ($_[0], length ($dirname) + 1);
109 return ($clname, $relname);
a2f6b7ee 110 } else {
111 $dirname =~ s/[\/\\]?[^\/\\]*$//;
112 }
113 }
a54caa56 114
115 return ('Unknown ChangeLog', $_[0]);
116}
117
118sub remove_suffixes ($) {
119 my $filename = $_[0];
120 $filename =~ s/^[ab]\///;
121 $filename =~ s/\.jj$//;
122 return $filename;
a2f6b7ee 123}
47e8b7ae 124
daf8408d 125sub is_context_hunk_start {
126 return @_[0] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/;
127}
128
129sub is_unified_hunk_start {
130 return @_[0] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/;
131}
132
0ef68fe5 133# Check if line is a top-level declaration.
0ef68fe5 134sub is_top_level {
463d1e8d 135 my ($function, $is_context_diff) = (@_);
daf8408d 136 if (is_unified_hunk_start ($function)
137 || is_context_hunk_start ($function)) {
138 return 1;
139 }
463d1e8d 140 if ($is_context_diff) {
141 $function =~ s/^..//;
142 } else {
143 $function =~ s/^.//;
144 }
083a7dfa 145 return $function && $function !~ /^[\s{#]/;
463d1e8d 146}
147
d5f9f518 148# Read contents of .diff file
149open (DFILE, $diff) or die "Could not open file $diff for reading";
150chomp (my @diff_lines = <DFILE>);
151close (DFILE);
152
153# Array diff_lines is modified by the log generation, so save a copy in
154# orig_diff_lines if needed.
155if ($inline) {
156 @orig_diff_lines = @diff_lines;
157}
158
47e8b7ae 159# For every file in the .diff print all the function names in ChangeLog
160# format.
a54caa56 161%cl_entries = ();
162$change_msg = undef;
163$look_for_funs = 0;
a2f6b7ee 164$clname = get_clname('');
463d1e8d 165$line_idx = 0;
166foreach (@diff_lines) {
0ef68fe5 167 # Stop processing functions if we found a new file.
a54caa56 168 # Remember both left and right names because one may be /dev/null.
0ef68fe5 169 # Don't be fooled by line markers in case of context diff.
170 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
a54caa56 171 $left = remove_suffixes ($1);
172 $look_for_funs = 0;
173 }
0ef68fe5 174 if (!/---$/ && /^--- +(\S+)?/) {
a54caa56 175 $right = remove_suffixes ($1);
176 $look_for_funs = 0;
177 }
178
179 # Check if the body of diff started.
180 # We should now have both left and right name,
181 # so we can decide filename.
182
0ef68fe5 183 if ($left && (/^\*{15}/ || /^@@ /)) {
47e8b7ae 184 # If we have not seen any function names in the previous file (ie,
a54caa56 185 # $change_msg is empty), we just write out a ':' before starting the next
47e8b7ae 186 # file.
a54caa56 187 if ($clname) {
188 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
189 }
190
191 if ($left eq $right) {
192 $filename = $left;
193 } elsif($left eq '/dev/null') {
194 $filename = $right;
195 } elsif($right eq '/dev/null') {
196 $filename = $left;
197 } else {
9d825ec8 198 my @ldirs = split /[\/\\]/, $left;
199 my @rdirs = split /[\/\\]/, $right;
200
201 $filename = '';
202 while ((my $l = pop @ldirs) && (my $r = pop @rdirs)) {
203 last if ($l ne $r);
204 $filename = "$l/$filename";
205 }
206 $filename =~ s/\/$//;
207
208 if (!$filename) {
209 print STDERR "Error: failed to parse diff for $left and $right\n";
210 exit 1;
211 }
47e8b7ae 212 }
a54caa56 213 $left = $right = undef;
214 ($clname, $relname) = get_clname ($filename);
215 $cl_entries{$clname} .= "\t* $relname";
216 $change_msg = '';
217 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
47e8b7ae 218 }
219
0ef68fe5 220 # Context diffs have extra whitespace after first char;
221 # remove it to make matching easier.
222 if ($is_context_diff) {
223 s/^([-+! ]) /\1/;
224 }
225
226 # Remember the last line in a diff block that might start
47e8b7ae 227 # a new function.
0ef68fe5 228 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
47e8b7ae 229 $save_fn = $1;
230 }
231
a54caa56 232 # Check if file is newly added.
233 # Two patterns: for context and unified diff.
234 if (/^\*\*\* 0 \*\*\*\*/
235 || /^@@ -0,0 \+1.* @@/) {
236 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ? ": New test.\n" : ": New file.\n";
237 $look_for_funs = 0;
238 }
239
240 # Check if file was removed.
241 # Two patterns: for context and unified diff.
242 if (/^--- 0 ----/
243 || /^@@ -1.* \+0,0 @@/) {
244 $change_msg = ": Remove.\n";
245 $look_for_funs = 0;
246 }
247
daf8408d 248 if (is_unified_hunk_start ($diff_lines[$line_idx])) {
0ef68fe5 249 $is_context_diff = 0;
463d1e8d 250 }
daf8408d 251 elsif (is_context_hunk_start ($diff_lines[$line_idx])) {
463d1e8d 252 $is_context_diff = 1;
253 }
254
47e8b7ae 255 # If we find a new function, print it in brackets. Special case if
256 # this is the first function in a file.
257 #
258 # Note that we don't try too hard to find good matches. This should
259 # return a superset of the actual set of functions in the .diff file.
260 #
0ef68fe5 261 # The first pattern works with context diff files (diff -c). The
262 # second pattern works with unified diff files (diff -u).
47e8b7ae 263 #
0ef68fe5 264 # The third pattern looks for the starts of functions or classes
265 # within a diff block both for context and unified diff files.
a54caa56 266 if ($look_for_funs
267 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
47e8b7ae 268 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
0ef68fe5 269 || /^[-+! ](\{)/))
47e8b7ae 270 {
271 $_ = $1;
272 my $fn;
273 if (/^\{/) {
274 # Beginning of a new function.
275 $_ = $save_fn;
276 } else {
277 $save_fn = "";
278 }
279 if (/;$/) {
280 # No usable function name found.
281 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
282 # Discard stuff after the class/struct/etc. tag.
283 $fn = $1;
284 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
285 # Discard template and function parameters.
286 $fn = $1;
287 1 while ($fn =~ s/<[^<>]*>//);
288 $fn =~ s/[ \t]*$//;
289 }
463d1e8d 290 # Check is function really modified
291 $no_real_change = 0;
daf8408d 292 $idx = $line_idx;
0ef68fe5 293 # Skip line info in context diffs.
daf8408d 294 while ($idx <= $#diff_lines && $is_context_diff
295 && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
296 ++$idx;
297 }
463d1e8d 298 # Check all lines till the first change
299 # for the presence of really changed function
daf8408d 300 do {
301 ++$idx;
302 $no_real_change = $idx > $#diff_lines
303 || is_top_level ($diff_lines[$idx], $is_context_diff);
304 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/));
463d1e8d 305 if ($fn && !$seen_names{$fn} && !$no_real_change) {
47e8b7ae 306 # If this is the first function in the file, we display it next
307 # to the filename, so we need an extra space before the opening
308 # brace.
a54caa56 309 if (!$change_msg) {
310 $change_msg .= " ";
47e8b7ae 311 } else {
a54caa56 312 $change_msg .= "\t";
47e8b7ae 313 }
314
a54caa56 315 $change_msg .= "($fn):\n";
47e8b7ae 316 $seen_names{$fn} = 1;
317 }
318 }
463d1e8d 319 $line_idx++;
47e8b7ae 320}
321
a54caa56 322# If we have not seen any function names (ie, $change_msg is empty), we just
47e8b7ae 323# write out a ':'. This happens when there is only one file with no
324# functions.
0ef68fe5 325$cl_entries{$clname} .= $change_msg ? "$change_msg\n" : ":\n";
a2f6b7ee 326
3979f787 327if ($inline && $diff ne "-") {
328 # Get a temp filename, rather than an open filehandle, because we use
329 # the open to truncate.
330 $tmp = mktemp("tmp.XXXXXXXX") or die "Could not create temp file: $!";
331
332 # Copy the permissions to the temp file (in File::Copy module version
333 # 2.15 and later).
334 cp $diff, $tmp or die "Could not copy patch file to temp file: $!";
335
336 # Open the temp file, clearing contents.
337 open (OUTPUTFILE, '>', $tmp) or die "Could not open temp file: $!";
338} else {
339 *OUTPUTFILE = STDOUT;
340}
341
342# Print the log
a2f6b7ee 343foreach my $clname (keys %cl_entries) {
3979f787 344 print OUTPUTFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
345}
346
347if ($inline) {
348 # Append the patch to the log
d5f9f518 349 foreach (@orig_diff_lines) {
3979f787 350 print OUTPUTFILE "$_\n";
351 }
352}
353
354if ($inline && $diff ne "-") {
355 # Close $tmp
356 close(OUTPUTFILE);
357
358 # Write new contents to $diff atomically
359 mv $tmp, $diff or die "Could not move temp file to patch file: $!";
47e8b7ae 360}
361
47e8b7ae 362exit 0;