Skip to content

Commit 26fdd2e

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 31f1590 commit 26fdd2e

File tree

7 files changed

+39
-2
lines changed

7 files changed

+39
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ New Compiler Flags
341341
``__attribute__((section(...)))``. This enables linker GC to collect unused
342342
symbols without having to use a per-symbol section.
343343

344+
- ``-fms-define-stdc`` and its clang-cl counterpart ``/Zc:__STDC__``.
345+
Matches MSVC behaviour by defining ``__STDC__`` to ``1`` when
346+
MSVC compatibility mode is used. It has no effect for C++ code.
347+
344348
Deprecated Compiler Flags
345349
-------------------------
346350

clang/include/clang/Basic/LangOptions.def

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

301301
LANGOPT(OpenACC , 1, 0, "OpenACC Enabled")
302302

303+
LANGOPT(MSVCEnableStdcMacro , 1, 0, "Define __STDC__ with '-fms-compatibility'")
303304
LANGOPT(SizedDeallocation , 1, 0, "sized deallocation")
304305
LANGOPT(AlignedAllocation , 1, 0, "aligned allocation")
305306
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
@@ -2981,6 +2981,10 @@ def fms_compatibility : Flag<["-"], "fms-compatibility">, Group<f_Group>,
29812981
Visibility<[ClangOption, CC1Option, CLOption]>,
29822982
HelpText<"Enable full Microsoft Visual C++ compatibility">,
29832983
MarshallingInfoFlag<LangOpts<"MSVCCompat">>;
2984+
def fms_define_stdc : Flag<["-"], "fms-define-stdc">, Group<f_Group>,
2985+
Visibility<[ClangOption, CC1Option, CLOption]>,
2986+
HelpText<"Define '__STDC__' to '1' in MSVC Compatibility mode">,
2987+
MarshallingInfoFlag<LangOpts<"MSVCEnableStdcMacro">>;
29842988
def fms_extensions : Flag<["-"], "fms-extensions">, Group<f_Group>,
29852989
Visibility<[ClangOption, CC1Option, CLOption]>,
29862990
HelpText<"Accept some non-standard constructs supported by the Microsoft compiler">,
@@ -8312,6 +8316,9 @@ def _SLASH_vd : CLJoined<"vd">, HelpText<"Control vtordisp placement">,
83128316
Alias<vtordisp_mode_EQ>;
83138317
def _SLASH_X : CLFlag<"X">,
83148318
HelpText<"Do not add %INCLUDE% to include search path">, Alias<nostdlibinc>;
8319+
def _SLASH_Zc___STDC__ : CLFlag<"Zc:__STDC__">,
8320+
HelpText<"Define __STDC__">,
8321+
Alias<fms_define_stdc>;
83158322
def _SLASH_Zc_sizedDealloc : CLFlag<"Zc:sizedDealloc">,
83168323
HelpText<"Enable C++14 sized global deallocation functions">,
83178324
Alias<fsized_deallocation>;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7027,8 +7027,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
70277027
options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility,
70287028
(IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
70297029
options::OPT_fno_ms_extensions, true)));
7030-
if (IsMSVCCompat)
7030+
if (IsMSVCCompat) {
70317031
CmdArgs.push_back("-fms-compatibility");
7032+
if (!types::isCXX(Input.getType()) &&
7033+
Args.hasArg(options::OPT_fms_define_stdc))
7034+
CmdArgs.push_back("-fms-define-stdc");
7035+
}
70327036

70337037
if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() &&
70347038
Args.hasArg(options::OPT_fms_runtime_lib_EQ))

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
432432
// [C++] Whether __STDC__ is predefined and if so, what its value is,
433433
// are implementation-defined.
434434
// (Removed in C++20.)
435-
if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP)
435+
if ((!LangOpts.MSVCCompat || LangOpts.MSVCEnableStdcMacro) &&
436+
!LangOpts.TraditionalCPP)
436437
Builder.defineMacro("__STDC__");
437438
// -- __STDC_HOSTED__
438439
// 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)