xref: /netbsd/external/gpl3/gcc/dist/contrib/mklog (revision af526226)
1#!/usr/bin/perl
2# Copyright (C) 2012 Free Software Foundation, Inc.
3#
4# This file is part of GCC.
5#
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.
10#
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.
15#
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.
20
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.
25#
26# Author: Diego Novillo <dnovillo@google.com> and
27#         Cary Coutant <ccoutant@google.com>
28
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);
36
37
38#-----------------------------------------------------------------------------
39# Program starts here. You should not need to edit anything below this
40# line.
41#-----------------------------------------------------------------------------
42if ( $#ARGV != 0 ) {
43    $prog = `basename $0`; chop ($prog);
44    print "usage: $prog file.diff\n\n";
45    print "Adds a ChangeLog template to the start of file.diff\n";
46    print "It assumes that file.diff has been created with -up or -cp.\n";
47    exit 1;
48}
49
50$diff = $ARGV[0];
51$dir = `dirname $diff`; chop ($dir);
52$basename = `basename $diff`; chop ($basename);
53$cl = `mktemp /tmp/$basename.XXXXXX` || exit 1; chop ($cl);
54$hdrline = "$date  $name  <$addr>";
55
56open (CLFILE, ">$cl") or die "Could not open file $cl for writing";
57
58print CLFILE "$hdrline\n\n";
59
60# For every file in the .diff print all the function names in ChangeLog
61# format.
62$bof = 0;
63open (DFILE, $diff) or die "Could not open file $diff for reading";
64while (<DFILE>) {
65    # Check if we found a new file.
66    if (/^\+\+\+ (b\/)?(\S+)/) {
67	# If we have not seen any function names in the previous file (ie,
68	# $bof == 1), we just write out a ':' before starting the next
69	# file.
70	if ($bof == 1) {
71	    print CLFILE ":\n";
72	}
73	$filename = $2;
74	print CLFILE "\t* $filename";
75	$bof = 1;
76    }
77
78    # Remember the last line in a unified diff block that might start
79    # a new function.
80    if (/^[-+ ]([a-zA-Z0-9_].*)/) {
81        $save_fn = $1;
82    }
83
84    # If we find a new function, print it in brackets.  Special case if
85    # this is the first function in a file.
86    #
87    # Note that we don't try too hard to find good matches.  This should
88    # return a superset of the actual set of functions in the .diff file.
89    #
90    # The first two patterns work with context diff files (diff -c). The
91    # third pattern works with unified diff files (diff -u).
92    #
93    # The fourth pattern looks for the starts of functions or classes
94    # within a unified diff block.
95
96    if (/^\*\*\*\*\*\** ([a-zA-Z0-9_].*)/
97        || /^[\-\+\!] ([a-zA-Z0-9_]+)[ \t]*\(.*/
98	|| /^@@ .* @@ ([a-zA-Z0-9_].*)/
99	|| /^[-+ ](\{)/)
100      {
101	$_ = $1;
102	my $fn;
103	if (/^\{/) {
104	    # Beginning of a new function.
105	    $_ = $save_fn;
106	} else {
107	    $save_fn = "";
108	}
109	if (/;$/) {
110	    # No usable function name found.
111	} elsif (/^((class|struct|union|enum) [a-zA-Z0-9_]+)/) {
112	    # Discard stuff after the class/struct/etc. tag.
113	    $fn = $1;
114	} elsif (/([a-zA-Z0-9_][^(]*)\(/) {
115	    # Discard template and function parameters.
116	    $fn = $1;
117	    1 while ($fn =~ s/<[^<>]*>//);
118	    $fn =~ s/[ \t]*$//;
119	}
120	if ($fn && $seen_names{$fn} == 0) {
121	    # If this is the first function in the file, we display it next
122	    # to the filename, so we need an extra space before the opening
123	    # brace.
124	    if ($bof) {
125		print CLFILE " ";
126		$bof = 0;
127	    } else {
128		print CLFILE "\t";
129	    }
130
131	    print CLFILE "($fn):\n";
132	    $seen_names{$fn} = 1;
133	}
134    }
135}
136
137# If we have not seen any function names (ie, $bof == 1), we just
138# write out a ':'. This happens when there is only one file with no
139# functions.
140if ($bof == 1) {
141    print CLFILE ":\n";
142}
143
144print CLFILE "\n";
145close (DFILE);
146
147# Concatenate the ChangeLog template and the original .diff file.
148system ("cat $diff >>$cl && mv $cl $diff") == 0
149    or die "Could not add the ChangeLog entry to $diff";
150
151exit 0;
152