2014-07-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / contrib / mklog
blobcdc64551d5f9412d4225215fae851ea1fa7282ff
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 # 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);
37 $gcc_root = $0;
38 $gcc_root =~ s/[^\\\/]+$/../;
39 chdir $gcc_root;
41 # if this is a git tree then take name and email from the git configuration
42 if (-d .git) {
43 $gitname = `git config user.name`;
44 chomp($gitname);
45 if ($gitname) {
46 $name = $gitname;
49 $gitaddr = `git config user.email`;
50 chomp($gitaddr);
51 if ($gitaddr) {
52 $addr = $gitaddr;
56 #-----------------------------------------------------------------------------
57 # Program starts here. You should not need to edit anything below this
58 # line.
59 #-----------------------------------------------------------------------------
60 if ($#ARGV != 0) {
61 $prog = `basename $0`; chop ($prog);
62 print <<EOF;
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.
68 EOF
69 exit 1;
72 $diff = $ARGV[0];
73 $dir = `dirname $diff`; chop ($dir);
74 $basename = `basename $diff`; chop ($basename);
75 $hdrline = "$date $name <$addr>";
77 sub get_clname ($) {
78 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
80 my $dirname = $_[0];
81 while ($dirname) {
82 my $clname = "$dirname/ChangeLog";
83 if (-f $clname) {
84 my $relname = substr ($_[0], length ($dirname) + 1);
85 return ($clname, $relname);
86 } else {
87 $dirname =~ s/[\/\\]?[^\/\\]*$//;
91 return ('Unknown ChangeLog', $_[0]);
94 sub remove_suffixes ($) {
95 my $filename = $_[0];
96 $filename =~ s/^[ab]\///;
97 $filename =~ s/\.jj$//;
98 return $filename;
101 # Check if line is a top-level declaration.
102 # TODO: ignore preprocessor directives except maybe #define ?
103 sub is_top_level {
104 my ($function, $is_context_diff) = (@_);
105 if ($is_context_diff) {
106 $function =~ s/^..//;
107 } else {
108 $function =~ s/^.//;
110 return $function && $function !~ /^[\s{}]/;
113 # For every file in the .diff print all the function names in ChangeLog
114 # format.
115 %cl_entries = ();
116 $change_msg = undef;
117 $look_for_funs = 0;
118 $clname = get_clname('');
119 open (DFILE, $diff) or die "Could not open file $diff for reading";
120 chomp (my @diff_lines = <DFILE>);
121 close (DFILE);
122 $line_idx = 0;
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);
129 $look_for_funs = 0;
131 if (!/---$/ && /^--- +(\S+)?/) {
132 $right = remove_suffixes ($1);
133 $look_for_funs = 0;
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
143 # file.
144 if ($clname) {
145 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
148 if ($left eq $right) {
149 $filename = $left;
150 } elsif($left eq '/dev/null') {
151 $filename = $right;
152 } elsif($right eq '/dev/null') {
153 $filename = $left;
154 } else {
155 print STDERR "Error: failed to parse diff for $left and $right\n";
156 exit 1;
158 $left = $right = undef;
159 ($clname, $relname) = get_clname ($filename);
160 $cl_entries{$clname} .= "\t* $relname";
161 $change_msg = '';
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) {
168 s/^([-+! ]) /\1/;
171 # Remember the last line in a diff block that might start
172 # a new function.
173 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
174 $save_fn = $1;
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";
182 $look_for_funs = 0;
185 # Check if file was removed.
186 # Two patterns: for context and unified diff.
187 if (/^--- 0 ----/
188 || /^@@ -1.* \+0,0 @@/) {
189 $change_msg = ": Remove.\n";
190 $look_for_funs = 0;
193 # Mark if we met doubtfully changed function.
194 $doubtfunc = 0;
195 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
196 $doubtfunc = 1;
197 $is_context_diff = 0;
199 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
200 $doubtfunc = 1;
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.
216 if ($look_for_funs
217 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
218 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
219 || /^[-+! ](\{)/))
221 $_ = $1;
222 my $fn;
223 if (/^\{/) {
224 # Beginning of a new function.
225 $_ = $save_fn;
226 } else {
227 $save_fn = "";
229 if (/;$/) {
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.
233 $fn = $1;
234 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
235 # Discard template and function parameters.
236 $fn = $1;
237 1 while ($fn =~ s/<[^<>]*>//);
238 $fn =~ s/[ \t]*$//;
240 # Check is function really modified
241 $no_real_change = 0;
242 if ($doubtfunc) {
243 $idx = $line_idx;
244 # Skip line info in context diffs.
245 while ($is_context_diff && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
246 ++$idx;
248 # Check all lines till the first change
249 # for the presence of really changed function
250 do {
251 ++$idx;
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
258 # brace.
259 if (!$change_msg) {
260 $change_msg .= " ";
261 } else {
262 $change_msg .= "\t";
265 $change_msg .= "($fn):\n";
266 $seen_names{$fn} = 1;
269 $line_idx++;
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
274 # functions.
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";
281 exit 0;