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

Aaron Ballman aaron at aaronballman.com
Tue Mar 6 05:31:42 PST 2012

On Mon, Mar 5, 2012 at 10: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++.

I think your guess may be off base.  A lot of those things are
includes.  At work, we have several sources like:

#if defined( _MSC_VER )
#include <Windows.h>
#elif defined( __GNUC__ )
#include <gtk/gtk.h>

I think the correct thing to do is define __clang__ and MSVC in
MS-compat mode, like we already do for __clang__ and __GNUC__ for GNU
mode.  It seems like a reasonable pattern.  If someone cares about
clang, that's turned on appropriately, as is the compiler they're

But I do wonder what should happen with -std=gnu++98
-fms-compatibility as a combination.  Seems almost like an error?


More information about the cfe-commits mailing list