Skip to content

Commit a89bee0

Browse files
committed
[clang] Add /Zc:__STDC__ flag to clang-cl
This commit adds the /Zc:__STDC__ argument from MSVC, which defines __STDC__. This means, alongside stronger feature parity with MSVC, that things that rely on __STDC__, such as autoconf, can work.
1 parent f26fb3e commit a89bee0

File tree

7 files changed

+41
-3
lines changed

7 files changed

+41
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ New Compiler Flags
184184
preserving ``#include`` directives for "system" headers instead of copying
185185
the preprocessed text to the output. This can greatly reduce the size of the
186186
preprocessed output, which can be helpful when trying to reduce a test case.
187+
* ``-fms-define-stdc`` and its clang-cl counterpart ``/Zc:__STDC__``.
188+
Matches MSVC behaviour by defining ``__STDC__`` to ``1`` when
189+
MSVC compatibility mode is used. It has no effect for C++ code.
187190

188191
Deprecated Compiler Flags
189192
-------------------------

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ LANGOPT(HIPUseNewLaunchAPI, 1, 0, "Use new kernel launching API for HIP")
282282
LANGOPT(OffloadUniformBlock, 1, 0, "Assume that kernels are launched with uniform block sizes (default true for CUDA/HIP and false otherwise)")
283283
LANGOPT(HIPStdPar, 1, 0, "Enable Standard Parallel Algorithm Acceleration for HIP (experimental)")
284284
LANGOPT(HIPStdParInterposeAlloc, 1, 0, "Replace allocations / deallocations with HIP RT calls when Standard Parallel Algorithm Acceleration for HIP is enabled (Experimental)")
285-
285+
LANGOPT(MSVCEnableStdcMacro , 1, 0, "Define __STDC__ with '-fms-compatability'")
286286
LANGOPT(SizedDeallocation , 1, 0, "sized deallocation")
287287
LANGOPT(AlignedAllocation , 1, 0, "aligned allocation")
288288
LANGOPT(AlignedAllocationUnavailable, 1, 0, "aligned allocation functions are unavailable")

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,6 +2821,10 @@ def fms_compatibility : Flag<["-"], "fms-compatibility">, Group<f_Group>,
28212821
Visibility<[ClangOption, CC1Option, CLOption]>,
28222822
HelpText<"Enable full Microsoft Visual C++ compatibility">,
28232823
MarshallingInfoFlag<LangOpts<"MSVCCompat">>;
2824+
def fms_define_stdc : Flag<["-"], "fms-define-stdc">, Group<f_Group>,
2825+
Visibility<[ClangOption, CC1Option]>,
2826+
HelpText<"Define '__STDC__' to '1' in MSVC Compatibility mode">,
2827+
MarshallingInfoFlag<LangOpts<"MSVCEnableStdcMacro">>;
28242828
def fms_extensions : Flag<["-"], "fms-extensions">, Group<f_Group>,
28252829
Visibility<[ClangOption, CC1Option, CLOption]>,
28262830
HelpText<"Accept some non-standard constructs supported by the Microsoft compiler">,
@@ -7933,6 +7937,9 @@ def _SLASH_vd : CLJoined<"vd">, HelpText<"Control vtordisp placement">,
79337937
Alias<vtordisp_mode_EQ>;
79347938
def _SLASH_X : CLFlag<"X">,
79357939
HelpText<"Do not add %INCLUDE% to include search path">, Alias<nostdlibinc>;
7940+
def _SLASH_Zc___STDC__ : CLFlag<"Zc:__STDC__">,
7941+
HelpText<"Define __STDC__">,
7942+
Alias<fms_define_stdc>;
79367943
def _SLASH_Zc_sizedDealloc : CLFlag<"Zc:sizedDealloc">,
79377944
HelpText<"Enable C++14 sized global deallocation functions">,
79387945
Alias<fsized_deallocation>;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6744,8 +6744,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
67446744
options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility,
67456745
(IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
67466746
options::OPT_fno_ms_extensions, true)));
6747-
if (IsMSVCCompat)
6747+
if (IsMSVCCompat) {
67486748
CmdArgs.push_back("-fms-compatibility");
6749+
if (!types::isCXX(Input.getType()) &&
6750+
Args.hasArg(options::OPT_fms_define_stdc))
6751+
CmdArgs.push_back("-fms-define-stdc");
6752+
}
67496753

67506754
if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() &&
67516755
Args.hasArg(options::OPT_fms_runtime_lib_EQ))
@@ -7922,6 +7926,9 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
79227926
CmdArgs.push_back("-fno-wchar");
79237927
}
79247928

7929+
if (!types::isCXX(InputType) && Args.hasArg(options::OPT_fms_define_stdc))
7930+
CmdArgs.push_back("-fms-define-stdc");
7931+
79257932
if (Args.hasArg(options::OPT__SLASH_kernel)) {
79267933
llvm::Triple::ArchType Arch = getToolChain().getArch();
79277934
std::vector<std::string> Values =

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
423423
// [C++] Whether __STDC__ is predefined and if so, what its value is,
424424
// are implementation-defined.
425425
// (Removed in C++20.)
426-
if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP)
426+
if ((!LangOpts.MSVCCompat || LangOpts.MSVCEnableStdcMacro) &&
427+
!LangOpts.TraditionalCPP)
427428
Builder.defineMacro("__STDC__");
428429
// -- __STDC_HOSTED__
429430
// The integer literal 1 if the implementation is a hosted

clang/test/Driver/cl-zc.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@
123123
// CHECK-CHAR8_T_: "-fno-char8_t"
124124

125125

126+
// RUN: %clang_cl /TC /dev/null /E -Xclang -dM 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=NOSTDC
127+
// RUN: %clang_cl /TC /dev/null /E -Xclang -dM /Zc:__STDC__ 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=YESSTDC
128+
// __STDC__ should never be defined in C++ mode with fms-compatibility.
129+
// RUN: %clang_cl /dev/null /E -Xclang -dM 2>&1 | FileCheck %s --check-prefix=NOSTDC
130+
// RUN: %clang_cl /dev/null /E -Xclang -dM /Zc:__STDC__ 2>&1 | FileCheck %s --check-prefix=ZCSTDCIGNORED
131+
// YESSTDC: #define __STDC__ 1
132+
// NOSTDC-NOT: #define __STDC__ 1
133+
// ZCSTDCIGNORED-NOT: #define __STDC__ 1
134+
// ZCSTDCIGNORED: argument unused during compilation
126135

127136
// These never warn, but don't have an effect yet.
128137

clang/test/Driver/ms-define-stdc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Note: %s must be preceded by --, otherwise it may be interpreted as a
2+
// command-line option, e.g. on Mac where %s is commonly under /Users.
3+
//
4+
// Note: see also cl-zc.cpp
5+
6+
// RUN: %clang_cl /TC /dev/null /E -Xclang -dM /Zc:__STDC__- 2>&1 | FileCheck %s --check-prefix=ZCSTDCIGNORED
7+
// ZCSTDCIGNORED-NOT: #define __STDC__ 1
8+
// ZCSTDCIGNORED: argument unused during compilation
9+
10+
// RUN: not %clang -Xclang -fno-ms-define-stdc %s 2>&1 | FileCheck %s --check-prefix="NOARG"
11+
// NOARG: error: unknown argument: '-fno-ms-define-stdc'

0 commit comments

Comments
 (0)