blob: 9bc8164dd48bd11fd81dacb504373186db5284f9 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000012#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/Instructions.h"
14#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Module.h"
17#include "llvm/IR/Type.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000018#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000019#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000020using namespace llvm;
21
22namespace {
23
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000024TEST(ContextAndReplaceableUsesTest, FromContext) {
25 LLVMContext Context;
26 ContextAndReplaceableUses CRU(Context);
27 EXPECT_EQ(&Context, &CRU.getContext());
28 EXPECT_FALSE(CRU.hasReplaceableUses());
29 EXPECT_FALSE(CRU.getReplaceableUses());
30}
31
32TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
33 LLVMContext Context;
34 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
35 EXPECT_EQ(&Context, &CRU.getContext());
36 EXPECT_TRUE(CRU.hasReplaceableUses());
37 EXPECT_TRUE(CRU.getReplaceableUses());
38}
39
40TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
41 LLVMContext Context;
42 ContextAndReplaceableUses CRU(Context);
43 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
44 EXPECT_EQ(&Context, &CRU.getContext());
45 EXPECT_TRUE(CRU.hasReplaceableUses());
46 EXPECT_TRUE(CRU.getReplaceableUses());
47}
48
49TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
50 LLVMContext Context;
51 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
52 auto *Ptr = ReplaceableUses.get();
53 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
54 ReplaceableUses = CRU.takeReplaceableUses();
55 EXPECT_EQ(&Context, &CRU.getContext());
56 EXPECT_FALSE(CRU.hasReplaceableUses());
57 EXPECT_FALSE(CRU.getReplaceableUses());
58 EXPECT_EQ(Ptr, ReplaceableUses.get());
59}
60
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000061class MetadataTest : public testing::Test {
62protected:
63 LLVMContext Context;
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000064 MDNode *getNode() { return MDNode::get(Context, None); }
65 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
66 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
67 Metadata *MDs[] = {MD1, MD2};
68 return MDNode::get(Context, MDs);
69 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000070};
71typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +000072
Nick Lewycky49f89192009-04-04 07:22:01 +000073// Test that construction of MDString with different value produces different
74// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000075TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +000076 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000077 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000078 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000079 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000080 EXPECT_NE(s1, s2);
81}
82
83// Test that creation of MDStrings with the same string contents produces the
84// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000085TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +000086 char x[4] = { 'a', 'b', 'c', 'X' };
87 char y[4] = { 'a', 'b', 'c', 'Y' };
88
Owen Anderson23587322009-07-31 21:38:10 +000089 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
90 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000091 EXPECT_EQ(s1, s2);
92}
93
94// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000095TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000096 char *str = new char[13];
97 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000098 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000099 strncpy(str, "aaaaaaaaaaaaa", 13);
100 delete[] str;
101
Chris Lattnerbe354a62009-08-23 04:47:35 +0000102 std::string Str;
103 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000104 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000105 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000106}
107
108// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000109TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000110 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000111 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000112 std::string Str;
113 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000114 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000115 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000116}
117
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000118typedef MetadataTest MDNodeTest;
119
Nick Lewycky49f89192009-04-04 07:22:01 +0000120// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000121TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000122 char x[3] = { 'a', 'b', 'c' };
123 char y[3] = { '1', '2', '3' };
124
Owen Anderson23587322009-07-31 21:38:10 +0000125 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
126 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000127 ConstantAsMetadata *CI = ConstantAsMetadata::get(
128 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000129
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000130 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000131 V.push_back(s1);
132 V.push_back(CI);
133 V.push_back(s2);
134
Jay Foad5514afe2011-04-21 19:59:31 +0000135 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000136 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000137 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000138 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000139 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000140 MDNode *n4 = MDNode::getIfExists(Context, V);
141 MDNode *n5 = MDNode::getIfExists(Context, c1);
142 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000143 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000144 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000145 EXPECT_EQ(n4, n1);
146 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000147 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000148
Chris Lattner9b493022009-12-31 01:22:29 +0000149 EXPECT_EQ(3u, n1->getNumOperands());
150 EXPECT_EQ(s1, n1->getOperand(0));
151 EXPECT_EQ(CI, n1->getOperand(1));
152 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000153
Chris Lattner9b493022009-12-31 01:22:29 +0000154 EXPECT_EQ(1u, n2->getNumOperands());
155 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000156}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000157
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000158TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000159 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
160 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000161
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000162 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000163 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000164 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000165
166 EXPECT_EQ(n, wvh);
167
168 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000169}
Devang Patel0924b332009-07-30 00:03:41 +0000170
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000171TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000172 // !0 = !{!0}
173 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000174 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000175 auto Temp = MDNode::getTemporary(Context, None);
176 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000177 MDNode *Self = MDNode::get(Context, Args);
178 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000179 ASSERT_EQ(Self, Self->getOperand(0));
180
181 // Self-references should be distinct, so MDNode::get() should grab a
182 // uniqued node that references Self, not Self.
183 Args[0] = Self;
184 MDNode *Ref1 = MDNode::get(Context, Args);
185 MDNode *Ref2 = MDNode::get(Context, Args);
186 EXPECT_NE(Self, Ref1);
187 EXPECT_EQ(Ref1, Ref2);
188 }
189
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000190 // !0 = !{!0, !{}}
191 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000192 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000193 auto Temp = MDNode::getTemporary(Context, None);
194 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000195 MDNode *Self = MDNode::get(Context, Args);
196 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000197 ASSERT_EQ(Self, Self->getOperand(0));
198
199 // Self-references should be distinct, so MDNode::get() should grab a
200 // uniqued node that references Self, not Self itself.
201 Args[0] = Self;
202 MDNode *Ref1 = MDNode::get(Context, Args);
203 MDNode *Ref2 = MDNode::get(Context, Args);
204 EXPECT_NE(Self, Ref1);
205 EXPECT_EQ(Ref1, Ref2);
206 }
207}
208
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000209TEST_F(MDNodeTest, Print) {
210 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
211 MDString *S = MDString::get(Context, "foo");
212 MDNode *N0 = getNode();
213 MDNode *N1 = getNode(N0);
214 MDNode *N2 = getNode(N0, N1);
215
216 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
217 MDNode *N = MDNode::get(Context, Args);
218
219 std::string Expected;
220 {
221 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000222 OS << "!{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000223 C->printAsOperand(OS);
224 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000225 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000226 OS << ", null";
227 MDNode *Nodes[] = {N0, N1, N2};
228 for (auto *Node : Nodes)
229 OS << ", <" << (void *)Node << ">";
230 OS << "}\n";
231 }
232
233 std::string Actual;
234 {
235 raw_string_ostream OS(Actual);
236 N->print(OS);
237 }
238
239 EXPECT_EQ(Expected, Actual);
240}
241
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000242TEST_F(MDNodeTest, NullOperand) {
243 // metadata !{}
244 MDNode *Empty = MDNode::get(Context, None);
245
246 // metadata !{metadata !{}}
247 Metadata *Ops[] = {Empty};
248 MDNode *N = MDNode::get(Context, Ops);
249 ASSERT_EQ(Empty, N->getOperand(0));
250
251 // metadata !{metadata !{}} => metadata !{null}
252 N->replaceOperandWith(0, nullptr);
253 ASSERT_EQ(nullptr, N->getOperand(0));
254
255 // metadata !{null}
256 Ops[0] = nullptr;
257 MDNode *NullOp = MDNode::get(Context, Ops);
258 ASSERT_EQ(nullptr, NullOp->getOperand(0));
259 EXPECT_EQ(N, NullOp);
260}
261
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000262TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
263 // !{}
264 MDNode *Empty = MDNode::get(Context, None);
265 ASSERT_TRUE(Empty->isResolved());
266 EXPECT_FALSE(Empty->isDistinct());
267
268 // !{!{}}
269 Metadata *Wrapped1Ops[] = {Empty};
270 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
271 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
272 ASSERT_TRUE(Wrapped1->isResolved());
273 EXPECT_FALSE(Wrapped1->isDistinct());
274
275 // !{!{!{}}}
276 Metadata *Wrapped2Ops[] = {Wrapped1};
277 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
278 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
279 ASSERT_TRUE(Wrapped2->isResolved());
280 EXPECT_FALSE(Wrapped2->isDistinct());
281
282 // !{!{!{}}} => !{!{}}
283 Wrapped2->replaceOperandWith(0, Empty);
284 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
285 EXPECT_TRUE(Wrapped2->isDistinct());
286 EXPECT_FALSE(Wrapped1->isDistinct());
287}
288
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000289TEST_F(MDNodeTest, getDistinct) {
290 // !{}
291 MDNode *Empty = MDNode::get(Context, None);
292 ASSERT_TRUE(Empty->isResolved());
293 ASSERT_FALSE(Empty->isDistinct());
294 ASSERT_EQ(Empty, MDNode::get(Context, None));
295
296 // distinct !{}
297 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
298 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
299 EXPECT_TRUE(Distinct1->isResolved());
300 EXPECT_TRUE(Distinct2->isDistinct());
301 EXPECT_NE(Empty, Distinct1);
302 EXPECT_NE(Empty, Distinct2);
303 EXPECT_NE(Distinct1, Distinct2);
304
305 // !{}
306 ASSERT_EQ(Empty, MDNode::get(Context, None));
307}
308
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000309TEST_F(MDNodeTest, isUniqued) {
310 MDNode *U = MDTuple::get(Context, None);
311 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000312 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000313 EXPECT_TRUE(U->isUniqued());
314 EXPECT_FALSE(D->isUniqued());
315 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000316}
317
318TEST_F(MDNodeTest, isDistinct) {
319 MDNode *U = MDTuple::get(Context, None);
320 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000321 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000322 EXPECT_FALSE(U->isDistinct());
323 EXPECT_TRUE(D->isDistinct());
324 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000325}
326
327TEST_F(MDNodeTest, isTemporary) {
328 MDNode *U = MDTuple::get(Context, None);
329 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000330 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000331 EXPECT_FALSE(U->isTemporary());
332 EXPECT_FALSE(D->isTemporary());
333 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000334}
335
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000336TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
337 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000338 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000339 ASSERT_FALSE(Temp->isResolved());
340
341 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000342 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000343 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
344 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000345 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000346
347 // temporary !{} => !{}
348 MDNode *Empty = MDNode::get(Context, None);
349 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000350 EXPECT_EQ(Empty, Distinct->getOperand(0));
351}
352
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000353TEST_F(MDNodeTest, handleChangedOperandRecursion) {
354 // !0 = !{}
355 MDNode *N0 = MDNode::get(Context, None);
356
357 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000358 auto Temp3 = MDTuple::getTemporary(Context, None);
359 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000360 MDNode *N1 = MDNode::get(Context, Ops1);
361
362 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000363 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000364 MDNode *N2 = MDNode::get(Context, Ops2);
365
366 // !3 = !{!2}
367 Metadata *Ops3[] = {N2};
368 MDNode *N3 = MDNode::get(Context, Ops3);
369 Temp3->replaceAllUsesWith(N3);
370
371 // !4 = !{!1}
372 Metadata *Ops4[] = {N1};
373 MDNode *N4 = MDNode::get(Context, Ops4);
374
375 // Confirm that the cycle prevented RAUW from getting dropped.
376 EXPECT_TRUE(N0->isResolved());
377 EXPECT_FALSE(N1->isResolved());
378 EXPECT_FALSE(N2->isResolved());
379 EXPECT_FALSE(N3->isResolved());
380 EXPECT_FALSE(N4->isResolved());
381
382 // Create a couple of distinct nodes to observe what's going on.
383 //
384 // !5 = distinct !{!2}
385 // !6 = distinct !{!3}
386 Metadata *Ops5[] = {N2};
387 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
388 Metadata *Ops6[] = {N3};
389 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
390
391 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
392 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
393 // references !3, this can cause a re-entry of handleChangedOperand() when !3
394 // is not ready for it.
395 //
396 // !2->replaceOperandWith(1, nullptr)
397 // !2: !{!3, !0} => !{!3, null}
398 // !2->replaceAllUsesWith(!1)
399 // !3: !{!2] => !{!1}
400 // !3->replaceAllUsesWith(!4)
401 N2->replaceOperandWith(1, nullptr);
402
403 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
404 // under us. Just check that the other nodes are sane.
405 //
406 // !1 = !{!4, null}
407 // !4 = !{!1}
408 // !5 = distinct !{!1}
409 // !6 = distinct !{!4}
410 EXPECT_EQ(N4, N1->getOperand(0));
411 EXPECT_EQ(N1, N4->getOperand(0));
412 EXPECT_EQ(N1, N5->getOperand(0));
413 EXPECT_EQ(N4, N6->getOperand(0));
414}
415
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000416TEST_F(MDNodeTest, replaceResolvedOperand) {
417 // Check code for replacing one resolved operand with another. If doing this
418 // directly (via replaceOperandWith()) becomes illegal, change the operand to
419 // a global value that gets RAUW'ed.
420 //
421 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000422 auto Temp = MDTuple::getTemporary(Context, None);
423 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000424
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000425 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000426 MDNode *N = MDTuple::get(Context, Ops);
427 EXPECT_EQ(nullptr, N->getOperand(0));
428 ASSERT_FALSE(N->isResolved());
429
430 // Check code for replacing resolved nodes.
431 N->replaceOperandWith(0, Empty);
432 EXPECT_EQ(Empty, N->getOperand(0));
433
434 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000435 N->replaceOperandWith(0, Temp.get());
436 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000437
438 // Remove the references to Temp; required for teardown.
439 Temp->replaceAllUsesWith(nullptr);
440}
441
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000442TEST_F(MDNodeTest, replaceWithUniqued) {
443 auto *Empty = MDTuple::get(Context, None);
444 MDTuple *FirstUniqued;
445 {
446 Metadata *Ops[] = {Empty};
447 auto Temp = MDTuple::getTemporary(Context, Ops);
448 EXPECT_TRUE(Temp->isTemporary());
449
450 // Don't expect a collision.
451 auto *Current = Temp.get();
452 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
453 EXPECT_TRUE(FirstUniqued->isUniqued());
454 EXPECT_TRUE(FirstUniqued->isResolved());
455 EXPECT_EQ(Current, FirstUniqued);
456 }
457 {
458 Metadata *Ops[] = {Empty};
459 auto Temp = MDTuple::getTemporary(Context, Ops);
460 EXPECT_TRUE(Temp->isTemporary());
461
462 // Should collide with Uniqued above this time.
463 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
464 EXPECT_TRUE(Uniqued->isUniqued());
465 EXPECT_TRUE(Uniqued->isResolved());
466 EXPECT_EQ(FirstUniqued, Uniqued);
467 }
468 {
469 auto Unresolved = MDTuple::getTemporary(Context, None);
470 Metadata *Ops[] = {Unresolved.get()};
471 auto Temp = MDTuple::getTemporary(Context, Ops);
472 EXPECT_TRUE(Temp->isTemporary());
473
474 // Shouldn't be resolved.
475 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
476 EXPECT_TRUE(Uniqued->isUniqued());
477 EXPECT_FALSE(Uniqued->isResolved());
478
479 // Should be a different node.
480 EXPECT_NE(FirstUniqued, Uniqued);
481
482 // Should resolve when we update its node (note: be careful to avoid a
483 // collision with any other nodes above).
484 Uniqued->replaceOperandWith(0, nullptr);
485 EXPECT_TRUE(Uniqued->isResolved());
486 }
487}
488
489TEST_F(MDNodeTest, replaceWithDistinct) {
490 {
491 auto *Empty = MDTuple::get(Context, None);
492 Metadata *Ops[] = {Empty};
493 auto Temp = MDTuple::getTemporary(Context, Ops);
494 EXPECT_TRUE(Temp->isTemporary());
495
496 // Don't expect a collision.
497 auto *Current = Temp.get();
498 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
499 EXPECT_TRUE(Distinct->isDistinct());
500 EXPECT_TRUE(Distinct->isResolved());
501 EXPECT_EQ(Current, Distinct);
502 }
503 {
504 auto Unresolved = MDTuple::getTemporary(Context, None);
505 Metadata *Ops[] = {Unresolved.get()};
506 auto Temp = MDTuple::getTemporary(Context, Ops);
507 EXPECT_TRUE(Temp->isTemporary());
508
509 // Don't expect a collision.
510 auto *Current = Temp.get();
511 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
512 EXPECT_TRUE(Distinct->isDistinct());
513 EXPECT_TRUE(Distinct->isResolved());
514 EXPECT_EQ(Current, Distinct);
515
516 // Cleanup; required for teardown.
517 Unresolved->replaceAllUsesWith(nullptr);
518 }
519}
520
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000521TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
522 TrackingMDRef Ref;
523 EXPECT_EQ(nullptr, Ref.get());
524 {
525 auto Temp = MDTuple::getTemporary(Context, None);
526 Ref.reset(Temp.get());
527 EXPECT_EQ(Temp.get(), Ref.get());
528 }
529 EXPECT_EQ(nullptr, Ref.get());
530}
531
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000532typedef MetadataTest MDLocationTest;
533
534TEST_F(MDLocationTest, Overflow) {
535 MDNode *N = MDNode::get(Context, None);
536 {
537 MDLocation *L = MDLocation::get(Context, 2, 7, N);
538 EXPECT_EQ(2u, L->getLine());
539 EXPECT_EQ(7u, L->getColumn());
540 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000541 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000542 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000543 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
544 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000545 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000546 }
547 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000548 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
549 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000550 EXPECT_EQ(0u, L->getColumn());
551 }
552 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000553 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
554 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000555 EXPECT_EQ(0u, L->getColumn());
556 }
557}
558
559TEST_F(MDLocationTest, getDistinct) {
560 MDNode *N = MDNode::get(Context, None);
561 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
562 EXPECT_TRUE(L0->isDistinct());
563 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
564 EXPECT_FALSE(L1->isDistinct());
565 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
566}
567
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000568TEST_F(MDLocationTest, getTemporary) {
569 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000570 auto L = MDLocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000571 EXPECT_TRUE(L->isTemporary());
572 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000573}
574
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000575typedef MetadataTest GenericDebugNodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000576
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000577TEST_F(GenericDebugNodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000578 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000579 auto *Empty = MDNode::get(Context, None);
580 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000581 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000582 EXPECT_EQ(15u, N->getTag());
583 EXPECT_EQ(2u, N->getNumOperands());
584 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000585 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000586 EXPECT_EQ(1u, N->getNumDwarfOperands());
587 EXPECT_EQ(Empty, N->getDwarfOperand(0));
588 EXPECT_EQ(Empty, N->getOperand(1));
589 ASSERT_TRUE(N->isUniqued());
590
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000591 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000592
593 N->replaceOperandWith(1, nullptr);
594 EXPECT_EQ(15u, N->getTag());
595 EXPECT_EQ(Header, N->getHeader());
596 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
597 ASSERT_TRUE(N->isUniqued());
598
599 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000600 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000601
602 N->replaceDwarfOperandWith(0, Empty);
603 EXPECT_EQ(15u, N->getTag());
604 EXPECT_EQ(Header, N->getHeader());
605 EXPECT_EQ(Empty, N->getDwarfOperand(0));
606 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000607 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000608}
609
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000610TEST_F(GenericDebugNodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000611 // Canonicalize !"" to null.
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000612 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
613 EXPECT_EQ(StringRef(), N->getHeader());
614 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000615}
616
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000617typedef MetadataTest MDSubrangeTest;
618
619TEST_F(MDSubrangeTest, get) {
620 auto *N = MDSubrange::get(Context, 5, 7);
621 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
622 EXPECT_EQ(5, N->getCount());
623 EXPECT_EQ(7, N->getLo());
624 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
625 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
626}
627
628typedef MetadataTest MDEnumeratorTest;
629
630TEST_F(MDEnumeratorTest, get) {
631 auto *N = MDEnumerator::get(Context, 7, "name");
632 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
633 EXPECT_EQ(7, N->getValue());
634 EXPECT_EQ("name", N->getName());
635 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
636
637 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
638 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
639}
640
641typedef MetadataTest MDBasicTypeTest;
642
643TEST_F(MDBasicTypeTest, get) {
644 auto *N =
645 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
646 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
647 EXPECT_EQ("special", N->getName());
648 EXPECT_EQ(33u, N->getSizeInBits());
649 EXPECT_EQ(26u, N->getAlignInBits());
650 EXPECT_EQ(7u, N->getEncoding());
651 EXPECT_EQ(0u, N->getLine());
652 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
653 26, 7));
654
655 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
656 "special", 33, 26, 7));
657 EXPECT_NE(N,
658 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
659 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
660 26, 7));
661 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
662 25, 7));
663 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
664 26, 6));
665}
666
667typedef MetadataTest MDDerivedTypeTest;
668
669TEST_F(MDDerivedTypeTest, get) {
670 Metadata *File = MDTuple::getDistinct(Context, None);
671 Metadata *Scope = MDTuple::getDistinct(Context, None);
672 Metadata *BaseType = MDTuple::getDistinct(Context, None);
673 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
674
675 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
676 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
677 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
678 EXPECT_EQ("something", N->getName());
679 EXPECT_EQ(File, N->getFile());
680 EXPECT_EQ(1u, N->getLine());
681 EXPECT_EQ(Scope, N->getScope());
682 EXPECT_EQ(BaseType, N->getBaseType());
683 EXPECT_EQ(2u, N->getSizeInBits());
684 EXPECT_EQ(3u, N->getAlignInBits());
685 EXPECT_EQ(4u, N->getOffsetInBits());
686 EXPECT_EQ(5u, N->getFlags());
687 EXPECT_EQ(ExtraData, N->getExtraData());
688 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
689 "something", File, 1, Scope, BaseType, 2, 3,
690 4, 5, ExtraData));
691
692 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
693 "something", File, 1, Scope, BaseType, 2, 3,
694 4, 5, ExtraData));
695 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
696 File, 1, Scope, BaseType, 2, 3, 4, 5,
697 ExtraData));
698 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
699 "something", Scope, 1, Scope, BaseType, 2, 3,
700 4, 5, ExtraData));
701 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
702 "something", File, 2, Scope, BaseType, 2, 3,
703 4, 5, ExtraData));
704 EXPECT_NE(N,
705 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
706 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
707 EXPECT_NE(N,
708 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
709 File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
710 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
711 "something", File, 1, Scope, BaseType, 3, 3,
712 4, 5, ExtraData));
713 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
714 "something", File, 1, Scope, BaseType, 2, 2,
715 4, 5, ExtraData));
716 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
717 "something", File, 1, Scope, BaseType, 2, 3,
718 5, 5, ExtraData));
719 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
720 "something", File, 1, Scope, BaseType, 2, 3,
721 4, 4, ExtraData));
722 EXPECT_NE(N,
723 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
724 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
725}
726
727typedef MetadataTest MDCompositeTypeTest;
728
729TEST_F(MDCompositeTypeTest, get) {
730 unsigned Tag = dwarf::DW_TAG_structure_type;
731 StringRef Name = "some name";
732 Metadata *File = MDTuple::getDistinct(Context, None);
733 unsigned Line = 1;
734 Metadata *Scope = MDTuple::getDistinct(Context, None);
735 Metadata *BaseType = MDTuple::getDistinct(Context, None);
736 unsigned SizeInBits = 2;
737 unsigned AlignInBits = 3;
738 unsigned OffsetInBits = 4;
739 unsigned Flags = 5;
740 Metadata *Elements = MDTuple::getDistinct(Context, None);
741 unsigned RuntimeLang = 6;
742 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
743 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
744 StringRef Identifier = "some id";
745
746 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
747 BaseType, SizeInBits, AlignInBits,
748 OffsetInBits, Flags, Elements, RuntimeLang,
749 VTableHolder, TemplateParams, Identifier);
750 EXPECT_EQ(Tag, N->getTag());
751 EXPECT_EQ(Name, N->getName());
752 EXPECT_EQ(File, N->getFile());
753 EXPECT_EQ(Line, N->getLine());
754 EXPECT_EQ(Scope, N->getScope());
755 EXPECT_EQ(BaseType, N->getBaseType());
756 EXPECT_EQ(SizeInBits, N->getSizeInBits());
757 EXPECT_EQ(AlignInBits, N->getAlignInBits());
758 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
759 EXPECT_EQ(Flags, N->getFlags());
760 EXPECT_EQ(Elements, N->getElements());
761 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
762 EXPECT_EQ(VTableHolder, N->getVTableHolder());
763 EXPECT_EQ(TemplateParams, N->getTemplateParams());
764 EXPECT_EQ(Identifier, N->getIdentifier());
765
766 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
767 BaseType, SizeInBits, AlignInBits,
768 OffsetInBits, Flags, Elements, RuntimeLang,
769 VTableHolder, TemplateParams, Identifier));
770
771 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
772 BaseType, SizeInBits, AlignInBits,
773 OffsetInBits, Flags, Elements, RuntimeLang,
774 VTableHolder, TemplateParams, Identifier));
775 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
776 BaseType, SizeInBits, AlignInBits,
777 OffsetInBits, Flags, Elements, RuntimeLang,
778 VTableHolder, TemplateParams, Identifier));
779 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
780 BaseType, SizeInBits, AlignInBits,
781 OffsetInBits, Flags, Elements, RuntimeLang,
782 VTableHolder, TemplateParams, Identifier));
783 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
784 BaseType, SizeInBits, AlignInBits,
785 OffsetInBits, Flags, Elements, RuntimeLang,
786 VTableHolder, TemplateParams, Identifier));
787 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
788 BaseType, SizeInBits, AlignInBits,
789 OffsetInBits, Flags, Elements, RuntimeLang,
790 VTableHolder, TemplateParams, Identifier));
791 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
792 SizeInBits, AlignInBits, OffsetInBits,
793 Flags, Elements, RuntimeLang, VTableHolder,
794 TemplateParams, Identifier));
795 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
796 BaseType, SizeInBits + 1, AlignInBits,
797 OffsetInBits, Flags, Elements, RuntimeLang,
798 VTableHolder, TemplateParams, Identifier));
799 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
800 BaseType, SizeInBits, AlignInBits + 1,
801 OffsetInBits, Flags, Elements, RuntimeLang,
802 VTableHolder, TemplateParams, Identifier));
803 EXPECT_NE(N, MDCompositeType::get(
804 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
805 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
806 VTableHolder, TemplateParams, Identifier));
807 EXPECT_NE(N, MDCompositeType::get(
808 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
809 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
810 VTableHolder, TemplateParams, Identifier));
811 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
812 BaseType, SizeInBits, AlignInBits,
813 OffsetInBits, Flags, File, RuntimeLang,
814 VTableHolder, TemplateParams, Identifier));
815 EXPECT_NE(N, MDCompositeType::get(
816 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
817 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
818 VTableHolder, TemplateParams, Identifier));
819 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
820 BaseType, SizeInBits, AlignInBits,
821 OffsetInBits, Flags, Elements, RuntimeLang,
822 File, TemplateParams, Identifier));
823 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
824 BaseType, SizeInBits, AlignInBits,
825 OffsetInBits, Flags, Elements, RuntimeLang,
826 VTableHolder, File, Identifier));
827 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
828 BaseType, SizeInBits, AlignInBits,
829 OffsetInBits, Flags, Elements, RuntimeLang,
830 VTableHolder, TemplateParams, "other"));
831
832 // Be sure that missing identifiers get null pointers.
833 EXPECT_FALSE(MDCompositeType::get(
834 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
835 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
836 VTableHolder, TemplateParams, "")->getRawIdentifier());
837 EXPECT_FALSE(MDCompositeType::get(
838 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
839 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
840 VTableHolder, TemplateParams)->getRawIdentifier());
841}
842
843typedef MetadataTest MDSubroutineTypeTest;
844
845TEST_F(MDSubroutineTypeTest, get) {
846 unsigned Flags = 1;
847 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
848
849 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
850 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
851 EXPECT_EQ(Flags, N->getFlags());
852 EXPECT_EQ(TypeArray, N->getTypeArray());
853 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
854
855 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
856 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
857 MDTuple::getDistinct(Context, None)));
858}
859
860typedef MetadataTest MDFileTest;
861
862TEST_F(MDFileTest, get) {
863 StringRef Filename = "file";
864 StringRef Directory = "dir";
865 auto *N = MDFile::get(Context, Filename, Directory);
866
867 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
868 EXPECT_EQ(Filename, N->getFilename());
869 EXPECT_EQ(Directory, N->getDirectory());
870 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
871
872 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
873 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
874}
875
876typedef MetadataTest MDCompileUnitTest;
877
878TEST_F(MDCompileUnitTest, get) {
879 unsigned SourceLanguage = 1;
880 Metadata *File = MDTuple::getDistinct(Context, None);
881 StringRef Producer = "some producer";
882 bool IsOptimized = false;
883 StringRef Flags = "flag after flag";
884 unsigned RuntimeVersion = 2;
885 StringRef SplitDebugFilename = "another/file";
886 unsigned EmissionKind = 3;
887 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
888 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
889 Metadata *Subprograms = MDTuple::getDistinct(Context, None);
890 Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
891 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
892 auto *N = MDCompileUnit::get(
893 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
894 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
895 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
896
897 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
898 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
899 EXPECT_EQ(File, N->getFile());
900 EXPECT_EQ(Producer, N->getProducer());
901 EXPECT_EQ(IsOptimized, N->isOptimized());
902 EXPECT_EQ(Flags, N->getFlags());
903 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
904 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
905 EXPECT_EQ(EmissionKind, N->getEmissionKind());
906 EXPECT_EQ(EnumTypes, N->getEnumTypes());
907 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
908 EXPECT_EQ(Subprograms, N->getSubprograms());
909 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
910 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
911 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
912 IsOptimized, Flags, RuntimeVersion,
913 SplitDebugFilename, EmissionKind, EnumTypes,
914 RetainedTypes, Subprograms, GlobalVariables,
915 ImportedEntities));
916
917 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
918 IsOptimized, Flags, RuntimeVersion,
919 SplitDebugFilename, EmissionKind, EnumTypes,
920 RetainedTypes, Subprograms, GlobalVariables,
921 ImportedEntities));
922 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
923 IsOptimized, Flags, RuntimeVersion,
924 SplitDebugFilename, EmissionKind, EnumTypes,
925 RetainedTypes, Subprograms, GlobalVariables,
926 ImportedEntities));
927 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
928 IsOptimized, Flags, RuntimeVersion,
929 SplitDebugFilename, EmissionKind, EnumTypes,
930 RetainedTypes, Subprograms, GlobalVariables,
931 ImportedEntities));
932 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
933 !IsOptimized, Flags, RuntimeVersion,
934 SplitDebugFilename, EmissionKind, EnumTypes,
935 RetainedTypes, Subprograms, GlobalVariables,
936 ImportedEntities));
937 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
938 IsOptimized, "other", RuntimeVersion,
939 SplitDebugFilename, EmissionKind, EnumTypes,
940 RetainedTypes, Subprograms, GlobalVariables,
941 ImportedEntities));
942 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
943 IsOptimized, Flags, RuntimeVersion + 1,
944 SplitDebugFilename, EmissionKind, EnumTypes,
945 RetainedTypes, Subprograms, GlobalVariables,
946 ImportedEntities));
947 EXPECT_NE(N,
948 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
949 IsOptimized, Flags, RuntimeVersion, "other",
950 EmissionKind, EnumTypes, RetainedTypes,
951 Subprograms, GlobalVariables, ImportedEntities));
952 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
953 IsOptimized, Flags, RuntimeVersion,
954 SplitDebugFilename, EmissionKind + 1,
955 EnumTypes, RetainedTypes, Subprograms,
956 GlobalVariables, ImportedEntities));
957 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
958 IsOptimized, Flags, RuntimeVersion,
959 SplitDebugFilename, EmissionKind, File,
960 RetainedTypes, Subprograms, GlobalVariables,
961 ImportedEntities));
962 EXPECT_NE(N, MDCompileUnit::get(
963 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
964 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
965 File, Subprograms, GlobalVariables, ImportedEntities));
966 EXPECT_NE(N, MDCompileUnit::get(
967 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
968 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
969 RetainedTypes, File, GlobalVariables, ImportedEntities));
970 EXPECT_NE(N, MDCompileUnit::get(
971 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
972 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
973 RetainedTypes, Subprograms, File, ImportedEntities));
974 EXPECT_NE(N, MDCompileUnit::get(
975 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
976 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
977 RetainedTypes, Subprograms, GlobalVariables, File));
978}
979
980typedef MetadataTest MDSubprogramTest;
981
982TEST_F(MDSubprogramTest, get) {
983 Metadata *Scope = MDTuple::getDistinct(Context, None);
984 StringRef Name = "name";
985 StringRef LinkageName = "linkage";
986 Metadata *File = MDTuple::getDistinct(Context, None);
987 unsigned Line = 2;
988 Metadata *Type = MDTuple::getDistinct(Context, None);
989 bool IsLocalToUnit = false;
990 bool IsDefinition = true;
991 unsigned ScopeLine = 3;
992 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
993 unsigned Virtuality = 4;
994 unsigned VirtualIndex = 5;
995 unsigned Flags = 6;
996 bool IsOptimized = false;
997 Metadata *Function = MDTuple::getDistinct(Context, None);
998 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
999 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1000 Metadata *Variables = MDTuple::getDistinct(Context, None);
1001
1002 auto *N = MDSubprogram::get(
1003 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1004 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1005 IsOptimized, Function, TemplateParams, Declaration, Variables);
1006
1007 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1008 EXPECT_EQ(Scope, N->getScope());
1009 EXPECT_EQ(Name, N->getName());
1010 EXPECT_EQ(LinkageName, N->getLinkageName());
1011 EXPECT_EQ(File, N->getFile());
1012 EXPECT_EQ(Line, N->getLine());
1013 EXPECT_EQ(Type, N->getType());
1014 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1015 EXPECT_EQ(IsDefinition, N->isDefinition());
1016 EXPECT_EQ(ScopeLine, N->getScopeLine());
1017 EXPECT_EQ(ContainingType, N->getContainingType());
1018 EXPECT_EQ(Virtuality, N->getVirtuality());
1019 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1020 EXPECT_EQ(Flags, N->getFlags());
1021 EXPECT_EQ(IsOptimized, N->isOptimized());
1022 EXPECT_EQ(Function, N->getFunction());
1023 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1024 EXPECT_EQ(Declaration, N->getDeclaration());
1025 EXPECT_EQ(Variables, N->getVariables());
1026 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1027 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1028 ContainingType, Virtuality, VirtualIndex,
1029 Flags, IsOptimized, Function, TemplateParams,
1030 Declaration, Variables));
1031
1032 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1033 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1034 ContainingType, Virtuality, VirtualIndex,
1035 Flags, IsOptimized, Function, TemplateParams,
1036 Declaration, Variables));
1037 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1038 Line, Type, IsLocalToUnit, IsDefinition,
1039 ScopeLine, ContainingType, Virtuality,
1040 VirtualIndex, Flags, IsOptimized, Function,
1041 TemplateParams, Declaration, Variables));
1042 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1043 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1044 ContainingType, Virtuality, VirtualIndex,
1045 Flags, IsOptimized, Function, TemplateParams,
1046 Declaration, Variables));
1047 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1048 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1049 ContainingType, Virtuality, VirtualIndex,
1050 Flags, IsOptimized, Function, TemplateParams,
1051 Declaration, Variables));
1052 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1053 Line + 1, Type, IsLocalToUnit, IsDefinition,
1054 ScopeLine, ContainingType, Virtuality,
1055 VirtualIndex, Flags, IsOptimized, Function,
1056 TemplateParams, Declaration, Variables));
1057 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1058 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1059 ContainingType, Virtuality, VirtualIndex,
1060 Flags, IsOptimized, Function, TemplateParams,
1061 Declaration, Variables));
1062 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1063 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1064 ContainingType, Virtuality, VirtualIndex,
1065 Flags, IsOptimized, Function, TemplateParams,
1066 Declaration, Variables));
1067 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1068 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1069 ContainingType, Virtuality, VirtualIndex,
1070 Flags, IsOptimized, Function, TemplateParams,
1071 Declaration, Variables));
1072 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1073 Type, IsLocalToUnit, IsDefinition,
1074 ScopeLine + 1, ContainingType, Virtuality,
1075 VirtualIndex, Flags, IsOptimized, Function,
1076 TemplateParams, Declaration, Variables));
1077 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1078 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1079 Type, Virtuality, VirtualIndex, Flags,
1080 IsOptimized, Function, TemplateParams,
1081 Declaration, Variables));
1082 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1083 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1084 ContainingType, Virtuality + 1, VirtualIndex,
1085 Flags, IsOptimized, Function, TemplateParams,
1086 Declaration, Variables));
1087 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1088 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1089 ContainingType, Virtuality, VirtualIndex + 1,
1090 Flags, IsOptimized, Function, TemplateParams,
1091 Declaration, Variables));
1092 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1093 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1094 ContainingType, Virtuality, VirtualIndex,
1095 ~Flags, IsOptimized, Function, TemplateParams,
1096 Declaration, Variables));
1097 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1098 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1099 ContainingType, Virtuality, VirtualIndex,
1100 Flags, !IsOptimized, Function, TemplateParams,
1101 Declaration, Variables));
1102 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1103 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1104 ContainingType, Virtuality, VirtualIndex,
1105 Flags, IsOptimized, Type, TemplateParams,
1106 Declaration, Variables));
1107 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1108 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1109 ContainingType, Virtuality, VirtualIndex,
1110 Flags, IsOptimized, Function, Type,
1111 Declaration, Variables));
1112 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1113 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1114 ContainingType, Virtuality, VirtualIndex,
1115 Flags, IsOptimized, Function, TemplateParams,
1116 Type, Variables));
1117 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1118 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1119 ContainingType, Virtuality, VirtualIndex,
1120 Flags, IsOptimized, Function, TemplateParams,
1121 Declaration, Type));
1122}
1123
1124typedef MetadataTest MDLexicalBlockTest;
1125
1126TEST_F(MDLexicalBlockTest, get) {
1127 Metadata *Scope = MDTuple::getDistinct(Context, None);
1128 Metadata *File = MDTuple::getDistinct(Context, None);
1129 unsigned Line = 5;
1130 unsigned Column = 8;
1131
1132 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1133
1134 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1135 EXPECT_EQ(Scope, N->getScope());
1136 EXPECT_EQ(File, N->getFile());
1137 EXPECT_EQ(Line, N->getLine());
1138 EXPECT_EQ(Column, N->getColumn());
1139 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1140
1141 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1142 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1143 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1144 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1145}
1146
1147typedef MetadataTest MDLexicalBlockFileTest;
1148
1149TEST_F(MDLexicalBlockFileTest, get) {
1150 Metadata *Scope = MDTuple::getDistinct(Context, None);
1151 Metadata *File = MDTuple::getDistinct(Context, None);
1152 unsigned Discriminator = 5;
1153
1154 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1155
1156 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1157 EXPECT_EQ(Scope, N->getScope());
1158 EXPECT_EQ(File, N->getFile());
1159 EXPECT_EQ(Discriminator, N->getDiscriminator());
1160 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1161
1162 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1163 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1164 EXPECT_NE(N,
1165 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1166}
1167
1168typedef MetadataTest MDNamespaceTest;
1169
1170TEST_F(MDNamespaceTest, get) {
1171 Metadata *Scope = MDTuple::getDistinct(Context, None);
1172 Metadata *File = MDTuple::getDistinct(Context, None);
1173 StringRef Name = "namespace";
1174 unsigned Line = 5;
1175
1176 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1177
1178 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1179 EXPECT_EQ(Scope, N->getScope());
1180 EXPECT_EQ(File, N->getFile());
1181 EXPECT_EQ(Name, N->getName());
1182 EXPECT_EQ(Line, N->getLine());
1183 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1184
1185 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1186 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1187 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1188 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1189}
1190
1191typedef MetadataTest MDTemplateTypeParameterTest;
1192
1193TEST_F(MDTemplateTypeParameterTest, get) {
1194 Metadata *Scope = MDTuple::getDistinct(Context, None);
1195 StringRef Name = "template";
1196 Metadata *Type = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001197
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001198 auto *N = MDTemplateTypeParameter::get(Context, Scope, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001199
1200 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1201 EXPECT_EQ(Scope, N->getScope());
1202 EXPECT_EQ(Name, N->getName());
1203 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001204 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Scope, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001205
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001206 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Type, Name, Type));
1207 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, "other", Type));
1208 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, Name, Scope));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001209}
1210
1211typedef MetadataTest MDTemplateValueParameterTest;
1212
1213TEST_F(MDTemplateValueParameterTest, get) {
1214 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1215 Metadata *Scope = MDTuple::getDistinct(Context, None);
1216 StringRef Name = "template";
1217 Metadata *Type = MDTuple::getDistinct(Context, None);
1218 Metadata *Value = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001219
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001220 auto *N =
1221 MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001222 EXPECT_EQ(Tag, N->getTag());
1223 EXPECT_EQ(Scope, N->getScope());
1224 EXPECT_EQ(Name, N->getName());
1225 EXPECT_EQ(Type, N->getType());
1226 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001227 EXPECT_EQ(
1228 N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001229
1230 EXPECT_NE(N, MDTemplateValueParameter::get(
1231 Context, dwarf::DW_TAG_GNU_template_template_param, Scope,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001232 Name, Type, Value));
1233 EXPECT_NE(
1234 N, MDTemplateValueParameter::get(Context, Tag, Type, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001235 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, "other", Type,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001236 Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001237 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Scope,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001238 Value));
1239 EXPECT_NE(
1240 N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Scope));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001241}
1242
1243typedef MetadataTest MDGlobalVariableTest;
1244
1245TEST_F(MDGlobalVariableTest, get) {
1246 Metadata *Scope = MDTuple::getDistinct(Context, None);
1247 StringRef Name = "name";
1248 StringRef LinkageName = "linkage";
1249 Metadata *File = MDTuple::getDistinct(Context, None);
1250 unsigned Line = 5;
1251 Metadata *Type = MDTuple::getDistinct(Context, None);
1252 bool IsLocalToUnit = false;
1253 bool IsDefinition = true;
1254 Metadata *Variable = MDTuple::getDistinct(Context, None);
1255 Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1256
1257 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1258 Type, IsLocalToUnit, IsDefinition, Variable,
1259 StaticDataMemberDeclaration);
1260 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1261 EXPECT_EQ(Scope, N->getScope());
1262 EXPECT_EQ(Name, N->getName());
1263 EXPECT_EQ(LinkageName, N->getLinkageName());
1264 EXPECT_EQ(File, N->getFile());
1265 EXPECT_EQ(Line, N->getLine());
1266 EXPECT_EQ(Type, N->getType());
1267 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1268 EXPECT_EQ(IsDefinition, N->isDefinition());
1269 EXPECT_EQ(Variable, N->getVariable());
1270 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1271 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1272 Line, Type, IsLocalToUnit, IsDefinition,
1273 Variable, StaticDataMemberDeclaration));
1274
1275 EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1276 Line, Type, IsLocalToUnit, IsDefinition,
1277 Variable, StaticDataMemberDeclaration));
1278 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1279 Line, Type, IsLocalToUnit, IsDefinition,
1280 Variable, StaticDataMemberDeclaration));
1281 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1282 Type, IsLocalToUnit, IsDefinition,
1283 Variable, StaticDataMemberDeclaration));
1284 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1285 Line, Type, IsLocalToUnit, IsDefinition,
1286 Variable, StaticDataMemberDeclaration));
1287 EXPECT_NE(N,
1288 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1289 Line + 1, Type, IsLocalToUnit, IsDefinition,
1290 Variable, StaticDataMemberDeclaration));
1291 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1292 Line, Scope, IsLocalToUnit, IsDefinition,
1293 Variable, StaticDataMemberDeclaration));
1294 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1295 Line, Type, !IsLocalToUnit, IsDefinition,
1296 Variable, StaticDataMemberDeclaration));
1297 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1298 Line, Type, IsLocalToUnit, !IsDefinition,
1299 Variable, StaticDataMemberDeclaration));
1300 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1301 Line, Type, IsLocalToUnit, IsDefinition,
1302 Type, StaticDataMemberDeclaration));
1303 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1304 Line, Type, IsLocalToUnit, IsDefinition,
1305 Variable, Type));
1306}
1307
1308typedef MetadataTest MDLocalVariableTest;
1309
1310TEST_F(MDLocalVariableTest, get) {
1311 unsigned Tag = dwarf::DW_TAG_arg_variable;
1312 Metadata *Scope = MDTuple::getDistinct(Context, None);
1313 StringRef Name = "name";
1314 Metadata *File = MDTuple::getDistinct(Context, None);
1315 unsigned Line = 5;
1316 Metadata *Type = MDTuple::getDistinct(Context, None);
1317 unsigned Arg = 6;
1318 unsigned Flags = 7;
1319 Metadata *InlinedAt = MDTuple::getDistinct(Context, None);
1320
1321 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1322 Arg, Flags, InlinedAt);
1323 EXPECT_EQ(Tag, N->getTag());
1324 EXPECT_EQ(Scope, N->getScope());
1325 EXPECT_EQ(Name, N->getName());
1326 EXPECT_EQ(File, N->getFile());
1327 EXPECT_EQ(Line, N->getLine());
1328 EXPECT_EQ(Type, N->getType());
1329 EXPECT_EQ(Arg, N->getArg());
1330 EXPECT_EQ(Flags, N->getFlags());
1331 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1332 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1333 Arg, Flags, InlinedAt));
1334
1335 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1336 Name, File, Line, Type, Arg, Flags,
1337 InlinedAt));
1338 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1339 Type, Arg, Flags, InlinedAt));
1340 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1341 Type, Arg, Flags, InlinedAt));
1342 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1343 Type, Arg, Flags, InlinedAt));
1344 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1345 Type, Arg, Flags, InlinedAt));
1346 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1347 Scope, Arg, Flags, InlinedAt));
1348 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1349 Arg + 1, Flags, InlinedAt));
1350 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1351 Arg, ~Flags, InlinedAt));
1352 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1353 Arg, Flags, Scope));
1354}
1355
1356typedef MetadataTest MDExpressionTest;
1357
1358TEST_F(MDExpressionTest, get) {
1359 uint64_t Elements[] = {2, 6, 9, 78, 0};
1360 auto *N = MDExpression::get(Context, Elements);
1361 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1362 EXPECT_EQ(N, MDExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001363
1364 EXPECT_EQ(5u, N->getNumElements());
1365 EXPECT_EQ(2u, N->getElement(0));
1366 EXPECT_EQ(6u, N->getElement(1));
1367 EXPECT_EQ(9u, N->getElement(2));
1368 EXPECT_EQ(78u, N->getElement(3));
1369 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001370}
1371
1372typedef MetadataTest MDObjCPropertyTest;
1373
1374TEST_F(MDObjCPropertyTest, get) {
1375 StringRef Name = "name";
1376 Metadata *File = MDTuple::getDistinct(Context, None);
1377 unsigned Line = 5;
1378 StringRef GetterName = "getter";
1379 StringRef SetterName = "setter";
1380 unsigned Attributes = 7;
1381 Metadata *Type = MDTuple::getDistinct(Context, None);
1382
1383 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1384 SetterName, Attributes, Type);
1385
1386 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1387 EXPECT_EQ(Name, N->getName());
1388 EXPECT_EQ(File, N->getFile());
1389 EXPECT_EQ(Line, N->getLine());
1390 EXPECT_EQ(GetterName, N->getGetterName());
1391 EXPECT_EQ(SetterName, N->getSetterName());
1392 EXPECT_EQ(Attributes, N->getAttributes());
1393 EXPECT_EQ(Type, N->getType());
1394 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1395 SetterName, Attributes, Type));
1396
1397 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1398 SetterName, Attributes, Type));
1399 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1400 SetterName, Attributes, Type));
1401 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1402 SetterName, Attributes, Type));
1403 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1404 SetterName, Attributes, Type));
1405 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1406 "other", Attributes, Type));
1407 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1408 SetterName, Attributes + 1, Type));
1409 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1410 SetterName, Attributes, File));
1411}
1412
1413typedef MetadataTest MDImportedEntityTest;
1414
1415TEST_F(MDImportedEntityTest, get) {
1416 unsigned Tag = dwarf::DW_TAG_imported_module;
1417 Metadata *Scope = MDTuple::getDistinct(Context, None);
1418 Metadata *Entity = MDTuple::getDistinct(Context, None);
1419 unsigned Line = 5;
1420 StringRef Name = "name";
1421
1422 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1423
1424 EXPECT_EQ(Tag, N->getTag());
1425 EXPECT_EQ(Scope, N->getScope());
1426 EXPECT_EQ(Entity, N->getEntity());
1427 EXPECT_EQ(Line, N->getLine());
1428 EXPECT_EQ(Name, N->getName());
1429 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1430
1431 EXPECT_NE(N,
1432 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1433 Scope, Entity, Line, Name));
1434 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1435 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1436 EXPECT_NE(N,
1437 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1438 EXPECT_NE(N,
1439 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1440}
1441
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001442typedef MetadataTest MetadataAsValueTest;
1443
1444TEST_F(MetadataAsValueTest, MDNode) {
1445 MDNode *N = MDNode::get(Context, None);
1446 auto *V = MetadataAsValue::get(Context, N);
1447 EXPECT_TRUE(V->getType()->isMetadataTy());
1448 EXPECT_EQ(N, V->getMetadata());
1449
1450 auto *V2 = MetadataAsValue::get(Context, N);
1451 EXPECT_EQ(V, V2);
1452}
1453
1454TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1455 MDNode *N = MDNode::get(Context, None);
1456 Metadata *Ops[] = {N};
1457 MDNode *N2 = MDNode::get(Context, Ops);
1458 auto *V = MetadataAsValue::get(Context, N2);
1459 EXPECT_TRUE(V->getType()->isMetadataTy());
1460 EXPECT_EQ(N2, V->getMetadata());
1461
1462 auto *V2 = MetadataAsValue::get(Context, N2);
1463 EXPECT_EQ(V, V2);
1464
1465 auto *V3 = MetadataAsValue::get(Context, N);
1466 EXPECT_TRUE(V3->getType()->isMetadataTy());
1467 EXPECT_NE(V, V3);
1468 EXPECT_EQ(N, V3->getMetadata());
1469}
1470
1471TEST_F(MetadataAsValueTest, MDNodeConstant) {
1472 auto *C = ConstantInt::getTrue(Context);
1473 auto *MD = ConstantAsMetadata::get(C);
1474 Metadata *Ops[] = {MD};
1475 auto *N = MDNode::get(Context, Ops);
1476
1477 auto *V = MetadataAsValue::get(Context, MD);
1478 EXPECT_TRUE(V->getType()->isMetadataTy());
1479 EXPECT_EQ(MD, V->getMetadata());
1480
1481 auto *V2 = MetadataAsValue::get(Context, N);
1482 EXPECT_EQ(MD, V2->getMetadata());
1483 EXPECT_EQ(V, V2);
1484}
1485
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001486typedef MetadataTest ValueAsMetadataTest;
1487
1488TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1489 Type *Ty = Type::getInt1PtrTy(Context);
1490 std::unique_ptr<GlobalVariable> GV0(
1491 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1492 auto *MD = ValueAsMetadata::get(GV0.get());
1493 EXPECT_TRUE(MD->getValue() == GV0.get());
1494 ASSERT_TRUE(GV0->use_empty());
1495
1496 std::unique_ptr<GlobalVariable> GV1(
1497 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1498 GV0->replaceAllUsesWith(GV1.get());
1499 EXPECT_TRUE(MD->getValue() == GV1.get());
1500}
1501
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001502TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1503 // Create a constant.
1504 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1505 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1506
1507 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001508 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001509
1510 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001511 Metadata *Ops1[] = {CI, CI, Temp.get()};
1512 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001513
1514 auto *N1 = MDTuple::get(Context, Ops1);
1515 auto *N2 = MDTuple::get(Context, Ops2);
1516 ASSERT_NE(N1, N2);
1517
1518 // Tell metadata that the constant is getting deleted.
1519 //
1520 // After this, N1 will be invalid, so don't touch it.
1521 ValueAsMetadata::handleDeletion(CI->getValue());
1522 EXPECT_EQ(nullptr, N2->getOperand(0));
1523 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001524 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001525
1526 // Clean up Temp for teardown.
1527 Temp->replaceAllUsesWith(nullptr);
1528}
1529
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001530typedef MetadataTest TrackingMDRefTest;
1531
1532TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1533 Type *Ty = Type::getInt1PtrTy(Context);
1534 std::unique_ptr<GlobalVariable> GV0(
1535 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1536 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
1537 EXPECT_TRUE(MD->getValue() == GV0.get());
1538 ASSERT_TRUE(GV0->use_empty());
1539
1540 std::unique_ptr<GlobalVariable> GV1(
1541 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1542 GV0->replaceAllUsesWith(GV1.get());
1543 EXPECT_TRUE(MD->getValue() == GV1.get());
1544
1545 // Reset it, so we don't inadvertently test deletion.
1546 MD.reset();
1547}
1548
1549TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
1550 Type *Ty = Type::getInt1PtrTy(Context);
1551 std::unique_ptr<GlobalVariable> GV(
1552 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1553 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
1554 EXPECT_TRUE(MD->getValue() == GV.get());
1555 ASSERT_TRUE(GV->use_empty());
1556
1557 GV.reset();
1558 EXPECT_TRUE(!MD);
1559}
1560
Devang Patel0924b332009-07-30 00:03:41 +00001561TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00001562 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001563 ConstantAsMetadata *C =
1564 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
1565 ConstantAsMetadata *C2 =
1566 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00001567
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001568 Metadata *const V = C;
1569 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00001570 MDNode *n = MDNode::get(Context, V);
1571 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00001572
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00001573 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00001574 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00001575 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
1576 NMD->addOperand(n);
1577 NMD->addOperand(n2);
1578
Chris Lattnerbe354a62009-08-23 04:47:35 +00001579 std::string Str;
1580 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00001581 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00001582 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00001583 oss.str().c_str());
1584}
Nick Lewycky49f89192009-04-04 07:22:01 +00001585}