[clang] 936d675 - [clang][msvc] Define _HAS_STATIC_RTTI to 0, when compiling with -fno-rtti
Markus Böck via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 10 08:03:16 PDT 2021
Author: Markus Böck
Date: 2021-06-10T17:02:44+02:00
New Revision: 936d6756ccfbe207a181b692b828f9fd8f1489f2
URL: https://github.com/llvm/llvm-project/commit/936d6756ccfbe207a181b692b828f9fd8f1489f2
DIFF: https://github.com/llvm/llvm-project/commit/936d6756ccfbe207a181b692b828f9fd8f1489f2.diff
LOG: [clang][msvc] Define _HAS_STATIC_RTTI to 0, when compiling with -fno-rtti
When using the -fno-rtti option of the GCC style clang++, using typeid results in an error. The MSVC STL however kindly provides a define flag called _HAS_STATIC_RTTI, which either enables or disables uses of typeid throughout the STL. By default, if undefined, it is set to 1, enabling the use of typeid.
With this patch, _HAS_STATIC_RTTI is set to 0 when -fno-rtti is specified. This way various headers of the MSVC STL like functional can be consumed without compilation failures.
Differential Revision: https://reviews.llvm.org/D103771
Added:
clang/test/Driver/msvc-static-rtti.cpp
Modified:
clang/lib/Driver/ToolChains/MSVC.cpp
clang/lib/Driver/ToolChains/MSVC.h
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index dd7fa5ebf6ff..9b1c320755ed 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -1573,3 +1573,13 @@ MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
return DAL;
}
+
+void MSVCToolChain::addClangTargetOptions(
+ const ArgList &DriverArgs, ArgStringList &CC1Args,
+ Action::OffloadKind DeviceOffloadKind) const {
+ // MSVC STL kindly allows removing all usages of typeid by defining
+ // _HAS_STATIC_RTTI to 0. Do so, when compiling with -fno-rtti
+ if (DriverArgs.hasArg(options::OPT_fno_rtti, options::OPT_frtti,
+ /*Default=*/false))
+ CC1Args.push_back("-D_HAS_STATIC_RTTI=0");
+}
diff --git a/clang/lib/Driver/ToolChains/MSVC.h b/clang/lib/Driver/ToolChains/MSVC.h
index 432d399e241e..19d94c5c606e 100644
--- a/clang/lib/Driver/ToolChains/MSVC.h
+++ b/clang/lib/Driver/ToolChains/MSVC.h
@@ -122,6 +122,11 @@ class LLVM_LIBRARY_VISIBILITY MSVCToolChain : public ToolChain {
bool FoundMSVCInstall() const { return !VCToolChainPath.empty(); }
+ void
+ addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
+ llvm::opt::ArgStringList &CC1Args,
+ Action::OffloadKind DeviceOffloadKind) const override;
+
protected:
void AddSystemIncludeWithSubfolder(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/test/Driver/msvc-static-rtti.cpp b/clang/test/Driver/msvc-static-rtti.cpp
new file mode 100644
index 000000000000..b352f07bc6c1
--- /dev/null
+++ b/clang/test/Driver/msvc-static-rtti.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang -target x86_64-pc-windows-msvc -fno-rtti -### %s 2>&1 | FileCheck %s -check-prefix STATIC-RTTI-DEF
+// RUN: %clang -target x86_64-pc-windows-msvc -frtti -### %s 2>&1 | FileCheck %s -check-prefix STATIC-RTTI-DEF-NOT
+
+// STATIC-RTTI-DEF: -D_HAS_STATIC_RTTI=0
+// STATIC-RTTI-DEF-NOT: -D_HAS_STATIC_RTTI=0
More information about the cfe-commits
mailing list