Index: include/llvm/ADT/Triple.h =================================================================== --- include/llvm/ADT/Triple.h +++ include/llvm/ADT/Triple.h @@ -266,6 +266,15 @@ std::string normalize() const { return normalize(Data); } /// @} + /// @name Canonicalization + /// @} + + /// canonicalize - Return the canonical triple form, that is a form where + /// each component uses the spelling as defined by Triple rather then passed + /// externally. + std::string canonicalize() const; + + /// @} /// @name Typed Component Access /// @{ Index: lib/Support/Triple.cpp =================================================================== --- lib/Support/Triple.cpp +++ lib/Support/Triple.cpp @@ -906,6 +906,20 @@ return Normalized; } +std::string Triple::canonicalize() const { + SmallString<64> Triple; + Triple += getArchTypeName(Arch); + Triple += "-"; + Triple += getVendorTypeName(Vendor); + Triple += "-"; + Triple += getOSTypeName(OS); + if (Environment != Triple::UnknownEnvironment) { + Triple += "-"; + Triple += getEnvironmentTypeName(Environment); + } + return Triple.str(); +} + StringRef Triple::getArchName() const { return StringRef(Data).split('-').first; // Isolate first component } Index: unittests/ADT/TripleTest.cpp =================================================================== --- unittests/ADT/TripleTest.cpp +++ unittests/ADT/TripleTest.cpp @@ -424,6 +424,24 @@ EXPECT_EQ("arm-none--eabi", Triple::normalize("arm-none-eabi")); // arm-none-eabi } +TEST(TripleTest, Canonicalization) { + Triple T; + + T.setTriple(Triple::normalize("x86_64-linux-gnu")); + EXPECT_EQ(Triple::x86_64, T.getArch()); + EXPECT_EQ(Triple::UnknownVendor, T.getVendor()); + EXPECT_EQ(Triple::Linux, T.getOS()); + EXPECT_EQ(Triple::GNU, T.getEnvironment()); + EXPECT_EQ("x86_64-unknown-linux-gnu", T.canonicalize()); + + T.setTriple(Triple::normalize("i386-freebsd")); + EXPECT_EQ(Triple::x86, T.getArch()); + EXPECT_EQ(Triple::UnknownVendor, T.getVendor()); + EXPECT_EQ(Triple::FreeBSD, T.getOS()); + EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment()); + EXPECT_EQ("i386-unknown-freebsd", T.canonicalize()); +} + TEST(TripleTest, MutateName) { Triple T; EXPECT_EQ(Triple::UnknownArch, T.getArch());