Sign in to follow this  
Followers 0
emendelson

Find/remove unused variables from source?

7 posts in this topic

Is there a tool that will read my source code and report any variables that I declare but never use? For the past three years, I've been modifying and expanding a script that is now 1,500 lines long, and I declare around a hundred variables at the top of the script.

After all the editing I've done on the script, I think some of these variables never get used. Is there an easy way to generate a list of them so that I can edit them out of my source?

Many thanks for any help.

Share this post


Link to post
Share on other sites



write a script that tries to find each variable, and if it doesn't have it delete that variables declaration.


The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

@Edward Mendelson

Download Scite4AutoIt and install it (if it's not already done)

Right click on your script and select "Compile with options" then go to "Obfuscator" tab.

Check the checkbox and write "/Striponly" as parameter.

Click on compile and open the obfuscated script

Tidy it (Ctrl+T) and you have only used functions and variables !

Maybe theres easier way to do that...ask to Jos for it >_<

Cheers, FireFox.

1 person likes this

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Download Scite4AutoIt and install it (if it's not already done)

Right click on your script and select "Compile with options" then go to "Obfuscator" tab.

Check the checkbox and write "/Striponly" as parameter.

Click on compile and open the obfuscated script

Tidy it (Ctrl+T) and you have only used functions and variables !

Thanks for that. However, I had already tried it - the obfuscated script still contains statements such as

Dim $unusedVar

even though $unusedVar is never used in the rest of the script.

Perhaps the best way is to write a script that modifies my script...

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I think your best bet is another script to read your script... How about something like this?

#include <Array.au3>

Dim $filename = "Your Script File.au3"
Dim $variables[1]
Dim $RegExp

$varFileContents = FileRead ($filename)

$RegExp = StringRegExp ($varFileContents, "(\$\w{1,50})(?:\s|\[)", 3)
If Not @error Then
    For $i = 0 to UBound($RegExp) - 1
        StringReplace ($varFileContents, $RegExp[$i], "")
        
;~      MsgBox (0, "", "Variable Name: " & $RegExp[$i] & @CRLF & "Number of times it occurs: " & @extended)
        
        If @extended = 1 Then
            $variables[UBound($variables)-1] = $RegExp[$i]
            ReDim $variables[UBound($variables+1)]
        EndIf
    Next
EndIf

_ArrayDisplay($variables, "Variables Used Only Once")

I'm pretty sure the Regular Expression should cover most variable/array names, but it could require a little tweaking.

Edited by exodius

Share this post


Link to post
Share on other sites

I think your best bet is another script to read your script... How about something like this?

That script worked beautifully - it even found a variable that I had misspelled in a comment block. Many thanks for that.

I think this script is worth making available permanently in the Example Scripts section of this forum. I've been using it on my other scripts and finding it very useful.

Thanks again!

Share this post


Link to post
Share on other sites

@Edward Mendelson

Download Scite4AutoIt and install it (if it's not already done)

Right click on your script and select "Compile with options" then go to "Obfuscator" tab.

Check the checkbox and write "/Striponly" as parameter.

Click on compile and open the obfuscated script

Tidy it (Ctrl+T) and you have only used functions and variables !

Maybe theres easier way to do that...ask to Jos for it >_<

Cheers, FireFox.

The simple way is to add the AutoIt3Wrapper directive which will run obfuscator and compile the stripped source.

A work of warning about the posted Regex script: Better be sure your script isn't using anything like Eval() because that uses variables without using the $.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0