PR target/56858
[official-gcc.git] / contrib / mklog
blobfb489b03a3bfc5c5b241615fb2ac898ba7ca33b5
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;
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 is a top-level declaration.
84 # TODO: ignore preprocessor directives except maybe #define ?
85 sub is_top_level {
86 my ($function, $is_context_diff) = (@_);
87 if ($is_context_diff) {
88 $function =~ s/^..//;
89 } else {
90 $function =~ s/^.//;
92 return $function && $function !~ /^[\s{}]/;
95 # For every file in the .diff print all the function names in ChangeLog
96 # format.
97 %cl_entries = ();
98 $change_msg = undef;
99 $look_for_funs = 0;
100 $clname = get_clname('');
101 open (DFILE, $diff) or die "Could not open file $diff for reading";
102 chomp (my @diff_lines = <DFILE>);
103 close (DFILE);
104 $line_idx = 0;
105 foreach (@diff_lines) {
106 # Stop processing functions if we found a new file.
107 # Remember both left and right names because one may be /dev/null.
108 # Don't be fooled by line markers in case of context diff.
109 if (!/\*\*\*$/ && /^[+*][+*][+*] +(\S+)/) {
110 $left = remove_suffixes ($1);
111 $look_for_funs = 0;
113 if (!/---$/ && /^--- +(\S+)?/) {
114 $right = remove_suffixes ($1);
115 $look_for_funs = 0;
118 # Check if the body of diff started.
119 # We should now have both left and right name,
120 # so we can decide filename.
122 if ($left && (/^\*{15}/ || /^@@ /)) {
123 # If we have not seen any function names in the previous file (ie,
124 # $change_msg is empty), we just write out a ':' before starting the next
125 # file.
126 if ($clname) {
127 $cl_entries{$clname} .= $change_msg ? "$change_msg" : ":\n";
130 if ($left eq $right) {
131 $filename = $left;
132 } elsif($left eq '/dev/null') {
133 $filename = $right;
134 } elsif($right eq '/dev/null') {
135 $filename = $left;
136 } else {
137 print STDERR "Error: failed to parse diff for $left and $right\n";
138 exit 1;
140 $left = $right = undef;
141 ($clname, $relname) = get_clname ($filename);
142 $cl_entries{$clname} .= "\t* $relname";
143 $change_msg = '';
144 $look_for_funs = $filename =~ '\.(c|cpp|C|cc|h|inc|def)$';
147 # Context diffs have extra whitespace after first char;
148 # remove it to make matching easier.
149 if ($is_context_diff) {
150 s/^([-+! ]) /\1/;
153 # Remember the last line in a diff block that might start
154 # a new function.
155 if (/^[-+! ]([a-zA-Z0-9_].*)/) {
156 $save_fn = $1;
159 # Check if file is newly added.
160 # Two patterns: for context and unified diff.
161 if (/^\*\*\* 0 \*\*\*\*/
162 || /^@@ -0,0 \+1.* @@/) {
163 $change_msg = $filename =~ /testsuite.*(?<!\.exp)$/ ? ": New test.\n" : ": New file.\n";
164 $look_for_funs = 0;
167 # Check if file was removed.
168 # Two patterns: for context and unified diff.
169 if (/^--- 0 ----/
170 || /^@@ -1.* \+0,0 @@/) {
171 $change_msg = ": Remove.\n";
172 $look_for_funs = 0;
175 # Mark if we met doubtfully changed function.
176 $doubtfunc = 0;
177 if ($diff_lines[$line_idx] =~ /^@@ .* @@ ([a-zA-Z0-9_].*)/) {
178 $doubtfunc = 1;
179 $is_context_diff = 0;
181 elsif ($diff_lines[$line_idx] =~ /^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/) {
182 $doubtfunc = 1;
183 $is_context_diff = 1;
186 # If we find a new function, print it in brackets. Special case if
187 # this is the first function in a file.
189 # Note that we don't try too hard to find good matches. This should
190 # return a superset of the actual set of functions in the .diff file.
192 # The first pattern works with context diff files (diff -c). The
193 # second pattern works with unified diff files (diff -u).
195 # The third pattern looks for the starts of functions or classes
196 # within a diff block both for context and unified diff files.
198 if ($look_for_funs
199 && (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
200 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
201 || /^[-+! ](\{)/))
203 $_ = $1;
204 my $fn;
205 if (/^\{/) {
206 # Beginning of a new function.
207 $_ = $save_fn;
208 } else {
209 $save_fn = "";
211 if (/;$/) {
212 # No usable function name found.
213 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
214 # Discard stuff after the class/struct/etc. tag.
215 $fn = $1;
216 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
217 # Discard template and function parameters.
218 $fn = $1;
219 1 while ($fn =~ s/<[^<>]*>//);
220 $fn =~ s/[ \t]*$//;
222 # Check is function really modified
223 $no_real_change = 0;
224 if ($doubtfunc) {
225 $idx = $line_idx;
226 # Skip line info in context diffs.
227 while ($is_context_diff && $diff_lines[$idx + 1] =~ /^[-\*]{3} [0-9]/) {
228 ++$idx;
230 # Check all lines till the first change
231 # for the presence of really changed function
232 do {
233 ++$idx;
234 $no_real_change = is_top_level ($diff_lines[$idx], $is_context_diff);
235 } while (!$no_real_change && ($diff_lines[$idx] !~ /^[-+!]/))
237 if ($fn && !$seen_names{$fn} && !$no_real_change) {
238 # If this is the first function in the file, we display it next
239 # to the filename, so we need an extra space before the opening
240 # brace.
241 if (!$change_msg) {
242 $change_msg .= " ";
243 } else {
244 $change_msg .= "\t";
247 $change_msg .= "($fn):\n";
248 $seen_names{$fn} = 1;
251 $line_idx++;
254 # If we have not seen any function names (ie, $change_msg is empty), we just
255 # write out a ':'. This happens when there is only one file with no
256 # functions.
257 $cl_entries{$clname} .= $change_msg ? "$change_msg\n" : ":\n";
259 $temp = `mktemp /tmp/$basename.XXXXXX` || exit 1; chop ($temp);
260 open (CLFILE, ">$temp") or die "Could not open file $temp for writing";
262 foreach my $clname (keys %cl_entries) {
263 print CLFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
266 # Concatenate the ChangeLog template and the original .diff file.
267 system ("cat $diff >>$temp && mv $temp $diff") == 0
268 or die "Could not add the ChangeLog entry to $diff";
270 exit 0;