Skip to content

Commit 778dbcb

Browse files
authored
[clang] Add /Zc:__STDC__ flag to clang-cl (#68690)
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. Link to MSVC documentation of this flag: https://learn.microsoft.com/en-us/cpp/build/reference/zc-stdc?view=msvc-170
1 parent 7dc2f66 commit 778dbcb

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
@@ -342,6 +342,10 @@ New Compiler Flags
342342
``__attribute__((section(...)))``. This enables linker GC to collect unused
343343
symbols without having to use a per-symbol section.
344344

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

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">,
@@ -8306,6 +8310,9 @@ def _SLASH_vd : CLJoined<"vd">, HelpText<"Control vtordisp placement">,
83068310
Alias<vtordisp_mode_EQ>;
83078311
def _SLASH_X : CLFlag<"X">,
83088312
HelpText<"Do not add %INCLUDE% to include search path">, Alias<nostdlibinc>;
8313+
def _SLASH_Zc___STDC__ : CLFlag<"Zc:__STDC__">,
8314+
HelpText<"Define __STDC__">,
8315+
Alias<fms_define_stdc>;
83098316
def _SLASH_Zc_sizedDealloc : CLFlag<"Zc:sizedDealloc">,
83108317
HelpText<"Enable C++14 sized global deallocation functions">,
83118318
Alias<fsized_deallocation>;

clang/lib/Driver/ToolChains/Clang.cpp

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

70327036
if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() &&
70337037
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)