[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
This commit is contained in:
Markus Böck 2021-06-10 17:02:44 +02:00
parent 9833b57981
commit 936d6756cc
3 changed files with 20 additions and 0 deletions

View File

@ -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");
}

View File

@ -122,6 +122,11 @@ public:
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,

View File

@ -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