[llvm-commits] [llvm] r77119 - in /llvm/trunk: include/llvm/ADT/Triple.h lib/Support/Triple.cpp unittests/ADT/TripleTest.cpp

Daniel Dunbar daniel at zuster.org
Sat Jul 25 20:31:52 PDT 2009


Author: ddunbar
Date: Sat Jul 25 22:31:47 2009
New Revision: 77119

URL: http://llvm.org/viewvc/llvm-project?rev=77119&view=rev
Log:
Update Triple to use StringRef/Twine based APIs.
 - This is now shorter, simpler, safer, and more efficient, what a deal.

Modified:
    llvm/trunk/include/llvm/ADT/Triple.h
    llvm/trunk/lib/Support/Triple.cpp
    llvm/trunk/unittests/ADT/TripleTest.cpp

Modified: llvm/trunk/include/llvm/ADT/Triple.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Triple.h?rev=77119&r1=77118&r2=77119&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/Triple.h (original)
+++ llvm/trunk/include/llvm/ADT/Triple.h Sat Jul 25 22:31:47 2009
@@ -10,9 +10,12 @@
 #ifndef LLVM_ADT_TRIPLE_H
 #define LLVM_ADT_TRIPLE_H
 
+#include "llvm/ADT/StringRef.h"
 #include <string>
 
 namespace llvm {
+class StringRef;
+class Twine;
 
 /// Triple - Helper class for working with target triples.
 ///
@@ -121,28 +124,25 @@
 
   const std::string &getTriple() const { return Data; }
 
-  // FIXME: Invent a lightweight string representation for these to
-  // use.
-
   /// getArchName - Get the architecture (first) component of the
   /// triple.
-  std::string getArchName() const;
+  StringRef getArchName() const;
 
   /// getVendorName - Get the vendor (second) component of the triple.
-  std::string getVendorName() const;
+  StringRef getVendorName() const;
 
   /// getOSName - Get the operating system (third) component of the
   /// triple.
-  std::string getOSName() const;
+  StringRef getOSName() const;
 
   /// getEnvironmentName - Get the optional environment (fourth)
   /// component of the triple, or "" if empty.
-  std::string getEnvironmentName() const;
+  StringRef getEnvironmentName() const;
 
   /// getOSAndEnvironmentName - Get the operating system and optional
   /// environment components as a single string (separated by a '-'
   /// if the environment component is present).
-  std::string getOSAndEnvironmentName() const;
+  StringRef getOSAndEnvironmentName() const;
 
   /// @}
   /// @name Mutators
@@ -161,27 +161,27 @@
   void setOS(OSType Kind);
 
   /// setTriple - Set all components to the new triple \arg Str.
-  void setTriple(const std::string &Str);
+  void setTriple(const Twine &Str);
 
   /// setArchName - Set the architecture (first) component of the
   /// triple by name.
-  void setArchName(const std::string &Str);
+  void setArchName(const StringRef &Str);
 
   /// setVendorName - Set the vendor (second) component of the triple
   /// by name.
-  void setVendorName(const std::string &Str);
+  void setVendorName(const StringRef &Str);
 
   /// setOSName - Set the operating system (third) component of the
   /// triple by name.
-  void setOSName(const std::string &Str);
+  void setOSName(const StringRef &Str);
 
   /// setEnvironmentName - Set the optional environment (fourth)
   /// component of the triple by name.
-  void setEnvironmentName(const std::string &Str);
+  void setEnvironmentName(const StringRef &Str);
 
   /// setOSAndEnvironmentName - Set the operating system and optional
   /// environment components with a single string.
-  void setOSAndEnvironmentName(const std::string &Str);
+  void setOSAndEnvironmentName(const StringRef &Str);
 
   /// @}
   /// @name Static helpers for IDs.

Modified: llvm/trunk/lib/Support/Triple.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Triple.cpp?rev=77119&r1=77118&r2=77119&view=diff

==============================================================================
--- llvm/trunk/lib/Support/Triple.cpp (original)
+++ llvm/trunk/lib/Support/Triple.cpp Sat Jul 25 22:31:47 2009
@@ -8,6 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/Triple.h"
+
+#include "llvm/ADT/Twine.h"
 #include <cassert>
 #include <cstring>
 using namespace llvm;
@@ -60,7 +62,7 @@
 void Triple::Parse() const {
   assert(!isInitialized() && "Invalid parse call.");
 
-  std::string ArchName = getArchName();
+  StringRef ArchName = getArchName();
   if (ArchName.size() == 4 && ArchName[0] == 'i' && 
       ArchName[2] == '8' && ArchName[3] == '6')
     Arch = x86;
@@ -73,7 +75,7 @@
   else
     Arch = UnknownArch;
 
-  std::string VendorName = getVendorName();
+  StringRef VendorName = getVendorName();
   if (VendorName == "apple")
     Vendor = Apple;
   else if (VendorName == "pc")
@@ -81,20 +83,20 @@
   else
     Vendor = UnknownVendor;
 
-  std::string OSName = getOSName();
-  if (memcmp(&OSName[0], "auroraux", 8) == 0)
+  StringRef OSName = getOSName();
+  if (OSName.startswith("auroraux"))
     OS = AuroraUX;
-  else if (memcmp(&OSName[0], "darwin", 6) == 0)
+  else if (OSName.startswith("darwin"))
     OS = Darwin;
-  else if (memcmp(&OSName[0], "dragonfly", 9) == 0)
+  else if (OSName.startswith("dragonfly"))
     OS = DragonFly;
-  else if (memcmp(&OSName[0], "freebsd", 7) == 0)
+  else if (OSName.startswith("freebsd"))
     OS = FreeBSD;
-  else if (memcmp(&OSName[0], "linux", 5) == 0)
+  else if (OSName.startswith("linux"))
     OS = Linux;
-  else if (memcmp(&OSName[0], "netbsd", 6) == 0)
+  else if (OSName.startswith("netbsd"))
     OS = NetBSD;
-  else if (memcmp(&OSName[0], "openbsd", 7) == 0)
+  else if (OSName.startswith("openbsd"))
     OS = OpenBSD;
   else
     OS = UnknownOS;
@@ -102,59 +104,34 @@
   assert(isInitialized() && "Failed to initialize!");
 }
 
-static std::string extract(const std::string &A,
-                           std::string::size_type begin,
-                           std::string::size_type end) {
-  if (begin == std::string::npos)
-    return "";
-  if (end == std::string::npos)
-    return A.substr(begin);
-  return A.substr(begin, end - begin);
-}
-
-static std::string extract1(const std::string &A,
-                           std::string::size_type begin,
-                           std::string::size_type end) {
-  if (begin == std::string::npos || begin == end)
-    return "";
-  return extract(A, begin + 1, end);
-}
-
-std::string Triple::getArchName() const {
-  std::string Tmp = Data;
-  return extract(Tmp, 0, Tmp.find('-'));
+StringRef Triple::getArchName() const {
+  return StringRef(Data).split('-').first;           // Isolate first component
 }
 
-std::string Triple::getVendorName() const {
-  std::string Tmp = Data;
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  return extract(Tmp, 0, Tmp.find('-'));
+StringRef Triple::getVendorName() const {
+  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
+  return Tmp.split('-').first;                       // Isolate second component
 }
 
-std::string Triple::getOSName() const {
-  std::string Tmp = Data;
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  return extract(Tmp, 0, Tmp.find('-'));
+StringRef Triple::getOSName() const {
+  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
+  Tmp = Tmp.split('-').second;                       // Strip second component
+  return Tmp.split('-').first;                       // Isolate third component
 }
 
-std::string Triple::getEnvironmentName() const {
-  std::string Tmp = Data;
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  return extract(Tmp, 0, std::string::npos);
+StringRef Triple::getEnvironmentName() const {
+  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
+  Tmp = Tmp.split('-').second;                       // Strip second component
+  return Tmp.split('-').second;                      // Strip third component
 }
 
-std::string Triple::getOSAndEnvironmentName() const {
-  std::string Tmp = Data;
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
-  return extract(Tmp, 0, std::string::npos);
+StringRef Triple::getOSAndEnvironmentName() const {
+  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
+  return Tmp.split('-').second;                      // Strip second component
 }
 
-void Triple::setTriple(const std::string &Str) {
-  Data = Str;
+void Triple::setTriple(const Twine &Str) {
+  Data = Str.str();
   Arch = InvalidArch;
 }
 
@@ -170,15 +147,15 @@
   setOSName(getOSTypeName(Kind));
 }
 
-void Triple::setArchName(const std::string &Str) {
+void Triple::setArchName(const StringRef &Str) {
   setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
 }
 
-void Triple::setVendorName(const std::string &Str) {
+void Triple::setVendorName(const StringRef &Str) {
   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
 }
 
-void Triple::setOSName(const std::string &Str) {
+void Triple::setOSName(const StringRef &Str) {
   if (hasEnvironment())
     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
               "-" + getEnvironmentName());
@@ -186,11 +163,11 @@
     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
 }
 
-void Triple::setEnvironmentName(const std::string &Str) {
+void Triple::setEnvironmentName(const StringRef &Str) {
   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 
             "-" + Str);
 }
 
-void Triple::setOSAndEnvironmentName(const std::string &Str) {
+void Triple::setOSAndEnvironmentName(const StringRef &Str) {
   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
 }

Modified: llvm/trunk/unittests/ADT/TripleTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/TripleTest.cpp?rev=77119&r1=77118&r2=77119&view=diff

==============================================================================
--- llvm/trunk/unittests/ADT/TripleTest.cpp (original)
+++ llvm/trunk/unittests/ADT/TripleTest.cpp Sat Jul 25 22:31:47 2009
@@ -18,58 +18,58 @@
   Triple T;
 
   T = Triple("");
-  EXPECT_EQ("", T.getArchName());
-  EXPECT_EQ("", T.getVendorName());
-  EXPECT_EQ("", T.getOSName());
-  EXPECT_EQ("", T.getEnvironmentName());
+  EXPECT_EQ("", T.getArchName().str());
+  EXPECT_EQ("", T.getVendorName().str());
+  EXPECT_EQ("", T.getOSName().str());
+  EXPECT_EQ("", T.getEnvironmentName().str());
 
   T = Triple("-");
-  EXPECT_EQ("", T.getArchName());
-  EXPECT_EQ("", T.getVendorName());
-  EXPECT_EQ("", T.getOSName());
-  EXPECT_EQ("", T.getEnvironmentName());
+  EXPECT_EQ("", T.getArchName().str());
+  EXPECT_EQ("", T.getVendorName().str());
+  EXPECT_EQ("", T.getOSName().str());
+  EXPECT_EQ("", T.getEnvironmentName().str());
 
   T = Triple("--");
-  EXPECT_EQ("", T.getArchName());
-  EXPECT_EQ("", T.getVendorName());
-  EXPECT_EQ("", T.getOSName());
-  EXPECT_EQ("", T.getEnvironmentName());
+  EXPECT_EQ("", T.getArchName().str());
+  EXPECT_EQ("", T.getVendorName().str());
+  EXPECT_EQ("", T.getOSName().str());
+  EXPECT_EQ("", T.getEnvironmentName().str());
 
   T = Triple("---");
-  EXPECT_EQ("", T.getArchName());
-  EXPECT_EQ("", T.getVendorName());
-  EXPECT_EQ("", T.getOSName());
-  EXPECT_EQ("", T.getEnvironmentName());
+  EXPECT_EQ("", T.getArchName().str());
+  EXPECT_EQ("", T.getVendorName().str());
+  EXPECT_EQ("", T.getOSName().str());
+  EXPECT_EQ("", T.getEnvironmentName().str());
 
   T = Triple("----");
-  EXPECT_EQ("", T.getArchName());
-  EXPECT_EQ("", T.getVendorName());
-  EXPECT_EQ("", T.getOSName());
-  EXPECT_EQ("-", T.getEnvironmentName());
+  EXPECT_EQ("", T.getArchName().str());
+  EXPECT_EQ("", T.getVendorName().str());
+  EXPECT_EQ("", T.getOSName().str());
+  EXPECT_EQ("-", T.getEnvironmentName().str());
 
   T = Triple("a");
-  EXPECT_EQ("a", T.getArchName());
-  EXPECT_EQ("", T.getVendorName());
-  EXPECT_EQ("", T.getOSName());
-  EXPECT_EQ("", T.getEnvironmentName());
+  EXPECT_EQ("a", T.getArchName().str());
+  EXPECT_EQ("", T.getVendorName().str());
+  EXPECT_EQ("", T.getOSName().str());
+  EXPECT_EQ("", T.getEnvironmentName().str());
 
   T = Triple("a-b");
-  EXPECT_EQ("a", T.getArchName());
-  EXPECT_EQ("b", T.getVendorName());
-  EXPECT_EQ("", T.getOSName());
-  EXPECT_EQ("", T.getEnvironmentName());
+  EXPECT_EQ("a", T.getArchName().str());
+  EXPECT_EQ("b", T.getVendorName().str());
+  EXPECT_EQ("", T.getOSName().str());
+  EXPECT_EQ("", T.getEnvironmentName().str());
 
   T = Triple("a-b-c");
-  EXPECT_EQ("a", T.getArchName());
-  EXPECT_EQ("b", T.getVendorName());
-  EXPECT_EQ("c", T.getOSName());
-  EXPECT_EQ("", T.getEnvironmentName());
+  EXPECT_EQ("a", T.getArchName().str());
+  EXPECT_EQ("b", T.getVendorName().str());
+  EXPECT_EQ("c", T.getOSName().str());
+  EXPECT_EQ("", T.getEnvironmentName().str());
 
   T = Triple("a-b-c-d");
-  EXPECT_EQ("a", T.getArchName());
-  EXPECT_EQ("b", T.getVendorName());
-  EXPECT_EQ("c", T.getOSName());
-  EXPECT_EQ("d", T.getEnvironmentName());
+  EXPECT_EQ("a", T.getArchName().str());
+  EXPECT_EQ("b", T.getVendorName().str());
+  EXPECT_EQ("c", T.getOSName().str());
+  EXPECT_EQ("d", T.getEnvironmentName().str());
 }
 
 TEST(TripleTest, ParsedIDs) {





More information about the llvm-commits mailing list