diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-07-26 03:31:47 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-07-26 03:31:47 +0000 |
commit | a14d225ef4d3d8bd8ec9ec913246d1fb47d2edc1 (patch) | |
tree | 0490ef0bdb198deebd60acfcf8a7c5cae828d38d | |
parent | d61918fc6898a89df8b0a03e068f234ded010cdf (diff) | |
download | external_llvm-a14d225ef4d3d8bd8ec9ec913246d1fb47d2edc1.zip external_llvm-a14d225ef4d3d8bd8ec9ec913246d1fb47d2edc1.tar.gz external_llvm-a14d225ef4d3d8bd8ec9ec913246d1fb47d2edc1.tar.bz2 |
Update Triple to use StringRef/Twine based APIs.
- This is now shorter, simpler, safer, and more efficient, what a deal.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77119 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/ADT/Triple.h | 28 | ||||
-rw-r--r-- | lib/Support/Triple.cpp | 93 | ||||
-rw-r--r-- | unittests/ADT/TripleTest.cpp | 72 |
3 files changed, 85 insertions, 108 deletions
diff --git a/include/llvm/ADT/Triple.h b/include/llvm/ADT/Triple.h index 2366dfe..5476d82 100644 --- a/include/llvm/ADT/Triple.h +++ b/include/llvm/ADT/Triple.h @@ -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 @@ public: 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 @@ public: 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. diff --git a/lib/Support/Triple.cpp b/lib/Support/Triple.cpp index db2b300..5231e8b 100644 --- a/lib/Support/Triple.cpp +++ b/lib/Support/Triple.cpp @@ -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 @@ const char *Triple::getOSTypeName(OSType Kind) { 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 @@ void Triple::Parse() const { else Arch = UnknownArch; - std::string VendorName = getVendorName(); + StringRef VendorName = getVendorName(); if (VendorName == "apple") Vendor = Apple; else if (VendorName == "pc") @@ -81,20 +83,20 @@ void Triple::Parse() const { 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 @@ void Triple::Parse() const { 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 @@ void Triple::setOS(OSType Kind) { 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 @@ void Triple::setOSName(const std::string &Str) { 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); } diff --git a/unittests/ADT/TripleTest.cpp b/unittests/ADT/TripleTest.cpp index dee09f4..cbb2ea4 100644 --- a/unittests/ADT/TripleTest.cpp +++ b/unittests/ADT/TripleTest.cpp @@ -18,58 +18,58 @@ TEST(TripleTest, BasicParsing) { 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) { |