[clang] Multilib support for libraries with exceptions (PR #75031)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 11 00:34:57 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-driver

Author: None (pwprzybyla)

<details>
<summary>Changes</summary>

For better multilib matching explicitly
match -fno-rtti and -fno-exceptions

---
Full diff: https://github.com/llvm/llvm-project/pull/75031.diff


3 Files Affected:

- (modified) clang/include/clang/Driver/ToolChain.h (+10) 
- (modified) clang/lib/Driver/ToolChain.cpp (+12-1) 
- (modified) clang/lib/Driver/ToolChains/BareMetal.cpp (+6) 


``````````diff
diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h
index 2d0c1f826c172..fbe2e8fe8e88d 100644
--- a/clang/include/clang/Driver/ToolChain.h
+++ b/clang/include/clang/Driver/ToolChain.h
@@ -120,6 +120,11 @@ class ToolChain {
     RM_Disabled,
   };
 
+  enum ExceptionsMode {
+    EM_Enabled,
+    EM_Disabled,
+  };
+
   struct BitCodeLibraryInfo {
     std::string Path;
     bool ShouldInternalize;
@@ -141,6 +146,8 @@ class ToolChain {
 
   const RTTIMode CachedRTTIMode;
 
+  const ExceptionsMode CachedExceptionsMode;
+
   /// The list of toolchain specific path prefixes to search for libraries.
   path_list LibraryPaths;
 
@@ -318,6 +325,9 @@ class ToolChain {
   // Returns the RTTIMode for the toolchain with the current arguments.
   RTTIMode getRTTIMode() const { return CachedRTTIMode; }
 
+  // Returns the ExceptionsMode for the toolchain with the current arguments.
+  ExceptionsMode getExceptionsMode() const { return CachedExceptionsMode; }
+
   /// Return any implicit target and/or mode flag for an invocation of
   /// the compiler driver as `ProgName`.
   ///
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index ab19166f18c2d..6ab651861196b 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -77,10 +77,21 @@ static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
   return NoRTTI ? ToolChain::RM_Disabled : ToolChain::RM_Enabled;
 }
 
+static ToolChain::ExceptionsMode CalculateExceptionsMode(const ArgList &Args) {
+
+  Arg *exceptionsArg = Args.getLastArg(options::OPT_fno_exceptions);
+  if (exceptionsArg &&
+      exceptionsArg->getOption().matches(options::OPT_fno_exceptions)) {
+    return ToolChain::EM_Disabled;
+  }
+  return ToolChain::EM_Enabled;
+}
+
 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
                      const ArgList &Args)
     : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
-      CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
+      CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)),
+      CachedExceptionsMode(CalculateExceptionsMode(Args)) {
   auto addIfExists = [this](path_list &List, const std::string &Path) {
     if (getVFS().exists(Path))
       List.push_back(Path);
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp b/clang/lib/Driver/ToolChains/BareMetal.cpp
index 42c8336e626c7..c62b1edbf6bcb 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -179,6 +179,12 @@ static void findMultilibsFromYAML(const ToolChain &TC, const Driver &D,
   if (!MB)
     return;
   Multilib::flags_list Flags = TC.getMultilibFlags(Args);
+  if (TC.getRTTIMode() == ToolChain::RTTIMode::RM_Disabled) {
+    Flags.push_back("-fno-rtti");
+  }
+  if (TC.getExceptionsMode() == ToolChain::ExceptionsMode::EM_Disabled) {
+    Flags.push_back("-fno-exceptions");
+  }
   llvm::ErrorOr<MultilibSet> ErrorOrMultilibSet =
       MultilibSet::parseYaml(*MB.get());
   if (ErrorOrMultilibSet.getError())

``````````

</details>


https://github.com/llvm/llvm-project/pull/75031


More information about the cfe-commits mailing list