List of defined but unused variables, constant etc.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

List of defined but unused variables, constant etc.

Post by Kurzer »

My current project is growing to a huge size and after making changes to some procedures it would be helpful if the IDE would provide a list of all defined but not used variables, constants, procedures and so on.

Sometimes there are subsequent changes (optimizations) in the code where one or more variables are no longer needed. Starting from a certain source code size it will be difficult to keep track of everything - or you have to search all variables manually in all files.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: List of defined but unused variables, constant etc.

Post by RSBasic »

+1
Image
Image
smishra
User
User
Posts: 70
Joined: Tue Jun 06, 2006 3:39 pm
Location: Maryland US

Re: List of defined but unused variables, constant etc.

Post by smishra »

+1

I have run into similar issues.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: List of defined but unused variables, constant etc.

Post by Little John »

+1
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 281
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: List of defined but unused variables, constant etc.

Post by oreopa »

+1
Proud supporter of PB! * Musician * C64/6502 Freak
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: List of defined but unused variables, constant etc.

Post by luis »

Already asked 10 years ago :shock:

viewtopic.php?f=3&t=35703
"Have you tried turning it off and on again ?"
A little PureBasic review
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: List of defined but unused variables, constant etc.

Post by davido »

+1
DE AA EB
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: List of defined but unused variables, constant etc.

Post by Sicro »

Since a long time, I have a PB-Tool on my ToDo-List, which should be able to do this, but it is a lot of work.

A native solution would be better and certainly easier, because the PB parser already has this information
and we don't have to write a code parser again ourselves.

+1
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: List of defined but unused variables, constant etc.

Post by Fig »

+1
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
NicTheQuick
Addict
Addict
Posts: 1223
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: List of defined but unused variables, constant etc.

Post by NicTheQuick »

+1
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: List of defined but unused variables, constant etc.

Post by Marc56us »

+1

I had the idea to do that too. (It's on my ToDo list one day or next next year or more :? )
:idea: The principle was to force user to use EnableExplicit, then the tool follow the source and "just" enter all the keywords in an array or map and count all the occurrences of the following words after "Define", "Global", "Dim", "NewList", "NewMap" etc starting from the main file and following all (X)IncludeFile
If a word in the table appears only once, it means that the variable or constant is unused.

There are some difficulties because it is sometimes necessary to differentiate between two keywords in succession (ex: Global Dim ABC())
So keyword must be exclude keyword following

It's not as simple as I imagined at first :o :shock:

When (If) done :arrow: Make it a program that will be launched before compilation or on demand

So that's the idea, but maybe there's something better or simpler?

In the same vein, we should list the AllocateMemory() that do not have FreeMemory(). Except when they are in a procedure that must automatically release when exiting (although I don't know if AllocateMemory() is not persistent?

8)
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: List of defined but unused variables, constant etc.

Post by skywalk »

Yes, in the meantime, we have to build our own.
I have started and stopped many times trying to build this.
I want to focus the Tool within a Procedure.
(Macro's would be more difficult due to runtime expansion.)
Main space variables could be added later since the PureBasic IDE already lists these and more.
Unfortunately, the IDE does not jump to their definitions?
And the IDE Variable browser in locked in a tabbed panel with dropdowns for each type.
It is many many clicks to search.
These should be individual floating windows.

Does this pseudo code satisfy a Procedure scan?
1. Parse all selected text(Procedure..EndProcedure) by newline.
2. Search for keywords that define Variables and Procedures.
Define
Shared
Protected
Static
= AllocateMemory(
= Ascii(
= UTF8(
Array: Dim, ReDim
Map: NewMap
List: NewList
3. Search within each found item from 2.
Add names to mapVAR()
4. Add names to mapVAR() from the Procedure call list.
5. Loop through all mapVAR() items and search parsed text for existence > 1.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
NicTheQuick
Addict
Addict
Posts: 1223
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: List of defined but unused variables, constant etc.

Post by NicTheQuick »

You miss something: Scopes. I can use the variable 'i' multiple times in my code, a new declaration in every procedure if I want to. You need to start a new scope for every procedure you encounter and also for every module and every procedure in every module. They are independent of each other.
And then thera are AllocateMemory(), AllocateStructure(), Ascii(), UTF8() and so on. You must not check these. You can not track down where these pointers will go at compile time. Just ignore them. If the developer forgot to free the allocated memory in the end it is his fault.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: List of defined but unused variables, constant etc.

Post by skywalk »

That's why I want to limit within a Procedure.
And reporting multiple or single instance pointers would not hurt anyone.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: List of defined but unused variables, constant etc.

Post by eck49 »

+1 (despite being over 2 years since the past post in the thread)

A report listing unreferenced variables and where they are defined would be very handy and the natural place to generate this is the compiler - perhaps as an optional feature. After all, the compiler has to cope with keywords, variable names built using macros, the whole works. Possibly unreferenced constants could be handled similarly - after all, the compiler needs to remember their values even if it is not allocating memory to them.

For variables, unreferenced and unused are almost synonymous if one ignores overlarge arrays and deliberate trickery with explicit memory allocation. But the same is not true for procedures. A procedure which is only called from an unused procedure is referenced but unused.

Such a report would greatly assist declutter and sometimes catch typos or cases where a line of code has been omitted and should be more reliable than the product of a separate program working on the project code.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply