Daily bump.
[official-gcc.git] / contrib / mklog
blobd3f044ee8581e67a11f96a7f67928e566b0fd535
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 my %cl_entries;
61 sub get_clname($) {
62 my $dirname = $_[0];
63 while ($dirname) {
64 my $clname = "$dirname/ChangeLog";
65 if (-f $clname) {
66 my $filename_rel = substr ($_[0], length ($dirname) + 1);
67 return ($filename_rel, $clname);
68 } else {
69 $dirname =~ s/[\/\\]?[^\/\\]*$//;
72 return ($_[0], 'Unknown Changelog');
75 # For every file in the .diff print all the function names in ChangeLog
76 # format.
77 $bof = 0;
78 $clname = get_clname('');
79 open (DFILE, $diff) or die "Could not open file $diff for reading";
80 while (<DFILE>) {
81 # Check if we found a new file.
82 if (/^\+\+\+ (b\/)?(\S+)/) {
83 # If we have not seen any function names in the previous file (ie,
84 # $bof == 1), we just write out a ':' before starting the next
85 # file.
86 if ($bof == 1) {
87 $cl_entries{$clname} .= ":\n";
89 $filename = $2;
90 ($filename_rel, $clname) = get_clname ($filename);
91 $cl_entries{$clname} .= "\t* $filename_rel";
92 $bof = 1;
95 # Remember the last line in a unified diff block that might start
96 # a new function.
97 if (/^[-+ ]([a-zA-Z0-9_].*)/) {
98 $save_fn = $1;
101 # If we find a new function, print it in brackets. Special case if
102 # this is the first function in a file.
104 # Note that we don't try too hard to find good matches. This should
105 # return a superset of the actual set of functions in the .diff file.
107 # The first two patterns work with context diff files (diff -c). The
108 # third pattern works with unified diff files (diff -u).
110 # The fourth pattern looks for the starts of functions or classes
111 # within a unified diff block.
113 if (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
114 || /^[\-\+\!] ([a-zA-Z0-9_]+)[ \t]*\(.*/
115 || /^@@ .* @@ ([a-zA-Z0-9_].*)/
116 || /^[-+ ](\{)/)
118 $_ = $1;
119 my $fn;
120 if (/^\{/) {
121 # Beginning of a new function.
122 $_ = $save_fn;
123 } else {
124 $save_fn = "";
126 if (/;$/) {
127 # No usable function name found.
128 } elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
129 # Discard stuff after the class/struct/etc. tag.
130 $fn = $1;
131 } elsif (/([a-zA-Z0-9_][^(]*)\(/) {
132 # Discard template and function parameters.
133 $fn = $1;
134 1 while ($fn =~ s/<[^<>]*>//);
135 $fn =~ s/[ \t]*$//;
137 if ($fn && $seen_names{$fn} == 0) {
138 # If this is the first function in the file, we display it next
139 # to the filename, so we need an extra space before the opening
140 # brace.
141 if ($bof) {
142 $cl_entries{$clname} .= " ";
143 $bof = 0;
144 } else {
145 $cl_entries{$clname} .= "\t";
148 $cl_entries{$clname} .= "($fn):\n";
149 $seen_names{$fn} = 1;
154 # If we have not seen any function names (ie, $bof == 1), we just
155 # write out a ':'. This happens when there is only one file with no
156 # functions.
157 if ($bof == 1) {
158 $cl_entries{$clname} .= ":\n";
161 $temp = `mktemp /tmp/$basename.XXXXXX` || exit 1; chop ($temp);
162 open (CLFILE, ">$temp") or die "Could not open file $temp for writing";
164 foreach my $clname (keys %cl_entries) {
165 print CLFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
168 close (DFILE);
170 # Concatenate the ChangeLog template and the original .diff file.
171 system ("cat $diff >>$temp && mv $temp $diff") == 0
172 or die "Could not add the ChangeLog entry to $diff";
174 exit 0;