Skip to content

Commit 18f1166

Browse files
authored
Multilib support for libraries with exceptions (#75031)
For better multilib matching explicitly match -fno-rtti and -fno-exceptions
1 parent 73c646a commit 18f1166

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ class ToolChain {
120120
RM_Disabled,
121121
};
122122

123+
enum ExceptionsMode {
124+
EM_Enabled,
125+
EM_Disabled,
126+
};
127+
123128
struct BitCodeLibraryInfo {
124129
std::string Path;
125130
bool ShouldInternalize;
@@ -141,6 +146,8 @@ class ToolChain {
141146

142147
const RTTIMode CachedRTTIMode;
143148

149+
const ExceptionsMode CachedExceptionsMode;
150+
144151
/// The list of toolchain specific path prefixes to search for libraries.
145152
path_list LibraryPaths;
146153

@@ -318,6 +325,9 @@ class ToolChain {
318325
// Returns the RTTIMode for the toolchain with the current arguments.
319326
RTTIMode getRTTIMode() const { return CachedRTTIMode; }
320327

328+
// Returns the ExceptionsMode for the toolchain with the current arguments.
329+
ExceptionsMode getExceptionsMode() const { return CachedExceptionsMode; }
330+
321331
/// Return any implicit target and/or mode flag for an invocation of
322332
/// the compiler driver as `ProgName`.
323333
///

clang/lib/Driver/ToolChain.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,19 @@ static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
7777
return NoRTTI ? ToolChain::RM_Disabled : ToolChain::RM_Enabled;
7878
}
7979

80+
static ToolChain::ExceptionsMode CalculateExceptionsMode(const ArgList &Args) {
81+
if (Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
82+
true)) {
83+
return ToolChain::EM_Enabled;
84+
}
85+
return ToolChain::EM_Disabled;
86+
}
87+
8088
ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
8189
const ArgList &Args)
8290
: D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
83-
CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
91+
CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)),
92+
CachedExceptionsMode(CalculateExceptionsMode(Args)) {
8493
auto addIfExists = [this](path_list &List, const std::string &Path) {
8594
if (getVFS().exists(Path))
8695
List.push_back(Path);
@@ -264,6 +273,18 @@ ToolChain::getMultilibFlags(const llvm::opt::ArgList &Args) const {
264273
break;
265274
}
266275

276+
// Include fno-exceptions and fno-rtti
277+
// to improve multilib selection
278+
if (getRTTIMode() == ToolChain::RTTIMode::RM_Disabled)
279+
Result.push_back("-fno-rtti");
280+
else
281+
Result.push_back("-frtti");
282+
283+
if (getExceptionsMode() == ToolChain::ExceptionsMode::EM_Disabled)
284+
Result.push_back("-fno-exceptions");
285+
else
286+
Result.push_back("-fexceptions");
287+
267288
// Sort and remove duplicates.
268289
std::sort(Result.begin(), Result.end());
269290
Result.erase(std::unique(Result.begin(), Result.end()), Result.end());

0 commit comments

Comments
 (0)