For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2004 > How can I perform search & proximity-replace over multiple lines in C source









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How can I perform search & proximity-replace over multiple lines in C source
Jumper Bones

2004-07-23, 3:56 pm

Hello, I am new to Perl, and I have a need to search for a certain function
call in a C source code file, and then look ahead a given range of lines of
varying code following that function to see if a different function call
exists below it (within that range). If it finds that "different" function
below it, I need to edit its argument(s) with new arguments.

You see, I have so many of these edits to make over so many source code
files (thousands, actually) that I'd like to get some guidance on how to do
this more efficiently.

Also, if I'm better off using Vim to do this, that's fine too. Either
way, I just need someone to point me in a direction on what might be a
solid way of acheiving this..

Thanks in advance,

Jumper Bones
Anno Siegel

2004-07-23, 3:56 pm

Jumper Bones <jumper_bones@hotmail.com> wrote in comp.lang.perl.misc:
> Hello, I am new to Perl, and I have a need to search for a certain function
> call in a C source code file, and then look ahead a given range of lines of
> varying code following that function to see if a different function call
> exists below it (within that range). If it finds that "different" function
> below it, I need to edit its argument(s) with new arguments.
>
> You see, I have so many of these edits to make over so many source code
> files (thousands, actually) that I'd like to get some guidance on how to do
> this more efficiently.


The answer is, as usual, it depends.

It depends mostly on the variability of the call patterns involved.
If these are guaranteed to be formatted in some reasonably easy-to-match
way, a simple Perl program involving pattern matching will do. If not,
nothing short of a full C parser will.

I think there is a C parser (or more) on CPAN, but I don't know how
well they work, nor how easy they are to apply.

If you can find a tool to identify all calls to a function, you could
use its output to guide a Perl program through the source. It could
fix the calls it recognizes and leave notes about the others to fix
manually. Any cross-referencing tool should be able to provide the
input.

This approach looks most promising to me, but it will involve some
substantial Perl programming. In particular, parsing the output
of another program often isn't trivial.

> Also, if I'm better off using Vim to do this, that's fine too. Either
> way, I just need someone to point me in a direction on what might be a
> solid way of acheiving this..


If you want to get fancy, you could output the info about manual fixes
in Vim's QuickFix format. Then you can jump from one place to the
next one, even across files. That could be fun, but it's probably not
worth while in a one-off project.

Anno
Ilmari Karonen

2004-07-23, 8:56 pm

On 2004-07-23, Jumper Bones <jumper_bones@hotmail.com> wrote:
> Hello, I am new to Perl, and I have a need to search for a certain function
> call in a C source code file, and then look ahead a given range of lines of
> varying code following that function to see if a different function call
> exists below it (within that range). If it finds that "different" function
> below it, I need to edit its argument(s) with new arguments.


Assuming you've already got regexps to match those functions:

use constant RANGE => 10;

my $line = - RANGE;
while (<> ) {
/$certain_function/ and $line = $.;
s/$other_function/edit_args($&)/eg if $. < $line + RANGE;
print;
}

--
Ilmari Karonen
If replying by e-mail, please replace ".invalid" with ".net" in address.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com