Script for removing PARAMS from gdb sources

Kevin Buettner
Mon May 22 11:52:00 GMT 2000

The script below will remove most occurrences of "PARAMS" from the gdb
sources.  It is invoked as

    nuke-params-indent /path/to/gdb/sources

The script will construct a list of the *.[chy] files in the given
source tree and then, in each of these files, search for patterns
representing declarations of the following form:

    type-spec identifier PARAMS ((parameter-list));

It removes the "PARAMS" and the outer set of parens and invokes GNU
indent on the resulting declaration.  The PARAM-less, reindented
declarations are reincorporated back into the source file in the
same positions as the original declarations.  (Note that indent
is only invoked on individual declarations, not entire source files.)

If GNU indent produces any warnings while processing a declaration,
the script will output to stderr a message indicating the file and
declaration that should be hand checked.  At the moment, this script
only flags one such declaration contained in config/pa/tm-hppa.h.  The
declaration in question is in some disabled (#if 0'd) code and is
missing a semicolon.

As noted in my other message, there are still around 260 occurrences
of PARAMS left in the gdb sources after running this script.  It is
easy to make it handle even more of them, but these results need to be
examined carefully (and in some cases edited) since many of them are
pointer variable declarations appearing in struct declarations.  The
script doesn't know that they appear in struct declarations (and in a
few instances in a function body) and causes GNU indent to mishandle
the leading indent.  So, for the first pass, I opted to make the script
handle fewer of the overall cases, so that (hopefully) no hand editing
will be needed.  On the second pass, I will likely employ a slightly
modified version of the script below along with hand editing to fix
the places where the leading indent is wrong.

--- nuke-params-indent ---
#!/usr/bin/perl -w

use File::Find;
use FileHandle;
use IPC::Open3;
use English;

my ($root) = @ARGV;

if (!defined($root)) {
    die "Usage: $0 root\n";

@ARGV = ();

    sub { 
	if (-f && -T && /\.[chy]$/) {
	    push @ARGV, $File::Find::name;

#$INPLACE_EDIT = '.bak';
undef $/;			# slurp entire files

while (<>) {
    s{^				# line start
      ([^\n]*\w+\W+\w+\s+)	# leading stuff, type name, identifier, spaces
      PARAMS			# what it says
      \ 			# a space
      \(\(			# double left parens
      ([^;]*)			# parameter list
      \)\)			# double right parens
      ( [;,] 			# semicolon or comma ...
       |			#   or
	(?:\s*ATTR_FORMAT [^;]+;)
				# ... one of those funky ATTR_FORMAT
				# thingies as found in language.h
	my ($rfh, $wfh, $efh) = (FileHandle->new, FileHandle->new,
	my $pid = open3($wfh, $rfh, $efh, "indent");
	my $decl = "$1 ($2)$3";
	my $replacement = <$rfh>;
	my $errstr = <$efh>;
	waitpid $pid, 0;
	$replacement =~ s#\n$##;
	if ($errstr ne "") {
	    print STDERR "Check $ARGV...\n$errstr\nInput:$decl\nOutput:$replacement\n\n"
--- end nuke-params-indent ---

Here's a sample run (to show the warning output):

    ocotillo:/tmp$ cp -a /ocotillo1/kev/sourceware/src/gdb gdb-nuked-params
    ocotillo:/tmp$ cd gdb-nuked-params/
    ocotillo:gdb-nuked-params$ /home/kev/ptests/nuke-params-indent .
    Check ./config/pa/tm-hppa.h...
    indent: Standard input:2: Warning:Extra )

    Input:extern void target_write_pc
     (CORE_ADDR, int))
	 extern CORE_ADDR target_read_pc PARAMS ((int);
    Output:extern void target_write_pc (CORE_ADDR, int))
	 extern CORE_ADDR target_read_pc PARAMS ((int);


More information about the Gdb-patches mailing list