https://github.com/xbjfk updated https://github.com/llvm/llvm-project/pull/68690
>From 92899d94e91a6812de8fa5ec7c15c37a81280108 Mon Sep 17 00:00:00 2001 From: Reagan Bohan <xbjfk.git...@gmail.com> Date: Tue, 10 Oct 2023 11:32:47 +0000 Subject: [PATCH 1/5] [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. --- clang/include/clang/Basic/LangOptions.def | 2 +- clang/include/clang/Driver/Options.td | 7 +++++++ clang/lib/Driver/ToolChains/Clang.cpp | 10 +++++++++- clang/lib/Frontend/InitPreprocessor.cpp | 3 ++- clang/test/Driver/cl-zc.cpp | 4 ++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index c0ea4ecb9806a5b..97101d0166a135a 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -282,7 +282,7 @@ LANGOPT(HIPUseNewLaunchAPI, 1, 0, "Use new kernel launching API for HIP") LANGOPT(OffloadUniformBlock, 1, 0, "Assume that kernels are launched with uniform block sizes (default true for CUDA/HIP and false otherwise)") LANGOPT(HIPStdPar, 1, 0, "Enable Standard Parallel Algorithm Acceleration for HIP (experimental)") LANGOPT(HIPStdParInterposeAlloc, 1, 0, "Replace allocations / deallocations with HIP RT calls when Standard Parallel Algorithm Acceleration for HIP is enabled (Experimental)") - +LANGOPT(MSVCEnableStdcMacro , 1, 0, "define __STDC__ with -fms-compatability") LANGOPT(SizedDeallocation , 1, 0, "sized deallocation") LANGOPT(AlignedAllocation , 1, 0, "aligned allocation") LANGOPT(AlignedAllocationUnavailable, 1, 0, "aligned allocation functions are unavailable") diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index c272a7f1c398aa6..fae1dcc2b5117f7 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2821,6 +2821,10 @@ def fms_compatibility : Flag<["-"], "fms-compatibility">, Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>, HelpText<"Enable full Microsoft Visual C++ compatibility">, MarshallingInfoFlag<LangOpts<"MSVCCompat">>; +def fms_define_stdc : Flag<["-"], "fms-define-stdc">, Group<f_Group>, + Visibility<[ClangOption, CC1Option]>, + HelpText<"Define __STDC__ in MSVC Compatibility mode">, + MarshallingInfoFlag<LangOpts<"MSVCEnableStdcMacro">>; def fms_extensions : Flag<["-"], "fms-extensions">, Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>, HelpText<"Accept some non-standard constructs supported by the Microsoft compiler">, @@ -7933,6 +7937,9 @@ def _SLASH_vd : CLJoined<"vd">, HelpText<"Control vtordisp placement">, Alias<vtordisp_mode_EQ>; def _SLASH_X : CLFlag<"X">, HelpText<"Do not add %INCLUDE% to include search path">, Alias<nostdlibinc>; +def _SLASH_Zc___STDC__ : CLFlag<"Zc:__STDC__">, + HelpText<"Define __STDC__">, + Alias<fms_define_stdc>; def _SLASH_Zc_sizedDealloc : CLFlag<"Zc:sizedDealloc">, HelpText<"Enable C++14 sized global deallocation functions">, Alias<fsized_deallocation>; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index bfd6c5c2864abf7..2834786ba17decf 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6744,8 +6744,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility, (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, true))); - if (IsMSVCCompat) + if (IsMSVCCompat) { CmdArgs.push_back("-fms-compatibility"); + bool IsMSVCDefineStdc = Args.hasArg(options::OPT_fms_define_stdc); + if (IsMSVCDefineStdc) + CmdArgs.push_back("-fms-define-stdc"); + } if (Triple.isWindowsMSVCEnvironment() && !D.IsCLMode() && Args.hasArg(options::OPT_fms_runtime_lib_EQ)) @@ -7922,6 +7926,10 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, CmdArgs.push_back("-fno-wchar"); } + if (Args.hasArg(options::OPT_fms_define_stdc)) { + CmdArgs.push_back("-fms-define-stdc"); + } + if (Args.hasArg(options::OPT__SLASH_kernel)) { llvm::Triple::ArchType Arch = getToolChain().getArch(); std::vector<std::string> Values = diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 846e5fce6de7b2c..e7ce08a8766d80c 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -423,7 +423,8 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI, // [C++] Whether __STDC__ is predefined and if so, what its value is, // are implementation-defined. // (Removed in C++20.) - if (!LangOpts.MSVCCompat && !LangOpts.TraditionalCPP) + if ((!LangOpts.MSVCCompat || LangOpts.MSVCEnableStdcMacro) && + !LangOpts.TraditionalCPP) Builder.defineMacro("__STDC__"); // -- __STDC_HOSTED__ // The integer literal 1 if the implementation is a hosted diff --git a/clang/test/Driver/cl-zc.cpp b/clang/test/Driver/cl-zc.cpp index c7cf5b1b6525bea..bb955222c647af3 100644 --- a/clang/test/Driver/cl-zc.cpp +++ b/clang/test/Driver/cl-zc.cpp @@ -123,6 +123,10 @@ // CHECK-CHAR8_T_: "-fno-char8_t" +// RUN: %clang_cl /dev/null /E -Xclang -dM 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=STDCOFF +// RUN: %clang_cl /dev/null /E -Xclang -dM /Zc:__STDC__ 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=STDCON +// STDCON: #define __STDC__ 1 +// STDCOFF-NOT: #define __STDC__ 1 // These never warn, but don't have an effect yet. >From ff1dd6421b30d89d8c85465aff8fa826bbc2e097 Mon Sep 17 00:00:00 2001 From: Reagan <xbjfk.git...@gmail.com> Date: Thu, 12 Oct 2023 10:23:50 +0000 Subject: [PATCH 2/5] improve LANGOPT description Co-authored-by: Aaron Ballman <aa...@aaronballman.com> --- clang/include/clang/Basic/LangOptions.def | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 97101d0166a135a..b170dbd35c7e783 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -282,7 +282,7 @@ LANGOPT(HIPUseNewLaunchAPI, 1, 0, "Use new kernel launching API for HIP") LANGOPT(OffloadUniformBlock, 1, 0, "Assume that kernels are launched with uniform block sizes (default true for CUDA/HIP and false otherwise)") LANGOPT(HIPStdPar, 1, 0, "Enable Standard Parallel Algorithm Acceleration for HIP (experimental)") LANGOPT(HIPStdParInterposeAlloc, 1, 0, "Replace allocations / deallocations with HIP RT calls when Standard Parallel Algorithm Acceleration for HIP is enabled (Experimental)") -LANGOPT(MSVCEnableStdcMacro , 1, 0, "define __STDC__ with -fms-compatability") +LANGOPT(MSVCEnableStdcMacro , 1, 0, "Define __STDC__ with '-fms-compatability'") LANGOPT(SizedDeallocation , 1, 0, "sized deallocation") LANGOPT(AlignedAllocation , 1, 0, "aligned allocation") LANGOPT(AlignedAllocationUnavailable, 1, 0, "aligned allocation functions are unavailable") >From 85d455717e882c06ab53a694e6913e07d9b2dba0 Mon Sep 17 00:00:00 2001 From: Reagan <xbjfk.git...@gmail.com> Date: Thu, 12 Oct 2023 10:24:05 +0000 Subject: [PATCH 3/5] improve help text Co-authored-by: Aaron Ballman <aa...@aaronballman.com> --- clang/include/clang/Driver/Options.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index fae1dcc2b5117f7..106a1de898764f3 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2823,7 +2823,7 @@ def fms_compatibility : Flag<["-"], "fms-compatibility">, Group<f_Group>, MarshallingInfoFlag<LangOpts<"MSVCCompat">>; def fms_define_stdc : Flag<["-"], "fms-define-stdc">, Group<f_Group>, Visibility<[ClangOption, CC1Option]>, - HelpText<"Define __STDC__ in MSVC Compatibility mode">, + HelpText<"Define '__STDC__' to '1' in MSVC Compatibility mode">, MarshallingInfoFlag<LangOpts<"MSVCEnableStdcMacro">>; def fms_extensions : Flag<["-"], "fms-extensions">, Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>, >From cce2cff7fba0fc567267d2a2bb25477e3e694a12 Mon Sep 17 00:00:00 2001 From: Reagan <xbjfk.git...@gmail.com> Date: Thu, 12 Oct 2023 10:27:53 +0000 Subject: [PATCH 4/5] remove redundant braces Co-authored-by: Aaron Ballman <aa...@aaronballman.com> --- clang/lib/Driver/ToolChains/Clang.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 2834786ba17decf..58aaaa6b6f90dfb 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7926,9 +7926,8 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, CmdArgs.push_back("-fno-wchar"); } - if (Args.hasArg(options::OPT_fms_define_stdc)) { + if (Args.hasArg(options::OPT_fms_define_stdc)) CmdArgs.push_back("-fms-define-stdc"); - } if (Args.hasArg(options::OPT__SLASH_kernel)) { llvm::Triple::ArchType Arch = getToolChain().getArch(); >From 97795b1af8108112e1eb23ca778953e203200cb0 Mon Sep 17 00:00:00 2001 From: Reagan Bohan <xbjfk.git...@gmail.com> Date: Thu, 12 Oct 2023 11:44:50 +0000 Subject: [PATCH 5/5] Never define __STDC__ with msvc compat + cxx --- clang/lib/Driver/ToolChains/Clang.cpp | 7 +++---- clang/test/Driver/cl-zc.cpp | 13 +++++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 58aaaa6b6f90dfb..682fa6087237cfa 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6746,8 +6746,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fno_ms_extensions, true))); if (IsMSVCCompat) { CmdArgs.push_back("-fms-compatibility"); - bool IsMSVCDefineStdc = Args.hasArg(options::OPT_fms_define_stdc); - if (IsMSVCDefineStdc) + if (!types::isCXX(Input.getType()) && Args.hasArg(options::OPT_fms_define_stdc)) CmdArgs.push_back("-fms-define-stdc"); } @@ -7926,8 +7925,8 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType, CmdArgs.push_back("-fno-wchar"); } - if (Args.hasArg(options::OPT_fms_define_stdc)) - CmdArgs.push_back("-fms-define-stdc"); + if (!types::isCXX(InputType) && Args.hasArg(options::OPT_fms_define_stdc)) + CmdArgs.push_back("-fms-define-stdc"); if (Args.hasArg(options::OPT__SLASH_kernel)) { llvm::Triple::ArchType Arch = getToolChain().getArch(); diff --git a/clang/test/Driver/cl-zc.cpp b/clang/test/Driver/cl-zc.cpp index bb955222c647af3..16df6799d5efaa0 100644 --- a/clang/test/Driver/cl-zc.cpp +++ b/clang/test/Driver/cl-zc.cpp @@ -123,10 +123,15 @@ // CHECK-CHAR8_T_: "-fno-char8_t" -// RUN: %clang_cl /dev/null /E -Xclang -dM 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=STDCOFF -// RUN: %clang_cl /dev/null /E -Xclang -dM /Zc:__STDC__ 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=STDCON -// STDCON: #define __STDC__ 1 -// STDCOFF-NOT: #define __STDC__ 1 +// RUN: %clang_cl /TC /dev/null /E -Xclang -dM 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=NOSTDC +// RUN: %clang_cl /TC /dev/null /E -Xclang -dM /Zc:__STDC__ 2> /dev/null | FileCheck -match-full-lines %s --check-prefix=YESSTDC +// __STDC__ should never be defined in C++ mode with fms-compatibility. +// RUN: %clang_cl /dev/null /E -Xclang -dM 2>&1 | FileCheck %s --check-prefix=NOSTDC +// RUN: %clang_cl /dev/null /E -Xclang -dM /Zc:__STDC__ 2>&1 | FileCheck %s --check-prefix=ZCSTDCIGNORED +// YESSTDC: #define __STDC__ 1 +// NOSTDC-NOT: #define __STDC__ 1 +// ZCSTDCIGNORED-NOT: #define __STDC__ 1 +// ZCSTDCIGNORED: argument unused during compilation // These never warn, but don't have an effect yet. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits