[PATCH] D32833: [Triple] Add method for triple canonicalization

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 3 15:29:47 PDT 2017


phosek created this revision.

Today, even with normalization it's still possible to get different string representation for the same triple. Specifically, using the short form <arch>-<os> after normalization returns <arch>--<os> (even though the vendor is correctly recognized as unknown) while <arch>-unknown-<os> gives <arch>-unknown-<os>. There should be a way to produce a single canonical triple variant independent of the input.

This is especially desirable in the Clang driver where triple may be used to specify path on the filesystem for a particular target.


Repository:
  rL LLVM

https://reviews.llvm.org/D32833

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


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());
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: 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
   /// @{
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32833.97735.patch
Type: text/x-patch
Size: 2302 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170503/5afe09ef/attachment.bin>


More information about the llvm-commits mailing list