[Lldb-commits] [lldb] r281662 - Allow ArchSpec to take a StringRef.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Thu Sep 15 14:32:57 PDT 2016


Author: zturner
Date: Thu Sep 15 16:32:57 2016
New Revision: 281662

URL: http://llvm.org/viewvc/llvm-project?rev=281662&view=rev
Log:
Allow ArchSpec to take a StringRef.

Modified:
    lldb/trunk/include/lldb/Core/ArchSpec.h
    lldb/trunk/source/Core/ArchSpec.cpp
    lldb/trunk/unittests/Core/ArchSpecTest.cpp

Modified: lldb/trunk/include/lldb/Core/ArchSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ArchSpec.h?rev=281662&r1=281661&r2=281662&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/ArchSpec.h (original)
+++ lldb/trunk/include/lldb/Core/ArchSpec.h Thu Sep 15 16:32:57 2016
@@ -257,7 +257,9 @@ public:
   //------------------------------------------------------------------
   explicit ArchSpec(const llvm::Triple &triple);
   explicit ArchSpec(const char *triple_cstr);
-  explicit ArchSpec(const char *triple_cstr, Platform *platform);
+  explicit ArchSpec(llvm::StringRef triple_str);
+  ArchSpec(const char *triple_cstr, Platform *platform);
+  ArchSpec(llvm::StringRef triple_str, Platform *platform);
   //------------------------------------------------------------------
   /// Constructor over architecture name.
   ///
@@ -505,8 +507,10 @@ public:
   //------------------------------------------------------------------
   bool SetTriple(const llvm::Triple &triple);
 
-  bool SetTriple(const char *triple_cstr);
+  bool SetTriple(llvm::StringRef triple_str);
+  bool SetTriple(llvm::StringRef triple_str, Platform *platform);
 
+  bool SetTriple(const char *triple_cstr);
   bool SetTriple(const char *triple_cstr, Platform *platform);
 
   //------------------------------------------------------------------
@@ -596,13 +600,13 @@ protected:
   bool IsEqualTo(const ArchSpec &rhs, bool exact_match) const;
 
   llvm::Triple m_triple;
-  Core m_core;
-  lldb::ByteOrder m_byte_order;
+  Core m_core = kCore_invalid;
+  lldb::ByteOrder m_byte_order = lldb::eByteOrderInvalid;
 
   // Additional arch flags which we cannot get from triple and core
   // For MIPS these are application specific extensions like
   // micromips, mips16 etc.
-  uint32_t m_flags;
+  uint32_t m_flags = 0;
 
   ConstString m_distribution_id;
 

