public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Compiler Directive to List Defined Macros?
@ 2000-06-28 17:19 Ross Combs
  2000-06-28 19:40 ` Geoff Keating
  2000-06-29  0:39 ` Martin v. Loewis
  0 siblings, 2 replies; 16+ messages in thread
From: Ross Combs @ 2000-06-28 17:19 UTC (permalink / raw)
  To: gcc

I was going to ask a similar question today.  I tried the -E -dD options as
suggested and they do print out many of the predefined symbols.

A few things seem to be missing though.  It doesn't show __FILE__, __LINE__,
__func__, or __PRETTY_FUNCTION__.  I understand these are a little different
since they are "dynamic", but it would be helpful to know which ones are
avaliable.

The reason I wanted to know was that I had some code like this:
 #ifdef __func__
   /* code using __func__ */
 #else
 # ifdef __PRETTY_FUNCTION__
   /* code using __PRETTY_FUNCTION__ */
 # else
   /* code that doewsn't use function names */
 # endif
 #endif

It kept acting like neither one was avaliable.  So I tried forcing it to use
__func__, and I got compile errors.  I though that was strange, so I tried
forcing it to use __PRETTY_FUNCTION__ and finally it worked.

So I thought "maybe dynamic macros aren't detected by #ifdef".  I tried:
 #ifdef __LINE__
 # error __LINE__ is defined
 #endif

And it did indeed print the error message.

So a few things are going on:
 1) This version of gcc (egcs-2.91.66 from Red Hat 6.1) doesn't support
    __func__, but another version I used did.
 2) This version of gcc doesn't allow detection of __PRETTY_FUNCTION__ with
    #ifdef or #defined.

I know that #1 was "fixed" by version 2.95.1, because it works on another
machine using that version of gcc.

But is #2 "fixed" yet?  I tried it in version 2 with 2.95.1 and it still
didn't work.  The test for __LINE__ did work with 2.95.1 also.

I would suggest this should be made consistant.  It is nice to be able to
support these extensions when they are avaliable, and detection through
ifdef seems like the logical way to do so.

(Also, I never got a response to my last email to this list about detecting
multiple side effects and issuing a warning... does anyone care?  Is it such
a horrible idea?)

Thanks for your time,
-Ross

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Compiler Directive to List Defined Macros?
@ 2000-06-30 14:34 Ross Combs
  0 siblings, 0 replies; 16+ messages in thread
From: Ross Combs @ 2000-06-30 14:34 UTC (permalink / raw)
  To: NeilB, rocombs; +Cc: gcc, martin

Yep. The double xstr() level is the trick I was missing.  It works
just fine.

Thanks!

-Ross

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Compiler Directive to List Defined Macros?
@ 2000-06-29 15:49 Ross Combs
  2000-06-30  1:36 ` Neil Booth
  0 siblings, 1 reply; 16+ messages in thread
From: Ross Combs @ 2000-06-29 15:49 UTC (permalink / raw)
  To: martin, rocombs; +Cc: gcc

> > So __PRETTY_FUNCTION__ has existed for a long time in gcc?  I can trust
> > that even old copies will support this?
>
> Yes. I could check when it was first introduced - but so could you.
> Then you could define a reliable test based on the values of __GNUC__
> and __GNUC_MINOR__

Look like 2.7 introduced this feature from looking at the release notes.
In 2.8 __FUNCTION__ and __PRETTY_FUNCTION__ became functions instead of
strings.  I'll add in the tst like this:

 #if defined(__GNUC__) && \
     (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) || \
     __GNUC__ > 2)

> (*) It still is, and some people claim that __func__ should have been
> one of these, also, to allow concatenation as in
>
>   __FILE__ ":" __FUNCTION__

Hmm.  That is how I originally tried to use it.  It is also annoying that
I couldn't find a way to stringize __LINE__ and then concatenate it to
__FILE__.  But such things can be worked around.

-Ross

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Compiler Directive to List Defined Macros?
@ 2000-06-29  8:28 Ross Combs
  2000-06-29 13:44 ` Martin v. Loewis
  0 siblings, 1 reply; 16+ messages in thread
From: Ross Combs @ 2000-06-29  8:28 UTC (permalink / raw)
  To: martin, rocombs; +Cc: gcc

> > A few things seem to be missing though.  It doesn't show __FILE__, __LINE__,
> > __func__, or __PRETTY_FUNCTION__.  I understand these are a little different
> > since they are "dynamic", but it would be helpful to know which ones are
> > avaliable.
>
> The complete list can be found be combining those required by the
> standard with those documented in the GCC documentation, in particular
> in the section "Function Names".

Ok.  I admit to being guilty of not reading the info documents.  I really
prefer the man format, but I know it has limitations and is considered to
be historical baggage.  I'm still curious as to why they aren't listed.
(Except for __func__ and __PRETTY_FUNCTION__.)

