Page 1 of 1

Defined(Foo, #PB_Macro) ?

Posted: Wed May 11, 2011 12:10 am
by Little John
Hi all,

I wanted to check, whether a macro is defined:

Code: Select all

CompilerIf Defined(Foo, #PB_Macro)
   Debug "Macro 'Foo' is already defined."
CompilerEndIf
But this does not work (tesed with PB 4.51 and PB 4.60 Beta 3).
Why is there no cnstant #PB_Macro for this purpose?
Is there any other possibility to check, whether a particular macro is defined?

Regards, Little John

Re: Defined(Foo, #PB_Macro) ?

Posted: Wed May 11, 2011 8:47 am
by STARGĂ…TE
You can not test in this way because the macro (the name) will replace already during the test.

Code: Select all

Macro Foo
 Debug 1
EndMacro

CompilerIf Defined(Foo, #PB_Macro)

CompilerEndIf
will be:

Code: Select all

Macro Foo
 Debug 1
EndMacro

CompilerIf Defined(Debug 1, #PB_Macro)
;[...]

Re: Defined(Foo, #PB_Macro) ?

Posted: Wed May 11, 2011 8:48 am
by Fred
That's it.

Re: Defined(Foo, #PB_Macro) ?

Posted: Wed May 11, 2011 3:38 pm
by Little John
Hi, thank you for the explanation!

Maybe it is possible (and desirable) to change the compiler a little, so that it makes and exception in this case:
If a macro name is an argument of Defined(), then the compiler will not expand the macro.

Regards, Little John

Re: Defined(Foo, #PB_Macro) ?

Posted: Wed May 11, 2011 8:13 pm
by freak
You can use a separate constant to guard against the double definition of a macro. Its not perfect, but it does the job for me:

Code: Select all

CompilerIf Defined(macro_test_defined, #PB_Constant) = 0
  #macro_test_defined = 1
  
  Macro test()
    ; something
  EndMacro
CompilerEndIf

Re: Defined(Foo, #PB_Macro) ?

Posted: Thu May 12, 2011 5:40 am
by Little John
That's a good idea, thank you!

Regards, Little John

Re: Defined(Foo, #PB_Macro) ?

Posted: Fri Jun 23, 2023 9:05 am
by jacdelad
Or the compiler could store the macro names in a map or list, so even if the are replaced it remembers they were there.

Re: Defined(Foo, #PB_Macro) ?

Posted: Fri Jun 23, 2023 11:05 am
by Fred
The whole line is evaluated before being parsed, so it wouldn't solve the issue.

Re: Defined(Foo, #PB_Macro) ?

Posted: Sat Jun 24, 2023 9:06 pm
by Olli
Solve --> A homemade IDE tool set as pre-process, feature available since long time.

That is not very complex to do. Just the homemade feature would not be native nor public...

Just a remark : what's so bad with this code below ?

Code: Select all

Macro aa()
 Debug 11
EndMacro

UndefineMacro aa

Macro aa()
 Debug 13
EndMacro

aa()