Bug 65401 - Weird macros for use in "#if" in Platform.h
: Weird macros for use in "#if" in Platform.h
Status: UNCONFIRMED
: WebKit
Platform
: 528+ (Nightly build)
: Unspecified Unspecified
: P2 Normal
Assigned To:
:
:
:
:
  Show dependency treegraph
 
Reported: 2011-07-29 17:06 PST by
Modified: 2011-07-31 21:39 PST (History)


Attachments


Note

You need to log in before you can comment on or make changes to this bug.


Description From 2011-07-29 17:06:03 PST
Source/JavaScriptCore/wtf/Platform.h contains macros like this one:

#define PLATFORM(WTF_FEATURE) (defined WTF_PLATFORM_##WTF_FEATURE  && WTF_PLATFORM_##WTF_FEATURE)

and their use is for code as follows

#if PLATFORM(MAC) || (PLATFORM(QT) && USE(QTKIT))
#include "MediaPlayerPrivateQTKit.h"
#if USE(AVFOUNDATION)
#include "MediaPlayerPrivateAVFoundationObjC.h"
#endif

There are two things that make me thing that the first part of that logical-AND in the definition of "PLATFORM" isn't needed and may actually be harmful. 

1. Why is it harmful? The C++ spec says at 16.1p4 "If the token 'defined' is generated as a result of this replacement process ..., the behavior is undefined.". That's what's happening here.

2. Why is it not needed? Because "#if" replaces any unknown macros by "0". And the macro already relies on this to happen. If it wouldn't happen, the "&& WTF_PLATFORM_##WTF_FEATURE" would be broken if the macro doesn't exist. 

I don't know why that code is chosen to be that way, but I recommend to change it to the following, if it works on all targets:

    #define PLATFORM(WTF_FEATURE) (WTF_PLATFORM_##WTF_FEATURE)
------- Comment #1 From 2011-07-29 17:29:16 PST -------
(In reply to comment #0)
> 1. Why is it harmful? The C++ spec says at 16.1p4 "If the token 'defined' is generated as a result of this replacement process ..., the behavior is undefined.". That's what's happening here.

Interesting. I did not know that.

> 2. Why is it not needed? Because "#if" replaces any unknown macros by "0". And the macro already relies on this to happen. If it wouldn't happen, the "&& WTF_PLATFORM_##WTF_FEATURE" would be broken if the macro doesn't exist.

It is needed because compilers like gcc have a mode where they warn if “unknown” macros are used. We want to be able to compile with such warnings turned on.
------- Comment #2 From 2011-07-31 21:38:17 PST -------
I forwarded this to the GCC crew: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49928 .
------- Comment #3 From 2011-07-31 21:39:22 PST -------
Getting an extension like that in GCC seems OK. We'd probably want it in clang too.