blob: 51a9e0bfc1272b7e84a981a29dfa8f7bf4cd6805 [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 Smithb3538492015-03-03 16:45:34 +000012#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000013#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000014#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
19#include "llvm/IR/Type.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000020#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000021#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022using namespace llvm;
23
24namespace {
25
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000026TEST(ContextAndReplaceableUsesTest, FromContext) {
27 LLVMContext Context;
28 ContextAndReplaceableUses CRU(Context);
29 EXPECT_EQ(&Context, &CRU.getContext());
30 EXPECT_FALSE(CRU.hasReplaceableUses());
31 EXPECT_FALSE(CRU.getReplaceableUses());
32}
33
34TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
35 LLVMContext Context;
36 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
37 EXPECT_EQ(&Context, &CRU.getContext());
38 EXPECT_TRUE(CRU.hasReplaceableUses());
39 EXPECT_TRUE(CRU.getReplaceableUses());
40}
41
42TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
43 LLVMContext Context;
44 ContextAndReplaceableUses CRU(Context);
45 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
46 EXPECT_EQ(&Context, &CRU.getContext());
47 EXPECT_TRUE(CRU.hasReplaceableUses());
48 EXPECT_TRUE(CRU.getReplaceableUses());
49}
50
51TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
52 LLVMContext Context;
53 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
54 auto *Ptr = ReplaceableUses.get();
55 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
56 ReplaceableUses = CRU.takeReplaceableUses();
57 EXPECT_EQ(&Context, &CRU.getContext());
58 EXPECT_FALSE(CRU.hasReplaceableUses());
59 EXPECT_FALSE(CRU.getReplaceableUses());
60 EXPECT_EQ(Ptr, ReplaceableUses.get());
61}
62
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000063class MetadataTest : public testing::Test {
64protected:
65 LLVMContext Context;
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000066 MDNode *getNode() { return MDNode::get(Context, None); }
67 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
68 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
69 Metadata *MDs[] = {MD1, MD2};
70 return MDNode::get(Context, MDs);
71 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000072
73 MDSubprogram *getSubprogram() {
74 return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
75 nullptr, false, false, 0, nullptr, 0, 0, 0,
76 0);
77 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000078};
79typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +000080
Nick Lewycky49f89192009-04-04 07:22:01 +000081// Test that construction of MDString with different value produces different
82// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000083TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +000084 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000085 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000086 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000087 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000088 EXPECT_NE(s1, s2);
89}
90
91// Test that creation of MDStrings with the same string contents produces the
92// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000093TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +000094 char x[4] = { 'a', 'b', 'c', 'X' };
95 char y[4] = { 'a', 'b', 'c', 'Y' };
96
Owen Anderson23587322009-07-31 21:38:10 +000097 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
98 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000099 EXPECT_EQ(s1, s2);
100}
101
102// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000103TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000104 char *str = new char[13];
105 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000106 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000107 strncpy(str, "aaaaaaaaaaaaa", 13);
108 delete[] str;
109
Chris Lattnerbe354a62009-08-23 04:47:35 +0000110 std::string Str;
111 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000112 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000113 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000114}
115
116// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000117TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000118 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000119 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000120 std::string Str;
121 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000122 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000123 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000124}
125
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000126typedef MetadataTest MDNodeTest;
127
Nick Lewycky49f89192009-04-04 07:22:01 +0000128// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000129TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000130 char x[3] = { 'a', 'b', 'c' };
131 char y[3] = { '1', '2', '3' };
132
Owen Anderson23587322009-07-31 21:38:10 +0000133 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
134 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000135 ConstantAsMetadata *CI = ConstantAsMetadata::get(
136 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000137
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000138 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000139 V.push_back(s1);
140 V.push_back(CI);
141 V.push_back(s2);
142
Jay Foad5514afe2011-04-21 19:59:31 +0000143 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000144 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000145 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000146 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000147 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000148 MDNode *n4 = MDNode::getIfExists(Context, V);
149 MDNode *n5 = MDNode::getIfExists(Context, c1);
150 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000151 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000152 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000153 EXPECT_EQ(n4, n1);
154 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000155 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000156
Chris Lattner9b493022009-12-31 01:22:29 +0000157 EXPECT_EQ(3u, n1->getNumOperands());
158 EXPECT_EQ(s1, n1->getOperand(0));
159 EXPECT_EQ(CI, n1->getOperand(1));
160 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000161
Chris Lattner9b493022009-12-31 01:22:29 +0000162 EXPECT_EQ(1u, n2->getNumOperands());
163 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000164}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000165
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000166TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000167 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
168 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000169
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000170 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000171 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000172 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000173
174 EXPECT_EQ(n, wvh);
175
176 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000177}
Devang Patel0924b332009-07-30 00:03:41 +0000178
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000179TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000180 // !0 = !{!0}
181 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000182 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000183 auto Temp = MDNode::getTemporary(Context, None);
184 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000185 MDNode *Self = MDNode::get(Context, Args);
186 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000187 ASSERT_EQ(Self, Self->getOperand(0));
188
189 // Self-references should be distinct, so MDNode::get() should grab a
190 // uniqued node that references Self, not Self.
191 Args[0] = Self;
192 MDNode *Ref1 = MDNode::get(Context, Args);
193 MDNode *Ref2 = MDNode::get(Context, Args);
194 EXPECT_NE(Self, Ref1);
195 EXPECT_EQ(Ref1, Ref2);
196 }
197
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000198 // !0 = !{!0, !{}}
199 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000200 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000201 auto Temp = MDNode::getTemporary(Context, None);
202 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000203 MDNode *Self = MDNode::get(Context, Args);
204 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000205 ASSERT_EQ(Self, Self->getOperand(0));
206
207 // Self-references should be distinct, so MDNode::get() should grab a
208 // uniqued node that references Self, not Self itself.
209 Args[0] = Self;
210 MDNode *Ref1 = MDNode::get(Context, Args);
211 MDNode *Ref2 = MDNode::get(Context, Args);
212 EXPECT_NE(Self, Ref1);
213 EXPECT_EQ(Ref1, Ref2);
214 }
215}
216
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000217TEST_F(MDNodeTest, Print) {
218 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
219 MDString *S = MDString::get(Context, "foo");
220 MDNode *N0 = getNode();
221 MDNode *N1 = getNode(N0);
222 MDNode *N2 = getNode(N0, N1);
223
224 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
225 MDNode *N = MDNode::get(Context, Args);
226
227 std::string Expected;
228 {
229 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000230 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000231 C->printAsOperand(OS);
232 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000233 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000234 OS << ", null";
235 MDNode *Nodes[] = {N0, N1, N2};
236 for (auto *Node : Nodes)
237 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000238 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000239 }
240
241 std::string Actual;
242 {
243 raw_string_ostream OS(Actual);
244 N->print(OS);
245 }
246
247 EXPECT_EQ(Expected, Actual);
248}
249
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000250#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
251 do { \
252 std::string Actual_; \
253 raw_string_ostream OS(Actual_); \
254 PRINT; \
255 OS.flush(); \
256 std::string Expected_(EXPECTED); \
257 EXPECT_EQ(Expected_, Actual_); \
258 } while (false)
259
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000260TEST_F(MDNodeTest, PrintTemporary) {
261 MDNode *Arg = getNode();
262 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
263 MDNode *N = getNode(Temp.get());
264 Module M("test", Context);
265 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
266 NMD->addOperand(N);
267
268 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
269 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
270 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
271
272 // Cleanup.
273 Temp->replaceAllUsesWith(Arg);
274}
275
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000276TEST_F(MDNodeTest, PrintFromModule) {
277 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
278 MDString *S = MDString::get(Context, "foo");
279 MDNode *N0 = getNode();
280 MDNode *N1 = getNode(N0);
281 MDNode *N2 = getNode(N0, N1);
282
283 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
284 MDNode *N = MDNode::get(Context, Args);
285 Module M("test", Context);
286 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
287 NMD->addOperand(N);
288
289 std::string Expected;
290 {
291 raw_string_ostream OS(Expected);
292 OS << "!0 = !{";
293 C->printAsOperand(OS);
294 OS << ", ";
295 S->printAsOperand(OS);
296 OS << ", null, !1, !2, !3}";
297 }
298
299 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
300}
301
302TEST_F(MDNodeTest, PrintFromFunction) {
303 Module M("test", Context);
304 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
305 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
306 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
307 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
308 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
309 auto *R0 = ReturnInst::Create(Context, BB0);
310 auto *R1 = ReturnInst::Create(Context, BB1);
311 auto *N0 = MDNode::getDistinct(Context, None);
312 auto *N1 = MDNode::getDistinct(Context, None);
313 R0->setMetadata("md", N0);
314 R1->setMetadata("md", N1);
315
316 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
317 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
318}
319
320TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
321 Module M("test", Context);
322
323 auto *Intrinsic =
324 Function::Create(FunctionType::get(Type::getVoidTy(Context),
325 Type::getMetadataTy(Context), false),
326 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
327
328 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
329 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
330 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
331 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
332 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
333 auto *N0 = MDNode::getDistinct(Context, None);
334 auto *N1 = MDNode::getDistinct(Context, None);
335 auto *MAV0 = MetadataAsValue::get(Context, N0);
336 auto *MAV1 = MetadataAsValue::get(Context, N1);
337 CallInst::Create(Intrinsic, MAV0, "", BB0);
338 CallInst::Create(Intrinsic, MAV1, "", BB1);
339
340 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
341 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
342 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
343 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
344 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
345 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
346}
347#undef EXPECT_PRINTER_EQ
348
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000349TEST_F(MDNodeTest, NullOperand) {
350 // metadata !{}
351 MDNode *Empty = MDNode::get(Context, None);
352
353 // metadata !{metadata !{}}
354 Metadata *Ops[] = {Empty};
355 MDNode *N = MDNode::get(Context, Ops);
356 ASSERT_EQ(Empty, N->getOperand(0));
357
358 // metadata !{metadata !{}} => metadata !{null}
359 N->replaceOperandWith(0, nullptr);
360 ASSERT_EQ(nullptr, N->getOperand(0));
361
362 // metadata !{null}
363 Ops[0] = nullptr;
364 MDNode *NullOp = MDNode::get(Context, Ops);
365 ASSERT_EQ(nullptr, NullOp->getOperand(0));
366 EXPECT_EQ(N, NullOp);
367}
368
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000369TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
370 // !{}
371 MDNode *Empty = MDNode::get(Context, None);
372 ASSERT_TRUE(Empty->isResolved());
373 EXPECT_FALSE(Empty->isDistinct());
374
375 // !{!{}}
376 Metadata *Wrapped1Ops[] = {Empty};
377 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
378 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
379 ASSERT_TRUE(Wrapped1->isResolved());
380 EXPECT_FALSE(Wrapped1->isDistinct());
381
382 // !{!{!{}}}
383 Metadata *Wrapped2Ops[] = {Wrapped1};
384 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
385 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
386 ASSERT_TRUE(Wrapped2->isResolved());
387 EXPECT_FALSE(Wrapped2->isDistinct());
388
389 // !{!{!{}}} => !{!{}}
390 Wrapped2->replaceOperandWith(0, Empty);
391 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
392 EXPECT_TRUE(Wrapped2->isDistinct());
393 EXPECT_FALSE(Wrapped1->isDistinct());
394}
395
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000396TEST_F(MDNodeTest, getDistinct) {
397 // !{}
398 MDNode *Empty = MDNode::get(Context, None);
399 ASSERT_TRUE(Empty->isResolved());
400 ASSERT_FALSE(Empty->isDistinct());
401 ASSERT_EQ(Empty, MDNode::get(Context, None));
402
403 // distinct !{}
404 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
405 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
406 EXPECT_TRUE(Distinct1->isResolved());
407 EXPECT_TRUE(Distinct2->isDistinct());
408 EXPECT_NE(Empty, Distinct1);
409 EXPECT_NE(Empty, Distinct2);
410 EXPECT_NE(Distinct1, Distinct2);
411
412 // !{}
413 ASSERT_EQ(Empty, MDNode::get(Context, None));
414}
415
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000416TEST_F(MDNodeTest, isUniqued) {
417 MDNode *U = MDTuple::get(Context, None);
418 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000419 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000420 EXPECT_TRUE(U->isUniqued());
421 EXPECT_FALSE(D->isUniqued());
422 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000423}
424
425TEST_F(MDNodeTest, isDistinct) {
426 MDNode *U = MDTuple::get(Context, None);
427 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000428 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000429 EXPECT_FALSE(U->isDistinct());
430 EXPECT_TRUE(D->isDistinct());
431 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000432}
433
434TEST_F(MDNodeTest, isTemporary) {
435 MDNode *U = MDTuple::get(Context, None);
436 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000437 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000438 EXPECT_FALSE(U->isTemporary());
439 EXPECT_FALSE(D->isTemporary());
440 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000441}
442
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000443TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
444 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000445 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000446 ASSERT_FALSE(Temp->isResolved());
447
448 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000449 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000450 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
451 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000452 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000453
454 // temporary !{} => !{}
455 MDNode *Empty = MDNode::get(Context, None);
456 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000457 EXPECT_EQ(Empty, Distinct->getOperand(0));
458}
459
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000460TEST_F(MDNodeTest, handleChangedOperandRecursion) {
461 // !0 = !{}
462 MDNode *N0 = MDNode::get(Context, None);
463
464 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000465 auto Temp3 = MDTuple::getTemporary(Context, None);
466 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000467 MDNode *N1 = MDNode::get(Context, Ops1);
468
469 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000470 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000471 MDNode *N2 = MDNode::get(Context, Ops2);
472
473 // !3 = !{!2}
474 Metadata *Ops3[] = {N2};
475 MDNode *N3 = MDNode::get(Context, Ops3);
476 Temp3->replaceAllUsesWith(N3);
477
478 // !4 = !{!1}
479 Metadata *Ops4[] = {N1};
480 MDNode *N4 = MDNode::get(Context, Ops4);
481
482 // Confirm that the cycle prevented RAUW from getting dropped.
483 EXPECT_TRUE(N0->isResolved());
484 EXPECT_FALSE(N1->isResolved());
485 EXPECT_FALSE(N2->isResolved());
486 EXPECT_FALSE(N3->isResolved());
487 EXPECT_FALSE(N4->isResolved());
488
489 // Create a couple of distinct nodes to observe what's going on.
490 //
491 // !5 = distinct !{!2}
492 // !6 = distinct !{!3}
493 Metadata *Ops5[] = {N2};
494 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
495 Metadata *Ops6[] = {N3};
496 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
497
498 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
499 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
500 // references !3, this can cause a re-entry of handleChangedOperand() when !3
501 // is not ready for it.
502 //
503 // !2->replaceOperandWith(1, nullptr)
504 // !2: !{!3, !0} => !{!3, null}
505 // !2->replaceAllUsesWith(!1)
506 // !3: !{!2] => !{!1}
507 // !3->replaceAllUsesWith(!4)
508 N2->replaceOperandWith(1, nullptr);
509
510 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
511 // under us. Just check that the other nodes are sane.
512 //
513 // !1 = !{!4, null}
514 // !4 = !{!1}
515 // !5 = distinct !{!1}
516 // !6 = distinct !{!4}
517 EXPECT_EQ(N4, N1->getOperand(0));
518 EXPECT_EQ(N1, N4->getOperand(0));
519 EXPECT_EQ(N1, N5->getOperand(0));
520 EXPECT_EQ(N4, N6->getOperand(0));
521}
522
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000523TEST_F(MDNodeTest, replaceResolvedOperand) {
524 // Check code for replacing one resolved operand with another. If doing this
525 // directly (via replaceOperandWith()) becomes illegal, change the operand to
526 // a global value that gets RAUW'ed.
527 //
528 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000529 auto Temp = MDTuple::getTemporary(Context, None);
530 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000531
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000532 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000533 MDNode *N = MDTuple::get(Context, Ops);
534 EXPECT_EQ(nullptr, N->getOperand(0));
535 ASSERT_FALSE(N->isResolved());
536
537 // Check code for replacing resolved nodes.
538 N->replaceOperandWith(0, Empty);
539 EXPECT_EQ(Empty, N->getOperand(0));
540
541 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000542 N->replaceOperandWith(0, Temp.get());
543 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000544
545 // Remove the references to Temp; required for teardown.
546 Temp->replaceAllUsesWith(nullptr);
547}
548
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000549TEST_F(MDNodeTest, replaceWithUniqued) {
550 auto *Empty = MDTuple::get(Context, None);
551 MDTuple *FirstUniqued;
552 {
553 Metadata *Ops[] = {Empty};
554 auto Temp = MDTuple::getTemporary(Context, Ops);
555 EXPECT_TRUE(Temp->isTemporary());
556
557 // Don't expect a collision.
558 auto *Current = Temp.get();
559 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
560 EXPECT_TRUE(FirstUniqued->isUniqued());
561 EXPECT_TRUE(FirstUniqued->isResolved());
562 EXPECT_EQ(Current, FirstUniqued);
563 }
564 {
565 Metadata *Ops[] = {Empty};
566 auto Temp = MDTuple::getTemporary(Context, Ops);
567 EXPECT_TRUE(Temp->isTemporary());
568
569 // Should collide with Uniqued above this time.
570 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
571 EXPECT_TRUE(Uniqued->isUniqued());
572 EXPECT_TRUE(Uniqued->isResolved());
573 EXPECT_EQ(FirstUniqued, Uniqued);
574 }
575 {
576 auto Unresolved = MDTuple::getTemporary(Context, None);
577 Metadata *Ops[] = {Unresolved.get()};
578 auto Temp = MDTuple::getTemporary(Context, Ops);
579 EXPECT_TRUE(Temp->isTemporary());
580
581 // Shouldn't be resolved.
582 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
583 EXPECT_TRUE(Uniqued->isUniqued());
584 EXPECT_FALSE(Uniqued->isResolved());
585
586 // Should be a different node.
587 EXPECT_NE(FirstUniqued, Uniqued);
588
589 // Should resolve when we update its node (note: be careful to avoid a
590 // collision with any other nodes above).
591 Uniqued->replaceOperandWith(0, nullptr);
592 EXPECT_TRUE(Uniqued->isResolved());
593 }
594}
595
596TEST_F(MDNodeTest, replaceWithDistinct) {
597 {
598 auto *Empty = MDTuple::get(Context, None);
599 Metadata *Ops[] = {Empty};
600 auto Temp = MDTuple::getTemporary(Context, Ops);
601 EXPECT_TRUE(Temp->isTemporary());
602
603 // Don't expect a collision.
604 auto *Current = Temp.get();
605 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
606 EXPECT_TRUE(Distinct->isDistinct());
607 EXPECT_TRUE(Distinct->isResolved());
608 EXPECT_EQ(Current, Distinct);
609 }
610 {
611 auto Unresolved = MDTuple::getTemporary(Context, None);
612 Metadata *Ops[] = {Unresolved.get()};
613 auto Temp = MDTuple::getTemporary(Context, Ops);
614 EXPECT_TRUE(Temp->isTemporary());
615
616 // Don't expect a collision.
617 auto *Current = Temp.get();
618 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
619 EXPECT_TRUE(Distinct->isDistinct());
620 EXPECT_TRUE(Distinct->isResolved());
621 EXPECT_EQ(Current, Distinct);
622
623 // Cleanup; required for teardown.
624 Unresolved->replaceAllUsesWith(nullptr);
625 }
626}
627
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000628TEST_F(MDNodeTest, replaceWithPermanent) {
629 Metadata *Ops[] = {nullptr};
630 auto Temp = MDTuple::getTemporary(Context, Ops);
631 auto *T = Temp.get();
632
633 // U is a normal, uniqued node that references T.
634 auto *U = MDTuple::get(Context, T);
635 EXPECT_TRUE(U->isUniqued());
636
637 // Make Temp self-referencing.
638 Temp->replaceOperandWith(0, T);
639
640 // Try to uniquify Temp. This should, despite the name in the API, give a
641 // 'distinct' node, since self-references aren't allowed to be uniqued.
642 //
643 // Since it's distinct, N should have the same address as when it was a
644 // temporary (i.e., be equal to T not U).
645 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
646 EXPECT_EQ(N, T);
647 EXPECT_TRUE(N->isDistinct());
648
649 // U should be the canonical unique node with N as the argument.
650 EXPECT_EQ(U, MDTuple::get(Context, N));
651 EXPECT_TRUE(U->isUniqued());
652
653 // This temporary should collide with U when replaced, but it should still be
654 // uniqued.
655 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
656 EXPECT_TRUE(U->isUniqued());
657
658 // This temporary should become a new uniqued node.
659 auto Temp2 = MDTuple::getTemporary(Context, U);
660 auto *V = Temp2.get();
661 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
662 EXPECT_TRUE(V->isUniqued());
663 EXPECT_EQ(U, V->getOperand(0));
664}
665
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000666TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
667 TrackingMDRef Ref;
668 EXPECT_EQ(nullptr, Ref.get());
669 {
670 auto Temp = MDTuple::getTemporary(Context, None);
671 Ref.reset(Temp.get());
672 EXPECT_EQ(Temp.get(), Ref.get());
673 }
674 EXPECT_EQ(nullptr, Ref.get());
675}
676
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000677typedef MetadataTest MDLocationTest;
678
679TEST_F(MDLocationTest, Overflow) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000680 MDSubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000681 {
682 MDLocation *L = MDLocation::get(Context, 2, 7, N);
683 EXPECT_EQ(2u, L->getLine());
684 EXPECT_EQ(7u, L->getColumn());
685 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000686 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000687 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000688 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
689 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000690 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000691 }
692 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000693 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
694 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000695 EXPECT_EQ(0u, L->getColumn());
696 }
697 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000698 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
699 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000700 EXPECT_EQ(0u, L->getColumn());
701 }
702}
703
704TEST_F(MDLocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000705 MDNode *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000706 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
707 EXPECT_TRUE(L0->isDistinct());
708 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
709 EXPECT_FALSE(L1->isDistinct());
710 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
711}
712
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000713TEST_F(MDLocationTest, getTemporary) {
714 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000715 auto L = MDLocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000716 EXPECT_TRUE(L->isTemporary());
717 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000718}
719
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000720typedef MetadataTest GenericDebugNodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000721
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000722TEST_F(GenericDebugNodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000723 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000724 auto *Empty = MDNode::get(Context, None);
725 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000726 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000727 EXPECT_EQ(15u, N->getTag());
728 EXPECT_EQ(2u, N->getNumOperands());
729 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000730 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000731 EXPECT_EQ(1u, N->getNumDwarfOperands());
732 EXPECT_EQ(Empty, N->getDwarfOperand(0));
733 EXPECT_EQ(Empty, N->getOperand(1));
734 ASSERT_TRUE(N->isUniqued());
735
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000736 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000737
738 N->replaceOperandWith(1, nullptr);
739 EXPECT_EQ(15u, N->getTag());
740 EXPECT_EQ(Header, N->getHeader());
741 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
742 ASSERT_TRUE(N->isUniqued());
743
744 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000745 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000746
747 N->replaceDwarfOperandWith(0, Empty);
748 EXPECT_EQ(15u, N->getTag());
749 EXPECT_EQ(Header, N->getHeader());
750 EXPECT_EQ(Empty, N->getDwarfOperand(0));
751 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000752 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000753
754 TempGenericDebugNode Temp = N->clone();
755 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000756}
757
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000758TEST_F(GenericDebugNodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000759 // Canonicalize !"" to null.
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000760 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
761 EXPECT_EQ(StringRef(), N->getHeader());
762 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000763}
764
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000765typedef MetadataTest MDSubrangeTest;
766
767TEST_F(MDSubrangeTest, get) {
768 auto *N = MDSubrange::get(Context, 5, 7);
769 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
770 EXPECT_EQ(5, N->getCount());
771 EXPECT_EQ(7, N->getLo());
772 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
773 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000774
775 TempMDSubrange Temp = N->clone();
776 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000777}
778
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000779TEST_F(MDSubrangeTest, getEmptyArray) {
780 auto *N = MDSubrange::get(Context, -1, 0);
781 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
782 EXPECT_EQ(-1, N->getCount());
783 EXPECT_EQ(0, N->getLo());
784 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
785}
786
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000787typedef MetadataTest MDEnumeratorTest;
788
789TEST_F(MDEnumeratorTest, get) {
790 auto *N = MDEnumerator::get(Context, 7, "name");
791 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
792 EXPECT_EQ(7, N->getValue());
793 EXPECT_EQ("name", N->getName());
794 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
795
796 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
797 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000798
799 TempMDEnumerator Temp = N->clone();
800 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000801}
802
803typedef MetadataTest MDBasicTypeTest;
804
805TEST_F(MDBasicTypeTest, get) {
806 auto *N =
807 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
808 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
809 EXPECT_EQ("special", N->getName());
810 EXPECT_EQ(33u, N->getSizeInBits());
811 EXPECT_EQ(26u, N->getAlignInBits());
812 EXPECT_EQ(7u, N->getEncoding());
813 EXPECT_EQ(0u, N->getLine());
814 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
815 26, 7));
816
817 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
818 "special", 33, 26, 7));
819 EXPECT_NE(N,
820 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
821 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
822 26, 7));
823 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
824 25, 7));
825 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
826 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000827
828 TempMDBasicType Temp = N->clone();
829 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000830}
831
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000832TEST_F(MDBasicTypeTest, getWithLargeValues) {
833 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
834 UINT64_MAX, UINT64_MAX - 1, 7);
835 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
836 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
837}
838
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000839TEST_F(MDBasicTypeTest, getUnspecified) {
840 auto *N =
841 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
842 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
843 EXPECT_EQ("unspecified", N->getName());
844 EXPECT_EQ(0u, N->getSizeInBits());
845 EXPECT_EQ(0u, N->getAlignInBits());
846 EXPECT_EQ(0u, N->getEncoding());
847 EXPECT_EQ(0u, N->getLine());
848}
849
850typedef MetadataTest MDTypeTest;
851
852TEST_F(MDTypeTest, clone) {
853 // Check that MDType has a specialized clone that returns TempMDType.
854 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
855 dwarf::DW_ATE_signed);
856
857 TempMDType Temp = N->clone();
858 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
859}
860
861TEST_F(MDTypeTest, setFlags) {
862 // void (void)
863 Metadata *TypesOps[] = {nullptr};
864 Metadata *Types = MDTuple::get(Context, TypesOps);
865
866 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
867 EXPECT_EQ(0u, D->getFlags());
868 D->setFlags(DIDescriptor::FlagRValueReference);
869 EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
870 D->setFlags(0u);
871 EXPECT_EQ(0u, D->getFlags());
872
873 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
874 EXPECT_EQ(0u, T->getFlags());
875 T->setFlags(DIDescriptor::FlagRValueReference);
876 EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
877 T->setFlags(0u);
878 EXPECT_EQ(0u, T->getFlags());
879}
880
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000881typedef MetadataTest MDDerivedTypeTest;
882
883TEST_F(MDDerivedTypeTest, get) {
884 Metadata *File = MDTuple::getDistinct(Context, None);
885 Metadata *Scope = MDTuple::getDistinct(Context, None);
886 Metadata *BaseType = MDTuple::getDistinct(Context, None);
887 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
888
889 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
890 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
891 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
892 EXPECT_EQ("something", N->getName());
893 EXPECT_EQ(File, N->getFile());
894 EXPECT_EQ(1u, N->getLine());
895 EXPECT_EQ(Scope, N->getScope());
896 EXPECT_EQ(BaseType, N->getBaseType());
897 EXPECT_EQ(2u, N->getSizeInBits());
898 EXPECT_EQ(3u, N->getAlignInBits());
899 EXPECT_EQ(4u, N->getOffsetInBits());
900 EXPECT_EQ(5u, N->getFlags());
901 EXPECT_EQ(ExtraData, N->getExtraData());
902 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
903 "something", File, 1, Scope, BaseType, 2, 3,
904 4, 5, ExtraData));
905
906 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
907 "something", File, 1, Scope, BaseType, 2, 3,
908 4, 5, ExtraData));
909 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
910 File, 1, Scope, BaseType, 2, 3, 4, 5,
911 ExtraData));
912 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
913 "something", Scope, 1, Scope, BaseType, 2, 3,
914 4, 5, ExtraData));
915 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
916 "something", File, 2, Scope, BaseType, 2, 3,
917 4, 5, ExtraData));
918 EXPECT_NE(N,
919 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
920 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
921 EXPECT_NE(N,
922 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
923 File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
924 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
925 "something", File, 1, Scope, BaseType, 3, 3,
926 4, 5, ExtraData));
927 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
928 "something", File, 1, Scope, BaseType, 2, 2,
929 4, 5, ExtraData));
930 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
931 "something", File, 1, Scope, BaseType, 2, 3,
932 5, 5, ExtraData));
933 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
934 "something", File, 1, Scope, BaseType, 2, 3,
935 4, 4, ExtraData));
936 EXPECT_NE(N,
937 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
938 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000939
940 TempMDDerivedType Temp = N->clone();
941 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000942}
943
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000944TEST_F(MDDerivedTypeTest, getWithLargeValues) {
945 Metadata *File = MDTuple::getDistinct(Context, None);
946 Metadata *Scope = MDTuple::getDistinct(Context, None);
947 Metadata *BaseType = MDTuple::getDistinct(Context, None);
948 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
949
950 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
951 File, 1, Scope, BaseType, UINT64_MAX,
952 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
953 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
954 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
955 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
956}
957
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000958typedef MetadataTest MDCompositeTypeTest;
959
960TEST_F(MDCompositeTypeTest, get) {
961 unsigned Tag = dwarf::DW_TAG_structure_type;
962 StringRef Name = "some name";
963 Metadata *File = MDTuple::getDistinct(Context, None);
964 unsigned Line = 1;
965 Metadata *Scope = MDTuple::getDistinct(Context, None);
966 Metadata *BaseType = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000967 uint64_t SizeInBits = 2;
968 uint64_t AlignInBits = 3;
969 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000970 unsigned Flags = 5;
971 Metadata *Elements = MDTuple::getDistinct(Context, None);
972 unsigned RuntimeLang = 6;
973 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
974 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
975 StringRef Identifier = "some id";
976
977 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
978 BaseType, SizeInBits, AlignInBits,
979 OffsetInBits, Flags, Elements, RuntimeLang,
980 VTableHolder, TemplateParams, Identifier);
981 EXPECT_EQ(Tag, N->getTag());
982 EXPECT_EQ(Name, N->getName());
983 EXPECT_EQ(File, N->getFile());
984 EXPECT_EQ(Line, N->getLine());
985 EXPECT_EQ(Scope, N->getScope());
986 EXPECT_EQ(BaseType, N->getBaseType());
987 EXPECT_EQ(SizeInBits, N->getSizeInBits());
988 EXPECT_EQ(AlignInBits, N->getAlignInBits());
989 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
990 EXPECT_EQ(Flags, N->getFlags());
991 EXPECT_EQ(Elements, N->getElements());
992 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
993 EXPECT_EQ(VTableHolder, N->getVTableHolder());
994 EXPECT_EQ(TemplateParams, N->getTemplateParams());
995 EXPECT_EQ(Identifier, N->getIdentifier());
996
997 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
998 BaseType, SizeInBits, AlignInBits,
999 OffsetInBits, Flags, Elements, RuntimeLang,
1000 VTableHolder, TemplateParams, Identifier));
1001
1002 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1003 BaseType, SizeInBits, AlignInBits,
1004 OffsetInBits, Flags, Elements, RuntimeLang,
1005 VTableHolder, TemplateParams, Identifier));
1006 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
1007 BaseType, SizeInBits, AlignInBits,
1008 OffsetInBits, Flags, Elements, RuntimeLang,
1009 VTableHolder, TemplateParams, Identifier));
1010 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
1011 BaseType, SizeInBits, AlignInBits,
1012 OffsetInBits, Flags, Elements, RuntimeLang,
1013 VTableHolder, TemplateParams, Identifier));
1014 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1015 BaseType, SizeInBits, AlignInBits,
1016 OffsetInBits, Flags, Elements, RuntimeLang,
1017 VTableHolder, TemplateParams, Identifier));
1018 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
1019 BaseType, SizeInBits, AlignInBits,
1020 OffsetInBits, Flags, Elements, RuntimeLang,
1021 VTableHolder, TemplateParams, Identifier));
1022 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
1023 SizeInBits, AlignInBits, OffsetInBits,
1024 Flags, Elements, RuntimeLang, VTableHolder,
1025 TemplateParams, Identifier));
1026 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1027 BaseType, SizeInBits + 1, AlignInBits,
1028 OffsetInBits, Flags, Elements, RuntimeLang,
1029 VTableHolder, TemplateParams, Identifier));
1030 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1031 BaseType, SizeInBits, AlignInBits + 1,
1032 OffsetInBits, Flags, Elements, RuntimeLang,
1033 VTableHolder, TemplateParams, Identifier));
1034 EXPECT_NE(N, MDCompositeType::get(
1035 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1036 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1037 VTableHolder, TemplateParams, Identifier));
1038 EXPECT_NE(N, MDCompositeType::get(
1039 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1040 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1041 VTableHolder, TemplateParams, Identifier));
1042 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1043 BaseType, SizeInBits, AlignInBits,
1044 OffsetInBits, Flags, File, RuntimeLang,
1045 VTableHolder, TemplateParams, Identifier));
1046 EXPECT_NE(N, MDCompositeType::get(
1047 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1048 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1049 VTableHolder, TemplateParams, Identifier));
1050 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1051 BaseType, SizeInBits, AlignInBits,
1052 OffsetInBits, Flags, Elements, RuntimeLang,
1053 File, TemplateParams, Identifier));
1054 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1055 BaseType, SizeInBits, AlignInBits,
1056 OffsetInBits, Flags, Elements, RuntimeLang,
1057 VTableHolder, File, Identifier));
1058 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1059 BaseType, SizeInBits, AlignInBits,
1060 OffsetInBits, Flags, Elements, RuntimeLang,
1061 VTableHolder, TemplateParams, "other"));
1062
1063 // Be sure that missing identifiers get null pointers.
1064 EXPECT_FALSE(MDCompositeType::get(
1065 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1066 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1067 VTableHolder, TemplateParams, "")->getRawIdentifier());
1068 EXPECT_FALSE(MDCompositeType::get(
1069 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1070 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1071 VTableHolder, TemplateParams)->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001072
1073 TempMDCompositeType Temp = N->clone();
1074 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001075}
1076
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001077TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1078 unsigned Tag = dwarf::DW_TAG_structure_type;
1079 StringRef Name = "some name";
1080 Metadata *File = MDTuple::getDistinct(Context, None);
1081 unsigned Line = 1;
1082 Metadata *Scope = MDTuple::getDistinct(Context, None);
1083 Metadata *BaseType = MDTuple::getDistinct(Context, None);
1084 uint64_t SizeInBits = UINT64_MAX;
1085 uint64_t AlignInBits = UINT64_MAX - 1;
1086 uint64_t OffsetInBits = UINT64_MAX - 2;
1087 unsigned Flags = 5;
1088 Metadata *Elements = MDTuple::getDistinct(Context, None);
1089 unsigned RuntimeLang = 6;
1090 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
1091 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1092 StringRef Identifier = "some id";
1093
1094 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1095 BaseType, SizeInBits, AlignInBits,
1096 OffsetInBits, Flags, Elements, RuntimeLang,
1097 VTableHolder, TemplateParams, Identifier);
1098 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1099 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1100 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1101}
1102
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001103TEST_F(MDCompositeTypeTest, replaceOperands) {
1104 unsigned Tag = dwarf::DW_TAG_structure_type;
1105 StringRef Name = "some name";
1106 Metadata *File = MDTuple::getDistinct(Context, None);
1107 unsigned Line = 1;
1108 Metadata *Scope = MDTuple::getDistinct(Context, None);
1109 Metadata *BaseType = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001110 uint64_t SizeInBits = 2;
1111 uint64_t AlignInBits = 3;
1112 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001113 unsigned Flags = 5;
1114 unsigned RuntimeLang = 6;
1115 StringRef Identifier = "some id";
1116
1117 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1118 BaseType, SizeInBits, AlignInBits,
1119 OffsetInBits, Flags, nullptr, RuntimeLang,
1120 nullptr, nullptr, Identifier);
1121
1122 auto *Elements = MDTuple::getDistinct(Context, None);
1123 EXPECT_EQ(nullptr, N->getElements());
1124 N->replaceElements(Elements);
1125 EXPECT_EQ(Elements, N->getElements());
1126 N->replaceElements(nullptr);
1127 EXPECT_EQ(nullptr, N->getElements());
1128
1129 auto *VTableHolder = MDTuple::getDistinct(Context, None);
1130 EXPECT_EQ(nullptr, N->getVTableHolder());
1131 N->replaceVTableHolder(VTableHolder);
1132 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1133 N->replaceVTableHolder(nullptr);
1134 EXPECT_EQ(nullptr, N->getVTableHolder());
1135
1136 auto *TemplateParams = MDTuple::getDistinct(Context, None);
1137 EXPECT_EQ(nullptr, N->getTemplateParams());
1138 N->replaceTemplateParams(TemplateParams);
1139 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1140 N->replaceTemplateParams(nullptr);
1141 EXPECT_EQ(nullptr, N->getTemplateParams());
1142}
1143
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001144typedef MetadataTest MDSubroutineTypeTest;
1145
1146TEST_F(MDSubroutineTypeTest, get) {
1147 unsigned Flags = 1;
1148 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
1149
1150 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1151 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1152 EXPECT_EQ(Flags, N->getFlags());
1153 EXPECT_EQ(TypeArray, N->getTypeArray());
1154 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1155
1156 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
1157 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
1158 MDTuple::getDistinct(Context, None)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001159
1160 TempMDSubroutineType Temp = N->clone();
1161 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001162
1163 // Test always-empty operands.
1164 EXPECT_EQ(nullptr, N->getScope());
1165 EXPECT_EQ(nullptr, N->getFile());
1166 EXPECT_EQ("", N->getName());
1167 EXPECT_EQ(nullptr, N->getBaseType());
1168 EXPECT_EQ(nullptr, N->getVTableHolder());
1169 EXPECT_EQ(nullptr, N->getTemplateParams());
1170 EXPECT_EQ("", N->getIdentifier());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001171}
1172
1173typedef MetadataTest MDFileTest;
1174
1175TEST_F(MDFileTest, get) {
1176 StringRef Filename = "file";
1177 StringRef Directory = "dir";
1178 auto *N = MDFile::get(Context, Filename, Directory);
1179
1180 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1181 EXPECT_EQ(Filename, N->getFilename());
1182 EXPECT_EQ(Directory, N->getDirectory());
1183 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1184
1185 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1186 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001187
1188 TempMDFile Temp = N->clone();
1189 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001190}
1191
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001192TEST_F(MDFileTest, ScopeGetFile) {
1193 // Ensure that MDScope::getFile() returns itself.
1194 MDScope *N = MDFile::get(Context, "file", "dir");
1195 EXPECT_EQ(N, N->getFile());
1196}
1197
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001198typedef MetadataTest MDCompileUnitTest;
1199
1200TEST_F(MDCompileUnitTest, get) {
1201 unsigned SourceLanguage = 1;
1202 Metadata *File = MDTuple::getDistinct(Context, None);
1203 StringRef Producer = "some producer";
1204 bool IsOptimized = false;
1205 StringRef Flags = "flag after flag";
1206 unsigned RuntimeVersion = 2;
1207 StringRef SplitDebugFilename = "another/file";
1208 unsigned EmissionKind = 3;
1209 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1210 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1211 Metadata *Subprograms = MDTuple::getDistinct(Context, None);
1212 Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
1213 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1214 auto *N = MDCompileUnit::get(
1215 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1216 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1217 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1218
1219 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1220 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1221 EXPECT_EQ(File, N->getFile());
1222 EXPECT_EQ(Producer, N->getProducer());
1223 EXPECT_EQ(IsOptimized, N->isOptimized());
1224 EXPECT_EQ(Flags, N->getFlags());
1225 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1226 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1227 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1228 EXPECT_EQ(EnumTypes, N->getEnumTypes());
1229 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1230 EXPECT_EQ(Subprograms, N->getSubprograms());
1231 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1232 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1233 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1234 IsOptimized, Flags, RuntimeVersion,
1235 SplitDebugFilename, EmissionKind, EnumTypes,
1236 RetainedTypes, Subprograms, GlobalVariables,
1237 ImportedEntities));
1238
1239 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1240 IsOptimized, Flags, RuntimeVersion,
1241 SplitDebugFilename, EmissionKind, EnumTypes,
1242 RetainedTypes, Subprograms, GlobalVariables,
1243 ImportedEntities));
1244 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
1245 IsOptimized, Flags, RuntimeVersion,
1246 SplitDebugFilename, EmissionKind, EnumTypes,
1247 RetainedTypes, Subprograms, GlobalVariables,
1248 ImportedEntities));
1249 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1250 IsOptimized, Flags, RuntimeVersion,
1251 SplitDebugFilename, EmissionKind, EnumTypes,
1252 RetainedTypes, Subprograms, GlobalVariables,
1253 ImportedEntities));
1254 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1255 !IsOptimized, Flags, RuntimeVersion,
1256 SplitDebugFilename, EmissionKind, EnumTypes,
1257 RetainedTypes, Subprograms, GlobalVariables,
1258 ImportedEntities));
1259 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1260 IsOptimized, "other", RuntimeVersion,
1261 SplitDebugFilename, EmissionKind, EnumTypes,
1262 RetainedTypes, Subprograms, GlobalVariables,
1263 ImportedEntities));
1264 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1265 IsOptimized, Flags, RuntimeVersion + 1,
1266 SplitDebugFilename, EmissionKind, EnumTypes,
1267 RetainedTypes, Subprograms, GlobalVariables,
1268 ImportedEntities));
1269 EXPECT_NE(N,
1270 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1271 IsOptimized, Flags, RuntimeVersion, "other",
1272 EmissionKind, EnumTypes, RetainedTypes,
1273 Subprograms, GlobalVariables, ImportedEntities));
1274 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1275 IsOptimized, Flags, RuntimeVersion,
1276 SplitDebugFilename, EmissionKind + 1,
1277 EnumTypes, RetainedTypes, Subprograms,
1278 GlobalVariables, ImportedEntities));
1279 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1280 IsOptimized, Flags, RuntimeVersion,
1281 SplitDebugFilename, EmissionKind, File,
1282 RetainedTypes, Subprograms, GlobalVariables,
1283 ImportedEntities));
1284 EXPECT_NE(N, MDCompileUnit::get(
1285 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1286 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1287 File, Subprograms, GlobalVariables, ImportedEntities));
1288 EXPECT_NE(N, MDCompileUnit::get(
1289 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1290 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1291 RetainedTypes, File, GlobalVariables, ImportedEntities));
1292 EXPECT_NE(N, MDCompileUnit::get(
1293 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1294 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1295 RetainedTypes, Subprograms, File, ImportedEntities));
1296 EXPECT_NE(N, MDCompileUnit::get(
1297 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1298 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1299 RetainedTypes, Subprograms, GlobalVariables, File));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001300
1301 TempMDCompileUnit Temp = N->clone();
1302 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001303}
1304
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001305TEST_F(MDCompileUnitTest, replaceArrays) {
1306 unsigned SourceLanguage = 1;
1307 Metadata *File = MDTuple::getDistinct(Context, None);
1308 StringRef Producer = "some producer";
1309 bool IsOptimized = false;
1310 StringRef Flags = "flag after flag";
1311 unsigned RuntimeVersion = 2;
1312 StringRef SplitDebugFilename = "another/file";
1313 unsigned EmissionKind = 3;
1314 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
1315 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
1316 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
1317 auto *N = MDCompileUnit::get(
1318 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1319 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1320 RetainedTypes, nullptr, nullptr, ImportedEntities);
1321
1322 auto *Subprograms = MDTuple::getDistinct(Context, None);
1323 EXPECT_EQ(nullptr, N->getSubprograms());
1324 N->replaceSubprograms(Subprograms);
1325 EXPECT_EQ(Subprograms, N->getSubprograms());
1326 N->replaceSubprograms(nullptr);
1327 EXPECT_EQ(nullptr, N->getSubprograms());
1328
1329 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1330 EXPECT_EQ(nullptr, N->getGlobalVariables());
1331 N->replaceGlobalVariables(GlobalVariables);
1332 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1333 N->replaceGlobalVariables(nullptr);
1334 EXPECT_EQ(nullptr, N->getGlobalVariables());
1335}
1336
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001337typedef MetadataTest MDSubprogramTest;
1338
1339TEST_F(MDSubprogramTest, get) {
1340 Metadata *Scope = MDTuple::getDistinct(Context, None);
1341 StringRef Name = "name";
1342 StringRef LinkageName = "linkage";
1343 Metadata *File = MDTuple::getDistinct(Context, None);
1344 unsigned Line = 2;
1345 Metadata *Type = MDTuple::getDistinct(Context, None);
1346 bool IsLocalToUnit = false;
1347 bool IsDefinition = true;
1348 unsigned ScopeLine = 3;
1349 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1350 unsigned Virtuality = 4;
1351 unsigned VirtualIndex = 5;
1352 unsigned Flags = 6;
1353 bool IsOptimized = false;
1354 Metadata *Function = MDTuple::getDistinct(Context, None);
1355 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1356 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1357 Metadata *Variables = MDTuple::getDistinct(Context, None);
1358
1359 auto *N = MDSubprogram::get(
1360 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1361 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1362 IsOptimized, Function, TemplateParams, Declaration, Variables);
1363
1364 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1365 EXPECT_EQ(Scope, N->getScope());
1366 EXPECT_EQ(Name, N->getName());
1367 EXPECT_EQ(LinkageName, N->getLinkageName());
1368 EXPECT_EQ(File, N->getFile());
1369 EXPECT_EQ(Line, N->getLine());
1370 EXPECT_EQ(Type, N->getType());
1371 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1372 EXPECT_EQ(IsDefinition, N->isDefinition());
1373 EXPECT_EQ(ScopeLine, N->getScopeLine());
1374 EXPECT_EQ(ContainingType, N->getContainingType());
1375 EXPECT_EQ(Virtuality, N->getVirtuality());
1376 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1377 EXPECT_EQ(Flags, N->getFlags());
1378 EXPECT_EQ(IsOptimized, N->isOptimized());
1379 EXPECT_EQ(Function, N->getFunction());
1380 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1381 EXPECT_EQ(Declaration, N->getDeclaration());
1382 EXPECT_EQ(Variables, N->getVariables());
1383 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1384 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1385 ContainingType, Virtuality, VirtualIndex,
1386 Flags, IsOptimized, Function, TemplateParams,
1387 Declaration, Variables));
1388
1389 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1390 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1391 ContainingType, Virtuality, VirtualIndex,
1392 Flags, IsOptimized, Function, TemplateParams,
1393 Declaration, Variables));
1394 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1395 Line, Type, IsLocalToUnit, IsDefinition,
1396 ScopeLine, ContainingType, Virtuality,
1397 VirtualIndex, Flags, IsOptimized, Function,
1398 TemplateParams, Declaration, Variables));
1399 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1400 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1401 ContainingType, Virtuality, VirtualIndex,
1402 Flags, IsOptimized, Function, TemplateParams,
1403 Declaration, Variables));
1404 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1405 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1406 ContainingType, Virtuality, VirtualIndex,
1407 Flags, IsOptimized, Function, TemplateParams,
1408 Declaration, Variables));
1409 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1410 Line + 1, Type, IsLocalToUnit, IsDefinition,
1411 ScopeLine, ContainingType, Virtuality,
1412 VirtualIndex, Flags, IsOptimized, Function,
1413 TemplateParams, Declaration, Variables));
1414 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1415 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1416 ContainingType, Virtuality, VirtualIndex,
1417 Flags, IsOptimized, Function, TemplateParams,
1418 Declaration, Variables));
1419 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1420 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1421 ContainingType, Virtuality, VirtualIndex,
1422 Flags, IsOptimized, Function, TemplateParams,
1423 Declaration, Variables));
1424 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1425 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1426 ContainingType, Virtuality, VirtualIndex,
1427 Flags, IsOptimized, Function, TemplateParams,
1428 Declaration, Variables));
1429 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1430 Type, IsLocalToUnit, IsDefinition,
1431 ScopeLine + 1, ContainingType, Virtuality,
1432 VirtualIndex, Flags, IsOptimized, Function,
1433 TemplateParams, Declaration, Variables));
1434 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1435 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1436 Type, Virtuality, VirtualIndex, Flags,
1437 IsOptimized, Function, TemplateParams,
1438 Declaration, Variables));
1439 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1440 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1441 ContainingType, Virtuality + 1, VirtualIndex,
1442 Flags, IsOptimized, Function, TemplateParams,
1443 Declaration, Variables));
1444 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1445 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1446 ContainingType, Virtuality, VirtualIndex + 1,
1447 Flags, IsOptimized, Function, TemplateParams,
1448 Declaration, Variables));
1449 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1450 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1451 ContainingType, Virtuality, VirtualIndex,
1452 ~Flags, IsOptimized, Function, TemplateParams,
1453 Declaration, Variables));
1454 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1455 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1456 ContainingType, Virtuality, VirtualIndex,
1457 Flags, !IsOptimized, Function, TemplateParams,
1458 Declaration, Variables));
1459 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1460 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1461 ContainingType, Virtuality, VirtualIndex,
1462 Flags, IsOptimized, Type, TemplateParams,
1463 Declaration, Variables));
1464 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1465 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1466 ContainingType, Virtuality, VirtualIndex,
1467 Flags, IsOptimized, Function, Type,
1468 Declaration, Variables));
1469 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1470 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1471 ContainingType, Virtuality, VirtualIndex,
1472 Flags, IsOptimized, Function, TemplateParams,
1473 Type, Variables));
1474 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1475 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1476 ContainingType, Virtuality, VirtualIndex,
1477 Flags, IsOptimized, Function, TemplateParams,
1478 Declaration, Type));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001479
1480 TempMDSubprogram Temp = N->clone();
1481 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001482}
1483
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001484TEST_F(MDSubprogramTest, replaceFunction) {
1485 Metadata *Scope = MDTuple::getDistinct(Context, None);
1486 StringRef Name = "name";
1487 StringRef LinkageName = "linkage";
1488 Metadata *File = MDTuple::getDistinct(Context, None);
1489 unsigned Line = 2;
1490 Metadata *Type = MDTuple::getDistinct(Context, None);
1491 bool IsLocalToUnit = false;
1492 bool IsDefinition = true;
1493 unsigned ScopeLine = 3;
1494 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1495 unsigned Virtuality = 4;
1496 unsigned VirtualIndex = 5;
1497 unsigned Flags = 6;
1498 bool IsOptimized = false;
1499 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1500 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1501 Metadata *Variables = MDTuple::getDistinct(Context, None);
1502
1503 auto *N = MDSubprogram::get(
1504 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1505 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1506 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1507
1508 EXPECT_EQ(nullptr, N->getFunction());
1509
1510 std::unique_ptr<Function> F(
1511 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1512 GlobalValue::ExternalLinkage));
1513 N->replaceFunction(F.get());
1514 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1515
1516 N->replaceFunction(nullptr);
1517 EXPECT_EQ(nullptr, N->getFunction());
1518}
1519
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001520typedef MetadataTest MDLexicalBlockTest;
1521
1522TEST_F(MDLexicalBlockTest, get) {
1523 Metadata *Scope = MDTuple::getDistinct(Context, None);
1524 Metadata *File = MDTuple::getDistinct(Context, None);
1525 unsigned Line = 5;
1526 unsigned Column = 8;
1527
1528 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1529
1530 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1531 EXPECT_EQ(Scope, N->getScope());
1532 EXPECT_EQ(File, N->getFile());
1533 EXPECT_EQ(Line, N->getLine());
1534 EXPECT_EQ(Column, N->getColumn());
1535 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1536
1537 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1538 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1539 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1540 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001541
1542 TempMDLexicalBlock Temp = N->clone();
1543 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001544}
1545
1546typedef MetadataTest MDLexicalBlockFileTest;
1547
1548TEST_F(MDLexicalBlockFileTest, get) {
1549 Metadata *Scope = MDTuple::getDistinct(Context, None);
1550 Metadata *File = MDTuple::getDistinct(Context, None);
1551 unsigned Discriminator = 5;
1552
1553 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1554
1555 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1556 EXPECT_EQ(Scope, N->getScope());
1557 EXPECT_EQ(File, N->getFile());
1558 EXPECT_EQ(Discriminator, N->getDiscriminator());
1559 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1560
1561 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1562 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1563 EXPECT_NE(N,
1564 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001565
1566 TempMDLexicalBlockFile Temp = N->clone();
1567 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001568}
1569
1570typedef MetadataTest MDNamespaceTest;
1571
1572TEST_F(MDNamespaceTest, get) {
1573 Metadata *Scope = MDTuple::getDistinct(Context, None);
1574 Metadata *File = MDTuple::getDistinct(Context, None);
1575 StringRef Name = "namespace";
1576 unsigned Line = 5;
1577
1578 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1579
1580 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1581 EXPECT_EQ(Scope, N->getScope());
1582 EXPECT_EQ(File, N->getFile());
1583 EXPECT_EQ(Name, N->getName());
1584 EXPECT_EQ(Line, N->getLine());
1585 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1586
1587 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1588 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1589 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1590 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001591
1592 TempMDNamespace Temp = N->clone();
1593 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001594}
1595
1596typedef MetadataTest MDTemplateTypeParameterTest;
1597
1598TEST_F(MDTemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001599 StringRef Name = "template";
1600 Metadata *Type = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001601 Metadata *Other = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001602
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001603 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001604
1605 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001606 EXPECT_EQ(Name, N->getName());
1607 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001608 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001609
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001610 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1611 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001612
1613 TempMDTemplateTypeParameter 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 MDTemplateValueParameterTest;
1618
1619TEST_F(MDTemplateValueParameterTest, get) {
1620 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001621 StringRef Name = "template";
1622 Metadata *Type = MDTuple::getDistinct(Context, None);
1623 Metadata *Value = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001624 Metadata *Other = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001625
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001626 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001627 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001628 EXPECT_EQ(Name, N->getName());
1629 EXPECT_EQ(Type, N->getType());
1630 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001631 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001632
1633 EXPECT_NE(N, MDTemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001634 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1635 Type, Value));
1636 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001637 Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001638 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Other,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001639 Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001640 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001641
1642 TempMDTemplateValueParameter Temp = N->clone();
1643 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001644}
1645
1646typedef MetadataTest MDGlobalVariableTest;
1647
1648TEST_F(MDGlobalVariableTest, get) {
1649 Metadata *Scope = MDTuple::getDistinct(Context, None);
1650 StringRef Name = "name";
1651 StringRef LinkageName = "linkage";
1652 Metadata *File = MDTuple::getDistinct(Context, None);
1653 unsigned Line = 5;
1654 Metadata *Type = MDTuple::getDistinct(Context, None);
1655 bool IsLocalToUnit = false;
1656 bool IsDefinition = true;
1657 Metadata *Variable = MDTuple::getDistinct(Context, None);
1658 Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1659
1660 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1661 Type, IsLocalToUnit, IsDefinition, Variable,
1662 StaticDataMemberDeclaration);
1663 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1664 EXPECT_EQ(Scope, N->getScope());
1665 EXPECT_EQ(Name, N->getName());
1666 EXPECT_EQ(LinkageName, N->getLinkageName());
1667 EXPECT_EQ(File, N->getFile());
1668 EXPECT_EQ(Line, N->getLine());
1669 EXPECT_EQ(Type, N->getType());
1670 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1671 EXPECT_EQ(IsDefinition, N->isDefinition());
1672 EXPECT_EQ(Variable, N->getVariable());
1673 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1674 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1675 Line, Type, IsLocalToUnit, IsDefinition,
1676 Variable, StaticDataMemberDeclaration));
1677
1678 EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1679 Line, Type, IsLocalToUnit, IsDefinition,
1680 Variable, StaticDataMemberDeclaration));
1681 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1682 Line, Type, IsLocalToUnit, IsDefinition,
1683 Variable, StaticDataMemberDeclaration));
1684 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1685 Type, IsLocalToUnit, IsDefinition,
1686 Variable, StaticDataMemberDeclaration));
1687 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1688 Line, Type, IsLocalToUnit, IsDefinition,
1689 Variable, StaticDataMemberDeclaration));
1690 EXPECT_NE(N,
1691 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1692 Line + 1, Type, IsLocalToUnit, IsDefinition,
1693 Variable, StaticDataMemberDeclaration));
1694 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1695 Line, Scope, IsLocalToUnit, IsDefinition,
1696 Variable, StaticDataMemberDeclaration));
1697 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1698 Line, Type, !IsLocalToUnit, IsDefinition,
1699 Variable, StaticDataMemberDeclaration));
1700 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1701 Line, Type, IsLocalToUnit, !IsDefinition,
1702 Variable, StaticDataMemberDeclaration));
1703 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1704 Line, Type, IsLocalToUnit, IsDefinition,
1705 Type, StaticDataMemberDeclaration));
1706 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1707 Line, Type, IsLocalToUnit, IsDefinition,
1708 Variable, Type));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001709
1710 TempMDGlobalVariable Temp = N->clone();
1711 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001712}
1713
1714typedef MetadataTest MDLocalVariableTest;
1715
1716TEST_F(MDLocalVariableTest, get) {
1717 unsigned Tag = dwarf::DW_TAG_arg_variable;
1718 Metadata *Scope = MDTuple::getDistinct(Context, None);
1719 StringRef Name = "name";
1720 Metadata *File = MDTuple::getDistinct(Context, None);
1721 unsigned Line = 5;
1722 Metadata *Type = MDTuple::getDistinct(Context, None);
1723 unsigned Arg = 6;
1724 unsigned Flags = 7;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001725 Metadata *InlinedAtScope = MDTuple::getDistinct(Context, None);
1726 Metadata *InlinedAt =
1727 MDLocation::getDistinct(Context, 10, 20, InlinedAtScope);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001728
1729 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1730 Arg, Flags, InlinedAt);
1731 EXPECT_EQ(Tag, N->getTag());
1732 EXPECT_EQ(Scope, N->getScope());
1733 EXPECT_EQ(Name, N->getName());
1734 EXPECT_EQ(File, N->getFile());
1735 EXPECT_EQ(Line, N->getLine());
1736 EXPECT_EQ(Type, N->getType());
1737 EXPECT_EQ(Arg, N->getArg());
1738 EXPECT_EQ(Flags, N->getFlags());
1739 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1740 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1741 Arg, Flags, InlinedAt));
1742
1743 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1744 Name, File, Line, Type, Arg, Flags,
1745 InlinedAt));
1746 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1747 Type, Arg, Flags, InlinedAt));
1748 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1749 Type, Arg, Flags, InlinedAt));
1750 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1751 Type, Arg, Flags, InlinedAt));
1752 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1753 Type, Arg, Flags, InlinedAt));
1754 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1755 Scope, Arg, Flags, InlinedAt));
1756 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1757 Arg + 1, Flags, InlinedAt));
1758 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1759 Arg, ~Flags, InlinedAt));
1760 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1761 Arg, Flags, Scope));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001762
1763 TempMDLocalVariable Temp = N->clone();
1764 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001765
1766 auto *Inlined = N->withoutInline();
1767 EXPECT_NE(N, Inlined);
1768 EXPECT_EQ(N->getTag(), Inlined->getTag());
1769 EXPECT_EQ(N->getScope(), Inlined->getScope());
1770 EXPECT_EQ(N->getName(), Inlined->getName());
1771 EXPECT_EQ(N->getFile(), Inlined->getFile());
1772 EXPECT_EQ(N->getLine(), Inlined->getLine());
1773 EXPECT_EQ(N->getType(), Inlined->getType());
1774 EXPECT_EQ(N->getArg(), Inlined->getArg());
1775 EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1776 EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1777 EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001778}
1779
1780typedef MetadataTest MDExpressionTest;
1781
1782TEST_F(MDExpressionTest, get) {
1783 uint64_t Elements[] = {2, 6, 9, 78, 0};
1784 auto *N = MDExpression::get(Context, Elements);
1785 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1786 EXPECT_EQ(N, MDExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001787
1788 EXPECT_EQ(5u, N->getNumElements());
1789 EXPECT_EQ(2u, N->getElement(0));
1790 EXPECT_EQ(6u, N->getElement(1));
1791 EXPECT_EQ(9u, N->getElement(2));
1792 EXPECT_EQ(78u, N->getElement(3));
1793 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001794
1795 TempMDExpression Temp = N->clone();
1796 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001797}
1798
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001799TEST_F(MDExpressionTest, isValid) {
1800#define EXPECT_VALID(...) \
1801 do { \
1802 uint64_t Elements[] = {__VA_ARGS__}; \
1803 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1804 } while (false)
1805#define EXPECT_INVALID(...) \
1806 do { \
1807 uint64_t Elements[] = {__VA_ARGS__}; \
1808 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1809 } while (false)
1810
1811 // Empty expression should be valid.
1812 EXPECT_TRUE(MDExpression::get(Context, None));
1813
1814 // Valid constructions.
1815 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1816 EXPECT_VALID(dwarf::DW_OP_deref);
1817 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1818 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1819 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1820 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1821 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1822
1823 // Invalid constructions.
1824 EXPECT_INVALID(~0u);
1825 EXPECT_INVALID(dwarf::DW_OP_plus);
1826 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1827 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1828 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1829 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1830
1831#undef EXPECT_VALID
1832#undef EXPECT_INVALID
1833}
1834
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001835typedef MetadataTest MDObjCPropertyTest;
1836
1837TEST_F(MDObjCPropertyTest, get) {
1838 StringRef Name = "name";
1839 Metadata *File = MDTuple::getDistinct(Context, None);
1840 unsigned Line = 5;
1841 StringRef GetterName = "getter";
1842 StringRef SetterName = "setter";
1843 unsigned Attributes = 7;
1844 Metadata *Type = MDTuple::getDistinct(Context, None);
1845
1846 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1847 SetterName, Attributes, Type);
1848
1849 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1850 EXPECT_EQ(Name, N->getName());
1851 EXPECT_EQ(File, N->getFile());
1852 EXPECT_EQ(Line, N->getLine());
1853 EXPECT_EQ(GetterName, N->getGetterName());
1854 EXPECT_EQ(SetterName, N->getSetterName());
1855 EXPECT_EQ(Attributes, N->getAttributes());
1856 EXPECT_EQ(Type, N->getType());
1857 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1858 SetterName, Attributes, Type));
1859
1860 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1861 SetterName, Attributes, Type));
1862 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1863 SetterName, Attributes, Type));
1864 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1865 SetterName, Attributes, Type));
1866 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1867 SetterName, Attributes, Type));
1868 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1869 "other", Attributes, Type));
1870 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1871 SetterName, Attributes + 1, Type));
1872 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1873 SetterName, Attributes, File));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001874
1875 TempMDObjCProperty Temp = N->clone();
1876 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001877}
1878
1879typedef MetadataTest MDImportedEntityTest;
1880
1881TEST_F(MDImportedEntityTest, get) {
1882 unsigned Tag = dwarf::DW_TAG_imported_module;
1883 Metadata *Scope = MDTuple::getDistinct(Context, None);
1884 Metadata *Entity = MDTuple::getDistinct(Context, None);
1885 unsigned Line = 5;
1886 StringRef Name = "name";
1887
1888 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1889
1890 EXPECT_EQ(Tag, N->getTag());
1891 EXPECT_EQ(Scope, N->getScope());
1892 EXPECT_EQ(Entity, N->getEntity());
1893 EXPECT_EQ(Line, N->getLine());
1894 EXPECT_EQ(Name, N->getName());
1895 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1896
1897 EXPECT_NE(N,
1898 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1899 Scope, Entity, Line, Name));
1900 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1901 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1902 EXPECT_NE(N,
1903 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1904 EXPECT_NE(N,
1905 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001906
1907 TempMDImportedEntity Temp = N->clone();
1908 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001909}
1910
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001911typedef MetadataTest MetadataAsValueTest;
1912
1913TEST_F(MetadataAsValueTest, MDNode) {
1914 MDNode *N = MDNode::get(Context, None);
1915 auto *V = MetadataAsValue::get(Context, N);
1916 EXPECT_TRUE(V->getType()->isMetadataTy());
1917 EXPECT_EQ(N, V->getMetadata());
1918
1919 auto *V2 = MetadataAsValue::get(Context, N);
1920 EXPECT_EQ(V, V2);
1921}
1922
1923TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1924 MDNode *N = MDNode::get(Context, None);
1925 Metadata *Ops[] = {N};
1926 MDNode *N2 = MDNode::get(Context, Ops);
1927 auto *V = MetadataAsValue::get(Context, N2);
1928 EXPECT_TRUE(V->getType()->isMetadataTy());
1929 EXPECT_EQ(N2, V->getMetadata());
1930
1931 auto *V2 = MetadataAsValue::get(Context, N2);
1932 EXPECT_EQ(V, V2);
1933
1934 auto *V3 = MetadataAsValue::get(Context, N);
1935 EXPECT_TRUE(V3->getType()->isMetadataTy());
1936 EXPECT_NE(V, V3);
1937 EXPECT_EQ(N, V3->getMetadata());
1938}
1939
1940TEST_F(MetadataAsValueTest, MDNodeConstant) {
1941 auto *C = ConstantInt::getTrue(Context);
1942 auto *MD = ConstantAsMetadata::get(C);
1943 Metadata *Ops[] = {MD};
1944 auto *N = MDNode::get(Context, Ops);
1945
1946 auto *V = MetadataAsValue::get(Context, MD);
1947 EXPECT_TRUE(V->getType()->isMetadataTy());
1948 EXPECT_EQ(MD, V->getMetadata());
1949
1950 auto *V2 = MetadataAsValue::get(Context, N);
1951 EXPECT_EQ(MD, V2->getMetadata());
1952 EXPECT_EQ(V, V2);
1953}
1954
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001955typedef MetadataTest ValueAsMetadataTest;
1956
1957TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1958 Type *Ty = Type::getInt1PtrTy(Context);
1959 std::unique_ptr<GlobalVariable> GV0(
1960 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1961 auto *MD = ValueAsMetadata::get(GV0.get());
1962 EXPECT_TRUE(MD->getValue() == GV0.get());
1963 ASSERT_TRUE(GV0->use_empty());
1964
1965 std::unique_ptr<GlobalVariable> GV1(
1966 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1967 GV0->replaceAllUsesWith(GV1.get());
1968 EXPECT_TRUE(MD->getValue() == GV1.get());
1969}
1970
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001971TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1972 // Create a constant.
1973 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1974 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1975
1976 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001977 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001978
1979 // 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 +00001980 Metadata *Ops1[] = {CI, CI, Temp.get()};
1981 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001982
1983 auto *N1 = MDTuple::get(Context, Ops1);
1984 auto *N2 = MDTuple::get(Context, Ops2);
1985 ASSERT_NE(N1, N2);
1986
1987 // Tell metadata that the constant is getting deleted.
1988 //
1989 // After this, N1 will be invalid, so don't touch it.
1990 ValueAsMetadata::handleDeletion(CI->getValue());
1991 EXPECT_EQ(nullptr, N2->getOperand(0));
1992 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001993 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001994
1995 // Clean up Temp for teardown.
1996 Temp->replaceAllUsesWith(nullptr);
1997}
1998
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001999typedef MetadataTest TrackingMDRefTest;
2000
2001TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2002 Type *Ty = Type::getInt1PtrTy(Context);
2003 std::unique_ptr<GlobalVariable> GV0(
2004 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2005 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2006 EXPECT_TRUE(MD->getValue() == GV0.get());
2007 ASSERT_TRUE(GV0->use_empty());
2008
2009 std::unique_ptr<GlobalVariable> GV1(
2010 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2011 GV0->replaceAllUsesWith(GV1.get());
2012 EXPECT_TRUE(MD->getValue() == GV1.get());
2013
2014 // Reset it, so we don't inadvertently test deletion.
2015 MD.reset();
2016}
2017
2018TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2019 Type *Ty = Type::getInt1PtrTy(Context);
2020 std::unique_ptr<GlobalVariable> GV(
2021 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2022 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2023 EXPECT_TRUE(MD->getValue() == GV.get());
2024 ASSERT_TRUE(GV->use_empty());
2025
2026 GV.reset();
2027 EXPECT_TRUE(!MD);
2028}
2029
Devang Patel0924b332009-07-30 00:03:41 +00002030TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002031 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002032 ConstantAsMetadata *C =
2033 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2034 ConstantAsMetadata *C2 =
2035 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002036
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002037 Metadata *const V = C;
2038 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002039 MDNode *n = MDNode::get(Context, V);
2040 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002041
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002042 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002043 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002044 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2045 NMD->addOperand(n);
2046 NMD->addOperand(n2);
2047
Chris Lattnerbe354a62009-08-23 04:47:35 +00002048 std::string Str;
2049 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002050 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002051 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002052 oss.str().c_str());
2053}
Nick Lewycky49f89192009-04-04 07:22:01 +00002054}