Modified: lldb/trunk/source/Core/ArchSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ArchSpec.cpp?rev=281662&r1=281661&r2=281662&view=diff
==============================================================================
--- lldb/trunk/source/Core/ArchSpec.cpp (original)
+++ lldb/trunk/source/Core/ArchSpec.cpp Thu Sep 15 16:32:57 2016
@@ -555,33 +555,27 @@ FindArchDefinitionEntry(const ArchDefini
 //===----------------------------------------------------------------------===//
 // Constructors and destructors.
 
-ArchSpec::ArchSpec()
-    : m_triple(), m_core(kCore_invalid), m_byte_order(eByteOrderInvalid),
-      m_flags(0), m_distribution_id() {}
-
-ArchSpec::ArchSpec(const char *triple_cstr, Platform *platform)
-    : m_triple(), m_core(kCore_invalid), m_byte_order(eByteOrderInvalid),
-      m_flags(0), m_distribution_id() {
+ArchSpec::ArchSpec() {}
+
+ArchSpec::ArchSpec(const char *triple_cstr, Platform *platform) {
   if (triple_cstr)
     SetTriple(triple_cstr, platform);
 }
 
-ArchSpec::ArchSpec(const char *triple_cstr)
-    : m_triple(), m_core(kCore_invalid), m_byte_order(eByteOrderInvalid),
-      m_flags(0), m_distribution_id() {
+ArchSpec::ArchSpec(llvm::StringRef triple_str, Platform *platform) {
+  SetTriple(triple_str, platform);
+}
+
+ArchSpec::ArchSpec(const char *triple_cstr) {
   if (triple_cstr)
     SetTriple(triple_cstr);
 }
 
-ArchSpec::ArchSpec(const llvm::Triple &triple)
-    : m_triple(), m_core(kCore_invalid), m_byte_order(eByteOrderInvalid),
-      m_flags(0), m_distribution_id() {
-  SetTriple(triple);
-}
+ArchSpec::ArchSpec(llvm::StringRef triple_str) { SetTriple(triple_str); }
+
+ArchSpec::ArchSpec(const llvm::Triple &triple) { SetTriple(triple); }
 
-ArchSpec::ArchSpec(ArchitectureType arch_type, uint32_t cpu, uint32_t subtype)
-    : m_triple(), m_core(kCore_invalid), m_byte_order(eByteOrderInvalid),
-      m_flags(0), m_distribution_id() {
+ArchSpec::ArchSpec(ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) {
   SetArchitecture(arch_type, cpu, subtype);
 }
 
@@ -857,99 +851,104 @@ bool lldb_private::ParseMachCPUDashSubty
 }
 
 bool ArchSpec::SetTriple(const char *triple_cstr) {
-  if (triple_cstr && triple_cstr[0]) {
-    llvm::StringRef triple_stref(triple_cstr);
+  llvm::StringRef str(triple_cstr ? triple_cstr : "");
+  return SetTriple(str);
+}
 
-    if (ParseMachCPUDashSubtypeTriple(triple_stref, *this))
-      return true;
+bool ArchSpec::SetTriple(const char *triple_cstr, Platform *platform) {
+  llvm::StringRef str(triple_cstr ? triple_cstr : "");
+  return SetTriple(str, platform);
+}
 
-    if (triple_stref.startswith(LLDB_ARCH_DEFAULT)) {
-      // Special case for the current host default architectures...
-      if (triple_stref.equals(LLDB_ARCH_DEFAULT_32BIT))
-        *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
-      else if (triple_stref.equals(LLDB_ARCH_DEFAULT_64BIT))
-        *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
-      else if (triple_stref.equals(LLDB_ARCH_DEFAULT))
-        *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
-    } else {
-      std::string normalized_triple_sstr(llvm::Triple::normalize(triple_stref));
-      triple_stref = normalized_triple_sstr;
-      SetTriple(llvm::Triple(triple_stref));
-    }
-  } else
+bool ArchSpec::SetTriple(llvm::StringRef triple) {
+  if (triple.empty()) {
     Clear();
+    return false;
+  }
+
+  if (ParseMachCPUDashSubtypeTriple(triple, *this))
+    return true;
+
+  if (triple.startswith(LLDB_ARCH_DEFAULT)) {
+    // Special case for the current host default architectures...
+    if (triple.equals(LLDB_ARCH_DEFAULT_32BIT))
+      *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
+    else if (triple.equals(LLDB_ARCH_DEFAULT_64BIT))
+      *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
+    else if (triple.equals(LLDB_ARCH_DEFAULT))
+      *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
+  } else {
+    SetTriple(llvm::Triple(llvm::Triple::normalize(triple)));
+  }
   return IsValid();
 }
 
-bool ArchSpec::SetTriple(const char *triple_cstr, Platform *platform) {
-  if (triple_cstr && triple_cstr[0]) {
-    if (ParseMachCPUDashSubtypeTriple(triple_cstr, *this))
-      return true;
-
-    llvm::StringRef triple_stref(triple_cstr);
-    if (triple_stref.startswith(LLDB_ARCH_DEFAULT)) {
-      // Special case for the current host default architectures...
-      if (triple_stref.equals(LLDB_ARCH_DEFAULT_32BIT))
-        *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
-      else if (triple_stref.equals(LLDB_ARCH_DEFAULT_64BIT))
-        *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
-      else if (triple_stref.equals(LLDB_ARCH_DEFAULT))
-        *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
-    } else {
-      ArchSpec raw_arch(triple_cstr);
-
-      std::string normalized_triple_sstr(llvm::Triple::normalize(triple_stref));
-      triple_stref = normalized_triple_sstr;
-      llvm::Triple normalized_triple(triple_stref);
-
-      const bool os_specified = normalized_triple.getOSName().size() > 0;
-      const bool vendor_specified =
-          normalized_triple.getVendorName().size() > 0;
-      const bool env_specified =
-          normalized_triple.getEnvironmentName().size() > 0;
-
-      // If we got an arch only, then default the vendor, os, environment
-      // to match the platform if one is supplied
-      if (!(os_specified || vendor_specified || env_specified)) {
-        if (platform) {
-          // If we were given a platform, use the platform's system
-          // architecture. If this is not available (might not be
-          // connected) use the first supported architecture.
-          ArchSpec compatible_arch;
-          if (platform->IsCompatibleArchitecture(raw_arch, false,
-                                                 &compatible_arch)) {
-            if (compatible_arch.IsValid()) {
-              const llvm::Triple &compatible_triple =
-                  compatible_arch.GetTriple();
-              if (!vendor_specified)
-                normalized_triple.setVendor(compatible_triple.getVendor());
-              if (!os_specified)
-                normalized_triple.setOS(compatible_triple.getOS());
-              if (!env_specified &&
-                  compatible_triple.getEnvironmentName().size())
-                normalized_triple.setEnvironment(
-                    compatible_triple.getEnvironment());
-            }
-          } else {
-            *this = raw_arch;
-            return IsValid();
-          }
-        } else {
-          // No platform specified, fall back to the host system for
-          // the default vendor, os, and environment.
-          llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
-          if (!vendor_specified)
-            normalized_triple.setVendor(host_triple.getVendor());
-          if (!vendor_specified)
-            normalized_triple.setOS(host_triple.getOS());
-          if (!env_specified && host_triple.getEnvironmentName().size())
-            normalized_triple.setEnvironment(host_triple.getEnvironment());
-        }
-      }
-      SetTriple(normalized_triple);
-    }
-  } else
+bool ArchSpec::SetTriple(llvm::StringRef triple, Platform *platform) {
+  if (triple.empty()) {
     Clear();
+    return false;
+  }
+  if (ParseMachCPUDashSubtypeTriple(triple, *this))
+    return true;
+
+  if (triple.startswith(LLDB_ARCH_DEFAULT)) {
+    // Special case for the current host default architectures...
+    if (triple.equals(LLDB_ARCH_DEFAULT_32BIT))
+      *this = HostInfo::GetArchitecture(HostInfo::eArchKind32);
+    else if (triple.equals(LLDB_ARCH_DEFAULT_64BIT))
+      *this = HostInfo::GetArchitecture(HostInfo::eArchKind64);
+    else if (triple.equals(LLDB_ARCH_DEFAULT))
+      *this = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
+    return IsValid();
+  }
+
+  ArchSpec raw_arch(triple);
+
+  llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
+
+  const bool os_specified = !normalized_triple.getOSName().empty();
+  const bool vendor_specified = !normalized_triple.getVendorName().empty();
+  const bool env_specified = !normalized_triple.getEnvironmentName().empty();
+
+  if (os_specified || vendor_specified || env_specified) {
+    SetTriple(normalized_triple);
+    return IsValid();
+  }
+
+  // We got an arch only.  If there is no platform, fallback to the host system
+  // for defaults.
+  if (!platform) {
+    llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
+    if (!vendor_specified)
+      normalized_triple.setVendor(host_triple.getVendor());
+    if (!vendor_specified)
+      normalized_triple.setOS(host_triple.getOS());
+    if (!env_specified && host_triple.getEnvironmentName().size())
+      normalized_triple.setEnvironment(host_triple.getEnvironment());
+    SetTriple(normalized_triple);
+    return IsValid();
+  }
+
+  // If we were given a platform, use the platform's system architecture. If
+  // this is not available (might not be connected) use the first supported
+  // architecture.
+  ArchSpec compatible_arch;
+  if (!platform->IsCompatibleArchitecture(raw_arch, false, &compatible_arch)) {
+    *this = raw_arch;
+    return IsValid();
+  }
+
+  if (compatible_arch.IsValid()) {
+    const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
+    if (!vendor_specified)
+      normalized_triple.setVendor(compatible_triple.getVendor());
+    if (!os_specified)
+      normalized_triple.setOS(compatible_triple.getOS());
+    if (!env_specified && compatible_triple.hasEnvironment())
+      normalized_triple.setEnvironment(compatible_triple.getEnvironment());
+  }
+
+  SetTriple(normalized_triple);
   return IsValid();
 }
 

Modified: lldb/trunk/unittests/Core/ArchSpecTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/ArchSpecTest.cpp?rev=281662&r1=281661&r2=281662&view=diff
==============================================================================
--- lldb/trunk/unittests/Core/ArchSpecTest.cpp (original)
+++ lldb/trunk/unittests/Core/ArchSpecTest.cpp Thu Sep 15 16:32:57 2016
@@ -11,6 +11,8 @@
 
 #include "lldb/Core/ArchSpec.h"
 
+#include "llvm/Support/MachO.h"
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -93,3 +95,42 @@ TEST(ArchSpecTest, TestParseMachCPUDashS
   EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("12-10.10", AS));
 }
 
+TEST(ArchSpecTest, TestSetTriple) {
+  ArchSpec AS;
+
+  // Various flavors of valid triples.
+  EXPECT_TRUE(AS.SetTriple("12-10-apple-darwin"));
+  EXPECT_EQ(llvm::MachO::CPU_TYPE_ARM, AS.GetMachOCPUType());
+  EXPECT_EQ(10, AS.GetMachOCPUSubType());
+  EXPECT_TRUE(llvm::StringRef(AS.GetTriple().str())
+                  .consume_front("armv7f-apple-darwin"));
+  EXPECT_EQ(ArchSpec::eCore_arm_armv7f, AS.GetCore());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(AS.SetTriple("18.100-apple-darwin"));
+  EXPECT_EQ(llvm::MachO::CPU_TYPE_POWERPC, AS.GetMachOCPUType());
+  EXPECT_EQ(100, AS.GetMachOCPUSubType());
+  EXPECT_TRUE(llvm::StringRef(AS.GetTriple().str())
+                  .consume_front("powerpc-apple-darwin"));
+  EXPECT_EQ(ArchSpec::eCore_ppc_ppc970, AS.GetCore());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(AS.SetTriple("i686-pc-windows"));
+  EXPECT_EQ(llvm::Triple::x86, AS.GetTriple().getArch());
+  EXPECT_EQ(llvm::Triple::PC, AS.GetTriple().getVendor());
+  EXPECT_EQ(llvm::Triple::Win32, AS.GetTriple().getOS());
+  EXPECT_TRUE(
+      llvm::StringRef(AS.GetTriple().str()).consume_front("i686-pc-windows"));
+  EXPECT_STREQ("i686", AS.GetArchitectureName());
+  EXPECT_EQ(ArchSpec::eCore_x86_32_i686, AS.GetCore());
+
+  // Various flavors of invalid triples.
+  AS = ArchSpec();
+  EXPECT_FALSE(AS.SetTriple("unknown-unknown-unknown"));
+
+  AS = ArchSpec();
+  EXPECT_FALSE(AS.SetTriple("unknown"));
+
+  AS = ArchSpec();
+  EXPECT_FALSE(AS.SetTriple(""));
+}
\ No newline at end of file




More information about the lldb-commits mailing list