* acinclude.m4 (GLIBCXX_ENABLE_C99): Fix typo.
[official-gcc.git] / contrib / mklog
blob16ce1914db6ba3d3b8f5ade558a2af7c351fe3b5
1 #!/usr/bin/perl
2 # Copyright (C) 2012 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 writes a skeleton ChangeLog file to stdout. 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;
42 #-----------------------------------------------------------------------------
43 # Program starts here. You should not need to edit anything below this
44 # line.
45 #-----------------------------------------------------------------------------
46 if ($#ARGV != 0) {
47 $prog = `basename $0`; chop ($prog);
48 print "usage: $prog file.diff\n\n";
49 print "Adds a ChangeLog template to the start of file.diff\n";
50 print "It assumes that file.diff has been created with -up or -cp.\n";
51 exit 1;
54 $diff = $ARGV[0];
55 $dir = `dirname $diff`; chop ($dir);
56 $basename = `basename $diff`; chop ($basename);
57 $hdrline = "$date $name <$addr>";
59 sub get_clname ($) {
60 return ('ChangeLog', $_[0]) if ($_[0] !~ /[\/\\]/);
62 my $dirname = $_[0];
63 while ($dirname) {
64 my $clname = "$dirname/ChangeLog";
65 if (-f $clname) {
66 my $relname = substr ($_[0], length ($dirname) + 1);
67 return ($clname, $relname);
68 } else {
69 $dirname =~ s/[\/\\]?[^\/\\]*$//;
73 return ('Unknown ChangeLog', $_[0]);
76 sub remove_suffixes ($) {
77 my $filename = $_[0];
78 $filename =~ s/^[ab]\///;
79 $filename =~ s/\.jj$//;
80 return $filename;
83 # Check if line can be a function declaration:
84 # First pattern cut extra symbols added by diff
85 # second pattern checks that line is not a comment or brace
86 sub is_function {
87 my ($function, $is_context_diff) = (@_);
88 if ($is_context_diff) {
89 $function =~ s/^..//;
90 } else {
91 $function =~ s/^.//;
93 return $function
94 && ($function !~ /^[\s{}]/);
97 # For every file in the .diff print all the function names in ChangeLog
98 # format.
99 %cl_entries = ();
100 $change_msg = undef;
101 $look_for_funs = 0;
102 $clname = get_clname('');
103 open (DFILE, $diff) or die "Could not open file $diff for reading";
104 chomp (my @diff_lines = <DFILE>);
105 close (DFILE);
106 $line_idx = 0;
107 foreach (@diff_lines) {
108 # Stop processing functions if we found a new file
109 # Remember both left and right names because one may be /dev/null.
110 if (/^[+*][+*][+*] +(\S+)/) {
111 $left = remove_suffixes ($1);
112 $look_for_funs = 0;
114 if (/^--- +(\S+)?/) {
115 $right = remove_suffixes ($1);
116 $look_for_funs = 0;
119 # Check if the body of diff started.
120 # We should now have both left and right name,
121 # so we can decide filename.
123 if ($left && (/^\*{15}$/ || /^@@ /)) {
124 # If we have not seen any function names in the previous file (ie,
125 # $change_msg is empty), we just write out a ':' before starting the next
126 # file.
127 if ($clname) {
128 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
131 if ($left eq $right) {
132 $filename = $left;
133 } elsif($left eq '/dev/null') {
134 $filename = $right;
135 } elsif($right eq '/dev/null') {
136 $filename = $left;
137 } else {
138 print STDERR "Error: failed to parse diff for $left and $right\n";
139 exit 1;
141 $left = $right = undef;
142 ($clname, $relname) = get_clname ($filename);
143 $cl_entries{$clname} .= "\t* $relname";
144 $change_msg = '';
145 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
148 # Remember the last line in a unified diff block that might start
149 # a new function.
150 if (/^[-+ ]([a-zA-Z0-9_].*)/) {
151 $save_fn = $1;
154 # Check if file is newly added.
155 # Two patterns: for context and unified diff.
156 if (/^\*\*\* 0 \*\*\*\*/
157 || /^@@ -0,0 \+1.* @@/) {
158 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ? ": New test.\n" : ": New file.\n";
159 $look_for_funs = 0;
162 # Check if file was removed.
163 # Two patterns: for context and unified diff.
164 if (/^--- 0 ----/
165 || /^@@ -1.* \+0,0 @@/) {
166 $change_msg = ": Remove.\n";
167 $look_for_funs = 0;
170 # Mark if we met doubtfully changed function.
171 $doubtfunc = 0;
172 $is_context_diff = 0;
173 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
174 $doubtfunc = 1;
176 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
177 $doubtfunc = 1;
178 $is_context_diff = 1;
181 # If we find a new function, print it in brackets. Special case if
182 # this is the first function in a file.
184 # Note that we don't try too hard to find good matches. This should
185 # return a superset of the actual set of functions in the .diff file.
187 # The first two patterns work with context diff files (diff -c). The
188 # third pattern works with unified diff files (diff -u).
190 # The fourth pattern looks for the starts of functions or classes
191 # within a unified diff block.
193 if ($look_for_funs
194 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
195 || /^[\-\+\!] ([a-zA-Z0-9_]+)[ \t]*\(.*/
196 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
197 || /^[-+ ](\{)/))
199 $_ = $1;
200 my $fn;
201 if (/^\{/) {
202 # Beginning of a new function.
203 $_ = $save_fn;
204 } else {
205 $save_fn = "";
207 if (/;$/) {
208 # No usable function name found.
209 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
210 # Discard stuff after the class/struct/etc. tag.
211 $fn = $1;
212 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
213 # Discard template and function parameters.
214 $fn = $1;
215 1 while ($fn =~ s/<[^<>]*>//);
216 $fn =~ s/[ \t]*$//;
218 # Check is function really modified
219 $no_real_change = 0;
220 if ($doubtfunc) {
221 $idx = $line_idx;
222 # Check all lines till the first change
223 # for the presence of really changed function
224 do {
225 ++$idx;
226 $no_real_change = is_function ($diff_lines[$idx], $is_context_diff);
227 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[\+\-\!]/))
229 if ($fn && !$seen_names{$fn} && !$no_real_change) {
230 # If this is the first function in the file, we display it next
231 # to the filename, so we need an extra space before the opening
232 # brace.
233 if (!$change_msg) {
234 $change_msg .= " ";
235 } else {
236 $change_msg .= "\t";
239 $change_msg .= "($fn):\n";
240 $seen_names{$fn} = 1;
243 $line_idx++;
246 # If we have not seen any function names (ie, $change_msg is empty), we just
247 # write out a ':'. This happens when there is only one file with no
248 # functions.
249 $cl_entries{$clname} .= $change_msg ? ": $change_msg\n" : ":\n";
251 $temp = `mktemp /tmp/$basename.XXXXXX` || exit 1; chop ($temp);
252 open (CLFILE, ">$temp") or die "Could not open file $temp for writing";
254 foreach my $clname (keys %cl_entries) {
255 print CLFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
258 # Concatenate the ChangeLog template and the original .diff file.
259 system ("cat $diff >>$temp && mv $temp $diff") == 0
260 or die "Could not add the ChangeLog entry to $diff";
262 exit 0;