[gcc/]
[official-gcc.git] / contrib / mklog
blob3d17dc54891206685548842e90d0df30365b406e
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/[^\\\/]+$/../;
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`;
43 chomp($gitname);
44 if ($gitname) {
45 $name = $gitname;
48 $gitaddr = `git config user.email`;
49 chomp($gitaddr);
50 if ($gitaddr) {
51 $addr = $gitaddr;
55 #-----------------------------------------------------------------------------
56 # Program starts here. You should not need to edit anything below this
57 # line.
58 #-----------------------------------------------------------------------------
59 if ($#ARGV != 0) {
60 $prog = `basename $0`; chop ($prog);
61 print <<EOF;
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.
67 EOF
68 exit 1;
71 $diff = $ARGV[0];
72 $dir = `dirname $diff`; chop ($dir);
73 $basename = `basename $diff`; chop ($basename);
74 $hdrline = "$date $name <$addr>";
76 sub get_clname ($) {
77 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
79 my $dirname = $_[0];
80 while ($dirname) {
81 my $clname = "$dirname/ChangeLog";
82 if (-f "$gcc_root/$clname") {
83 my $relname = substr ($_[0], length ($dirname) + 1);
84 return ($clname, $relname);
85 } else {
86 $dirname =~ s/[\/\\]?[^\/\\]*$//;
90 return ('Unknown ChangeLog', $_[0]);
93 sub remove_suffixes ($) {
94 my $filename = $_[0];
95 $filename =~ s/^[ab]\///;
96 $filename =~ s/\.jj$//;
97 return $filename;
100 # Check if line is a top-level declaration.
101 # TODO: ignore preprocessor directives except maybe #define ?
102 sub is_top_level {
103 my ($function, $is_context_diff) = (@_);
104 if ($is_context_diff) {
105 $function =~ s/^..//;
106 } else {
107 $function =~ s/^.//;
109 return $function && $function !~ /^[\s{}]/;
112 # For every file in the .diff print all the function names in ChangeLog
113 # format.
114 %cl_entries = ();
115 $change_msg = undef;
116 $look_for_funs = 0;
117 $clname = get_clname('');
118 open (DFILE, $diff) or die "Could not open file $diff for reading";
119 chomp (my @diff_lines = <DFILE>);
120 close (DFILE);
121 $line_idx = 0;
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);
128 $look_for_funs = 0;
130 if (!/---$/ && /^--- +(\S+)?/) {
131 $right = remove_suffixes ($1);
132 $look_for_funs = 0;
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
142 # file.
143 if ($clname) {
144 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
147 if ($left eq $right) {
148 $filename = $left;
149 } elsif($left eq '/dev/null') {
150 $filename = $right;
151 } elsif($right eq '/dev/null') {
152 $filename = $left;
153 } else {
154 print STDERR "Error: failed to parse diff for $left and $right\n";
155 exit 1;
157 $left = $right = undef;
158 ($clname, $relname) = get_clname ($filename);
159 $cl_entries{$clname} .= "\t* $relname";
160 $change_msg = '';
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) {
167 s/^([-+! ]) /\1/;
170 # Remember the last line in a diff block that might start
171 # a new function.
172 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
173 $save_fn = $1;
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";
181 $look_for_funs = 0;
184 # Check if file was removed.
185 # Two patterns: for context and unified diff.
186 if (/^--- 0 ----/
187 || /^@@ -1.* \+0,0 @@/) {
188 $change_msg = ": Remove.\n";
189 $look_for_funs = 0;
192 # Mark if we met doubtfully changed function.
193 $doubtfunc = 0;
194 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
195 $doubtfunc = 1;
196 $is_context_diff = 0;
198 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
199 $doubtfunc = 1;
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.
215 if ($look_for_funs
216 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
217 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
218 || /^[-+! ](\{)/))
220 $_ = $1;
221 my $fn;
222 if (/^\{/) {
223 # Beginning of a new function.
224 $_ = $save_fn;
225 } else {
226 $save_fn = "";
228 if (/;$/) {
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.
232 $fn = $1;
233 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
234 # Discard template and function parameters.
235 $fn = $1;
236 1 while ($fn =~ s/<[^<>]*>//);
237 $fn =~ s/[ \t]*$//;
239 # Check is function really modified
240 $no_real_change = 0;
241 if ($doubtfunc) {
242 $idx = $line_idx;
243 # Skip line info in context diffs.
244 while ($is_context_diff && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
245 ++$idx;
247 # Check all lines till the first change
248 # for the presence of really changed function
249 do {
250 ++$idx;
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
257 # brace.
258 if (!$change_msg) {
259 $change_msg .= " ";
260 } else {
261 $change_msg .= "\t";
264 $change_msg .= "($fn):\n";
265 $seen_names{$fn} = 1;
268 $line_idx++;
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
273 # functions.
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";
280 exit 0;