Tapatalk

Macro to find unused variables in proc

Macro to find unused variables in proc

344
MasterMaster
344

    Jun 09, 2006#1

    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

      Jun 15, 2006#2

      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
      Normally using all newest english version incl. each hotfix. Win 10 64 bit

      6,685587
      Grand MasterGrand Master
      6,685587

        Jun 16, 2006#3

        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.
        Best regards from an UC/UE/UES for Windows user from Austria

        344
        MasterMaster
        344

          Jul 28, 2006#4

          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
          Normally using all newest english version incl. each hotfix. Win 10 64 bit