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
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");
+}
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,
--- /dev/null
+// 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