Skip to content

Commit 391eb28

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 15e9478 commit 391eb28

File tree

7 files changed

+41
-2
lines changed

7 files changed

+41
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ New Compiler Flags
177177
- ``-Wmissing-designated-field-initializers``, grouped under ``-Wmissing-field-initializers``.
178178
This diagnostic can be disabled to make ``-Wmissing-field-initializers`` behave
179179
like it did before Clang 18.x. Fixes (`#56628 <https://github.com/llvm/llvm-project/issues/68933>`_)
180+
- ``-fms-define-stdc`` and its clang-cl counterpart ``/Zc:__STDC__``.
181+
Matches MSVC behaviour by defining ``__STDC__`` to ``1`` when
182+
MSVC compatibility mode is used. It has no effect for C++ code.
180183

181184
Deprecated Compiler Flags
182185
-------------------------

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ LANGOPT(HIPStdParInterposeAlloc, 1, 0, "Replace allocations / deallocations with
291291

292292
LANGOPT(OpenACC , 1, 0, "OpenACC Enabled")
293293

294+
LANGOPT(MSVCEnableStdcMacro , 1, 0, "Define __STDC__ with '-fms-compatability'")
294295
LANGOPT(SizedDeallocation , 1, 0, "sized deallocation")
295296
LANGOPT(AlignedAllocation , 1, 0, "aligned allocation")
296297
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
@@ -2934,6 +2934,10 @@ def fms_compatibility : Flag<["-"], "fms-compatibility">, Group<f_Group>,
29342934
Visibility<[ClangOption, CC1Option, CLOption]>,
29352935
HelpText<"Enable full Microsoft Visual C++ compatibility">,
29362936
MarshallingInfoFlag<LangOpts<"MSVCCompat">>;
2937+
def fms_define_stdc : Flag<["-"], "fms-define-stdc">, Group<f_Group>,
2938+
Visibility<[ClangOption, CC1Option]>,
2939+
HelpText<"Define '__STDC__' to '1' in MSVC Compatibility mode">,
2940+
MarshallingInfoFlag<LangOpts<"MSVCEnableStdcMacro">>;
29372941
def fms_extensions : Flag<["-"], "fms-extensions">, Group<f_Group>,
29382942
Visibility<[ClangOption, CC1Option, CLOption]>,
29392943
HelpText<"Accept some non-standard constructs supported by the Microsoft compiler">,
@@ -8172,6 +8176,9 @@ def _SLASH_vd : CLJoined<"vd">, HelpText<"Control vtordisp placement">,
81728176
Alias<vtordisp_mode_EQ>;
81738177
def _SLASH_X : CLFlag<"X">,
81748178
HelpText<"Do not add %INCLUDE% to include search path">, Alias<nostdlibinc>;
8179+
def _SLASH_Zc___STDC__ : CLFlag<"Zc:__STDC__">,
8180+
HelpText<"Define __STDC__">,
8181+
Alias<fms_define_stdc>;
81758182
def _SLASH_Zc_sizedDealloc : CLFlag<"Zc:sizedDealloc">,
81768183
HelpText<"Enable C++14 sized global deallocation functions">,
81778184
Alias<fsized_deallocation>;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6888,8 +6888,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
68886888
options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility,
68896889
(IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
68906890
options::OPT_fno_ms_extensions, true)));
6891-
if (IsMSVCCompat)
6891+
if (IsMSVCCompat) {
68926892
CmdArgs.push_back("-fms-compatibility");
6893+
if (!types::isCXX(Input.getType()) &&
6894+
Args.hasArg(options::OPT_fms_define_stdc))
6895+
CmdArgs.push_back("-fms-define-stdc");
6896+
}
68936897

68946898
if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() &&
68956899
Args.hasArg(options::OPT_fms_runtime_lib_EQ))
@@ -8065,6 +8069,9 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
80658069
CmdArgs.push_back("-fno-wchar");
80668070
}
80678071

8072+
if (!types::isCXX(InputType) && Args.hasArg(options::OPT_fms_define_stdc))
8073+
CmdArgs.push_back("-fms-define-stdc");
8074+
80688075
if (Args.hasArg(options::OPT__SLASH_kernel)) {
80698076
llvm::Triple::ArchType Arch = getToolChain().getArch();
80708077
std::vector<std::string> Values =

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
433433
// [C++] Whether __STDC__ is predefined and if so, what its value is,
434434
// are implementation-defined.
435435
// (Removed in C++20.)
436-
if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP)
436+
if ((!LangOpts.MSVCCompat || LangOpts.MSVCEnableStdcMacro) &&
437+
!LangOpts.TraditionalCPP)
437438
Builder.defineMacro("__STDC__");
438439
// -- __STDC_HOSTED__
439440
// The integer literal 1 if the implementation is a hosted

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'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cl /TC /dev/null /E -Xclang -dM 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=NOSTDC
2+
// RUN: %clang_cl /TC /dev/null /E -Xclang -dM /Zc:__STDC__ 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=YESSTDC
3+
// __STDC__ should never be defined in C++ mode with fms-compatibility.
4+
// RUN: %clang_cl /dev/null /E -Xclang -dM 2>&1 | FileCheck %s --check-prefix=NOSTDC
5+
// RUN: %clang_cl /dev/null /E -Xclang -dM /Zc:__STDC__ 2>&1 | FileCheck %s --check-prefix=ZCSTDCIGNORED
6+
// YESSTDC: #define __STDC__ 1
7+
// NOSTDC-NOT: #define __STDC__ 1
8+
// ZCSTDCIGNORED-NOT: #define __STDC__ 1
9+
// ZCSTDCIGNORED: argument unused during compilation

0 commit comments

Comments
 (0)