> As Geoff explains, __func__ is not a preprocessor macro. Instead, it
> is an identifier. The proper way of testing for it is to write
>
> #if __STDC_VERSION__+0 >= 199901L
>
> since __func__ is defined by C99.

Ah.  But I'm getting version 199409 even though __func__ is defined...
Was there a C94 standard?  Was C9X released as a standard or is it still
in development?

> >  # ifdef __PRETTY_FUNCTION__
> >    /* code using __PRETTY_FUNCTION__ */
> > ...
>
> The proper way of testing for these is to write
>
> #ifdef __GNUC__
>
> in which case you can use either one.

So __PRETTY_FUNCTION__ has existed for a long time in gcc?  I can trust
that even old copies will support this?

> No, that is not possible - the preprocessor has no way of knowing what
> the current function is. That's why they are identifiers, or string
> literals.

I understand that now.  I'm sorry for not figuring that out before.  It
still seems like __PRETTY_FUNCTION__ looks like a macro to me.

> > But is #2 "fixed" yet?
>
> It's not broken. It can't possibly work the way you expect it to
> work. Instead, you must use other tests in portable code.

In my reply to Geoff I noted one way to make it work.  It isn't needed
as you point out there are other ways to detect these things.  Is it
standard to make function names all uppercase though?

Thanks for showing me how to fix this.

-Ross

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: Compiler Directive to List Defined Macros?
@ 2000-06-29  8:18 Ross Combs
  2000-06-29 11:38 ` Geoff Keating
  0 siblings, 1 reply; 16+ messages in thread
From: Ross Combs @ 2000-06-29  8:18 UTC (permalink / raw)
  To: geoffk, rocombs; +Cc: gcc

First, thanks for taking the time to explain this.

> > It kept acting like neither one was avaliable.  So I tried forcing it to use
> > __func__, and I got compile errors.  I though that was strange, so I tried
> > forcing it to use __PRETTY_FUNCTION__ and finally it worked.
> ...
>
> I don't think you can reliably use #ifdef on __func__ or
> __PRETTY_FUNCTION__.  This is because they're not macros.  They're
> predefined static variables in every function's scope.

I see.  I guess the capitalization of __PRETTY_FUNCTION__ and the similarity
to __LINE__ threw me off.  Obviously the preprocessor doesn't know about
functions.  But why not make it something like an implicit:

 #define __PRETTY_FUNCTION__ __pretty_function__

and then I could test for it, but more importantly it would follow traditional
capitalization rules.

> I believe that the new C standard specifies a standard way to do this,
> and that GCC now does it in the standard way.  I forget what the exact
> name is, perhaps __function__.

Hmm... I thought it was __func__.  That is why I tried using it in preference
to __PRETTY_FUNCTION__.  Do you know how you are suppossed to test for it?

> > (Also, I never got a response to my last email to this list about detecting
> > multiple side effects and issuing a warning... does anyone care?  Is it such
> > a horrible idea?)
>
> It's not a bad idea.  Perhaps you could submit a patch?

I wouldn't mind working on one but there is no patch yet.  I haven't signed
a copyright release form and that might be a problem.  I also want to discuss
which cases should be warned about but that is a subject for another email...

-Ross

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Compiler Directive to List Defined Macros?
@ 2000-06-28  6:44 Bolan Meek
  2000-06-28  7:27 ` Franz Sirl
  0 siblings, 1 reply; 16+ messages in thread
From: Bolan Meek @ 2000-06-28  6:44 UTC (permalink / raw)
  To: gcc

Greetings:  God bless you.

I've searched `info gcc` and the mailing list archives,
but have found nothing on this:

Is there any compiler directive that will cause a listing
of defined macros to be printed to stdout or stderr?
A #pragma something, perhaps?

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2000-06-30 14:34 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-06-28 17:19 Compiler Directive to List Defined Macros? Ross Combs
2000-06-28 19:40 ` Geoff Keating
2000-06-29  0:39 ` Martin v. Loewis
  -- strict thread matches above, loose matches on Subject: below --
2000-06-30 14:34 Ross Combs
2000-06-29 15:49 Ross Combs
2000-06-30  1:36 ` Neil Booth
2000-06-29  8:28 Ross Combs
2000-06-29 13:44 ` Martin v. Loewis
2000-06-29  8:18 Ross Combs
2000-06-29 11:38 ` Geoff Keating
2000-06-28  6:44 Bolan Meek
2000-06-28  7:27 ` Franz Sirl
2000-06-28  7:35   ` Bolan Meek
2000-06-28  7:56     ` Alexandre Oliva
2000-06-28  8:06       ` Bolan Meek
2000-06-28  8:14         ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).