[llvm] r340702 - Replace fancy use of initializer lists with simple functions that return

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 26 03:03:09 PDT 2018


Author: chandlerc
Date: Sun Aug 26 03:03:08 2018
New Revision: 340702

URL: http://llvm.org/viewvc/llvm-project?rev=340702&view=rev
Log:
Replace fancy use of initializer lists with simple functions that return
vectors, and move this test code into an anonymous namespace.

Hoping that this will avoid hitting an MSVC bug that causes it to crash
and burn pretty spectacularly. Also, this degree of clever use of
initializer lists seems somewhat questionable in general. ;]

Modified:
    llvm/trunk/unittests/Support/ItaniumManglingCanonicalizerTest.cpp

Modified: llvm/trunk/unittests/Support/ItaniumManglingCanonicalizerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ItaniumManglingCanonicalizerTest.cpp?rev=340702&r1=340701&r2=340702&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ItaniumManglingCanonicalizerTest.cpp (original)
+++ llvm/trunk/unittests/Support/ItaniumManglingCanonicalizerTest.cpp Sun Aug 26 03:03:08 2018
@@ -8,11 +8,17 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/ItaniumManglingCanonicalizer.h"
-
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringRef.h"
 #include "gtest/gtest.h"
 
 #include <cstdlib>
 #include <map>
