blob: 4de6ecc0f9b0c331932a97f911199308df175f59 [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"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000013#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/Instructions.h"
15#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Module.h"
18#include "llvm/IR/Type.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000019#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000020#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000021using namespace llvm;
22
23namespace {
24
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000025TEST(ContextAndReplaceableUsesTest, FromContext) {
26 LLVMContext Context;
27 ContextAndReplaceableUses CRU(Context);
28 EXPECT_EQ(&Context, &CRU.getContext());
29 EXPECT_FALSE(CRU.hasReplaceableUses());
30 EXPECT_FALSE(CRU.getReplaceableUses());
31}
32
33TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
34 LLVMContext Context;
35 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
36 EXPECT_EQ(&Context, &CRU.getContext());
37 EXPECT_TRUE(CRU.hasReplaceableUses());
38 EXPECT_TRUE(CRU.getReplaceableUses());
39}
40
41TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
42 LLVMContext Context;
43 ContextAndReplaceableUses CRU(Context);
44 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
45 EXPECT_EQ(&Context, &CRU.getContext());
46 EXPECT_TRUE(CRU.hasReplaceableUses());
47 EXPECT_TRUE(CRU.getReplaceableUses());
48}
49
50TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
51 LLVMContext Context;
52 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
53 auto *Ptr = ReplaceableUses.get();
54 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
55 ReplaceableUses = CRU.takeReplaceableUses();
56 EXPECT_EQ(&Context, &CRU.getContext());
57 EXPECT_FALSE(CRU.hasReplaceableUses());
58 EXPECT_FALSE(CRU.getReplaceableUses());
59 EXPECT_EQ(Ptr, ReplaceableUses.get());
60}
61
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000062class MetadataTest : public testing::Test {
63protected:
64 LLVMContext Context;
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000065 MDNode *getNode() { return MDNode::get(Context, None); }
66 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
67 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
68 Metadata *MDs[] = {MD1, MD2};
69 return MDNode::get(Context, MDs);
70 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000071};
72typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +000073
Nick Lewycky49f89192009-04-04 07:22:01 +000074// Test that construction of MDString with different value produces different
75// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000076TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +000077 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000078 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000079 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000080 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000081 EXPECT_NE(s1, s2);
82}
83
84// Test that creation of MDStrings with the same string contents produces the
85// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000086TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +000087 char x[4] = { 'a', 'b', 'c', 'X' };
88 char y[4] = { 'a', 'b', 'c', 'Y' };
89
Owen Anderson23587322009-07-31 21:38:10 +000090 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
91 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000092 EXPECT_EQ(s1, s2);
93}
94
95// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000096TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000097 char *str = new char[13];
98 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000099 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000100 strncpy(str, "aaaaaaaaaaaaa", 13);
101 delete[] str;
102
Chris Lattnerbe354a62009-08-23 04:47:35 +0000103 std::string Str;
104 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000105 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000106 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000107}
108
109// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000110TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000111 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000112 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000113 std::string Str;
114 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000115 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000116 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000117}
118
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000119typedef MetadataTest MDNodeTest;
120
Nick Lewycky49f89192009-04-04 07:22:01 +0000121// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000122TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000123 char x[3] = { 'a', 'b', 'c' };
124 char y[3] = { '1', '2', '3' };
125
Owen Anderson23587322009-07-31 21:38:10 +0000126 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
127 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000128 ConstantAsMetadata *CI = ConstantAsMetadata::get(
129 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000130
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000131 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000132 V.push_back(s1);
133 V.push_back(CI);
134 V.push_back(s2);
135
Jay Foad5514afe2011-04-21 19:59:31 +0000136 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000137 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000138 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000139 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000140 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000141 MDNode *n4 = MDNode::getIfExists(Context, V);
142 MDNode *n5 = MDNode::getIfExists(Context, c1);
143 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000144 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000145 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000146 EXPECT_EQ(n4, n1);
147 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000148 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000149
Chris Lattner9b493022009-12-31 01:22:29 +0000150 EXPECT_EQ(3u, n1->getNumOperands());
151 EXPECT_EQ(s1, n1->getOperand(0));
152 EXPECT_EQ(CI, n1->getOperand(1));
153 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000154
Chris Lattner9b493022009-12-31 01:22:29 +0000155 EXPECT_EQ(1u, n2->getNumOperands());
156 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000157}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000158
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000159TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000160 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
161 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000162
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000163 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000164 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000165 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000166
167 EXPECT_EQ(n, wvh);
168
169 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000170}
Devang Patel0924b332009-07-30 00:03:41 +0000171
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000172TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000173 // !0 = !{!0}
174 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000175 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000176 auto Temp = MDNode::getTemporary(Context, None);
177 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000178 MDNode *Self = MDNode::get(Context, Args);
179 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000180 ASSERT_EQ(Self, Self->getOperand(0));
181
182 // Self-references should be distinct, so MDNode::get() should grab a
183 // uniqued node that references Self, not Self.
184 Args[0] = Self;
185 MDNode *Ref1 = MDNode::get(Context, Args);
186 MDNode *Ref2 = MDNode::get(Context, Args);
187 EXPECT_NE(Self, Ref1);
188 EXPECT_EQ(Ref1, Ref2);
189 }
190
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000191 // !0 = !{!0, !{}}
192 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000193 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000194 auto Temp = MDNode::getTemporary(Context, None);
195 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000196 MDNode *Self = MDNode::get(Context, Args);
197 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000198 ASSERT_EQ(Self, Self->getOperand(0));
199
200 // Self-references should be distinct, so MDNode::get() should grab a
201 // uniqued node that references Self, not Self itself.
202 Args[0] = Self;
203 MDNode *Ref1 = MDNode::get(Context, Args);
204 MDNode *Ref2 = MDNode::get(Context, Args);
205 EXPECT_NE(Self, Ref1);
206 EXPECT_EQ(Ref1, Ref2);
207 }
208}
209
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000210TEST_F(MDNodeTest, Print) {
211 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
212 MDString *S = MDString::get(Context, "foo");
213 MDNode *N0 = getNode();
214 MDNode *N1 = getNode(N0);
215 MDNode *N2 = getNode(N0, N1);
216
217 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
218 MDNode *N = MDNode::get(Context, Args);
219
220 std::string Expected;
221 {
222 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000223 OS << "!{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000224 C->printAsOperand(OS);
225 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000226 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000227 OS << ", null";
228 MDNode *Nodes[] = {N0, N1, N2};
229 for (auto *Node : Nodes)
230 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000231 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000232 }
233
234 std::string Actual;
235 {
236 raw_string_ostream OS(Actual);
237 N->print(OS);
238 }
239
240 EXPECT_EQ(Expected, Actual);
241}
242
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000243TEST_F(MDNodeTest, NullOperand) {
244 // metadata !{}
245 MDNode *Empty = MDNode::get(Context, None);
246
247 // metadata !{metadata !{}}
248 Metadata *Ops[] = {Empty};
249 MDNode *N = MDNode::get(Context, Ops);
250 ASSERT_EQ(Empty, N->getOperand(0));
251
252 // metadata !{metadata !{}} => metadata !{null}
253 N->replaceOperandWith(0, nullptr);
254 ASSERT_EQ(nullptr, N->getOperand(0));
255
256 // metadata !{null}
257 Ops[0] = nullptr;
258 MDNode *NullOp = MDNode::get(Context, Ops);
259 ASSERT_EQ(nullptr, NullOp->getOperand(0));
260 EXPECT_EQ(N, NullOp);
261}
262
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000263TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
264 // !{}
265 MDNode *Empty = MDNode::get(Context, None);
266 ASSERT_TRUE(Empty->isResolved());
267 EXPECT_FALSE(Empty->isDistinct());
268
269 // !{!{}}
270 Metadata *Wrapped1Ops[] = {Empty};
271 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
272 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
273 ASSERT_TRUE(Wrapped1->isResolved());
274 EXPECT_FALSE(Wrapped1->isDistinct());
275
276 // !{!{!{}}}
277 Metadata *Wrapped2Ops[] = {Wrapped1};
278 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
279 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
280 ASSERT_TRUE(Wrapped2->isResolved());
281 EXPECT_FALSE(Wrapped2->isDistinct());
282
283 // !{!{!{}}} => !{!{}}
284 Wrapped2->replaceOperandWith(0, Empty);
285 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
286 EXPECT_TRUE(Wrapped2->isDistinct());
287 EXPECT_FALSE(Wrapped1->isDistinct());
288}
289
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000290TEST_F(MDNodeTest, getDistinct) {
291 // !{}
292 MDNode *Empty = MDNode::get(Context, None);
293 ASSERT_TRUE(Empty->isResolved());
294 ASSERT_FALSE(Empty->isDistinct());
295 ASSERT_EQ(Empty, MDNode::get(Context, None));
296
297 // distinct !{}
298 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
299 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
300 EXPECT_TRUE(Distinct1->isResolved());
301 EXPECT_TRUE(Distinct2->isDistinct());
302 EXPECT_NE(Empty, Distinct1);
303 EXPECT_NE(Empty, Distinct2);
304 EXPECT_NE(Distinct1, Distinct2);
305
306 // !{}
307 ASSERT_EQ(Empty, MDNode::get(Context, None));
308}
309
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000310TEST_F(MDNodeTest, isUniqued) {
311 MDNode *U = MDTuple::get(Context, None);
312 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000313 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000314 EXPECT_TRUE(U->isUniqued());
315 EXPECT_FALSE(D->isUniqued());
316 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000317}
318
319TEST_F(MDNodeTest, isDistinct) {
320 MDNode *U = MDTuple::get(Context, None);
321 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000322 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000323 EXPECT_FALSE(U->isDistinct());
324 EXPECT_TRUE(D->isDistinct());
325 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000326}
327
328TEST_F(MDNodeTest, isTemporary) {
329 MDNode *U = MDTuple::get(Context, None);
330 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000331 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000332 EXPECT_FALSE(U->isTemporary());
333 EXPECT_FALSE(D->isTemporary());
334 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000335}
336
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000337TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
338 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000339 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000340 ASSERT_FALSE(Temp->isResolved());
341
342 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000343 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000344 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
345 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000346 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000347
348 // temporary !{} => !{}
349 MDNode *Empty = MDNode::get(Context, None);
350 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000351 EXPECT_EQ(Empty, Distinct->getOperand(0));
352}
353
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000354TEST_F(MDNodeTest, handleChangedOperandRecursion) {
355 // !0 = !{}
356 MDNode *N0 = MDNode::get(Context, None);
357
358 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000359 auto Temp3 = MDTuple::getTemporary(Context, None);
360 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000361 MDNode *N1 = MDNode::get(Context, Ops1);
362
363 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000364 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000365 MDNode *N2 = MDNode::get(Context, Ops2);
366
367 // !3 = !{!2}
368 Metadata *Ops3[] = {N2};
369 MDNode *N3 = MDNode::get(Context, Ops3);
370 Temp3->replaceAllUsesWith(N3);
371
372 // !4 = !{!1}
373 Metadata *Ops4[] = {N1};
374 MDNode *N4 = MDNode::get(Context, Ops4);
375
376 // Confirm that the cycle prevented RAUW from getting dropped.
377 EXPECT_TRUE(N0->isResolved());
378 EXPECT_FALSE(N1->isResolved());
379 EXPECT_FALSE(N2->isResolved());
380 EXPECT_FALSE(N3->isResolved());
381 EXPECT_FALSE(N4->isResolved());
382
383 // Create a couple of distinct nodes to observe what's going on.
384 //
385 // !5 = distinct !{!2}
386 // !6 = distinct !{!3}
387 Metadata *Ops5[] = {N2};
388 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
389 Metadata *Ops6[] = {N3};
390 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
391
392 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
393 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
394 // references !3, this can cause a re-entry of handleChangedOperand() when !3
395 // is not ready for it.
396 //
397 // !2->replaceOperandWith(1, nullptr)
398 // !2: !{!3, !0} => !{!3, null}
399 // !2->replaceAllUsesWith(!1)
400 // !3: !{!2] => !{!1}
401 // !3->replaceAllUsesWith(!4)
402 N2->replaceOperandWith(1, nullptr);
403
404 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
405 // under us. Just check that the other nodes are sane.
406 //
407 // !1 = !{!4, null}
408 // !4 = !{!1}
409 // !5 = distinct !{!1}
410 // !6 = distinct !{!4}
411 EXPECT_EQ(N4, N1->getOperand(0));
412 EXPECT_EQ(N1, N4->getOperand(0));
413 EXPECT_EQ(N1, N5->getOperand(0));
414 EXPECT_EQ(N4, N6->getOperand(0));
415}
416
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000417TEST_F(MDNodeTest, replaceResolvedOperand) {
418 // Check code for replacing one resolved operand with another. If doing this
419 // directly (via replaceOperandWith()) becomes illegal, change the operand to
420 // a global value that gets RAUW'ed.
421 //
422 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000423 auto Temp = MDTuple::getTemporary(Context, None);
424 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000425
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000426 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000427 MDNode *N = MDTuple::get(Context, Ops);
428 EXPECT_EQ(nullptr, N->getOperand(0));
429 ASSERT_FALSE(N->isResolved());
430
431 // Check code for replacing resolved nodes.
432 N->replaceOperandWith(0, Empty);
433 EXPECT_EQ(Empty, N->getOperand(0));
434
435 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000436 N->replaceOperandWith(0, Temp.get());
437 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000438
439 // Remove the references to Temp; required for teardown.
440 Temp->replaceAllUsesWith(nullptr);
441}
442
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000443TEST_F(MDNodeTest, replaceWithUniqued) {
444 auto *Empty = MDTuple::get(Context, None);
445 MDTuple *FirstUniqued;
446 {
447 Metadata *Ops[] = {Empty};
448 auto Temp = MDTuple::getTemporary(Context, Ops);
449 EXPECT_TRUE(Temp->isTemporary());
450
451 // Don't expect a collision.
452 auto *Current = Temp.get();
453 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
454 EXPECT_TRUE(FirstUniqued->isUniqued());
455 EXPECT_TRUE(FirstUniqued->isResolved());
456 EXPECT_EQ(Current, FirstUniqued);
457 }
458 {
459 Metadata *Ops[] = {Empty};
460 auto Temp = MDTuple::getTemporary(Context, Ops);
461 EXPECT_TRUE(Temp->isTemporary());
462
463 // Should collide with Uniqued above this time.
464 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
465 EXPECT_TRUE(Uniqued->isUniqued());
466 EXPECT_TRUE(Uniqued->isResolved());
467 EXPECT_EQ(FirstUniqued, Uniqued);
468 }
469 {
470 auto Unresolved = MDTuple::getTemporary(Context, None);
471 Metadata *Ops[] = {Unresolved.get()};
472 auto Temp = MDTuple::getTemporary(Context, Ops);
473 EXPECT_TRUE(Temp->isTemporary());
474
475 // Shouldn't be resolved.
476 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
477 EXPECT_TRUE(Uniqued->isUniqued());
478 EXPECT_FALSE(Uniqued->isResolved());
479
480 // Should be a different node.
481 EXPECT_NE(FirstUniqued, Uniqued);
482
483 // Should resolve when we update its node (note: be careful to avoid a
484 // collision with any other nodes above).
485 Uniqued->replaceOperandWith(0, nullptr);
486 EXPECT_TRUE(Uniqued->isResolved());
487 }
488}
489
490TEST_F(MDNodeTest, replaceWithDistinct) {
491 {
492 auto *Empty = MDTuple::get(Context, None);
493 Metadata *Ops[] = {Empty};
494 auto Temp = MDTuple::getTemporary(Context, Ops);
495 EXPECT_TRUE(Temp->isTemporary());
496
497 // Don't expect a collision.
498 auto *Current = Temp.get();
499 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
500 EXPECT_TRUE(Distinct->isDistinct());
501 EXPECT_TRUE(Distinct->isResolved());
502 EXPECT_EQ(Current, Distinct);
503 }
504 {
505 auto Unresolved = MDTuple::getTemporary(Context, None);
506 Metadata *Ops[] = {Unresolved.get()};
507 auto Temp = MDTuple::getTemporary(Context, Ops);
508 EXPECT_TRUE(Temp->isTemporary());
509
510 // Don't expect a collision.
511 auto *Current = Temp.get();
512 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
513 EXPECT_TRUE(Distinct->isDistinct());
514 EXPECT_TRUE(Distinct->isResolved());
515 EXPECT_EQ(Current, Distinct);
516
517 // Cleanup; required for teardown.
518 Unresolved->replaceAllUsesWith(nullptr);
519 }
520}
521
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000522TEST_F(MDNodeTest, replaceWithPermanent) {
523 Metadata *Ops[] = {nullptr};
524 auto Temp = MDTuple::getTemporary(Context, Ops);
525 auto *T = Temp.get();
526
527 // U is a normal, uniqued node that references T.
528 auto *U = MDTuple::get(Context, T);
529 EXPECT_TRUE(U->isUniqued());
530
531 // Make Temp self-referencing.
532 Temp->replaceOperandWith(0, T);
533
534 // Try to uniquify Temp. This should, despite the name in the API, give a
535 // 'distinct' node, since self-references aren't allowed to be uniqued.
536 //
537 // Since it's distinct, N should have the same address as when it was a
538 // temporary (i.e., be equal to T not U).
539 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
540 EXPECT_EQ(N, T);
541 EXPECT_TRUE(N->isDistinct());
542
543 // U should be the canonical unique node with N as the argument.
544 EXPECT_EQ(U, MDTuple::get(Context, N));
545 EXPECT_TRUE(U->isUniqued());
546
547 // This temporary should collide with U when replaced, but it should still be
548 // uniqued.
549 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
550 EXPECT_TRUE(U->isUniqued());
551
552 // This temporary should become a new uniqued node.
553 auto Temp2 = MDTuple::getTemporary(Context, U);
554 auto *V = Temp2.get();
555 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
556 EXPECT_TRUE(V->isUniqued());
557 EXPECT_EQ(U, V->getOperand(0));
558}
559
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000560TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
561 TrackingMDRef Ref;
562 EXPECT_EQ(nullptr, Ref.get());
563 {
564 auto Temp = MDTuple::getTemporary(Context, None);
565 Ref.reset(Temp.get());
566 EXPECT_EQ(Temp.get(), Ref.get());
567 }
568 EXPECT_EQ(nullptr, Ref.get());
569}
570
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000571typedef MetadataTest MDLocationTest;
572
573TEST_F(MDLocationTest, Overflow) {
574 MDNode *N = MDNode::get(Context, None);
575 {
576 MDLocation *L = MDLocation::get(Context, 2, 7, N);
577 EXPECT_EQ(2u, L->getLine());
578 EXPECT_EQ(7u, L->getColumn());
579 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000580 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000581 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000582 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
583 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000584 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000585 }
586 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000587 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
588 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000589 EXPECT_EQ(0u, L->getColumn());
590 }
591 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000592 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
593 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000594 EXPECT_EQ(0u, L->getColumn());
595 }
596}
597
598TEST_F(MDLocationTest, getDistinct) {
599 MDNode *N = MDNode::get(Context, None);
600 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
601 EXPECT_TRUE(L0->isDistinct());
602 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
603 EXPECT_FALSE(L1->isDistinct());
604 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
605}
606
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000607TEST_F(MDLocationTest, getTemporary) {
608 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000609 auto L = MDLocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000610 EXPECT_TRUE(L->isTemporary());
611 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000612}
613
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000614typedef MetadataTest GenericDebugNodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000615
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000616TEST_F(GenericDebugNodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000617 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000618 auto *Empty = MDNode::get(Context, None);
619 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000620 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000621 EXPECT_EQ(15u, N->getTag());
622 EXPECT_EQ(2u, N->getNumOperands());
623 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000624 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000625 EXPECT_EQ(1u, N->getNumDwarfOperands());
626 EXPECT_EQ(Empty, N->getDwarfOperand(0));
627 EXPECT_EQ(Empty, N->getOperand(1));
628 ASSERT_TRUE(N->isUniqued());
629
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000630 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000631
632 N->replaceOperandWith(1, nullptr);
633 EXPECT_EQ(15u, N->getTag());
634 EXPECT_EQ(Header, N->getHeader());
635 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
636 ASSERT_TRUE(N->isUniqued());
637
638 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000639 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000640
641 N->replaceDwarfOperandWith(0, Empty);
642 EXPECT_EQ(15u, N->getTag());
643 EXPECT_EQ(Header, N->getHeader());
644 EXPECT_EQ(Empty, N->getDwarfOperand(0));
645 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000646 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000647
648 TempGenericDebugNode Temp = N->clone();
649 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000650}
651
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000652TEST_F(GenericDebugNodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000653 // Canonicalize !"" to null.
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000654 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
655 EXPECT_EQ(StringRef(), N->getHeader());
656 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000657}
658
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000659typedef MetadataTest MDSubrangeTest;
660
661TEST_F(MDSubrangeTest, get) {
662 auto *N = MDSubrange::get(Context, 5, 7);
663 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
664 EXPECT_EQ(5, N->getCount());
665 EXPECT_EQ(7, N->getLo());
666 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
667 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000668
669 TempMDSubrange Temp = N->clone();
670 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000671}
672
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000673TEST_F(MDSubrangeTest, getEmptyArray) {
674 auto *N = MDSubrange::get(Context, -1, 0);
675 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
676 EXPECT_EQ(-1, N->getCount());
677 EXPECT_EQ(0, N->getLo());
678 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
679}
680
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000681typedef MetadataTest MDEnumeratorTest;
682
683TEST_F(MDEnumeratorTest, get) {
684 auto *N = MDEnumerator::get(Context, 7, "name");
685 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
686 EXPECT_EQ(7, N->getValue());
687 EXPECT_EQ("name", N->getName());
688 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
689
690 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
691 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000692
693 TempMDEnumerator Temp = N->clone();
694 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000695}
696
697typedef MetadataTest MDBasicTypeTest;
698
699TEST_F(MDBasicTypeTest, get) {
700 auto *N =
701 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
702 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
703 EXPECT_EQ("special", N->getName());
704 EXPECT_EQ(33u, N->getSizeInBits());
705 EXPECT_EQ(26u, N->getAlignInBits());
706 EXPECT_EQ(7u, N->getEncoding());
707 EXPECT_EQ(0u, N->getLine());
708 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
709 26, 7));
710
711 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
712 "special", 33, 26, 7));
713 EXPECT_NE(N,
714 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
715 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
716 26, 7));
717 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
718 25, 7));
719 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
720 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000721
722 TempMDBasicType Temp = N->clone();
723 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000724}
725
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000726TEST_F(MDBasicTypeTest, getWithLargeValues) {
727 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
728 UINT64_MAX, UINT64_MAX - 1, 7);
729 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
730 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
731}
732
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000733typedef MetadataTest MDDerivedTypeTest;
734
735TEST_F(MDDerivedTypeTest, get) {
736 Metadata *File = MDTuple::getDistinct(Context, None);
737 Metadata *Scope = MDTuple::getDistinct(Context, None);
738 Metadata *BaseType = MDTuple::getDistinct(Context, None);
739 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
740
741 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
742 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
743 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
744 EXPECT_EQ("something", N->getName());
745 EXPECT_EQ(File, N->getFile());
746 EXPECT_EQ(1u, N->getLine());
747 EXPECT_EQ(Scope, N->getScope());
748 EXPECT_EQ(BaseType, N->getBaseType());
749 EXPECT_EQ(2u, N->getSizeInBits());
750 EXPECT_EQ(3u, N->getAlignInBits());
751 EXPECT_EQ(4u, N->getOffsetInBits());
752 EXPECT_EQ(5u, N->getFlags());
753 EXPECT_EQ(ExtraData, N->getExtraData());
754 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
755 "something", File, 1, Scope, BaseType, 2, 3,
756 4, 5, ExtraData));
757
758 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
759 "something", File, 1, Scope, BaseType, 2, 3,
760 4, 5, ExtraData));
761 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
762 File, 1, Scope, BaseType, 2, 3, 4, 5,
763 ExtraData));
764 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
765 "something", Scope, 1, Scope, BaseType, 2, 3,
766 4, 5, ExtraData));
767 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
768 "something", File, 2, Scope, BaseType, 2, 3,
769 4, 5, ExtraData));
770 EXPECT_NE(N,
771 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
772 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
773 EXPECT_NE(N,
774 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
775 File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
776 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
777 "something", File, 1, Scope, BaseType, 3, 3,
778 4, 5, ExtraData));
779 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
780 "something", File, 1, Scope, BaseType, 2, 2,
781 4, 5, ExtraData));
782 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
783 "something", File, 1, Scope, BaseType, 2, 3,
784 5, 5, ExtraData));
785 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
786 "something", File, 1, Scope, BaseType, 2, 3,
787 4, 4, ExtraData));
788 EXPECT_NE(N,
789 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
790 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000791
792 TempMDDerivedType Temp = N->clone();
793 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000794}
795
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000796TEST_F(MDDerivedTypeTest, getWithLargeValues) {
797 Metadata *File = MDTuple::getDistinct(Context, None);
798 Metadata *Scope = MDTuple::getDistinct(Context, None);
799 Metadata *BaseType = MDTuple::getDistinct(Context, None);
800 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
801
802 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
803 File, 1, Scope, BaseType, UINT64_MAX,
804 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
805 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
806 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
807 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
808}
809
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000810typedef MetadataTest MDCompositeTypeTest;
811
812TEST_F(MDCompositeTypeTest, get) {
813 unsigned Tag = dwarf::DW_TAG_structure_type;
814 StringRef Name = "some name";
815 Metadata *File = MDTuple::getDistinct(Context, None);
816 unsigned Line = 1;
817 Metadata *Scope = MDTuple::getDistinct(Context, None);
818 Metadata *BaseType = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000819 uint64_t SizeInBits = 2;
820 uint64_t AlignInBits = 3;
821 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000822 unsigned Flags = 5;
823 Metadata *Elements = MDTuple::getDistinct(Context, None);
824 unsigned RuntimeLang = 6;
825 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
826 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
827 StringRef Identifier = "some id";
828
829 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
830 BaseType, SizeInBits, AlignInBits,
831 OffsetInBits, Flags, Elements, RuntimeLang,
832 VTableHolder, TemplateParams, Identifier);
833 EXPECT_EQ(Tag, N->getTag());
834 EXPECT_EQ(Name, N->getName());
835 EXPECT_EQ(File, N->getFile());
836 EXPECT_EQ(Line, N->getLine());
837 EXPECT_EQ(Scope, N->getScope());
838 EXPECT_EQ(BaseType, N->getBaseType());
839 EXPECT_EQ(SizeInBits, N->getSizeInBits());
840 EXPECT_EQ(AlignInBits, N->getAlignInBits());
841 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
842 EXPECT_EQ(Flags, N->getFlags());
843 EXPECT_EQ(Elements, N->getElements());
844 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
845 EXPECT_EQ(VTableHolder, N->getVTableHolder());
846 EXPECT_EQ(TemplateParams, N->getTemplateParams());
847 EXPECT_EQ(Identifier, N->getIdentifier());
848
849 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
850 BaseType, SizeInBits, AlignInBits,
851 OffsetInBits, Flags, Elements, RuntimeLang,
852 VTableHolder, TemplateParams, Identifier));
853
854 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
855 BaseType, SizeInBits, AlignInBits,
856 OffsetInBits, Flags, Elements, RuntimeLang,
857 VTableHolder, TemplateParams, Identifier));
858 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
859 BaseType, SizeInBits, AlignInBits,
860 OffsetInBits, Flags, Elements, RuntimeLang,
861 VTableHolder, TemplateParams, Identifier));
862 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
863 BaseType, SizeInBits, AlignInBits,
864 OffsetInBits, Flags, Elements, RuntimeLang,
865 VTableHolder, TemplateParams, Identifier));
866 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
867 BaseType, SizeInBits, AlignInBits,
868 OffsetInBits, Flags, Elements, RuntimeLang,
869 VTableHolder, TemplateParams, Identifier));
870 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
871 BaseType, SizeInBits, AlignInBits,
872 OffsetInBits, Flags, Elements, RuntimeLang,
873 VTableHolder, TemplateParams, Identifier));
874 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
875 SizeInBits, AlignInBits, OffsetInBits,
876 Flags, Elements, RuntimeLang, VTableHolder,
877 TemplateParams, Identifier));
878 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
879 BaseType, SizeInBits + 1, AlignInBits,
880 OffsetInBits, Flags, Elements, RuntimeLang,
881 VTableHolder, TemplateParams, Identifier));
882 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
883 BaseType, SizeInBits, AlignInBits + 1,
884 OffsetInBits, Flags, Elements, RuntimeLang,
885 VTableHolder, TemplateParams, Identifier));
886 EXPECT_NE(N, MDCompositeType::get(
887 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
888 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
889 VTableHolder, TemplateParams, Identifier));
890 EXPECT_NE(N, MDCompositeType::get(
891 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
892 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
893 VTableHolder, TemplateParams, Identifier));
894 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
895 BaseType, SizeInBits, AlignInBits,
896 OffsetInBits, Flags, File, RuntimeLang,
897 VTableHolder, TemplateParams, Identifier));
898 EXPECT_NE(N, MDCompositeType::get(
899 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
900 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
901 VTableHolder, TemplateParams, Identifier));
902 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
903 BaseType, SizeInBits, AlignInBits,
904 OffsetInBits, Flags, Elements, RuntimeLang,
905 File, TemplateParams, Identifier));
906 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
907 BaseType, SizeInBits, AlignInBits,
908 OffsetInBits, Flags, Elements, RuntimeLang,
909 VTableHolder, File, Identifier));
910 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
911 BaseType, SizeInBits, AlignInBits,
912 OffsetInBits, Flags, Elements, RuntimeLang,
913 VTableHolder, TemplateParams, "other"));
914
915 // Be sure that missing identifiers get null pointers.
916 EXPECT_FALSE(MDCompositeType::get(
917 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
918 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
919 VTableHolder, TemplateParams, "")->getRawIdentifier());
920 EXPECT_FALSE(MDCompositeType::get(
921 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
922 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
923 VTableHolder, TemplateParams)->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000924
925 TempMDCompositeType Temp = N->clone();
926 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000927}
928
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000929TEST_F(MDCompositeTypeTest, getWithLargeValues) {
930 unsigned Tag = dwarf::DW_TAG_structure_type;
931 StringRef Name = "some name";
932 Metadata *File = MDTuple::getDistinct(Context, None);
933 unsigned Line = 1;
934 Metadata *Scope = MDTuple::getDistinct(Context, None);
935 Metadata *BaseType = MDTuple::getDistinct(Context, None);
936 uint64_t SizeInBits = UINT64_MAX;
937 uint64_t AlignInBits = UINT64_MAX - 1;
938 uint64_t OffsetInBits = UINT64_MAX - 2;
939 unsigned Flags = 5;
940 Metadata *Elements = MDTuple::getDistinct(Context, None);
941 unsigned RuntimeLang = 6;
942 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
943 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
944 StringRef Identifier = "some id";
945
946 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
947 BaseType, SizeInBits, AlignInBits,
948 OffsetInBits, Flags, Elements, RuntimeLang,
949 VTableHolder, TemplateParams, Identifier);
950 EXPECT_EQ(SizeInBits, N->getSizeInBits());
951 EXPECT_EQ(AlignInBits, N->getAlignInBits());
952 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
953}
954
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +0000955TEST_F(MDCompositeTypeTest, replaceOperands) {
956 unsigned Tag = dwarf::DW_TAG_structure_type;
957 StringRef Name = "some name";
958 Metadata *File = MDTuple::getDistinct(Context, None);
959 unsigned Line = 1;
960 Metadata *Scope = MDTuple::getDistinct(Context, None);
961 Metadata *BaseType = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000962 uint64_t SizeInBits = 2;
963 uint64_t AlignInBits = 3;
964 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +0000965 unsigned Flags = 5;
966 unsigned RuntimeLang = 6;
967 StringRef Identifier = "some id";
968
969 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
970 BaseType, SizeInBits, AlignInBits,
971 OffsetInBits, Flags, nullptr, RuntimeLang,
972 nullptr, nullptr, Identifier);
973
974 auto *Elements = MDTuple::getDistinct(Context, None);
975 EXPECT_EQ(nullptr, N->getElements());
976 N->replaceElements(Elements);
977 EXPECT_EQ(Elements, N->getElements());
978 N->replaceElements(nullptr);
979 EXPECT_EQ(nullptr, N->getElements());
980
981 auto *VTableHolder = MDTuple::getDistinct(Context, None);
982 EXPECT_EQ(nullptr, N->getVTableHolder());
983 N->replaceVTableHolder(VTableHolder);
984 EXPECT_EQ(VTableHolder, N->getVTableHolder());
985 N->replaceVTableHolder(nullptr);
986 EXPECT_EQ(nullptr, N->getVTableHolder());
987
988 auto *TemplateParams = MDTuple::getDistinct(Context, None);
989 EXPECT_EQ(nullptr, N->getTemplateParams());
990 N->replaceTemplateParams(TemplateParams);
991 EXPECT_EQ(TemplateParams, N->getTemplateParams());
992 N->replaceTemplateParams(nullptr);
993 EXPECT_EQ(nullptr, N->getTemplateParams());
994}
995
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000996typedef MetadataTest MDSubroutineTypeTest;
997
998TEST_F(MDSubroutineTypeTest, get) {
999 unsigned Flags = 1;
1000 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
1001
1002 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1003 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1004 EXPECT_EQ(Flags, N->getFlags());
1005 EXPECT_EQ(TypeArray, N->getTypeArray());
1006 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1007
1008 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1009 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
1010 MDTuple::getDistinct(Context, None)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001011
1012 TempMDSubroutineType Temp = N->clone();
1013 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001014
1015 // Test always-empty operands.
1016 EXPECT_EQ(nullptr, N->getScope());
1017 EXPECT_EQ(nullptr, N->getFile());
1018 EXPECT_EQ("", N->getName());
1019 EXPECT_EQ(nullptr, N->getBaseType());
1020 EXPECT_EQ(nullptr, N->getVTableHolder());
1021 EXPECT_EQ(nullptr, N->getTemplateParams());
1022 EXPECT_EQ("", N->getIdentifier());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001023}
1024
1025typedef MetadataTest MDFileTest;
1026
1027TEST_F(MDFileTest, get) {
1028 StringRef Filename = "file";
1029 StringRef Directory = "dir";
1030 auto *N = MDFile::get(Context, Filename, Directory);
1031
1032 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1033 EXPECT_EQ(Filename, N->getFilename());
1034 EXPECT_EQ(Directory, N->getDirectory());
1035 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1036
1037 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1038 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001039
1040 TempMDFile Temp = N->clone();
1041 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001042}
1043
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001044TEST_F(MDFileTest, ScopeGetFile) {
1045 // Ensure that MDScope::getFile() returns itself.
1046 MDScope *N = MDFile::get(Context, "file", "dir");
1047 EXPECT_EQ(N, N->getFile());
1048}
1049
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001050typedef MetadataTest MDCompileUnitTest;
1051
1052TEST_F(MDCompileUnitTest, get) {
1053 unsigned SourceLanguage = 1;
1054 Metadata *File = MDTuple::getDistinct(Context, None);
1055 StringRef Producer = "some producer";
1056 bool IsOptimized = false;
1057 StringRef Flags = "flag after flag";
1058 unsigned RuntimeVersion = 2;
1059 StringRef SplitDebugFilename = "another/file";
1060 unsigned EmissionKind = 3;
1061 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1062 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1063 Metadata *Subprograms = MDTuple::getDistinct(Context, None);
1064 Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
1065 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1066 auto *N = MDCompileUnit::get(
1067 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1068 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1069 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1070
1071 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1072 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1073 EXPECT_EQ(File, N->getFile());
1074 EXPECT_EQ(Producer, N->getProducer());
1075 EXPECT_EQ(IsOptimized, N->isOptimized());
1076 EXPECT_EQ(Flags, N->getFlags());
1077 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1078 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1079 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1080 EXPECT_EQ(EnumTypes, N->getEnumTypes());
1081 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1082 EXPECT_EQ(Subprograms, N->getSubprograms());
1083 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1084 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1085 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1086 IsOptimized, Flags, RuntimeVersion,
1087 SplitDebugFilename, EmissionKind, EnumTypes,
1088 RetainedTypes, Subprograms, GlobalVariables,
1089 ImportedEntities));
1090
1091 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1092 IsOptimized, Flags, RuntimeVersion,
1093 SplitDebugFilename, EmissionKind, EnumTypes,
1094 RetainedTypes, Subprograms, GlobalVariables,
1095 ImportedEntities));
1096 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
1097 IsOptimized, Flags, RuntimeVersion,
1098 SplitDebugFilename, EmissionKind, EnumTypes,
1099 RetainedTypes, Subprograms, GlobalVariables,
1100 ImportedEntities));
1101 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1102 IsOptimized, Flags, RuntimeVersion,
1103 SplitDebugFilename, EmissionKind, EnumTypes,
1104 RetainedTypes, Subprograms, GlobalVariables,
1105 ImportedEntities));
1106 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1107 !IsOptimized, Flags, RuntimeVersion,
1108 SplitDebugFilename, EmissionKind, EnumTypes,
1109 RetainedTypes, Subprograms, GlobalVariables,
1110 ImportedEntities));
1111 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1112 IsOptimized, "other", RuntimeVersion,
1113 SplitDebugFilename, EmissionKind, EnumTypes,
1114 RetainedTypes, Subprograms, GlobalVariables,
1115 ImportedEntities));
1116 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1117 IsOptimized, Flags, RuntimeVersion + 1,
1118 SplitDebugFilename, EmissionKind, EnumTypes,
1119 RetainedTypes, Subprograms, GlobalVariables,
1120 ImportedEntities));
1121 EXPECT_NE(N,
1122 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1123 IsOptimized, Flags, RuntimeVersion, "other",
1124 EmissionKind, EnumTypes, RetainedTypes,
1125 Subprograms, GlobalVariables, ImportedEntities));
1126 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1127 IsOptimized, Flags, RuntimeVersion,
1128 SplitDebugFilename, EmissionKind + 1,
1129 EnumTypes, RetainedTypes, Subprograms,
1130 GlobalVariables, ImportedEntities));
1131 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1132 IsOptimized, Flags, RuntimeVersion,
1133 SplitDebugFilename, EmissionKind, File,
1134 RetainedTypes, Subprograms, GlobalVariables,
1135 ImportedEntities));
1136 EXPECT_NE(N, MDCompileUnit::get(
1137 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1138 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1139 File, Subprograms, GlobalVariables, ImportedEntities));
1140 EXPECT_NE(N, MDCompileUnit::get(
1141 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1142 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1143 RetainedTypes, File, GlobalVariables, ImportedEntities));
1144 EXPECT_NE(N, MDCompileUnit::get(
1145 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1146 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1147 RetainedTypes, Subprograms, File, ImportedEntities));
1148 EXPECT_NE(N, MDCompileUnit::get(
1149 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1150 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1151 RetainedTypes, Subprograms, GlobalVariables, File));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001152
1153 TempMDCompileUnit Temp = N->clone();
1154 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001155}
1156
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001157TEST_F(MDCompileUnitTest, replaceArrays) {
1158 unsigned SourceLanguage = 1;
1159 Metadata *File = MDTuple::getDistinct(Context, None);
1160 StringRef Producer = "some producer";
1161 bool IsOptimized = false;
1162 StringRef Flags = "flag after flag";
1163 unsigned RuntimeVersion = 2;
1164 StringRef SplitDebugFilename = "another/file";
1165 unsigned EmissionKind = 3;
1166 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1167 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1168 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1169 auto *N = MDCompileUnit::get(
1170 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1171 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1172 RetainedTypes, nullptr, nullptr, ImportedEntities);
1173
1174 auto *Subprograms = MDTuple::getDistinct(Context, None);
1175 EXPECT_EQ(nullptr, N->getSubprograms());
1176 N->replaceSubprograms(Subprograms);
1177 EXPECT_EQ(Subprograms, N->getSubprograms());
1178 N->replaceSubprograms(nullptr);
1179 EXPECT_EQ(nullptr, N->getSubprograms());
1180
1181 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1182 EXPECT_EQ(nullptr, N->getGlobalVariables());
1183 N->replaceGlobalVariables(GlobalVariables);
1184 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1185 N->replaceGlobalVariables(nullptr);
1186 EXPECT_EQ(nullptr, N->getGlobalVariables());
1187}
1188
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001189typedef MetadataTest MDSubprogramTest;
1190
1191TEST_F(MDSubprogramTest, get) {
1192 Metadata *Scope = MDTuple::getDistinct(Context, None);
1193 StringRef Name = "name";
1194 StringRef LinkageName = "linkage";
1195 Metadata *File = MDTuple::getDistinct(Context, None);
1196 unsigned Line = 2;
1197 Metadata *Type = MDTuple::getDistinct(Context, None);
1198 bool IsLocalToUnit = false;
1199 bool IsDefinition = true;
1200 unsigned ScopeLine = 3;
1201 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1202 unsigned Virtuality = 4;
1203 unsigned VirtualIndex = 5;
1204 unsigned Flags = 6;
1205 bool IsOptimized = false;
1206 Metadata *Function = MDTuple::getDistinct(Context, None);
1207 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1208 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1209 Metadata *Variables = MDTuple::getDistinct(Context, None);
1210
1211 auto *N = MDSubprogram::get(
1212 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1213 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1214 IsOptimized, Function, TemplateParams, Declaration, Variables);
1215
1216 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1217 EXPECT_EQ(Scope, N->getScope());
1218 EXPECT_EQ(Name, N->getName());
1219 EXPECT_EQ(LinkageName, N->getLinkageName());
1220 EXPECT_EQ(File, N->getFile());
1221 EXPECT_EQ(Line, N->getLine());
1222 EXPECT_EQ(Type, N->getType());
1223 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1224 EXPECT_EQ(IsDefinition, N->isDefinition());
1225 EXPECT_EQ(ScopeLine, N->getScopeLine());
1226 EXPECT_EQ(ContainingType, N->getContainingType());
1227 EXPECT_EQ(Virtuality, N->getVirtuality());
1228 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1229 EXPECT_EQ(Flags, N->getFlags());
1230 EXPECT_EQ(IsOptimized, N->isOptimized());
1231 EXPECT_EQ(Function, N->getFunction());
1232 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1233 EXPECT_EQ(Declaration, N->getDeclaration());
1234 EXPECT_EQ(Variables, N->getVariables());
1235 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1236 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1237 ContainingType, Virtuality, VirtualIndex,
1238 Flags, IsOptimized, Function, TemplateParams,
1239 Declaration, Variables));
1240
1241 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1242 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1243 ContainingType, Virtuality, VirtualIndex,
1244 Flags, IsOptimized, Function, TemplateParams,
1245 Declaration, Variables));
1246 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1247 Line, Type, IsLocalToUnit, IsDefinition,
1248 ScopeLine, ContainingType, Virtuality,
1249 VirtualIndex, Flags, IsOptimized, Function,
1250 TemplateParams, Declaration, Variables));
1251 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1252 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1253 ContainingType, Virtuality, VirtualIndex,
1254 Flags, IsOptimized, Function, TemplateParams,
1255 Declaration, Variables));
1256 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1257 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1258 ContainingType, Virtuality, VirtualIndex,
1259 Flags, IsOptimized, Function, TemplateParams,
1260 Declaration, Variables));
1261 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1262 Line + 1, Type, IsLocalToUnit, IsDefinition,
1263 ScopeLine, ContainingType, Virtuality,
1264 VirtualIndex, Flags, IsOptimized, Function,
1265 TemplateParams, Declaration, Variables));
1266 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1267 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1268 ContainingType, Virtuality, VirtualIndex,
1269 Flags, IsOptimized, Function, TemplateParams,
1270 Declaration, Variables));
1271 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1272 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1273 ContainingType, Virtuality, VirtualIndex,
1274 Flags, IsOptimized, Function, TemplateParams,
1275 Declaration, Variables));
1276 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1277 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1278 ContainingType, Virtuality, VirtualIndex,
1279 Flags, IsOptimized, Function, TemplateParams,
1280 Declaration, Variables));
1281 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1282 Type, IsLocalToUnit, IsDefinition,
1283 ScopeLine + 1, ContainingType, Virtuality,
1284 VirtualIndex, Flags, IsOptimized, Function,
1285 TemplateParams, Declaration, Variables));
1286 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1287 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1288 Type, Virtuality, VirtualIndex, Flags,
1289 IsOptimized, Function, TemplateParams,
1290 Declaration, Variables));
1291 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1292 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1293 ContainingType, Virtuality + 1, VirtualIndex,
1294 Flags, IsOptimized, Function, TemplateParams,
1295 Declaration, Variables));
1296 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1297 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1298 ContainingType, Virtuality, VirtualIndex + 1,
1299 Flags, IsOptimized, Function, TemplateParams,
1300 Declaration, Variables));
1301 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1302 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1303 ContainingType, Virtuality, VirtualIndex,
1304 ~Flags, IsOptimized, Function, TemplateParams,
1305 Declaration, Variables));
1306 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1307 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1308 ContainingType, Virtuality, VirtualIndex,
1309 Flags, !IsOptimized, Function, TemplateParams,
1310 Declaration, Variables));
1311 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1312 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1313 ContainingType, Virtuality, VirtualIndex,
1314 Flags, IsOptimized, Type, TemplateParams,
1315 Declaration, Variables));
1316 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1317 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1318 ContainingType, Virtuality, VirtualIndex,
1319 Flags, IsOptimized, Function, Type,
1320 Declaration, Variables));
1321 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1322 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1323 ContainingType, Virtuality, VirtualIndex,
1324 Flags, IsOptimized, Function, TemplateParams,
1325 Type, Variables));
1326 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1327 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1328 ContainingType, Virtuality, VirtualIndex,
1329 Flags, IsOptimized, Function, TemplateParams,
1330 Declaration, Type));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001331
1332 TempMDSubprogram Temp = N->clone();
1333 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001334}
1335
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001336TEST_F(MDSubprogramTest, replaceFunction) {
1337 Metadata *Scope = MDTuple::getDistinct(Context, None);
1338 StringRef Name = "name";
1339 StringRef LinkageName = "linkage";
1340 Metadata *File = MDTuple::getDistinct(Context, None);
1341 unsigned Line = 2;
1342 Metadata *Type = MDTuple::getDistinct(Context, None);
1343 bool IsLocalToUnit = false;
1344 bool IsDefinition = true;
1345 unsigned ScopeLine = 3;
1346 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1347 unsigned Virtuality = 4;
1348 unsigned VirtualIndex = 5;
1349 unsigned Flags = 6;
1350 bool IsOptimized = false;
1351 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1352 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1353 Metadata *Variables = MDTuple::getDistinct(Context, None);
1354
1355 auto *N = MDSubprogram::get(
1356 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1357 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1358 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1359
1360 EXPECT_EQ(nullptr, N->getFunction());
1361
1362 std::unique_ptr<Function> F(
1363 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1364 GlobalValue::ExternalLinkage));
1365 N->replaceFunction(F.get());
1366 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1367
1368 N->replaceFunction(nullptr);
1369 EXPECT_EQ(nullptr, N->getFunction());
1370}
1371
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001372typedef MetadataTest MDLexicalBlockTest;
1373
1374TEST_F(MDLexicalBlockTest, get) {
1375 Metadata *Scope = MDTuple::getDistinct(Context, None);
1376 Metadata *File = MDTuple::getDistinct(Context, None);
1377 unsigned Line = 5;
1378 unsigned Column = 8;
1379
1380 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1381
1382 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1383 EXPECT_EQ(Scope, N->getScope());
1384 EXPECT_EQ(File, N->getFile());
1385 EXPECT_EQ(Line, N->getLine());
1386 EXPECT_EQ(Column, N->getColumn());
1387 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1388
1389 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1390 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1391 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1392 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001393
1394 TempMDLexicalBlock Temp = N->clone();
1395 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001396}
1397
1398typedef MetadataTest MDLexicalBlockFileTest;
1399
1400TEST_F(MDLexicalBlockFileTest, get) {
1401 Metadata *Scope = MDTuple::getDistinct(Context, None);
1402 Metadata *File = MDTuple::getDistinct(Context, None);
1403 unsigned Discriminator = 5;
1404
1405 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1406
1407 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1408 EXPECT_EQ(Scope, N->getScope());
1409 EXPECT_EQ(File, N->getFile());
1410 EXPECT_EQ(Discriminator, N->getDiscriminator());
1411 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1412
1413 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1414 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1415 EXPECT_NE(N,
1416 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001417
1418 TempMDLexicalBlockFile Temp = N->clone();
1419 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001420}
1421
1422typedef MetadataTest MDNamespaceTest;
1423
1424TEST_F(MDNamespaceTest, get) {
1425 Metadata *Scope = MDTuple::getDistinct(Context, None);
1426 Metadata *File = MDTuple::getDistinct(Context, None);
1427 StringRef Name = "namespace";
1428 unsigned Line = 5;
1429
1430 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1431
1432 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1433 EXPECT_EQ(Scope, N->getScope());
1434 EXPECT_EQ(File, N->getFile());
1435 EXPECT_EQ(Name, N->getName());
1436 EXPECT_EQ(Line, N->getLine());
1437 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1438
1439 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1440 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1441 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1442 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001443
1444 TempMDNamespace Temp = N->clone();
1445 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001446}
1447
1448typedef MetadataTest MDTemplateTypeParameterTest;
1449
1450TEST_F(MDTemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001451 StringRef Name = "template";
1452 Metadata *Type = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001453 Metadata *Other = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001454
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001455 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001456
1457 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001458 EXPECT_EQ(Name, N->getName());
1459 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001460 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001461
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001462 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1463 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001464
1465 TempMDTemplateTypeParameter Temp = N->clone();
1466 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001467}
1468
1469typedef MetadataTest MDTemplateValueParameterTest;
1470
1471TEST_F(MDTemplateValueParameterTest, get) {
1472 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001473 StringRef Name = "template";
1474 Metadata *Type = MDTuple::getDistinct(Context, None);
1475 Metadata *Value = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001476 Metadata *Other = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001477
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001478 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001479 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001480 EXPECT_EQ(Name, N->getName());
1481 EXPECT_EQ(Type, N->getType());
1482 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001483 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001484
1485 EXPECT_NE(N, MDTemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001486 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1487 Type, Value));
1488 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001489 Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001490 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Other,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001491 Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001492 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001493
1494 TempMDTemplateValueParameter Temp = N->clone();
1495 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001496}
1497
1498typedef MetadataTest MDGlobalVariableTest;
1499
1500TEST_F(MDGlobalVariableTest, get) {
1501 Metadata *Scope = MDTuple::getDistinct(Context, None);
1502 StringRef Name = "name";
1503 StringRef LinkageName = "linkage";
1504 Metadata *File = MDTuple::getDistinct(Context, None);
1505 unsigned Line = 5;
1506 Metadata *Type = MDTuple::getDistinct(Context, None);
1507 bool IsLocalToUnit = false;
1508 bool IsDefinition = true;
1509 Metadata *Variable = MDTuple::getDistinct(Context, None);
1510 Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1511
1512 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1513 Type, IsLocalToUnit, IsDefinition, Variable,
1514 StaticDataMemberDeclaration);
1515 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1516 EXPECT_EQ(Scope, N->getScope());
1517 EXPECT_EQ(Name, N->getName());
1518 EXPECT_EQ(LinkageName, N->getLinkageName());
1519 EXPECT_EQ(File, N->getFile());
1520 EXPECT_EQ(Line, N->getLine());
1521 EXPECT_EQ(Type, N->getType());
1522 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1523 EXPECT_EQ(IsDefinition, N->isDefinition());
1524 EXPECT_EQ(Variable, N->getVariable());
1525 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1526 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1527 Line, Type, IsLocalToUnit, IsDefinition,
1528 Variable, StaticDataMemberDeclaration));
1529
1530 EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1531 Line, Type, IsLocalToUnit, IsDefinition,
1532 Variable, StaticDataMemberDeclaration));
1533 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1534 Line, Type, IsLocalToUnit, IsDefinition,
1535 Variable, StaticDataMemberDeclaration));
1536 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1537 Type, IsLocalToUnit, IsDefinition,
1538 Variable, StaticDataMemberDeclaration));
1539 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1540 Line, Type, IsLocalToUnit, IsDefinition,
1541 Variable, StaticDataMemberDeclaration));
1542 EXPECT_NE(N,
1543 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1544 Line + 1, Type, IsLocalToUnit, IsDefinition,
1545 Variable, StaticDataMemberDeclaration));
1546 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1547 Line, Scope, IsLocalToUnit, IsDefinition,
1548 Variable, StaticDataMemberDeclaration));
1549 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1550 Line, Type, !IsLocalToUnit, IsDefinition,
1551 Variable, StaticDataMemberDeclaration));
1552 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1553 Line, Type, IsLocalToUnit, !IsDefinition,
1554 Variable, StaticDataMemberDeclaration));
1555 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1556 Line, Type, IsLocalToUnit, IsDefinition,
1557 Type, StaticDataMemberDeclaration));
1558 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1559 Line, Type, IsLocalToUnit, IsDefinition,
1560 Variable, Type));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001561
1562 TempMDGlobalVariable Temp = N->clone();
1563 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001564}
1565
1566typedef MetadataTest MDLocalVariableTest;
1567
1568TEST_F(MDLocalVariableTest, get) {
1569 unsigned Tag = dwarf::DW_TAG_arg_variable;
1570 Metadata *Scope = MDTuple::getDistinct(Context, None);
1571 StringRef Name = "name";
1572 Metadata *File = MDTuple::getDistinct(Context, None);
1573 unsigned Line = 5;
1574 Metadata *Type = MDTuple::getDistinct(Context, None);
1575 unsigned Arg = 6;
1576 unsigned Flags = 7;
1577 Metadata *InlinedAt = MDTuple::getDistinct(Context, None);
1578
1579 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1580 Arg, Flags, InlinedAt);
1581 EXPECT_EQ(Tag, N->getTag());
1582 EXPECT_EQ(Scope, N->getScope());
1583 EXPECT_EQ(Name, N->getName());
1584 EXPECT_EQ(File, N->getFile());
1585 EXPECT_EQ(Line, N->getLine());
1586 EXPECT_EQ(Type, N->getType());
1587 EXPECT_EQ(Arg, N->getArg());
1588 EXPECT_EQ(Flags, N->getFlags());
1589 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1590 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1591 Arg, Flags, InlinedAt));
1592
1593 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1594 Name, File, Line, Type, Arg, Flags,
1595 InlinedAt));
1596 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1597 Type, Arg, Flags, InlinedAt));
1598 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1599 Type, Arg, Flags, InlinedAt));
1600 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1601 Type, Arg, Flags, InlinedAt));
1602 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1603 Type, Arg, Flags, InlinedAt));
1604 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1605 Scope, Arg, Flags, InlinedAt));
1606 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1607 Arg + 1, Flags, InlinedAt));
1608 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1609 Arg, ~Flags, InlinedAt));
1610 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1611 Arg, Flags, Scope));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001612
1613 TempMDLocalVariable Temp = N->clone();
1614 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001615}
1616
1617typedef MetadataTest MDExpressionTest;
1618
1619TEST_F(MDExpressionTest, get) {
1620 uint64_t Elements[] = {2, 6, 9, 78, 0};
1621 auto *N = MDExpression::get(Context, Elements);
1622 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1623 EXPECT_EQ(N, MDExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001624
1625 EXPECT_EQ(5u, N->getNumElements());
1626 EXPECT_EQ(2u, N->getElement(0));
1627 EXPECT_EQ(6u, N->getElement(1));
1628 EXPECT_EQ(9u, N->getElement(2));
1629 EXPECT_EQ(78u, N->getElement(3));
1630 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001631
1632 TempMDExpression Temp = N->clone();
1633 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001634}
1635
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001636TEST_F(MDExpressionTest, isValid) {
1637#define EXPECT_VALID(...) \
1638 do { \
1639 uint64_t Elements[] = {__VA_ARGS__}; \
1640 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1641 } while (false)
1642#define EXPECT_INVALID(...) \
1643 do { \
1644 uint64_t Elements[] = {__VA_ARGS__}; \
1645 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1646 } while (false)
1647
1648 // Empty expression should be valid.
1649 EXPECT_TRUE(MDExpression::get(Context, None));
1650
1651 // Valid constructions.
1652 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1653 EXPECT_VALID(dwarf::DW_OP_deref);
1654 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1655 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1656 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1657 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1658 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1659
1660 // Invalid constructions.
1661 EXPECT_INVALID(~0u);
1662 EXPECT_INVALID(dwarf::DW_OP_plus);
1663 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1664 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1665 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1666 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1667
1668#undef EXPECT_VALID
1669#undef EXPECT_INVALID
1670}
1671
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001672typedef MetadataTest MDObjCPropertyTest;
1673
1674TEST_F(MDObjCPropertyTest, get) {
1675 StringRef Name = "name";
1676 Metadata *File = MDTuple::getDistinct(Context, None);
1677 unsigned Line = 5;
1678 StringRef GetterName = "getter";
1679 StringRef SetterName = "setter";
1680 unsigned Attributes = 7;
1681 Metadata *Type = MDTuple::getDistinct(Context, None);
1682
1683 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1684 SetterName, Attributes, Type);
1685
1686 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1687 EXPECT_EQ(Name, N->getName());
1688 EXPECT_EQ(File, N->getFile());
1689 EXPECT_EQ(Line, N->getLine());
1690 EXPECT_EQ(GetterName, N->getGetterName());
1691 EXPECT_EQ(SetterName, N->getSetterName());
1692 EXPECT_EQ(Attributes, N->getAttributes());
1693 EXPECT_EQ(Type, N->getType());
1694 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1695 SetterName, Attributes, Type));
1696
1697 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1698 SetterName, Attributes, Type));
1699 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1700 SetterName, Attributes, Type));
1701 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1702 SetterName, Attributes, Type));
1703 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1704 SetterName, Attributes, Type));
1705 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1706 "other", Attributes, Type));
1707 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1708 SetterName, Attributes + 1, Type));
1709 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1710 SetterName, Attributes, File));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001711
1712 TempMDObjCProperty Temp = N->clone();
1713 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001714}
1715
1716typedef MetadataTest MDImportedEntityTest;
1717
1718TEST_F(MDImportedEntityTest, get) {
1719 unsigned Tag = dwarf::DW_TAG_imported_module;
1720 Metadata *Scope = MDTuple::getDistinct(Context, None);
1721 Metadata *Entity = MDTuple::getDistinct(Context, None);
1722 unsigned Line = 5;
1723 StringRef Name = "name";
1724
1725 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1726
1727 EXPECT_EQ(Tag, N->getTag());
1728 EXPECT_EQ(Scope, N->getScope());
1729 EXPECT_EQ(Entity, N->getEntity());
1730 EXPECT_EQ(Line, N->getLine());
1731 EXPECT_EQ(Name, N->getName());
1732 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1733
1734 EXPECT_NE(N,
1735 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1736 Scope, Entity, Line, Name));
1737 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1738 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1739 EXPECT_NE(N,
1740 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1741 EXPECT_NE(N,
1742 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001743
1744 TempMDImportedEntity Temp = N->clone();
1745 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001746}
1747
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001748typedef MetadataTest MetadataAsValueTest;
1749
1750TEST_F(MetadataAsValueTest, MDNode) {
1751 MDNode *N = MDNode::get(Context, None);
1752 auto *V = MetadataAsValue::get(Context, N);
1753 EXPECT_TRUE(V->getType()->isMetadataTy());
1754 EXPECT_EQ(N, V->getMetadata());
1755
1756 auto *V2 = MetadataAsValue::get(Context, N);
1757 EXPECT_EQ(V, V2);
1758}
1759
1760TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1761 MDNode *N = MDNode::get(Context, None);
1762 Metadata *Ops[] = {N};
1763 MDNode *N2 = MDNode::get(Context, Ops);
1764 auto *V = MetadataAsValue::get(Context, N2);
1765 EXPECT_TRUE(V->getType()->isMetadataTy());
1766 EXPECT_EQ(N2, V->getMetadata());
1767
1768 auto *V2 = MetadataAsValue::get(Context, N2);
1769 EXPECT_EQ(V, V2);
1770
1771 auto *V3 = MetadataAsValue::get(Context, N);
1772 EXPECT_TRUE(V3->getType()->isMetadataTy());
1773 EXPECT_NE(V, V3);
1774 EXPECT_EQ(N, V3->getMetadata());
1775}
1776
1777TEST_F(MetadataAsValueTest, MDNodeConstant) {
1778 auto *C = ConstantInt::getTrue(Context);
1779 auto *MD = ConstantAsMetadata::get(C);
1780 Metadata *Ops[] = {MD};
1781 auto *N = MDNode::get(Context, Ops);
1782
1783 auto *V = MetadataAsValue::get(Context, MD);
1784 EXPECT_TRUE(V->getType()->isMetadataTy());
1785 EXPECT_EQ(MD, V->getMetadata());
1786
1787 auto *V2 = MetadataAsValue::get(Context, N);
1788 EXPECT_EQ(MD, V2->getMetadata());
1789 EXPECT_EQ(V, V2);
1790}
1791
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001792typedef MetadataTest ValueAsMetadataTest;
1793
1794TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1795 Type *Ty = Type::getInt1PtrTy(Context);
1796 std::unique_ptr<GlobalVariable> GV0(
1797 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1798 auto *MD = ValueAsMetadata::get(GV0.get());
1799 EXPECT_TRUE(MD->getValue() == GV0.get());
1800 ASSERT_TRUE(GV0->use_empty());
1801
1802 std::unique_ptr<GlobalVariable> GV1(
1803 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1804 GV0->replaceAllUsesWith(GV1.get());
1805 EXPECT_TRUE(MD->getValue() == GV1.get());
1806}
1807
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001808TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1809 // Create a constant.
1810 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1811 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1812
1813 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001814 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001815
1816 // 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 +00001817 Metadata *Ops1[] = {CI, CI, Temp.get()};
1818 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001819
1820 auto *N1 = MDTuple::get(Context, Ops1);
1821 auto *N2 = MDTuple::get(Context, Ops2);
1822 ASSERT_NE(N1, N2);
1823
1824 // Tell metadata that the constant is getting deleted.
1825 //
1826 // After this, N1 will be invalid, so don't touch it.
1827 ValueAsMetadata::handleDeletion(CI->getValue());
1828 EXPECT_EQ(nullptr, N2->getOperand(0));
1829 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001830 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001831
1832 // Clean up Temp for teardown.
1833 Temp->replaceAllUsesWith(nullptr);
1834}
1835
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001836typedef MetadataTest TrackingMDRefTest;
1837
1838TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1839 Type *Ty = Type::getInt1PtrTy(Context);
1840 std::unique_ptr<GlobalVariable> GV0(
1841 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1842 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
1843 EXPECT_TRUE(MD->getValue() == GV0.get());
1844 ASSERT_TRUE(GV0->use_empty());
1845
1846 std::unique_ptr<GlobalVariable> GV1(
1847 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1848 GV0->replaceAllUsesWith(GV1.get());
1849 EXPECT_TRUE(MD->getValue() == GV1.get());
1850
1851 // Reset it, so we don't inadvertently test deletion.
1852 MD.reset();
1853}
1854
1855TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
1856 Type *Ty = Type::getInt1PtrTy(Context);
1857 std::unique_ptr<GlobalVariable> GV(
1858 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1859 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
1860 EXPECT_TRUE(MD->getValue() == GV.get());
1861 ASSERT_TRUE(GV->use_empty());
1862
1863 GV.reset();
1864 EXPECT_TRUE(!MD);
1865}
1866
Devang Patel0924b332009-07-30 00:03:41 +00001867TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00001868 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001869 ConstantAsMetadata *C =
1870 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
1871 ConstantAsMetadata *C2 =
1872 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00001873
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001874 Metadata *const V = C;
1875 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00001876 MDNode *n = MDNode::get(Context, V);
1877 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00001878
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00001879 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00001880 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00001881 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
1882 NMD->addOperand(n);
1883 NMD->addOperand(n2);
1884
Chris Lattnerbe354a62009-08-23 04:47:35 +00001885 std::string Str;
1886 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00001887 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00001888 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00001889 oss.str().c_str());
1890}
Nick Lewycky49f89192009-04-04 07:22:01 +00001891}