List of defined but unused variables, constant etc.
List of defined but unused variables, constant etc.
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.
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!"
"Happiness is a pet." | "Never run a changing system!"
Re: List of defined but unused variables, constant etc.
+1
I have run into similar issues.
I have run into similar issues.
-
- Addict
- Posts: 4519
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: List of defined but unused variables, constant etc.
+1
Proud supporter of PB! * Musician * C64/6502 Freak
Re: List of defined but unused variables, constant etc.
"Have you tried turning it off and on again ?"
A little PureBasic review
A little PureBasic review
Re: List of defined but unused variables, constant etc.
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
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
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
Re: List of defined but unused variables, constant etc.
+1
There are 2 methods to program bugless.
But only the third works fine.
Win10, Pb x64 5.71 LTS
But only the third works fine.
Win10, Pb x64 5.71 LTS
- NicTheQuick
- Addict
- Posts: 1224
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: List of defined but unused variables, constant etc.
+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.
Re: List of defined but unused variables, constant etc.
+1
I had the idea to do that too. (It's on my ToDo list one day or next next year or more )
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
When (If) done 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?
I had the idea to do that too. (It's on my ToDo list one day or next next year or more )
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
When (If) done 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?
Re: List of defined but unused variables, constant etc.
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.
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
- NicTheQuick
- Addict
- Posts: 1224
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: List of defined but unused variables, constant etc.
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.
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.
Re: List of defined but unused variables, constant etc.
That's why I want to limit within a Procedure.
And reporting multiple or single instance pointers would not hurt anyone.
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
Re: List of defined but unused variables, constant etc.
+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.
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)
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)