blob: b8a42f64f898b3e946b73c645e8d314265b4034e [file] [log] [blame]
Richard Smith2ae84682018-08-24 22:31:51 +00001//===-------------- ItaniumManglingCanonicalizerTest.cpp ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Richard Smith2ae84682018-08-24 22:31:51 +000010#include "llvm/Support/ItaniumManglingCanonicalizer.h"
Chandler Carruth6eb2b132018-08-26 10:03:08 +000011#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/StringRef.h"
Richard Smith2ae84682018-08-24 22:31:51 +000013#include "gtest/gtest.h"
14
Richard Smith9c2e4f32018-08-24 23:26:05 +000015#include <cstdlib>
16#include <map>
Chandler Carruth6eb2b132018-08-26 10:03:08 +000017#include <vector>
18
19using namespace llvm;
20
21namespace {
Richard Smith9c2e4f32018-08-24 23:26:05 +000022
Richard Smith2ae84682018-08-24 22:31:51 +000023using EquivalenceError = llvm::ItaniumManglingCanonicalizer::EquivalenceError;
24using FragmentKind = llvm::ItaniumManglingCanonicalizer::FragmentKind;
25
26struct Equivalence {
27 FragmentKind Kind;
28 llvm::StringRef First;
29 llvm::StringRef Second;
30};
31
32// A set of manglings that should all be considered equivalent.
Chandler Carruth6eb2b132018-08-26 10:03:08 +000033using EquivalenceClass = std::vector<llvm::StringRef>;
Richard Smith2ae84682018-08-24 22:31:51 +000034
35struct Testcase {
36 // A set of equivalences to register.
Chandler Carruth6eb2b132018-08-26 10:03:08 +000037 std::vector<Equivalence> Equivalences;
Richard Smith2ae84682018-08-24 22:31:51 +000038 // A set of distinct equivalence classes created by registering the
39 // equivalences.
Chandler Carruth6eb2b132018-08-26 10:03:08 +000040 std::vector<EquivalenceClass> Classes;
Richard Smith2ae84682018-08-24 22:31:51 +000041};
42
Chandler Carruth6eb2b132018-08-26 10:03:08 +000043// A function that returns a set of test cases.
44static std::vector<Testcase> getTestcases() {
45 return {
46 // Three different manglings for std::string (old libstdc++, new libstdc++,
47 // libc++).
Richard Smith2ae84682018-08-24 22:31:51 +000048 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +000049 {
50 {FragmentKind::Type, "Ss",
51 "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE"},
52 {FragmentKind::Type, "Ss",
53 "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
54 },
55 {
56 {"_Z1fv"},
57 {"_Z1fSs",
58 "_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
59 "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
60 {"_ZNKSs4sizeEv",
61 "_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv",
62 "_ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4sizeEv"},
63 }
Richard Smith2ae84682018-08-24 22:31:51 +000064 },
Richard Smith2ae84682018-08-24 22:31:51 +000065
Chandler Carruth6eb2b132018-08-26 10:03:08 +000066 // Check that substitutions are properly handled.
Richard Smith2ae84682018-08-24 22:31:51 +000067 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +000068 {
69 // ::X <-> ::N::X<int>
70 {FragmentKind::Type, "1X", "N1N1XIiEE"},
71 // ::T<T<int, int>, T<int, int>> <-> T<int>
72 {FragmentKind::Type, "1TIS_IiiES0_E", "1TIiE"},
73 // A::B::foo <-> AB::foo
74 {FragmentKind::Name, "N1A1B3fooE", "N2AB3fooE"},
75 },
76 {
77 {"_Z1f1XPS_RS_", "_Z1fN1N1XIiEEPS1_RS1_"},
78 {"_ZN1A1B3fooE1TIS1_IiiES2_EPS3_RS3_", "_ZN2AB3fooE1TIiEPS1_RS1_"},
79 }
Richard Smith2ae84682018-08-24 22:31:51 +000080 },
Richard Smith2ae84682018-08-24 22:31:51 +000081
Chandler Carruth6eb2b132018-08-26 10:03:08 +000082 // Check that nested equivalences are properly handled.
Richard Smith2ae84682018-08-24 22:31:51 +000083 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +000084 {
85 // std::__1::char_traits == std::__cxx11::char_traits
86 // (Note that this is unused and should make no difference,
87 // but it should not cause us to fail to match up the cases
88 // below.)
89 {FragmentKind::Name,
90 "NSt3__111char_traitsE",
91 "NSt7__cxx1111char_traitsE"},
92 // std::__1::allocator == std::allocator
93 {FragmentKind::Name,
94 "NSt3__19allocatorE",
95 "Sa"}, // "Sa" is not strictly a <name> but we accept it as one.
96 // std::__1::vector == std::vector
97 {FragmentKind::Name,
98 "St6vector",
99 "NSt3__16vectorE"},
100 // std::__1::basic_string<
101 // char
102 // std::__1::char_traits<char>,
103 // std::__1::allocator<char>> ==
104 // std::__cxx11::basic_string<
105 // char,
106 // std::char_traits<char>,
107 // std::allocator<char>>
108 {FragmentKind::Type,
109 "NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
110 "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
111 // X<A> <-> X<B>
112 {FragmentKind::Type, "1XI1AE", "1XI1BE"},
113 // X <-> Y
114 {FragmentKind::Name, "1X", "1Y"},
115 },
116 {
117 // f(std::string)
118 {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
119 "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
120 // f(std::vector<int>)
121 {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
122 // f(X<A>), f(X<B>), f(Y<A>), f(Y<B>)
123 {"_Z1f1XI1AE", "_Z1f1XI1BE", "_Z1f1YI1AE", "_Z1f1YI1BE"},
124 // f(X<C>), f(Y<C>)
125 {"_Z1f1XI1CE", "_Z1f1YI1CE"},
126 }
Richard Smith2ae84682018-08-24 22:31:51 +0000127 },
Richard Smith2ae84682018-08-24 22:31:51 +0000128
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000129 // Check namespace equivalences.
Richard Smith2ae84682018-08-24 22:31:51 +0000130 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000131 {
132 // std::__1 == std::__cxx11
133 {FragmentKind::Name, "St3__1", "St7__cxx11"},
134 // std::__1::allocator == std::allocator
135 {FragmentKind::Name, "NSt3__19allocatorE", "Sa"},
136 // std::vector == std::__1::vector
137 {FragmentKind::Name, "St6vector", "NSt3__16vectorE"},
138 // std::__cxx11::char_traits == std::char_traits
139 // (This indirectly means that std::__1::char_traits == std::char_traits,
140 // due to the std::__cxx11 == std::__1 equivalence, which is what we rely
141 // on below.)
142 {FragmentKind::Name, "NSt7__cxx1111char_traitsE", "St11char_traits"},
143 },
144 {
145 // f(std::foo)
146 {"_Z1fNSt7__cxx113fooE",
147 "_Z1fNSt3__13fooE"},
148 // f(std::string)
149 {"_Z1fNSt7__cxx1111char_traitsIcEE",
150 "_Z1fNSt3__111char_traitsIcEE",
151 "_Z1fSt11char_traitsIcE"},
152 // f(std::string)
153 {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
154 "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
155 // f(std::vector<int>)
156 {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
157 }
Richard Smith2ae84682018-08-24 22:31:51 +0000158 },
Richard Smith2ae84682018-08-24 22:31:51 +0000159
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000160 // Check namespace equivalences for namespace 'std'. We support using 'St'
161 // for this, despite it not technically being a <name>.
Richard Smith2ae84682018-08-24 22:31:51 +0000162 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000163 {
164 // std::__1 == std
165 {FragmentKind::Name, "St3__1", "St"},
166 // std::__1 == std::__cxx11
167 {FragmentKind::Name, "St3__1", "St7__cxx11"},
168 // FIXME: Should a 'std' equivalence also cover the predefined
169 // substitutions?
170 // std::__1::allocator == std::allocator
171 {FragmentKind::Name, "NSt3__19allocatorE", "Sa"},
172 },
173 {
174 {"_Z1fSt3foo", "_Z1fNSt3__13fooE", "_Z1fNSt7__cxx113fooE"},
175 {"_Z1fNSt3bar3bazE", "_Z1fNSt3__13bar3bazE"},
176 // f(std::string)
177 {"_Z1fNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE",
178 "_Z1fNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE"},
179 // f(std::vector<int>)
180 {"_Z1fSt6vectorIiSaIiEE", "_Z1fNSt3__16vectorIiNS_9allocatorIiEEEE"},
181 }
Richard Smith2ae84682018-08-24 22:31:51 +0000182 },
Richard Smith2ae84682018-08-24 22:31:51 +0000183
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000184 // Check mutually-recursive equivalences.
Richard Smith2ae84682018-08-24 22:31:51 +0000185 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000186 {
187 {FragmentKind::Type, "1A", "1B"},
188 {FragmentKind::Type, "1A", "1C"},
189 {FragmentKind::Type, "1D", "1B"},
190 {FragmentKind::Type, "1C", "1E"},
191 },
192 {
193 {"_Z1f1A", "_Z1f1B", "_Z1f1C", "_Z1f1D", "_Z1f1E"},
194 {"_Z1f1F"},
195 }
Richard Smith2ae84682018-08-24 22:31:51 +0000196 },
Richard Smith2ae84682018-08-24 22:31:51 +0000197
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000198 // Check <encoding>s.
Richard Smith2ae84682018-08-24 22:31:51 +0000199 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000200 {
201 {FragmentKind::Encoding, "1fv", "1gv"},
202 },
203 {
204 // f(void) -> g(void)
205 {"_Z1fv", "_Z1gv"},
206 // static local 'n' in f(void) -> static local 'n' in g(void)
207 {"_ZZ1fvE1n", "_ZZ1gvE1n"},
208 }
Richard Smith2ae84682018-08-24 22:31:51 +0000209 },
Richard Smith2ae84682018-08-24 22:31:51 +0000210
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000211 // Corner case: the substitution can appear within its own expansion.
Richard Smith2ae84682018-08-24 22:31:51 +0000212 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000213 {
214 // X <-> Y<X>
215 {FragmentKind::Type, "1X", "1YI1XE"},
216 // A<B> <-> B
217 {FragmentKind::Type, "1AI1BE", "1B"},
218 },
219 {
220 // f(X) == f(Y<X>) == f(Y<Y<X>>) == f(Y<Y<Y<X>>>)
221 {"_Z1f1X", "_Z1f1YI1XE", "_Z1f1YIS_I1XEE", "_Z1f1YIS_IS_I1XEEE"},
222 // f(B) == f(A<B>) == f(A<A<B>>) == f(A<A<A<B>>>)
223 {"_Z1f1B", "_Z1f1AI1BE", "_Z1f1AIS_I1BEE", "_Z1f1AIS_IS_I1BEEE"},
224 }
Richard Smith2ae84682018-08-24 22:31:51 +0000225 },
Richard Smith2ae84682018-08-24 22:31:51 +0000226
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000227 // Redundant equivalences are accepted (and have no effect).
Richard Smith2ae84682018-08-24 22:31:51 +0000228 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000229 {
230 {FragmentKind::Name, "3std", "St"},
231 {FragmentKind::Name, "1X", "1Y"},
232 {FragmentKind::Name, "N1X1ZE", "N1Y1ZE"},
233 },
234 {}
Richard Smith2ae84682018-08-24 22:31:51 +0000235 },
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000236 };
237}
Richard Smith2ae84682018-08-24 22:31:51 +0000238
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000239// A function to get a set of test cases for forward template references.
240static std::vector<Testcase> getForwardTemplateReferenceTestcases() {
241 return {
242 // ForwardTemplateReference does not support canonicalization.
243 // FIXME: We should consider ways of fixing this, perhaps by eliminating
244 // the ForwardTemplateReference node with a tree transformation.
Richard Smith2ae84682018-08-24 22:31:51 +0000245 {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000246 {
247 // X::operator T() <with T = A> == Y::operator T() <with T = A>
248 {FragmentKind::Encoding, "N1XcvT_I1AEEv", "N1YcvT_I1AEEv"},
249 // A == B
250 {FragmentKind::Name, "1A", "1B"},
251 },
252 {
253 // All combinations result in unique equivalence classes.
254 {"_ZN1XcvT_I1AEEv"},
255 {"_ZN1XcvT_I1BEEv"},
256 {"_ZN1YcvT_I1AEEv"},
257 {"_ZN1YcvT_I1BEEv"},
258 // Even giving the same string twice gives a new class.
259 {"_ZN1XcvT_I1AEEv"},
260 }
Richard Smith2ae84682018-08-24 22:31:51 +0000261 },
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000262 };
263}
Richard Smith2ae84682018-08-24 22:31:51 +0000264
Richard Smith9c2e4f32018-08-24 23:26:05 +0000265template<bool CanonicalizeFirst>
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000266static void testTestcases(ArrayRef<Testcase> Testcases) {
Richard Smith2ae84682018-08-24 22:31:51 +0000267 for (const auto &Testcase : Testcases) {
268 llvm::ItaniumManglingCanonicalizer Canonicalizer;
269 for (const auto &Equiv : Testcase.Equivalences) {
270 auto Result =
271 Canonicalizer.addEquivalence(Equiv.Kind, Equiv.First, Equiv.Second);
272 EXPECT_EQ(Result, EquivalenceError::Success)
273 << "couldn't add equivalence between " << Equiv.First << " and "
274 << Equiv.Second;
275 }
276
277 using CanonKey = llvm::ItaniumManglingCanonicalizer::Key;
Richard Smith9c2e4f32018-08-24 23:26:05 +0000278
279 std::map<const EquivalenceClass*, CanonKey> Keys;
280 if (CanonicalizeFirst)
281 for (const auto &Class : Testcase.Classes)
282 Keys.insert({&Class, Canonicalizer.canonicalize(*Class.begin())});
283
Richard Smith2ae84682018-08-24 22:31:51 +0000284 std::map<CanonKey, llvm::StringRef> Found;
285 for (const auto &Class : Testcase.Classes) {
Richard Smith9c2e4f32018-08-24 23:26:05 +0000286 CanonKey ClassKey = Keys[&Class];
Richard Smith2ae84682018-08-24 22:31:51 +0000287 for (llvm::StringRef Str : Class) {
Richard Smith9c2e4f32018-08-24 23:26:05 +0000288 // Force a copy to be made when calling lookup to test that it doesn't
289 // retain any part of the provided string.
290 CanonKey ThisKey = CanonicalizeFirst
291 ? Canonicalizer.lookup(std::string(Str))
292 : Canonicalizer.canonicalize(Str);
Richard Smith2ae84682018-08-24 22:31:51 +0000293 EXPECT_NE(ThisKey, CanonKey()) << "couldn't canonicalize " << Str;
294 if (ClassKey) {
295 EXPECT_EQ(ThisKey, ClassKey)
296 << Str << " not in the same class as " << *Class.begin();
297 } else {
298 ClassKey = ThisKey;
299 }
300 }
301 EXPECT_TRUE(Found.insert({ClassKey, *Class.begin()}).second)
302 << *Class.begin() << " is in the same class as " << Found[ClassKey];
303 }
304 }
305}
306
Richard Smith9c2e4f32018-08-24 23:26:05 +0000307TEST(ItaniumManglingCanonicalizerTest, TestCanonicalize) {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000308 testTestcases<false>(getTestcases());
Richard Smith9c2e4f32018-08-24 23:26:05 +0000309}
310
311TEST(ItaniumManglingCanonicalizerTest, TestLookup) {
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000312 testTestcases<true>(getTestcases());
Richard Smith9c2e4f32018-08-24 23:26:05 +0000313}
314
315TEST(ItaniumManglingCanonicalizerTest, TestForwardTemplateReference) {
316 // lookup(...) after canonicalization (intentionally) returns different
317 // values for this testcase.
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000318 testTestcases<false>(getForwardTemplateReferenceTestcases());
Richard Smith9c2e4f32018-08-24 23:26:05 +0000319}
320
321
Richard Smith2ae84682018-08-24 22:31:51 +0000322TEST(ItaniumManglingCanonicalizerTest, TestInvalidManglings) {
323 llvm::ItaniumManglingCanonicalizer Canonicalizer;
324 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "", "1X"),
325 EquivalenceError::InvalidFirstMangling);
326 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "1X", "1ab"),
327 EquivalenceError::InvalidSecondMangling);
328 EXPECT_EQ(Canonicalizer.canonicalize("_Z3fooE"),
329 llvm::ItaniumManglingCanonicalizer::Key());
330 EXPECT_EQ(Canonicalizer.canonicalize("foo"),
331 llvm::ItaniumManglingCanonicalizer::Key());
332
333 // A reference to a template parameter ('T_' etc) cannot appear in a <name>,
334 // because we don't have template arguments to bind to it. (The arguments in
335 // an 'I ... E' construct in the <name> aren't registered as
336 // backreferenceable arguments in this sense, because they're not part of
337 // the template argument list of an <encoding>.
338 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Name, "N1XcvT_I1AEE",
339 "1f"),
340 EquivalenceError::InvalidFirstMangling);
341}
342
343TEST(ItaniumManglingCanonicalizerTest, TestBadEquivalenceOrder) {
344 llvm::ItaniumManglingCanonicalizer Canonicalizer;
345 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "N1P1XE", "N1Q1XE"),
346 EquivalenceError::Success);
347 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "1P", "1Q"),
348 EquivalenceError::ManglingAlreadyUsed);
349
350 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "N1C1XE", "N1A1YE"),
351 EquivalenceError::Success);
352 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "1A", "1B"),
353 EquivalenceError::Success);
354 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "1C", "1D"),
355 EquivalenceError::Success);
356 EXPECT_EQ(Canonicalizer.addEquivalence(FragmentKind::Type, "1B", "1D"),
357 EquivalenceError::ManglingAlreadyUsed);
358}
Chandler Carruth6eb2b132018-08-26 10:03:08 +0000359
360} // end anonymous namespace