Could we have an option to flag unused variable names,please

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Captn. Jinguji
User
User
Posts: 84
Joined: Sun Oct 24, 2004 9:25 am

Could we have an option to flag unused variable names,please

Post by Captn. Jinguji »

Ok, EnableExplicit more or less protects me from using mis-typed
varnamens.

But I also find myself re-working code after some time,
and it could be quite helpful to have variable names flagged
that are never or no longer actually referenced in the code.

Thks.
Mistrel
Addict
Addict
Posts: 3379
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

+1
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

that would be very usefull.
Maybe someone could make a PlugIn? :wink:
pb 5.11
akj
Enthusiast
Enthusiast
Posts: 665
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

I was thinking of writing code to solve this as I badly need it myself, but it would be a major undertaking. I have previously written a similar routine in Amstrad CPC Locomotive Basic and it worked very well.

One main problem would be acquiring a list of reserved words including the core words such as For and Select and SizeOf(), and the names of all installed library routines such as AddGadgetColumn(). I probably would not need a list of the API routines as I could cheat by assuming any name that ended in an underscore immediately before an opening parenthesis was an API routine.

Does anyone know of a quick and easy was of acquiring such lists? It would need to be done by interrogating files within the PureBasic installation directory structure otherwise it would be a pain to keep them up-to-date.

Another problem will be catering for the overloading of '*' which is used for both multiplication and the first character of the names of pointer variables e.g. *mypointer .
Anthony Jordan
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I probably would not need a list of the API routines as I could cheat by
> assuming any name that ended in an underscore immediately before an
> opening parenthesis was an API routine

Actually, the API commands are those just ending in an underscore. You can
have a space between the underscore and opening parenthesis, and indeed
I've seen code samples in these forums just like that.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
akj
Enthusiast
Enthusiast
Posts: 665
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@PB:

Yes, I agree with you about the possibility of intrusive spaces, but it probably doesn't matter. It is standard practice to remove redundant white space during any parsing process, especially white space around punctuation (and I would count parentheses as punctuation).

Also, if I went ahead and coded a routine to identify user variables, I would do so for typically written programs and not those that failed to follow normal programming layouts. Otherwise it would take far too long to write it.
Anthony Jordan
akj
Enthusiast
Enthusiast
Posts: 665
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

I have now discovered how to get a complete (?) list of all the PureBasic library functions:

Code: Select all

#file = 0
Define folder$, flags, prog, ln$, p, output.b=#False

CreateFile(#file, "C:\Temp\PB Functions.txt")
WriteStringN(#file, "PureBasic Library Functions")
WriteStringN(#file, "")

folder$ = #PB_Compiler_Home+"\Compilers\"
flags = #PB_Program_Open|#PB_Program_Read|#PB_Program_Write
prog = RunProgram(folder$+"pbcompiler.exe", "/STANDBY", folder$, flags)
WriteProgramStringN(prog, "FUNCTIONLIST")

Repeat
  ln$ = ReadProgramString(prog)
  If output
    p = FindString(ln$, ")", 1)
    If p
      ln$ = Left(ln$, p)
      WriteStringN(#file, RemoveString(ln$, " "))
    EndIf
  EndIf
  If ln$="READY": output = #True: EndIf
Until ln$ = "OUTPUT"+#TAB$+"COMPLETE"

CloseFile(#file)
WriteProgramStringN(prog, "END")
WaitProgram(prog)
Debug "Program exit code = " + Str(ProgramExitCode(prog))
CloseProgram(prog)

End
Anthony Jordan
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

I tried my self on it, pls test it:

http://www.purebasic.fr/english/viewtop ... 426#271426
pb 5.11
Captn. Jinguji
User
User
Posts: 84
Joined: Sun Oct 24, 2004 9:25 am

Post by Captn. Jinguji »

gnasen wrote:I tried my self on it, pls test it:

http://www.purebasic.fr/english/viewtop ... 426#271426
Thanks for the effort. Is it possible that lines like

Define Varname.s = "something"

are being dismissed ? The tools seems to see my procedures names, but not any variables. Since I have "EnableExplicit" active all the time, I must define all vars.
Additionally, i never write "a=40" but always "a = 40" instead. Maybe, that's another problem for the tool?


Genereally, I was hoping that fred or someone else came up with a tool that analyses a program's symbol table at compile time and draw conclusions from what it finds there.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

It should work with all kind of formalism.
I updated it some times this day, so you may have an to old version.

I tried both, both works fine.

Maybe he could not open the file (if it shows nothing at all).
I updated it ;)
pb 5.11
User avatar
luis
Addict
Addict
Posts: 3705
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy
Contact:

Re: Could we have an option to flag unused variable names,pl

Post by luis »

Captn. Jinguji wrote: But I also find myself re-working code after some time,
and it could be quite helpful to have variable names flagged
that are never or no longer actually referenced in the code.
Yes, it would be very nice to have an (optional maybe) warning.
Captn. Jinguji
User
User
Posts: 84
Joined: Sun Oct 24, 2004 9:25 am

Re: Could we have an option to flag unused variable names,pl

Post by Captn. Jinguji »

luis wrote:
Captn. Jinguji wrote: But I also find myself re-working code after some time,
and it could be quite helpful to have variable names flagged
that are never or no longer actually referenced in the code.
Yes, it would be very nice to have an (optional maybe) warning.
Sure, I wouldn't want to HAVE to adjust things to make the program actually compile.
Or at least : only with a selectable "paranoid" set of preferences for a "Production compile". Not that something like that could replacea developer's responsibilities, but I think it is desirable for any program's productive release to have its build run at the highest (i.e. most scrutinizing) warning level available, and get that finished with not a single diagnostic (other than "0 errors, 0 Warnings").
Captn. Jinguji
User
User
Posts: 84
Joined: Sun Oct 24, 2004 9:25 am

Post by Captn. Jinguji »

gnasen wrote:It should work with all kind of formalism.
I updated it some times this day, so you may have an to old version.
[/qoute]
Mhhm... that may well be the case. I'll check hte new version out later. If your tool still won't work as desired, I'll supply an example of unruly code of mine.
gnasen wrote: I tried both, both works fine.
Maybe he could not open the file (if it shows nothing at all).
I updated it ;)
Strangely, I had that effect when I ran the tool standalone.
When I made it known to the ide as a tool, it saw and showed procedure names of my my code examples, but only very few variables, and the latter came from procedures generated by the old Visual Designer, which does not implement "EnableExplicit" in code that it generates.

Nevertheless, your tool will be helpful.

Maybe a general flag/option "Show unreferenced code elements only" could be useful ? (Probably easier to implement than a color coding ?)
Captn. Jinguji
User
User
Posts: 84
Joined: Sun Oct 24, 2004 9:25 am

Post by Captn. Jinguji »

gnasen wrote:It should work with all kind of formalism.
I updated it some times this day, so you may have an to old version.

I tried both, both works fine.

Maybe he could not open the file (if it shows nothing at all).
I updated it ;)
Yes, it works well for me now, I obviously HAD downloaded the old version befor you had published the fresh version.

Very good work.

I only now see what that if/select/ business is actually about. Good idea, too, though I'd suggest to make the flagging read-only. (It confused me that I could set/reset the marks without any obvious effect.)
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post by gnasen »

nice to hear that it works now as expected.
I took the selectboxes to show it, because it was the easiest way I think.
I would be pleased if you (or anyone else) has a better idea to show the results and could tell me :wink:
pb 5.11
Post Reply