Page 1 of 2

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

Posted: Thu Dec 18, 2008 4:20 pm
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.

Posted: Fri Dec 19, 2008 1:21 pm
by Mistrel
+1

Posted: Fri Dec 19, 2008 4:40 pm
by gnasen
that would be very usefull.
Maybe someone could make a PlugIn? :wink:

Posted: Fri Dec 19, 2008 8:42 pm
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 .

Posted: Fri Dec 19, 2008 11:53 pm
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.

Posted: Sat Dec 20, 2008 12:28 am
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.

Posted: Sat Dec 20, 2008 3:14 am
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

Posted: Sun Dec 21, 2008 3:04 am
by gnasen
I tried my self on it, pls test it:

http://www.purebasic.fr/english/viewtop ... 426#271426

Posted: Sun Dec 21, 2008 2:09 pm
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.

Posted: Sun Dec 21, 2008 11:27 pm
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 ;)

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

Posted: Sun Dec 21, 2008 11:32 pm
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.

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

Posted: Mon Dec 22, 2008 8:29 am
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").

Posted: Mon Dec 22, 2008 8:47 am
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 ?)

Posted: Thu Dec 25, 2008 1:43 pm
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.)

Posted: Thu Dec 25, 2008 4:59 pm
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: