Skip to content

Commit 7cf98d0

Browse files
committed
Add a new Triple::clone() method
1 parent f8df240 commit 7cf98d0

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,27 @@ class Triple {
463463

464464
const std::string &str() const { return Data; }
465465

466+
/// Return the triple string but only keep the first \p N components.
467+
///
468+
/// The returned string will preserve the first \p N components exactly the
469+
/// same as the original (including the leading "-" and the value, empty or
470+
/// not).
471+
///
472+
/// E.g. Triple("arm64-apple-ios").str(5) == "arm64-apple-ios"
473+
/// E.g. Triple("arm64-apple-ios--").str(5) == "arm64-apple-ios--"
474+
/// E.g. Triple("arm64-apple-ios--").str(4) == "arm64-apple-ios-"
475+
/// E.g. Triple("arm64-apple-ios--").str(3) == "arm64-apple-ios"
476+
/// E.g. Triple("arm64-apple-ios--").str(2) == "arm64-apple"
477+
/// E.g. Triple("arm64-apple-ios--").str(1) == "arm64"
478+
/// E.g. Triple("arm64-apple-ios--").str(0) == ""
479+
///
480+
/// This method does not normalize any triple strings. Clients that need to
481+
/// handle the non-canonical triples that users often specify should use the
482+
/// normalize method.
483+
///
484+
/// \returns the (shorterned) triple string.
485+
StringRef str(size_t N) const;
486+
466487
const std::string &getTriple() const { return Data; }
467488

468489
/// Whether the triple is empty / default constructed.

llvm/lib/TargetParser/Triple.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,30 @@ std::string Triple::merge(const Triple &Other) const {
21072107
return Other.str();
21082108
}
21092109

2110+
StringRef Triple::str(size_t N) const {
2111+
// If empty, return empty
2112+
if (N == 0 || Data == "")
2113+
return "";
2114+
2115+
// If keeping all components, return a full clone
2116+
if (N >= 5)
2117+
return Data;
2118+
2119+
// Find the N-th separator (which is after the N'th component)
2120+
size_t p = StringRef::npos;
2121+
for (uint32_t i = 0; i < N; ++i) {
2122+
p = Data.find('-', p + 1);
2123+
if (p == StringRef::npos)
2124+
break;
2125+
}
2126+
2127+
// Create a triple
2128+
if (p == StringRef::npos)
2129+
return Data;
2130+
else
2131+
return StringRef(Data).substr(0, p);
2132+
}
2133+
21102134
bool Triple::isMacOSXVersionLT(unsigned Major, unsigned Minor,
21112135
unsigned Micro) const {
21122136
assert(isMacOSX() && "Not an OS X triple!");

llvm/unittests/TargetParser/TripleTest.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,4 +2926,97 @@ TEST(TripleTest, isCompatibleWith) {
29262926
EXPECT_TRUE(DoTest(C.B, C.A, C.Result));
29272927
}
29282928
}
2929+
2930+
TEST(TripleTest, StrFirstN) {
2931+
// Empty triple
2932+
{
2933+
llvm::Triple triple;
2934+
ASSERT_EQ(triple.str(), "");
2935+
ASSERT_EQ(triple.str(5), "");
2936+
}
2937+
2938+
// Normal triple with 3 components
2939+
{
2940+
llvm::Triple triple("arm64-apple-ios");
2941+
ASSERT_EQ(triple.str(), "arm64-apple-ios");
2942+
ASSERT_EQ(triple.str(5), "arm64-apple-ios");
2943+
ASSERT_EQ(triple.str(4), "arm64-apple-ios");
2944+
ASSERT_EQ(triple.str(3), "arm64-apple-ios");
2945+
ASSERT_EQ(triple.str(2), "arm64-apple");
2946+
ASSERT_EQ(triple.str(1), "arm64");
2947+
ASSERT_EQ(triple.str(0), "");
2948+
}
2949+
2950+
// Normal triple with 4 components
2951+
{
2952+
llvm::Triple triple("arm64-apple-ios-simulator");
2953+
ASSERT_EQ(triple.str(), "arm64-apple-ios-simulator");
2954+
ASSERT_EQ(triple.str(5), "arm64-apple-ios-simulator");
2955+
ASSERT_EQ(triple.str(4), "arm64-apple-ios-simulator");
2956+
ASSERT_EQ(triple.str(3), "arm64-apple-ios");
2957+
ASSERT_EQ(triple.str(2), "arm64-apple");
2958+
ASSERT_EQ(triple.str(1), "arm64");
2959+
ASSERT_EQ(triple.str(0), "");
2960+
}
2961+
2962+
// Normal triple with 5 components
2963+
{
2964+
llvm::Triple triple("arm64-apple-ios-simulator-macho");
2965+
ASSERT_EQ(triple.str(), "arm64-apple-ios-simulator-macho");
2966+
ASSERT_EQ(triple.str(5), "arm64-apple-ios-simulator-macho");
2967+
ASSERT_EQ(triple.str(4), "arm64-apple-ios-simulator");
2968+
ASSERT_EQ(triple.str(3), "arm64-apple-ios");
2969+
ASSERT_EQ(triple.str(2), "arm64-apple");
2970+
ASSERT_EQ(triple.str(1), "arm64");
2971+
ASSERT_EQ(triple.str(0), "");
2972+
}
2973+
2974+
// Empty vendor and os
2975+
{
2976+
llvm::Triple triple("arm64---simulator-macho");
2977+
ASSERT_EQ(triple.str(), "arm64---simulator-macho");
2978+
ASSERT_EQ(triple.str(5), "arm64---simulator-macho");
2979+
ASSERT_EQ(triple.str(4), "arm64---simulator");
2980+
ASSERT_EQ(triple.str(3), "arm64--");
2981+
ASSERT_EQ(triple.str(2), "arm64-");
2982+
ASSERT_EQ(triple.str(1), "arm64");
2983+
ASSERT_EQ(triple.str(0), "");
2984+
}
2985+
2986+
// Empty environment
2987+
{
2988+
llvm::Triple triple("arm64-apple-ios-");
2989+
ASSERT_EQ(triple.str(), "arm64-apple-ios-");
2990+
ASSERT_EQ(triple.str(5), "arm64-apple-ios-");
2991+
ASSERT_EQ(triple.str(4), "arm64-apple-ios-");
2992+
ASSERT_EQ(triple.str(3), "arm64-apple-ios");
2993+
ASSERT_EQ(triple.str(2), "arm64-apple");
2994+
ASSERT_EQ(triple.str(1), "arm64");
2995+
ASSERT_EQ(triple.str(0), "");
2996+
}
2997+
2998+
// Empty object format
2999+
{
3000+
llvm::Triple triple("arm64-apple-ios-simulator-");
3001+
ASSERT_EQ(triple.str(), "arm64-apple-ios-simulator-");
3002+
ASSERT_EQ(triple.str(5), "arm64-apple-ios-simulator-");
3003+
ASSERT_EQ(triple.str(4), "arm64-apple-ios-simulator");
3004+
ASSERT_EQ(triple.str(3), "arm64-apple-ios");
3005+
ASSERT_EQ(triple.str(2), "arm64-apple");
3006+
ASSERT_EQ(triple.str(1), "arm64");
3007+
ASSERT_EQ(triple.str(0), "");
3008+
}
3009+
3010+
// Empty environment, but has object format
3011+
{
3012+
llvm::Triple triple("arm64----macho");
3013+
ASSERT_EQ(triple.str(), "arm64----macho");
3014+
ASSERT_EQ(triple.str(5), "arm64----macho");
3015+
ASSERT_EQ(triple.str(4), "arm64---");
3016+
ASSERT_EQ(triple.str(3), "arm64--");
3017+
ASSERT_EQ(triple.str(2), "arm64-");
3018+
ASSERT_EQ(triple.str(1), "arm64");
3019+
ASSERT_EQ(triple.str(0), "");
3020+
}
3021+
}
29293022
} // end anonymous namespace

0 commit comments

Comments
 (0)