[cfe-commits] [PATCH RFC] Stop defining __GNUC__ for MSVC builds

Nico Weber thakis at chromium.org
Mon Mar 5 20:53:50 PST 2012


On Mon, Mar 5, 2012 at 8:32 PM, Douglas Gregor <dgregor at apple.com> wrote:
>
> On Mar 2, 2012, at 4:16 PM, Aaron Ballman wrote:
>
>> There are a lot of references on the web which relate __GNUC__ to GCC
>> for compiler discovery
>> (<http://sourceforge.net/apps/mediawiki/predef/index.php?title=Compilers#GCC_C.2FC.2B.2B>,
>> <http://www.ocf.berkeley.edu/~pad/tigcc/doc/html/cpp_SEC15_GNUC.html>,
>> etc).  This translates into a fair number of cross-compiler projects
>> with code like this:
>>
>> #if defined(__GNUC__)
>>  // Do GCC things
>> #elif defined(_MSC_VER)
>>  // Do MSVC things
>> ...
>> #endif
>>
>> This currently leads to problems when compiling with Clang because
>> __GNUC__ (and friends) are always defined, even when compiling for MS
>> compatibility (PR 11790).  I've attached a patch which addresses this
>> by only defining __GNUC__ et al when MSVC mode is not set.  This means
>> code like the above will work as expected.
>>
>> Thoughts?
>
> -  // Currently claim to be compatible with GCC 4.2.1-5621.
> -  Builder.defineMacro("__GNUC_MINOR__", "2");
> -  Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
> -  Builder.defineMacro("__GNUC__", "4");
> -  Builder.defineMacro("__GXX_ABI_VERSION", "1002");
> +  if (!LangOpts.MSCVersion) {
>
> This should probably just check LangOpts.MicrosoftMode.
>
> I'm not sure about the general approach, though. Even when compiling in Microsoft-compatible mode, Clang is still much more GCC-like than MSVC-like. I would venture a guess that
>
>        // Do GCC things
>
> is far more likely to be standards-conforming code that Clang will accept than
>
>        // Do MSVC things
>
> especially when dealing with template libraries in C++.

For what it's worth, I'm using a patch like this locally for chromium
too. The reason is that many of chromium's source files say

#if defined(__GNUC__)
#include "file_posix.h"
#elif defined(_MSC_VER)
#include "file_win.h"
#endif

or similar, just like Aaron said. That is, many places in the source
assume that "os: windows == compiler: msvc".

(My longer term plan was to stop setting __gnuc__ when using the
mythical cl-compatible driver.)

Nico




More information about the cfe-commits mailing list