[Lldb-commits] [lldb] 6093a77 - [lldb] Create an enum to specify the kind of ArchSpec matching

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue Aug 2 07:21:26 PDT 2022


Author: Pavel Labath
Date: 2022-08-02T16:20:13+02:00
New Revision: 6093a77caf44aad1bc62056295988d08e13cd421

URL: https://github.com/llvm/llvm-project/commit/6093a77caf44aad1bc62056295988d08e13cd421
DIFF: https://github.com/llvm/llvm-project/commit/6093a77caf44aad1bc62056295988d08e13cd421.diff

LOG: [lldb] Create an enum to specify the kind of ArchSpec matching

s/true/ArchSpec::ExactMatch
s/false/ArchSpec::CompatibleMatch

Differential Revision: https://reviews.llvm.org/D121290

Added: 
    

Modified: 
    lldb/include/lldb/Target/Platform.h
    lldb/include/lldb/Utility/ArchSpec.h
    lldb/source/Interpreter/OptionGroupPlatform.cpp
    lldb/source/Target/Platform.cpp
    lldb/source/Target/Process.cpp
    lldb/source/Target/Target.cpp
    lldb/source/Target/TargetList.cpp
    lldb/source/Utility/ArchSpec.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h
index aa09b345e4e1..868f10689bb1 100644
--- a/lldb/include/lldb/Target/Platform.h
+++ b/lldb/include/lldb/Target/Platform.h
@@ -329,7 +329,7 @@ class Platform : public PluginInterface {
   /// the target triple contained within.
   virtual bool IsCompatibleArchitecture(const ArchSpec &arch,
                                         const ArchSpec &process_host_arch,
-                                        bool exact_arch_match,
+                                        ArchSpec::MatchType match,
                                         ArchSpec *compatible_arch_ptr);
 
   /// Not all platforms will support debugging a process by spawning somehow

diff  --git a/lldb/include/lldb/Utility/ArchSpec.h b/lldb/include/lldb/Utility/ArchSpec.h
index f67acedf11c2..aad5c27cbb97 100644
--- a/lldb/include/lldb/Utility/ArchSpec.h
+++ b/lldb/include/lldb/Utility/ArchSpec.h
@@ -488,19 +488,25 @@ class ArchSpec {
   ///         architecture and false otherwise.
   bool CharIsSignedByDefault() const;
 
-  /// Compare an ArchSpec to another ArchSpec, requiring an exact cpu type
-  /// match between them. e.g. armv7s is not an exact match with armv7 - this
-  /// would return false
+  enum MatchType : bool { CompatibleMatch, ExactMatch };
+
+  /// Compare this ArchSpec to another ArchSpec. \a match specifies the kind of
+  /// matching that is to be done. CompatibleMatch requires only a compatible
+  /// cpu type (e.g., armv7s is compatible with armv7). ExactMatch requires an
+  /// exact match (armv7s is not an exact match with armv7).
   ///
   /// \return true if the two ArchSpecs match.
-  bool IsExactMatch(const ArchSpec &rhs) const;
+  bool IsMatch(const ArchSpec &rhs, MatchType match) const;
 
-  /// Compare an ArchSpec to another ArchSpec, requiring a compatible cpu type
-  /// match between them. e.g. armv7s is compatible with armv7 - this method
-  /// would return true
-  ///
-  /// \return true if the two ArchSpecs are compatible
-  bool IsCompatibleMatch(const ArchSpec &rhs) const;
+  /// Shorthand for IsMatch(rhs, ExactMatch).
+  bool IsExactMatch(const ArchSpec &rhs) const {
+    return IsMatch(rhs, ExactMatch);
+  }
+
+  /// Shorthand for IsMatch(rhs, CompatibleMatch).
+  bool IsCompatibleMatch(const ArchSpec &rhs) const {
+    return IsMatch(rhs, CompatibleMatch);
+  }
 
   bool IsFullySpecifiedTriple() const;
 
@@ -529,7 +535,6 @@ class ArchSpec {
   void SetFlags(const std::string &elf_abi);
 
 protected:
-  bool IsEqualTo(const ArchSpec &rhs, bool exact_match) const;
   void UpdateCore();
 
   llvm::Triple m_triple;

diff  --git a/lldb/source/Interpreter/OptionGroupPlatform.cpp b/lldb/source/Interpreter/OptionGroupPlatform.cpp
index acdf3f293496..60107eb28806 100644
--- a/lldb/source/Interpreter/OptionGroupPlatform.cpp
+++ b/lldb/source/Interpreter/OptionGroupPlatform.cpp
@@ -30,8 +30,9 @@ PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
           m_platform_name);
     }
     if (platform_sp) {
-      if (platform_arch.IsValid() && !platform_sp->IsCompatibleArchitecture(
-                                         arch, {}, false, &platform_arch)) {
+      if (platform_arch.IsValid() &&
+          !platform_sp->IsCompatibleArchitecture(
+              arch, {}, ArchSpec::CompatibleMatch, &platform_arch)) {
         error.SetErrorStringWithFormatv("platform '{0}' doesn't support '{1}'",
                                         platform_sp->GetPluginName(),
                                         arch.GetTriple().getTriple());

diff  --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp
index 61c4cba22c23..b9b32bff0730 100644
--- a/lldb/source/Target/Platform.cpp
+++ b/lldb/source/Target/Platform.cpp
@@ -929,7 +929,8 @@ ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
 
   ArchSpec compatible_arch;
   ArchSpec raw_arch(triple);
-  if (!IsCompatibleArchitecture(raw_arch, {}, false, &compatible_arch))
+  if (!IsCompatibleArchitecture(raw_arch, {}, ArchSpec::CompatibleMatch,
+                                &compatible_arch))
     return raw_arch;
 
   if (!compatible_arch.IsValid())
@@ -1156,16 +1157,14 @@ Platform::CreateArchList(llvm::ArrayRef<llvm::Triple::ArchType> archs,
 /// architecture and the target triple contained within.
 bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
                                         const ArchSpec &process_host_arch,
-                                        bool exact_arch_match,
+                                        ArchSpec::MatchType match,
                                         ArchSpec *compatible_arch_ptr) {
   // If the architecture is invalid, we must answer true...
   if (arch.IsValid()) {
     ArchSpec platform_arch;
-    auto match = exact_arch_match ? &ArchSpec::IsExactMatch
-                                  : &ArchSpec::IsCompatibleMatch;
     for (const ArchSpec &platform_arch :
          GetSupportedArchitectures(process_host_arch)) {
-      if ((arch.*match)(platform_arch)) {
+      if (arch.IsMatch(platform_arch, match)) {
         if (compatible_arch_ptr)
           *compatible_arch_ptr = platform_arch;
         return true;
@@ -1966,14 +1965,15 @@ PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
   // First try exact arch matches across all platforms already created
   for (const auto &platform_sp : m_platforms) {
-    if (platform_sp->IsCompatibleArchitecture(arch, process_host_arch, true,
-                                              platform_arch_ptr))
+    if (platform_sp->IsCompatibleArchitecture(
+            arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr))
       return platform_sp;
   }
 
   // Next try compatible arch matches across all platforms already created
   for (const auto &platform_sp : m_platforms) {
-    if (platform_sp->IsCompatibleArchitecture(arch, process_host_arch, false,
+    if (platform_sp->IsCompatibleArchitecture(arch, process_host_arch,
+                                              ArchSpec::CompatibleMatch,
                                               platform_arch_ptr))
       return platform_sp;
   }
@@ -1985,8 +1985,9 @@ PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
        (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
        ++idx) {
     PlatformSP platform_sp = create_callback(false, &arch);
-    if (platform_sp && platform_sp->IsCompatibleArchitecture(
-                           arch, process_host_arch, true, platform_arch_ptr)) {
+    if (platform_sp &&
+        platform_sp->IsCompatibleArchitecture(
+            arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr)) {
       m_platforms.push_back(platform_sp);
       return platform_sp;
     }
@@ -1997,7 +1998,8 @@ PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
        ++idx) {
     PlatformSP platform_sp = create_callback(false, &arch);
     if (platform_sp && platform_sp->IsCompatibleArchitecture(
-                           arch, process_host_arch, false, platform_arch_ptr)) {
+                           arch, process_host_arch, ArchSpec::CompatibleMatch,
+                           platform_arch_ptr)) {
       m_platforms.push_back(platform_sp);
       return platform_sp;
     }
@@ -2031,7 +2033,7 @@ PlatformSP PlatformList::GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
   if (m_selected_platform_sp) {
     for (const ArchSpec &arch : archs) {
       if (m_selected_platform_sp->IsCompatibleArchitecture(
-              arch, process_host_arch, false, nullptr))
+              arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
         return m_selected_platform_sp;
     }
   }
@@ -2039,8 +2041,8 @@ PlatformSP PlatformList::GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
   // Prefer the host platform if it matches at least one architecture.
   if (host_platform_sp) {
     for (const ArchSpec &arch : archs) {
-      if (host_platform_sp->IsCompatibleArchitecture(arch, process_host_arch,
-                                                     false, nullptr))
+      if (host_platform_sp->IsCompatibleArchitecture(
+              arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
         return host_platform_sp;
     }
   }

diff  --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index b2f1318ca91d..1b8df33d39e5 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -2905,9 +2905,9 @@ void Process::CompleteAttach() {
   ArchSpec process_host_arch = GetSystemArchitecture();
   if (platform_sp) {
     const ArchSpec &target_arch = GetTarget().GetArchitecture();
-    if (target_arch.IsValid() &&
-        !platform_sp->IsCompatibleArchitecture(target_arch, process_host_arch,
-                                               false, nullptr)) {
+    if (target_arch.IsValid() && !platform_sp->IsCompatibleArchitecture(
+                                     target_arch, process_host_arch,
+                                     ArchSpec::CompatibleMatch, nullptr)) {
       ArchSpec platform_arch;
       platform_sp = GetTarget().GetDebugger().GetPlatformList().GetOrCreate(
           target_arch, process_host_arch, &platform_arch);

diff  --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp
index af511d645455..6f361a49cbf6 100644
--- a/lldb/source/Target/Target.cpp
+++ b/lldb/source/Target/Target.cpp
@@ -1488,8 +1488,8 @@ bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform) {
   if (set_platform) {
     if (other.IsValid()) {
       auto platform_sp = GetPlatform();
-      if (!platform_sp ||
-          !platform_sp->IsCompatibleArchitecture(other, {}, false, nullptr)) {
+      if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
+                              other, {}, ArchSpec::CompatibleMatch, nullptr)) {
         ArchSpec platform_arch;
         if (PlatformSP arch_platform_sp =
                 GetDebugger().GetPlatformList().GetOrCreate(other, {},

diff  --git a/lldb/source/Target/TargetList.cpp b/lldb/source/Target/TargetList.cpp
index 5d60c1eecdcd..8ce2ae8c2898 100644
--- a/lldb/source/Target/TargetList.cpp
+++ b/lldb/source/Target/TargetList.cpp
@@ -216,7 +216,8 @@ Status TargetList::CreateTargetInternal(
   // If we have a valid architecture, make sure the current platform is
   // compatible with that architecture.
   if (!prefer_platform_arch && arch.IsValid()) {
-    if (!platform_sp->IsCompatibleArchitecture(arch, {}, false, nullptr)) {
+    if (!platform_sp->IsCompatibleArchitecture(
+            arch, {}, ArchSpec::CompatibleMatch, nullptr)) {
       platform_sp = platform_list.GetOrCreate(arch, {}, &platform_arch);
       if (platform_sp)
         platform_list.SetSelectedPlatform(platform_sp);
@@ -225,7 +226,8 @@ Status TargetList::CreateTargetInternal(
     // If "arch" isn't valid, yet "platform_arch" is, it means we have an
     // executable file with a single architecture which should be used.
     ArchSpec fixed_platform_arch;
-    if (!platform_sp->IsCompatibleArchitecture(platform_arch, {}, false, nullptr)) {
+    if (!platform_sp->IsCompatibleArchitecture(
+            platform_arch, {}, ArchSpec::CompatibleMatch, nullptr)) {
       platform_sp =
           platform_list.GetOrCreate(platform_arch, {}, &fixed_platform_arch);
       if (platform_sp)
@@ -256,8 +258,8 @@ Status TargetList::CreateTargetInternal(Debugger &debugger,
   ArchSpec arch(specified_arch);
 
   if (arch.IsValid()) {
-    if (!platform_sp ||
-        !platform_sp->IsCompatibleArchitecture(arch, {}, false, nullptr))
+    if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
+                            arch, {}, ArchSpec::CompatibleMatch, nullptr))
       platform_sp =
           debugger.GetPlatformList().GetOrCreate(specified_arch, {}, &arch);
   }

diff  --git a/lldb/source/Utility/ArchSpec.cpp b/lldb/source/Utility/ArchSpec.cpp
index a99aed82bc88..7ea76c3966a5 100644
--- a/lldb/source/Utility/ArchSpec.cpp
+++ b/lldb/source/Utility/ArchSpec.cpp
@@ -928,14 +928,6 @@ uint32_t ArchSpec::GetMaximumOpcodeByteSize() const {
   return 0;
 }
 
-bool ArchSpec::IsExactMatch(const ArchSpec &rhs) const {
-  return IsEqualTo(rhs, true);
-}
-
-bool ArchSpec::IsCompatibleMatch(const ArchSpec &rhs) const {
-  return IsEqualTo(rhs, false);
-}
-
 static bool IsCompatibleEnvironment(llvm::Triple::EnvironmentType lhs,
                                     llvm::Triple::EnvironmentType rhs) {
   if (lhs == rhs)
@@ -967,11 +959,11 @@ static bool IsCompatibleEnvironment(llvm::Triple::EnvironmentType lhs,
   return false;
 }
 
-bool ArchSpec::IsEqualTo(const ArchSpec &rhs, bool exact_match) const {
+bool ArchSpec::IsMatch(const ArchSpec &rhs, MatchType match) const {
   // explicitly ignoring m_distribution_id in this method.
 
   if (GetByteOrder() != rhs.GetByteOrder() ||
-      !cores_match(GetCore(), rhs.GetCore(), true, exact_match))
+      !cores_match(GetCore(), rhs.GetCore(), true, match == ExactMatch))
     return false;
 
   const llvm::Triple &lhs_triple = GetTriple();
@@ -988,7 +980,7 @@ bool ArchSpec::IsEqualTo(const ArchSpec &rhs, bool exact_match) const {
   // On Windows, the vendor field doesn't have any practical effect, but
   // it is often set to either "pc" or "w64".
   if ((lhs_triple_vendor != rhs_triple_vendor) &&
-      (exact_match || !both_windows)) {
+      (match == ExactMatch || !both_windows)) {
     const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
     const bool lhs_vendor_specified = TripleVendorWasSpecified();
     // Both architectures had the vendor specified, so if they aren't equal
@@ -1007,7 +999,7 @@ bool ArchSpec::IsEqualTo(const ArchSpec &rhs, bool exact_match) const {
   const llvm::Triple::EnvironmentType rhs_triple_env =
       rhs_triple.getEnvironment();
 
-  if (!exact_match) {
+  if (match == CompatibleMatch) {
     // x86_64-apple-ios-macabi, x86_64-apple-macosx are compatible, no match.
     if ((lhs_triple_os == llvm::Triple::IOS &&
          lhs_triple_env == llvm::Triple::MacABI &&
@@ -1034,12 +1026,13 @@ bool ArchSpec::IsEqualTo(const ArchSpec &rhs, bool exact_match) const {
       return false;
 
     // If the pair of os+env is both unspecified, match any other os+env combo.
-    if (!exact_match && ((!lhs_os_specified && !lhs_triple.hasEnvironment()) ||
-                         (!rhs_os_specified && !rhs_triple.hasEnvironment())))
+    if (match == CompatibleMatch &&
+        ((!lhs_os_specified && !lhs_triple.hasEnvironment()) ||
+         (!rhs_os_specified && !rhs_triple.hasEnvironment())))
       return true;
   }
 
-  if (!exact_match && both_windows)
+  if (match == CompatibleMatch && both_windows)
     return true; // The Windows environments (MSVC vs GNU) are compatible
 
   return IsCompatibleEnvironment(lhs_triple_env, rhs_triple_env);


        


More information about the lldb-commits mailing list