* ipa-polymorphic-call.c
[official-gcc.git] / contrib / mklog
blob840f6f8c2663e912277b2341d8bcffe294177275
1 #!/usr/bin/perl
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)
9 # any later version.
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
24 # approximation.
26 # Author: Diego Novillo <dnovillo@google.com> and
27 # Cary Coutant <ccoutant@google.com>
29 use File::Temp;
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);
40 $gcc_root = $0;
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`;
46 chomp($gitname);
47 if ($gitname) {
48 $name = $gitname;
51 $gitaddr = `git config user.email`;
52 chomp($gitaddr);
53 if ($gitaddr) {
54 $addr = $gitaddr;
58 #-----------------------------------------------------------------------------
59 # Program starts here. You should not need to edit anything below this
60 # line.
61 #-----------------------------------------------------------------------------
62 $inline = 0;
63 if ($#ARGV == 1 && ("$ARGV[0]" eq "-i" || "$ARGV[0]" eq "--inline")) {
64 shift;
65 $inline = 1;
66 } elsif ($#ARGV != 0) {
67 $prog = `basename $0`; chop ($prog);
68 print <<EOF;
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
74 file.diff.
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
77 writes to stdout.
78 EOF
79 exit 1;
82 $diff = $ARGV[0];
83 $dir = `dirname $diff`; chop ($dir);
84 $basename = `basename $diff`; chop ($basename);
85 $hdrline = "$date $name <$addr>";
87 sub get_clname ($) {
88 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
90 my $dirname = $_[0];
91 while ($dirname) {
92 my $clname = "$dirname/ChangeLog";
93 if (-f "$gcc_root/$clname") {
94 my $relname = substr ($_[0], length ($dirname) + 1);
95 return ($clname, $relname);
96 } else {
97 $dirname =~ s/[\/\\]?[^\/\\]*$//;
101 return ('Unknown ChangeLog', $_[0]);
104 sub remove_suffixes ($) {
105 my $filename = $_[0];
106 $filename =~ s/^[ab]\///;
107 $filename =~ s/\.jj$//;
108 return $filename;
111 sub is_context_hunk_start {
112 return @_[0] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/;
115 sub is_unified_hunk_start {
116 return @_[0] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/;
119 # Check if line is a top-level declaration.
120 # TODO: ignore preprocessor directives except maybe #define ?
121 sub is_top_level {
122 my ($function, $is_context_diff) = (@_);
123 if (is_unified_hunk_start ($function)
124 || is_context_hunk_start ($function)) {
125 return 1;
127 if ($is_context_diff) {
128 $function =~ s/^..//;
129 } else {
130 $function =~ s/^.//;
132 return $function && $function !~ /^[\s{]/;
135 # Read contents of .diff file
136 open (DFILE, $diff) or die "Could not open file $diff for reading";
137 chomp (my @diff_lines = <DFILE>);
138 close (DFILE);
140 # Array diff_lines is modified by the log generation, so save a copy in
141 # orig_diff_lines if needed.
142 if ($inline) {
143 @orig_diff_lines = @diff_lines;
146 # For every file in the .diff print all the function names in ChangeLog
147 # format.
148 %cl_entries = ();
149 $change_msg = undef;
150 $look_for_funs = 0;
151 $clname = get_clname('');
152 $line_idx = 0;
153 foreach (@diff_lines) {
154 # Stop processing functions if we found a new file.
155 # Remember both left and right names because one may be /dev/null.
156 # Don't be fooled by line markers in case of context diff.
157 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
158 $left = remove_suffixes ($1);
159 $look_for_funs = 0;
161 if (!/---$/ && /^--- +(\S+)?/) {
162 $right = remove_suffixes ($1);
163 $look_for_funs = 0;
166 # Check if the body of diff started.
167 # We should now have both left and right name,
168 # so we can decide filename.
170 if ($left && (/^\*{15}/ || /^@@ /)) {
171 # If we have not seen any function names in the previous file (ie,
172 # $change_msg is empty), we just write out a ':' before starting the next
173 # file.
174 if ($clname) {
175 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
178 if ($left eq $right) {
179 $filename = $left;
180 } elsif($left eq '/dev/null') {
181 $filename = $right;
182 } elsif($right eq '/dev/null') {
183 $filename = $left;
184 } else {
185 print STDERR "Error: failed to parse diff for $left and $right\n";
186 exit 1;
188 $left = $right = undef;
189 ($clname, $relname) = get_clname ($filename);
190 $cl_entries{$clname} .= "\t* $relname";
191 $change_msg = '';
192 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
195 # Context diffs have extra whitespace after first char;
196 # remove it to make matching easier.
197 if ($is_context_diff) {
198 s/^([-+! ]) /\1/;
201 # Remember the last line in a diff block that might start
202 # a new function.
203 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
204 $save_fn = $1;
207 # Check if file is newly added.
208 # Two patterns: for context and unified diff.
209 if (/^\*\*\* 0 \*\*\*\*/
210 || /^@@ -0,0 \+1.* @@/) {
211 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ? ": New test.\n" : ": New file.\n";
212 $look_for_funs = 0;
215 # Check if file was removed.
216 # Two patterns: for context and unified diff.
217 if (/^--- 0 ----/
218 || /^@@ -1.* \+0,0 @@/) {
219 $change_msg = ": Remove.\n";
220 $look_for_funs = 0;
223 if (is_unified_hunk_start ($diff_lines[$line_idx])) {
224 $is_context_diff = 0;
226 elsif (is_context_hunk_start ($diff_lines[$line_idx])) {
227 $is_context_diff = 1;
230 # If we find a new function, print it in brackets. Special case if
231 # this is the first function in a file.
233 # Note that we don't try too hard to find good matches. This should
234 # return a superset of the actual set of functions in the .diff file.
236 # The first pattern works with context diff files (diff -c). The
237 # second pattern works with unified diff files (diff -u).
239 # The third pattern looks for the starts of functions or classes
240 # within a diff block both for context and unified diff files.
241 if ($look_for_funs
242 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
243 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
244 || /^[-+! ](\{)/))
246 $_ = $1;
247 my $fn;
248 if (/^\{/) {
249 # Beginning of a new function.
250 $_ = $save_fn;
251 } else {
252 $save_fn = "";
254 if (/;$/) {
255 # No usable function name found.
256 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
257 # Discard stuff after the class/struct/etc. tag.
258 $fn = $1;
259 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
260 # Discard template and function parameters.
261 $fn = $1;
262 1 while ($fn =~ s/<[^<>]*>//);
263 $fn =~ s/[ \t]*$//;
265 # Check is function really modified
266 $no_real_change = 0;
267 $idx = $line_idx;
268 # Skip line info in context diffs.
269 while ($idx <= $#diff_lines && $is_context_diff
270 && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
271 ++$idx;
273 # Check all lines till the first change
274 # for the presence of really changed function
275 do {
276 ++$idx;
277 $no_real_change = $idx > $#diff_lines
278 || is_top_level ($diff_lines[$idx], $is_context_diff);
279 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/));
280 if ($fn && !$seen_names{$fn} && !$no_real_change) {
281 # If this is the first function in the file, we display it next
282 # to the filename, so we need an extra space before the opening
283 # brace.
284 if (!$change_msg) {
285 $change_msg .= " ";
286 } else {
287 $change_msg .= "\t";
290 $change_msg .= "($fn):\n";
291 $seen_names{$fn} = 1;
294 $line_idx++;
297 # If we have not seen any function names (ie, $change_msg is empty), we just
298 # write out a ':'. This happens when there is only one file with no
299 # functions.
300 $cl_entries{$clname} .= $change_msg ? "$change_msg\n" : ":\n";
302 if ($inline && $diff ne "-") {
303 # Get a temp filename, rather than an open filehandle, because we use
304 # the open to truncate.
305 $tmp = mktemp("tmp.XXXXXXXX") or die "Could not create temp file: $!";
307 # Copy the permissions to the temp file (in File::Copy module version
308 # 2.15 and later).
309 cp $diff, $tmp or die "Could not copy patch file to temp file: $!";
311 # Open the temp file, clearing contents.
312 open (OUTPUTFILE, '>', $tmp) or die "Could not open temp file: $!";
313 } else {
314 *OUTPUTFILE = STDOUT;
317 # Print the log
318 foreach my $clname (keys %cl_entries) {
319 print OUTPUTFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
322 if ($inline) {
323 # Append the patch to the log
324 foreach (@orig_diff_lines) {
325 print OUTPUTFILE "$_\n";
329 if ($inline && $diff ne "-") {
330 # Close $tmp
331 close(OUTPUTFILE);
333 # Write new contents to $diff atomically
334 mv $tmp, $diff or die "Could not move temp file to patch file: $!";
337 exit 0;