+#include <vector>
+
+using namespace llvm;
+
+namespace {
 
 using EquivalenceError = llvm::ItaniumManglingCanonicalizer::EquivalenceError;
 using FragmentKind = llvm::ItaniumManglingCanonicalizer::FragmentKind;
@@ -24,234 +30,240 @@ struct Equivalence {
 };
 
 // A set of manglings that should all be considered equivalent.
-using EquivalenceClass = std::initializer_list<llvm::StringRef>;
+using EquivalenceClass = std::vector<llvm::StringRef>;
 
 struct Testcase {
   // A set of equivalences to register.
-  std::initializer_list<Equivalence> Equivalences;
+  std::vector<Equivalence> Equivalences;
   // A set of distinct equivalence classes created by registering the
   // equivalences.
-  std::initializer_list<EquivalenceClass> Classes;
+  std::vector<EquivalenceClass> Classes;
 };
 
-static std::initializer_list<Testcase> Testcases = {
-  // Three different manglings for std::string (old libstdc++, new libstdc++,
-  // libc++).
-  {
-    {
-      {FragmentKind::Type, "Ss",
-       "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE"},
-      {FragmentKind::Type, "Ss",
-       "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
+// A function that returns a set of test cases.
+static std::vector<Testcase> getTestcases() {
+  return {
+    // Three different manglings for std::string (old libstdc++, new libstdc++,
+    // libc++).
+    {
+      {
+        {FragmentKind::Type, "Ss",
+         "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE"},
+        {FragmentKind::Type, "Ss",
+         "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
+      },
+      {
+        {"_Z1fv"},
+        {"_Z1fSs",
+         "_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
+         "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
+        {"_ZNKSs4sizeEv",
+         "_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv",
+         "_ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4sizeEv"},
+      }
     },
-    {
-      {"_Z1fv"},
-      {"_Z1fSs",
-       "_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
-       "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
-      {"_ZNKSs4sizeEv",
-       "_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv",
-       "_ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4sizeEv"},
-    }
-  },
 
-  // Check that substitutions are properly handled.
-  {
+    // Check that substitutions are properly handled.
     {
-      // ::X <-> ::N::X<int>
-      {FragmentKind::Type, "1X", "N1N1XIiEE"},
-      // ::T<T<int, int>, T<int, int>> <-> T<int>
-      {FragmentKind::Type, "1TIS_IiiES0_E", "1TIiE"},
-      // A::B::foo <-> AB::foo
-      {FragmentKind::Name, "N1A1B3fooE", "N2AB3fooE"},
+      {
+        // ::X <-> ::N::X<int>
+        {FragmentKind::Type, "1X", "N1N1XIiEE"},
+        // ::T<T<int, int>, T<int, int>> <-> T<int>
+        {FragmentKind::Type, "1TIS_IiiES0_E", "1TIiE"},
+        // A::B::foo <-> AB::foo
+        {FragmentKind::Name, "N1A1B3fooE", "N2AB3fooE"},
+      },
+      {
+        {"_Z1f1XPS_RS_", "_Z1fN1N1XIiEEPS1_RS1_"},
+        {"_ZN1A1B3fooE1TIS1_IiiES2_EPS3_RS3_", "_ZN2AB3fooE1TIiEPS1_RS1_"},
+      }
     },
-    {
-      {"_Z1f1XPS_RS_", "_Z1fN1N1XIiEEPS1_RS1_"},
-      {"_ZN1A1B3fooE1TIS1_IiiES2_EPS3_RS3_", "_ZN2AB3fooE1TIiEPS1_RS1_"},
-    }
-  },
 
-  // Check that nested equivalences are properly handled.
-  {
+    // Check that nested equivalences are properly handled.
     {
-      // std::__1::char_traits == std::__cxx11::char_traits
-      // (Note that this is unused and should make no difference,
-      // but it should not cause us to fail to match up the cases
-      // below.)
-      {FragmentKind::Name,
-       "NSt3__111char_traitsE",
-       "NSt7__cxx1111char_traitsE"},
-      // std::__1::allocator == std::allocator
-      {FragmentKind::Name,
-       "NSt3__19allocatorE",
-       "Sa"}, // "Sa" is not strictly a <name> but we accept it as one.
-      // std::__1::vector == std::vector
-      {FragmentKind::Name,
-       "St6vector",
-       "NSt3__16vectorE"},
-      // std::__1::basic_string<
-      //   char
-      //   std::__1::char_traits<char>,
-      //   std::__1::allocator<char>> ==
-      // std::__cxx11::basic_string<
-      //   char,
-      //   std::char_traits<char>,
-      //   std::allocator<char>>
-      {FragmentKind::Type,
-       "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
-       "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
-      // X<A> <-> X<B>
-      {FragmentKind::Type, "1XI1AE", "1XI1BE"},
-      // X <-> Y
-      {FragmentKind::Name, "1X", "1Y"},
+      {
+        // std::__1::char_traits == std::__cxx11::char_traits
+        // (Note that this is unused and should make no difference,
+        // but it should not cause us to fail to match up the cases
+        // below.)
+        {FragmentKind::Name,
+         "NSt3__111char_traitsE",
+         "NSt7__cxx1111char_traitsE"},
+        // std::__1::allocator == std::allocator
+        {FragmentKind::Name,
+         "NSt3__19allocatorE",
+         "Sa"}, // "Sa" is not strictly a <name> but we accept it as one.
+        // std::__1::vector == std::vector
+        {FragmentKind::Name,
+         "St6vector",
+         "NSt3__16vectorE"},
+        // std::__1::basic_string<
+        //   char
+        //   std::__1::char_traits<char>,
+        //   std::__1::allocator<char>> ==
+        // std::__cxx11::basic_string<
+        //   char,
+        //   std::char_traits<char>,
+        //   std::allocator<char>>
+        {FragmentKind::Type,
+         "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
+         "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
+        // X<A> <-> X<B>
+        {FragmentKind::Type, "1XI1AE", "1XI1BE"},
+        // X <-> Y
+        {FragmentKind::Name, "1X", "1Y"},
+      },
+      {
+        // f(std::string)
+        {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
+         "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
+        // f(std::vector<int>)
+        {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
+        // f(X<A>), f(X<B>), f(Y<A>), f(Y<B>)
+        {"_Z1f1XI1AE", "_Z1f1XI1BE", "_Z1f1YI1AE", "_Z1f1YI1BE"},
+        // f(X<C>), f(Y<C>)
+        {"_Z1f1XI1CE", "_Z1f1YI1CE"},
+      }
     },
-    {
-      // f(std::string)
-      {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
-       "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
-      // f(std::vector<int>)
-      {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
-      // f(X<A>), f(X<B>), f(Y<A>), f(Y<B>)
-      {"_Z1f1XI1AE", "_Z1f1XI1BE", "_Z1f1YI1AE", "_Z1f1YI1BE"},
-      // f(X<C>), f(Y<C>)
-      {"_Z1f1XI1CE", "_Z1f1YI1CE"},
-    }
-  },
 
-  // Check namespace equivalences.
-  {
+    // Check namespace equivalences.
     {
-      // std::__1 == std::__cxx11
-      {FragmentKind::Name, "St3__1", "St7__cxx11"},
-      // std::__1::allocator == std::allocator
-      {FragmentKind::Name, "NSt3__19allocatorE", "Sa"},
-      // std::vector == std::__1::vector
-      {FragmentKind::Name, "St6vector", "NSt3__16vectorE"},
-      // std::__cxx11::char_traits == std::char_traits
-      // (This indirectly means that std::__1::char_traits == std::char_traits,
-      // due to the std::__cxx11 == std::__1 equivalence, which is what we rely
-      // on below.)
-      {FragmentKind::Name, "NSt7__cxx1111char_traitsE", "St11char_traits"},
+      {
+        // std::__1 == std::__cxx11
+        {FragmentKind::Name, "St3__1", "St7__cxx11"},
+        // std::__1::allocator == std::allocator
+        {FragmentKind::Name, "NSt3__19allocatorE", "Sa"},
+        // std::vector == std::__1::vector
+        {FragmentKind::Name, "St6vector", "NSt3__16vectorE"},
+        // std::__cxx11::char_traits == std::char_traits
+        // (This indirectly means that std::__1::char_traits == std::char_traits,
+        // due to the std::__cxx11 == std::__1 equivalence, which is what we rely
+        // on below.)
+        {FragmentKind::Name, "NSt7__cxx1111char_traitsE", "St11char_traits"},
+      },
+      {
+        // f(std::foo)
+        {"_Z1fNSt7__cxx113fooE",
+         "_Z1fNSt3__13fooE"},
+        // f(std::string)
+        {"_Z1fNSt7__cxx1111char_traitsIcEE",
+         "_Z1fNSt3__111char_traitsIcEE",
+         "_Z1fSt11char_traitsIcE"},
+        // f(std::string)
+        {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
+         "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
+        // f(std::vector<int>)
+        {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
+      }
     },
-    {
-      // f(std::foo)
-      {"_Z1fNSt7__cxx113fooE",
-       "_Z1fNSt3__13fooE"},
-      // f(std::string)
-      {"_Z1fNSt7__cxx1111char_traitsIcEE",
-       "_Z1fNSt3__111char_traitsIcEE",
-       "_Z1fSt11char_traitsIcE"},
-      // f(std::string)
-      {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
-       "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
-      // f(std::vector<int>)
-      {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
-    }
-  },
 
-  // Check namespace equivalences for namespace 'std'. We support using 'St'
-  // for this, despite it not technically being a <name>.
-  {
+    // Check namespace equivalences for namespace 'std'. We support using 'St'
+    // for this, despite it not technically being a <name>.
     {
-      // std::__1 == std
-      {FragmentKind::Name, "St3__1", "St"},
-      // std::__1 == std::__cxx11
-      {FragmentKind::Name, "St3__1", "St7__cxx11"},
-      // FIXME: Should a 'std' equivalence also cover the predefined
-      // substitutions?
-      // std::__1::allocator == std::allocator
-      {FragmentKind::Name, "NSt3__19allocatorE", "Sa"},
+      {
+        // std::__1 == std
+        {FragmentKind::Name, "St3__1", "St"},
+        // std::__1 == std::__cxx11
+        {FragmentKind::Name, "St3__1", "St7__cxx11"},
+        // FIXME: Should a 'std' equivalence also cover the predefined
+        // substitutions?
+        // std::__1::allocator == std::allocator
+        {FragmentKind::Name, "NSt3__19allocatorE", "Sa"},
+      },
+      {
+        {"_Z1fSt3foo", "_Z1fNSt3__13fooE", "_Z1fNSt7__cxx113fooE"},
+        {"_Z1fNSt3bar3bazE", "_Z1fNSt3__13bar3bazE"},
+        // f(std::string)
+        {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
+         "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
+        // f(std::vector<int>)
+        {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
+      }
     },
-    {
-      {"_Z1fSt3foo", "_Z1fNSt3__13fooE", "_Z1fNSt7__cxx113fooE"},
-      {"_Z1fNSt3bar3bazE", "_Z1fNSt3__13bar3bazE"},
-      // f(std::string)
-      {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
-       "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
-      // f(std::vector<int>)
-      {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
-    }
-  },
 
-  // Check mutually-recursive equivalences.
-  {
+    // Check mutually-recursive equivalences.
     {
-      {FragmentKind::Type, "1A", "1B"},
-      {FragmentKind::Type, "1A", "1C"},
-      {FragmentKind::Type, "1D", "1B"},
-      {FragmentKind::Type, "1C", "1E"},
+      {
+        {FragmentKind::Type, "1A", "1B"},
+        {FragmentKind::Type, "1A", "1C"},
+        {FragmentKind::Type, "1D", "1B"},
+        {FragmentKind::Type, "1C", "1E"},
+      },
+      {
+        {"_Z1f1A", "_Z1f1B", "_Z1f1C", "_Z1f1D", "_Z1f1E"},
+        {"_Z1f1F"},
+      }
     },
-    {
-      {"_Z1f1A", "_Z1f1B", "_Z1f1C", "_Z1f1D", "_Z1f1E"},
-      {"_Z1f1F"},
-    }
-  },
 
-  // Check <encoding>s.
-  {
+    // Check <encoding>s.
     {
-      {FragmentKind::Encoding, "1fv", "1gv"},
+      {
+        {FragmentKind::Encoding, "1fv", "1gv"},
+      },
+      {
+        // f(void) -> g(void)
+        {"_Z1fv", "_Z1gv"},
+        // static local 'n' in f(void) -> static local 'n' in g(void)
+        {"_ZZ1fvE1n", "_ZZ1gvE1n"},
+      }
     },
-    {
-      // f(void) -> g(void)
-      {"_Z1fv", "_Z1gv"},
-      // static local 'n' in f(void) -> static local 'n' in g(void)
-      {"_ZZ1fvE1n", "_ZZ1gvE1n"},
-    }
-  },
 
-  // Corner case: the substitution can appear within its own expansion.
-  {
+    // Corner case: the substitution can appear within its own expansion.
     {
-      // X <-> Y<X>
-      {FragmentKind::Type, "1X", "1YI1XE"},
-      // A<B> <-> B
-      {FragmentKind::Type, "1AI1BE", "1B"},
+      {
+        // X <-> Y<X>
+        {FragmentKind::Type, "1X", "1YI1XE"},
+        // A<B> <-> B
+        {FragmentKind::Type, "1AI1BE", "1B"},
+      },
+      {
+        // f(X) == f(Y<X>) == f(Y<Y<X>>) == f(Y<Y<Y<X>>>)
+        {"_Z1f1X", "_Z1f1YI1XE", "_Z1f1YIS_I1XEE", "_Z1f1YIS_IS_I1XEEE"},
+        // f(B) == f(A<B>) == f(A<A<B>>) == f(A<A<A<B>>>)
+        {"_Z1f1B", "_Z1f1AI1BE", "_Z1f1AIS_I1BEE", "_Z1f1AIS_IS_I1BEEE"},
+      }
     },
-    {
-      // f(X) == f(Y<X>) == f(Y<Y<X>>) == f(Y<Y<Y<X>>>)
-      {"_Z1f1X", "_Z1f1YI1XE", "_Z1f1YIS_I1XEE", "_Z1f1YIS_IS_I1XEEE"},
-      // f(B) == f(A<B>) == f(A<A<B>>) == f(A<A<A<B>>>)
-      {"_Z1f1B", "_Z1f1AI1BE", "_Z1f1AIS_I1BEE", "_Z1f1AIS_IS_I1BEEE"},
-    }
-  },
 
-  // Redundant equivalences are accepted (and have no effect).
-  {
+    // Redundant equivalences are accepted (and have no effect).
     {
-      {FragmentKind::Name, "3std", "St"},
-      {FragmentKind::Name, "1X", "1Y"},
-      {FragmentKind::Name, "N1X1ZE", "N1Y1ZE"},
+      {
+        {FragmentKind::Name, "3std", "St"},
+        {FragmentKind::Name, "1X", "1Y"},
+        {FragmentKind::Name, "N1X1ZE", "N1Y1ZE"},
+      },
+      {}
     },
-    {}
-  },
-};
+  };
+}
 
-static std::initializer_list<Testcase> ForwardTemplateReferenceTestcases = {
-  // ForwardTemplateReference does not support canonicalization.
-  // FIXME: We should consider ways of fixing this, perhaps by eliminating
-  // the ForwardTemplateReference node with a tree transformation.
-  {
-    {
-      // X::operator T() <with T = A> == Y::operator T() <with T = A>
-      {FragmentKind::Encoding, "N1XcvT_I1AEEv", "N1YcvT_I1AEEv"},
-      // A == B
-      {FragmentKind::Name, "1A", "1B"},
+// A function to get a set of test cases for forward template references.
+static std::vector<Testcase> getForwardTemplateReferenceTestcases() {
+  return {
+    // ForwardTemplateReference does not support canonicalization.
+    // FIXME: We should consider ways of fixing this, perhaps by eliminating
+    // the ForwardTemplateReference node with a tree transformation.
+    {
+      {
+        // X::operator T() <with T = A> == Y::operator T() <with T = A>
+        {FragmentKind::Encoding, "N1XcvT_I1AEEv", "N1YcvT_I1AEEv"},
+        // A == B
+        {FragmentKind::Name, "1A", "1B"},
+      },
+      {
+        // All combinations result in unique equivalence classes.
+        {"_ZN1XcvT_I1AEEv"},
+        {"_ZN1XcvT_I1BEEv"},
+        {"_ZN1YcvT_I1AEEv"},
+        {"_ZN1YcvT_I1BEEv"},
+        // Even giving the same string twice gives a new class.
+        {"_ZN1XcvT_I1AEEv"},
+      }
     },
-    {
-      // All combinations result in unique equivalence classes.
-      {"_ZN1XcvT_I1AEEv"},
-      {"_ZN1XcvT_I1BEEv"},
-      {"_ZN1YcvT_I1AEEv"},
-      {"_ZN1YcvT_I1BEEv"},
-      // Even giving the same string twice gives a new class.
-      {"_ZN1XcvT_I1AEEv"},
-    }
-  },
-};
+  };
+}
 
 template<bool CanonicalizeFirst>
-static void testTestcases(std::initializer_list<Testcase> Testcases) {
+static void testTestcases(ArrayRef<Testcase> Testcases) {
   for (const auto &Testcase : Testcases) {
     llvm::ItaniumManglingCanonicalizer Canonicalizer;
     for (const auto &Equiv : Testcase.Equivalences) {
@@ -293,17 +305,17 @@ static void testTestcases(std::initializ
 }
 
 TEST(ItaniumManglingCanonicalizerTest, TestCanonicalize) {
-  testTestcases<false>(Testcases);
+  testTestcases<false>(getTestcases());
 }
 
 TEST(ItaniumManglingCanonicalizerTest, TestLookup) {
-  testTestcases<true>(Testcases);
+  testTestcases<true>(getTestcases());
 }
 
 TEST(ItaniumManglingCanonicalizerTest, TestForwardTemplateReference) {
   // lookup(...) after canonicalization (intentionally) returns different
   // values for this testcase.
-  testTestcases<false>(ForwardTemplateReferenceTestcases);
+  testTestcases<false>(getForwardTemplateReferenceTestcases());
 }
 
 
@@ -344,3 +356,5 @@ TEST(ItaniumManglingCanonicalizerTest, T
   EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "1B", "1D"),
             EquivalenceError::ManglingAlreadyUsed);
 }
+
+} // end anonymous namespace




More information about the llvm-commits mailing list