Macro to find unused variables in proc

Help with writing and playing macros

Macro to find unused variables in proc

Postby Bego » Fri Jun 09, 2006 9:01 am

Hi dudes,

I'd like to have a macro that looks for all defined variables in an (Oracle) Procedure or function that are not used there (maybe list those in a separate new file window, thats ok or -hey- even better: bookmark 'em)

EXAMPLE:

Code: Select all
PROCEDURE log_queue(p_xml_clob IN CLOB) IS
    v_proz VARCHAR2(30) := 'log_queue';
    v_from  NUMBER := 1;
    v_to    NUMBER := 4000;
    v_notused  NUMBER;
    v_ges   NUMBER;

BEGIN
    log(v_proz);

    v_ges := 123;
    WHILE v_from < v_to LOOP
        log('bla');
        v_from := v_from + 1;
    END LOOP;
    log(v_proz);
END log_queue;


So here v_notused should be found.

Some rules:
All variables are found between the keywords PROCEDURE/FUNCTION <name> and BEGIN.
PROC/FUNC always ends with END <name>



Just before I start:
- Does anybody of you guys have sth. similar to that, so I don't need to start from scratch ?
- What do you think about it ?

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Macro to find unused variables in proc

Postby Bego » Thu Jun 15, 2006 5:48 pm

OK, seems not to hit the peoples ass ....
Anyway. I ran into trouble.
I decided to code a small solution.
- Mark first word in cursor line and copy it (I know: clipboard x would be nice, but keep it simple.)
- Then, find that string down. I HAVE to use UE style here :-(
- If found: ok, if not: Mark it as "dead variable".

PROBLEM: THE SECOND FIND does not work.

...and, yes, I set the property "continue find if nothing found" of the macro.

Any ideas ? Is it a prob of mixing search styles ?
(default: Perl regexp in uedit.ini)
Code: Select all
InsertMode
ColumnModeOff
HexOff
PerlReOn
Find RegExp "\S[\w]*\S"
Copy
ToggleBookmark
UnixReOff
Find Select "^c"
IfFound
PreviousBookmark
ToggleBookmark
Key DOWN ARROW
Else
PreviousBookmark
"NOT_FOUND:"
Paste
"END_NOT_FOUND"
ToggleBookmark
Key DOWN ARROW
EndIf


Example:
PROCEDURE log_queue(p_xml_clob IN CLOB) IS
v_from VARCHAR2(30) := 'log_queue';
v_to NUMBER := 4000;
v_notused NUMBER;
v_ges NUMBER;

BEGIN
log(v_proz);

v_ges := 123;
WHILE jo < v_to LOOP
log('bla');
v_from := v_from + 1;
EN LOOP;
log(v_proz);
END log_queue;

After running macro with cursor in line 2 at the beginning of the line:

PROCEDURE log_queue(p_xml_clob IN CLOB) IS
NOT_FOUND:v_fromEND_NOT_FOUND v_from VARCHAR2(30) := 'log_queue';
v_to NUMBER := 4000;
. . .




rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Macro to find unused variables in proc

Postby Mofi » Fri Jun 16, 2006 9:27 am

Hi Bego!

It's really interesting that the Find Select with ^c does not work. It must have something to do with the Perl engine used before. If I use

UnixReOff
Find RegExp "[a-z0-9_]+"

instead of

PerlReOn
Find RegExp "\S[\w]*\S"

your macro works perfect. Actually it is not necessary to switch to UltraEdit style for the Find with ^c because this is not a regular expression search. ^c works also with Perl engine enabled, if it is not a regular expression search. But not in your macro. This must be a bug of UE. A ^c search with Perl engine enabled but Regular Expression option disabled executed via the Find dialog works.

However, here is a macro which does what you actually want. Don't forget to enable the macro property Continue if a Find with Replace not found and replace the 2 green tab by the real tab character or modify the string to whatever you want.

This macro uses ^c within a regular expression search. But the regex search is only necessary because I don't know if you have anywhere a tab or more than 1 space between END and procedure name. If you have always only 1 space at the end of the procedure between END and the name, you can use a normal find and then the whole macro should also work with the Perl engine after transforming the regex searches to Perl syntax.

InsertMode
ColumnModeOff
HexOff
Key END
UnixReOff
Find MatchCase RegExp Up "^{PROCEDURE^}^{FUNCTION^}"

IfNotFound
PerlReOn
ExitMacro
EndIf
Clipboard 9
StartSelect
Find RegExp "[0-9a-z_]+"
Copy
EndSelect
Key HOME
IfColNumGt 1
Key HOME
EndIf
StartSelect
Find MatchCase RegExp Select "END[ ^t]+^c;"
Copy
NewFile
Paste
Top
TrimTrailingSpaces
DeleteLine
Loop
Find "^p^p"
Replace All "^p"
IfNotFound
ExitLoop
EndIf
EndLoop
Find MatchCase "BEGIN"
Key HOME
"#"
Key LEFT ARROW
SelectToTop
Find RegExp "%[ ^t]++^([0-9a-z_]+^)*$"
Replace All SelectText "^1"
EndSelect
Top
Loop
IfCharIs "#"
SelectToBottom
Delete
EndSelect
ExitLoop
EndIf
SelectWord
Copy
EndSelect
Key LEFT ARROW
Key RIGHT ARROW
Find MatchCase "^c"
Replace All "^c"
IfNotFound
"tabtab<- not found !!!"
EndIf
Key HOME
Key DOWN ARROW
EndLoop
ClearClipboard
Clipboard 0
PerlReOn

Edit: Modified the red marked lines.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Macro to find unused variables in proc

Postby Bego » Fri Jul 28, 2006 9:43 am

Tag Mofi,

So now I FINALLY found some time to have a more detailed look on the macro. The long time it took has NOTHING to do with disappreciation.
In summary, the macro just works excellent and supports me writing more clean code (since I found some "Variablen-Leichen" ....)

The only minor change is that i do NOT find MATCHCASE the "end blabla" - Statement, but thats just mega-minor.

cu, Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany


Return to Macros