Clang on Windows:如何禁用默认的 MSVC 兼容性?

Clang on Windows: how to disable the default MSVC compatibility?

来自 Clang 的 documentation

When Clang compiles C++ code for Windows, it attempts to be compatible with MSVC.

特别是 Clang defines _MSC_VER.

MSVC 不支持 #pragma STDC FENV_ACCESS ON:

cl t125.c /std:c11
t125.c(11): warning C4068: unknown pragma 'STDC'

相反,MSVC support #pragma fenv_access (on)。这导致此代码:

#if _MSC_VER
#pragma fenv_access (on)
#else
#pragma STDC FENV_ACCESS ON
#endif

现在尝试使用 Windows 上的最新 Clang 编译此代码:

$ /cygdrive/d/SOFTWARE/LLVM/12.0.0/bin/clang t125.c -Wall -Wextra
t125.c:10:9: warning: unknown pragma ignored [-Wunknown-pragmas]
#pragma fenv_access (on)

意思是因为 #pragma fenv_access (on)unknown pragmaignored,那么 MSVC 兼容性未完成/未完成?困惑。

问题:如何禁用默认的 MSVC 兼容性(以便 Clang 不定义 _MSC_VER)?

P.S。这最终导致了这段代码:

#if _MSC_VER && ! __clang__ && ! __INTEL_COMPILER 
#pragma fenv_access (on)    /* true MSVC here?? */
#else
#pragma STDC FENV_ACCESS ON
#endif

更新。 pragma支持MS拼写的补丁:https://reviews.llvm.org/D111440.

how to disable the default MSVC compatibility (so that Clang does not define _MSC_VER)?

添加 -fms-compatibility-version=0-fmsc-version=0 开关应该可以防止定义 _MSC_VER。这个和其他一些相关的 VC++ 兼容性选项记录在 Clang command line argument reference page under Target-independent compilation options.

-fms-compatibility, -fno-ms-compatibility
Enable full Microsoft Visual C++ compatibility

-fms-compatibility-version=<arg>
Dot-separated value representing the Microsoft compiler version number to report in _MSC_VER (0 = don’t define it (default))

-fms-extensions, -fno-ms-extensions
Accept some non-standard constructs supported by the Microsoft compiler

-fms-memptr-rep=<arg>

-fms-volatile

-fmsc-version=<arg>
Microsoft compiler version number to report in _MSC_VER (0 = don’t define it (default))