blob: ac6d3df6f804df9ef64c17f3ab9febe26648234a [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 {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000064public:
65 MetadataTest() : Counter(0) {}
66
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000067protected:
68 LLVMContext Context;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000069 int Counter;
70
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000071 MDNode *getNode() { return MDNode::get(Context, None); }
72 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
73 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
74 Metadata *MDs[] = {MD1, MD2};
75 return MDNode::get(Context, MDs);
76 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000077
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000078 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000079 MDSubprogram *getSubprogram() {
80 return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
81 nullptr, false, false, 0, nullptr, 0, 0, 0,
82 0);
83 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000084 MDFile *getFile() {
85 return MDFile::getDistinct(Context, "file.c", "/path/to/dir");
86 }
87 MDBasicType *getBasicType(StringRef Name) {
88 return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
89 }
90 MDDerivedType *getDerivedType() {
91 return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
92 nullptr, 0, nullptr,
93 getBasicType("basictype"), 1, 2, 0, 0);
94 }
95 ConstantAsMetadata *getConstantAsMetadata() {
96 return ConstantAsMetadata::get(
97 ConstantInt::get(Type::getInt32Ty(Context), Counter++));
98 }
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000099 MDCompositeType *getCompositeType() {
100 return MDCompositeType::getDistinct(
101 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
102 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "");
103 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000104};
105typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000106
Nick Lewycky49f89192009-04-04 07:22:01 +0000107// Test that construction of MDString with different value produces different
108// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000109TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000110 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000111 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000112 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000113 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000114 EXPECT_NE(s1, s2);
115}
116
117// Test that creation of MDStrings with the same string contents produces the
118// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000119TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000120 char x[4] = { 'a', 'b', 'c', 'X' };
121 char y[4] = { 'a', 'b', 'c', 'Y' };
122
Owen Anderson23587322009-07-31 21:38:10 +0000123 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
124 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000125 EXPECT_EQ(s1, s2);
126}
127
128// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000129TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000130 char *str = new char[13];
131 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000132 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000133 strncpy(str, "aaaaaaaaaaaaa", 13);
134 delete[] str;
135
Chris Lattnerbe354a62009-08-23 04:47:35 +0000136 std::string Str;
137 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000138 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000139 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000140}
141
142// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000143TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000144 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000145 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000146 std::string Str;
147 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000148 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000149 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000150}
151
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000152typedef MetadataTest MDNodeTest;
153
Nick Lewycky49f89192009-04-04 07:22:01 +0000154// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000155TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000156 char x[3] = { 'a', 'b', 'c' };
157 char y[3] = { '1', '2', '3' };
158
Owen Anderson23587322009-07-31 21:38:10 +0000159 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
160 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000161 ConstantAsMetadata *CI = ConstantAsMetadata::get(
162 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000163
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000164 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000165 V.push_back(s1);
166 V.push_back(CI);
167 V.push_back(s2);
168
Jay Foad5514afe2011-04-21 19:59:31 +0000169 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000170 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000171 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000172 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000173 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000174 MDNode *n4 = MDNode::getIfExists(Context, V);
175 MDNode *n5 = MDNode::getIfExists(Context, c1);
176 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000177 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000178 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000179 EXPECT_EQ(n4, n1);
180 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000181 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000182
Chris Lattner9b493022009-12-31 01:22:29 +0000183 EXPECT_EQ(3u, n1->getNumOperands());
184 EXPECT_EQ(s1, n1->getOperand(0));
185 EXPECT_EQ(CI, n1->getOperand(1));
186 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000187
Chris Lattner9b493022009-12-31 01:22:29 +0000188 EXPECT_EQ(1u, n2->getNumOperands());
189 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000190}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000191
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000192TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000193 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
194 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000195
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000196 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000197 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000198 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000199
200 EXPECT_EQ(n, wvh);
201
202 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000203}
Devang Patel0924b332009-07-30 00:03:41 +0000204
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000205TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000206 // !0 = !{!0}
207 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000208 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000209 auto Temp = MDNode::getTemporary(Context, None);
210 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000211 MDNode *Self = MDNode::get(Context, Args);
212 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000213 ASSERT_EQ(Self, Self->getOperand(0));
214
215 // Self-references should be distinct, so MDNode::get() should grab a
216 // uniqued node that references Self, not Self.
217 Args[0] = Self;
218 MDNode *Ref1 = MDNode::get(Context, Args);
219 MDNode *Ref2 = MDNode::get(Context, Args);
220 EXPECT_NE(Self, Ref1);
221 EXPECT_EQ(Ref1, Ref2);
222 }
223
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000224 // !0 = !{!0, !{}}
225 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000226 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000227 auto Temp = MDNode::getTemporary(Context, None);
228 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000229 MDNode *Self = MDNode::get(Context, Args);
230 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000231 ASSERT_EQ(Self, Self->getOperand(0));
232
233 // Self-references should be distinct, so MDNode::get() should grab a
234 // uniqued node that references Self, not Self itself.
235 Args[0] = Self;
236 MDNode *Ref1 = MDNode::get(Context, Args);
237 MDNode *Ref2 = MDNode::get(Context, Args);
238 EXPECT_NE(Self, Ref1);
239 EXPECT_EQ(Ref1, Ref2);
240 }
241}
242
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000243TEST_F(MDNodeTest, Print) {
244 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
245 MDString *S = MDString::get(Context, "foo");
246 MDNode *N0 = getNode();
247 MDNode *N1 = getNode(N0);
248 MDNode *N2 = getNode(N0, N1);
249
250 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
251 MDNode *N = MDNode::get(Context, Args);
252
253 std::string Expected;
254 {
255 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000256 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000257 C->printAsOperand(OS);
258 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000259 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000260 OS << ", null";
261 MDNode *Nodes[] = {N0, N1, N2};
262 for (auto *Node : Nodes)
263 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000264 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000265 }
266
267 std::string Actual;
268 {
269 raw_string_ostream OS(Actual);
270 N->print(OS);
271 }
272
273 EXPECT_EQ(Expected, Actual);
274}
275
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000276#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
277 do { \
278 std::string Actual_; \
279 raw_string_ostream OS(Actual_); \
280 PRINT; \
281 OS.flush(); \
282 std::string Expected_(EXPECTED); \
283 EXPECT_EQ(Expected_, Actual_); \
284 } while (false)
285
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000286TEST_F(MDNodeTest, PrintTemporary) {
287 MDNode *Arg = getNode();
288 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
289 MDNode *N = getNode(Temp.get());
290 Module M("test", Context);
291 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
292 NMD->addOperand(N);
293
294 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
295 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
296 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
297
298 // Cleanup.
299 Temp->replaceAllUsesWith(Arg);
300}
301
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000302TEST_F(MDNodeTest, PrintFromModule) {
303 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
304 MDString *S = MDString::get(Context, "foo");
305 MDNode *N0 = getNode();
306 MDNode *N1 = getNode(N0);
307 MDNode *N2 = getNode(N0, N1);
308
309 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
310 MDNode *N = MDNode::get(Context, Args);
311 Module M("test", Context);
312 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
313 NMD->addOperand(N);
314
315 std::string Expected;
316 {
317 raw_string_ostream OS(Expected);
318 OS << "!0 = !{";
319 C->printAsOperand(OS);
320 OS << ", ";
321 S->printAsOperand(OS);
322 OS << ", null, !1, !2, !3}";
323 }
324
325 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
326}
327
328TEST_F(MDNodeTest, PrintFromFunction) {
329 Module M("test", Context);
330 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
331 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
332 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
333 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
334 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
335 auto *R0 = ReturnInst::Create(Context, BB0);
336 auto *R1 = ReturnInst::Create(Context, BB1);
337 auto *N0 = MDNode::getDistinct(Context, None);
338 auto *N1 = MDNode::getDistinct(Context, None);
339 R0->setMetadata("md", N0);
340 R1->setMetadata("md", N1);
341
342 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
343 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
344}
345
346TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
347 Module M("test", Context);
348
349 auto *Intrinsic =
350 Function::Create(FunctionType::get(Type::getVoidTy(Context),
351 Type::getMetadataTy(Context), false),
352 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
353
354 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
355 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
356 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
357 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
358 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
359 auto *N0 = MDNode::getDistinct(Context, None);
360 auto *N1 = MDNode::getDistinct(Context, None);
361 auto *MAV0 = MetadataAsValue::get(Context, N0);
362 auto *MAV1 = MetadataAsValue::get(Context, N1);
363 CallInst::Create(Intrinsic, MAV0, "", BB0);
364 CallInst::Create(Intrinsic, MAV1, "", BB1);
365
366 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
367 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
368 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
369 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
370 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
371 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
372}
373#undef EXPECT_PRINTER_EQ
374
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000375TEST_F(MDNodeTest, NullOperand) {
376 // metadata !{}
377 MDNode *Empty = MDNode::get(Context, None);
378
379 // metadata !{metadata !{}}
380 Metadata *Ops[] = {Empty};
381 MDNode *N = MDNode::get(Context, Ops);
382 ASSERT_EQ(Empty, N->getOperand(0));
383
384 // metadata !{metadata !{}} => metadata !{null}
385 N->replaceOperandWith(0, nullptr);
386 ASSERT_EQ(nullptr, N->getOperand(0));
387
388 // metadata !{null}
389 Ops[0] = nullptr;
390 MDNode *NullOp = MDNode::get(Context, Ops);
391 ASSERT_EQ(nullptr, NullOp->getOperand(0));
392 EXPECT_EQ(N, NullOp);
393}
394
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000395TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
396 // !{}
397 MDNode *Empty = MDNode::get(Context, None);
398 ASSERT_TRUE(Empty->isResolved());
399 EXPECT_FALSE(Empty->isDistinct());
400
401 // !{!{}}
402 Metadata *Wrapped1Ops[] = {Empty};
403 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
404 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
405 ASSERT_TRUE(Wrapped1->isResolved());
406 EXPECT_FALSE(Wrapped1->isDistinct());
407
408 // !{!{!{}}}
409 Metadata *Wrapped2Ops[] = {Wrapped1};
410 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
411 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
412 ASSERT_TRUE(Wrapped2->isResolved());
413 EXPECT_FALSE(Wrapped2->isDistinct());
414
415 // !{!{!{}}} => !{!{}}
416 Wrapped2->replaceOperandWith(0, Empty);
417 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
418 EXPECT_TRUE(Wrapped2->isDistinct());
419 EXPECT_FALSE(Wrapped1->isDistinct());
420}
421
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000422TEST_F(MDNodeTest, getDistinct) {
423 // !{}
424 MDNode *Empty = MDNode::get(Context, None);
425 ASSERT_TRUE(Empty->isResolved());
426 ASSERT_FALSE(Empty->isDistinct());
427 ASSERT_EQ(Empty, MDNode::get(Context, None));
428
429 // distinct !{}
430 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
431 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
432 EXPECT_TRUE(Distinct1->isResolved());
433 EXPECT_TRUE(Distinct2->isDistinct());
434 EXPECT_NE(Empty, Distinct1);
435 EXPECT_NE(Empty, Distinct2);
436 EXPECT_NE(Distinct1, Distinct2);
437
438 // !{}
439 ASSERT_EQ(Empty, MDNode::get(Context, None));
440}
441
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000442TEST_F(MDNodeTest, isUniqued) {
443 MDNode *U = MDTuple::get(Context, None);
444 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000445 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000446 EXPECT_TRUE(U->isUniqued());
447 EXPECT_FALSE(D->isUniqued());
448 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000449}
450
451TEST_F(MDNodeTest, isDistinct) {
452 MDNode *U = MDTuple::get(Context, None);
453 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000454 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000455 EXPECT_FALSE(U->isDistinct());
456 EXPECT_TRUE(D->isDistinct());
457 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000458}
459
460TEST_F(MDNodeTest, isTemporary) {
461 MDNode *U = MDTuple::get(Context, None);
462 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000463 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000464 EXPECT_FALSE(U->isTemporary());
465 EXPECT_FALSE(D->isTemporary());
466 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000467}
468
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000469TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
470 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000471 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000472 ASSERT_FALSE(Temp->isResolved());
473
474 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000475 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000476 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
477 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000478 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000479
480 // temporary !{} => !{}
481 MDNode *Empty = MDNode::get(Context, None);
482 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000483 EXPECT_EQ(Empty, Distinct->getOperand(0));
484}
485
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000486TEST_F(MDNodeTest, handleChangedOperandRecursion) {
487 // !0 = !{}
488 MDNode *N0 = MDNode::get(Context, None);
489
490 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000491 auto Temp3 = MDTuple::getTemporary(Context, None);
492 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000493 MDNode *N1 = MDNode::get(Context, Ops1);
494
495 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000496 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000497 MDNode *N2 = MDNode::get(Context, Ops2);
498
499 // !3 = !{!2}
500 Metadata *Ops3[] = {N2};
501 MDNode *N3 = MDNode::get(Context, Ops3);
502 Temp3->replaceAllUsesWith(N3);
503
504 // !4 = !{!1}
505 Metadata *Ops4[] = {N1};
506 MDNode *N4 = MDNode::get(Context, Ops4);
507
508 // Confirm that the cycle prevented RAUW from getting dropped.
509 EXPECT_TRUE(N0->isResolved());
510 EXPECT_FALSE(N1->isResolved());
511 EXPECT_FALSE(N2->isResolved());
512 EXPECT_FALSE(N3->isResolved());
513 EXPECT_FALSE(N4->isResolved());
514
515 // Create a couple of distinct nodes to observe what's going on.
516 //
517 // !5 = distinct !{!2}
518 // !6 = distinct !{!3}
519 Metadata *Ops5[] = {N2};
520 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
521 Metadata *Ops6[] = {N3};
522 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
523
524 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
525 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
526 // references !3, this can cause a re-entry of handleChangedOperand() when !3
527 // is not ready for it.
528 //
529 // !2->replaceOperandWith(1, nullptr)
530 // !2: !{!3, !0} => !{!3, null}
531 // !2->replaceAllUsesWith(!1)
532 // !3: !{!2] => !{!1}
533 // !3->replaceAllUsesWith(!4)
534 N2->replaceOperandWith(1, nullptr);
535
536 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
537 // under us. Just check that the other nodes are sane.
538 //
539 // !1 = !{!4, null}
540 // !4 = !{!1}
541 // !5 = distinct !{!1}
542 // !6 = distinct !{!4}
543 EXPECT_EQ(N4, N1->getOperand(0));
544 EXPECT_EQ(N1, N4->getOperand(0));
545 EXPECT_EQ(N1, N5->getOperand(0));
546 EXPECT_EQ(N4, N6->getOperand(0));
547}
548
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000549TEST_F(MDNodeTest, replaceResolvedOperand) {
550 // Check code for replacing one resolved operand with another. If doing this
551 // directly (via replaceOperandWith()) becomes illegal, change the operand to
552 // a global value that gets RAUW'ed.
553 //
554 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000555 auto Temp = MDTuple::getTemporary(Context, None);
556 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000557
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000558 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000559 MDNode *N = MDTuple::get(Context, Ops);
560 EXPECT_EQ(nullptr, N->getOperand(0));
561 ASSERT_FALSE(N->isResolved());
562
563 // Check code for replacing resolved nodes.
564 N->replaceOperandWith(0, Empty);
565 EXPECT_EQ(Empty, N->getOperand(0));
566
567 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000568 N->replaceOperandWith(0, Temp.get());
569 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000570
571 // Remove the references to Temp; required for teardown.
572 Temp->replaceAllUsesWith(nullptr);
573}
574
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000575TEST_F(MDNodeTest, replaceWithUniqued) {
576 auto *Empty = MDTuple::get(Context, None);
577 MDTuple *FirstUniqued;
578 {
579 Metadata *Ops[] = {Empty};
580 auto Temp = MDTuple::getTemporary(Context, Ops);
581 EXPECT_TRUE(Temp->isTemporary());
582
583 // Don't expect a collision.
584 auto *Current = Temp.get();
585 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
586 EXPECT_TRUE(FirstUniqued->isUniqued());
587 EXPECT_TRUE(FirstUniqued->isResolved());
588 EXPECT_EQ(Current, FirstUniqued);
589 }
590 {
591 Metadata *Ops[] = {Empty};
592 auto Temp = MDTuple::getTemporary(Context, Ops);
593 EXPECT_TRUE(Temp->isTemporary());
594
595 // Should collide with Uniqued above this time.
596 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
597 EXPECT_TRUE(Uniqued->isUniqued());
598 EXPECT_TRUE(Uniqued->isResolved());
599 EXPECT_EQ(FirstUniqued, Uniqued);
600 }
601 {
602 auto Unresolved = MDTuple::getTemporary(Context, None);
603 Metadata *Ops[] = {Unresolved.get()};
604 auto Temp = MDTuple::getTemporary(Context, Ops);
605 EXPECT_TRUE(Temp->isTemporary());
606
607 // Shouldn't be resolved.
608 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
609 EXPECT_TRUE(Uniqued->isUniqued());
610 EXPECT_FALSE(Uniqued->isResolved());
611
612 // Should be a different node.
613 EXPECT_NE(FirstUniqued, Uniqued);
614
615 // Should resolve when we update its node (note: be careful to avoid a
616 // collision with any other nodes above).
617 Uniqued->replaceOperandWith(0, nullptr);
618 EXPECT_TRUE(Uniqued->isResolved());
619 }
620}
621
622TEST_F(MDNodeTest, replaceWithDistinct) {
623 {
624 auto *Empty = MDTuple::get(Context, None);
625 Metadata *Ops[] = {Empty};
626 auto Temp = MDTuple::getTemporary(Context, Ops);
627 EXPECT_TRUE(Temp->isTemporary());
628
629 // Don't expect a collision.
630 auto *Current = Temp.get();
631 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
632 EXPECT_TRUE(Distinct->isDistinct());
633 EXPECT_TRUE(Distinct->isResolved());
634 EXPECT_EQ(Current, Distinct);
635 }
636 {
637 auto Unresolved = MDTuple::getTemporary(Context, None);
638 Metadata *Ops[] = {Unresolved.get()};
639 auto Temp = MDTuple::getTemporary(Context, Ops);
640 EXPECT_TRUE(Temp->isTemporary());
641
642 // Don't expect a collision.
643 auto *Current = Temp.get();
644 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
645 EXPECT_TRUE(Distinct->isDistinct());
646 EXPECT_TRUE(Distinct->isResolved());
647 EXPECT_EQ(Current, Distinct);
648
649 // Cleanup; required for teardown.
650 Unresolved->replaceAllUsesWith(nullptr);
651 }
652}
653
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000654TEST_F(MDNodeTest, replaceWithPermanent) {
655 Metadata *Ops[] = {nullptr};
656 auto Temp = MDTuple::getTemporary(Context, Ops);
657 auto *T = Temp.get();
658
659 // U is a normal, uniqued node that references T.
660 auto *U = MDTuple::get(Context, T);
661 EXPECT_TRUE(U->isUniqued());
662
663 // Make Temp self-referencing.
664 Temp->replaceOperandWith(0, T);
665
666 // Try to uniquify Temp. This should, despite the name in the API, give a
667 // 'distinct' node, since self-references aren't allowed to be uniqued.
668 //
669 // Since it's distinct, N should have the same address as when it was a
670 // temporary (i.e., be equal to T not U).
671 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
672 EXPECT_EQ(N, T);
673 EXPECT_TRUE(N->isDistinct());
674
675 // U should be the canonical unique node with N as the argument.
676 EXPECT_EQ(U, MDTuple::get(Context, N));
677 EXPECT_TRUE(U->isUniqued());
678
679 // This temporary should collide with U when replaced, but it should still be
680 // uniqued.
681 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
682 EXPECT_TRUE(U->isUniqued());
683
684 // This temporary should become a new uniqued node.
685 auto Temp2 = MDTuple::getTemporary(Context, U);
686 auto *V = Temp2.get();
687 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
688 EXPECT_TRUE(V->isUniqued());
689 EXPECT_EQ(U, V->getOperand(0));
690}
691
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000692TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
693 TrackingMDRef Ref;
694 EXPECT_EQ(nullptr, Ref.get());
695 {
696 auto Temp = MDTuple::getTemporary(Context, None);
697 Ref.reset(Temp.get());
698 EXPECT_EQ(Temp.get(), Ref.get());
699 }
700 EXPECT_EQ(nullptr, Ref.get());
701}
702
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000703typedef MetadataTest MDLocationTest;
704
705TEST_F(MDLocationTest, Overflow) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000706 MDSubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000707 {
708 MDLocation *L = MDLocation::get(Context, 2, 7, N);
709 EXPECT_EQ(2u, L->getLine());
710 EXPECT_EQ(7u, L->getColumn());
711 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000712 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000713 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000714 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
715 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000716 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000717 }
718 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000719 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
720 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000721 EXPECT_EQ(0u, L->getColumn());
722 }
723 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000724 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
725 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000726 EXPECT_EQ(0u, L->getColumn());
727 }
728}
729
730TEST_F(MDLocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000731 MDNode *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000732 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
733 EXPECT_TRUE(L0->isDistinct());
734 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
735 EXPECT_FALSE(L1->isDistinct());
736 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
737}
738
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000739TEST_F(MDLocationTest, getTemporary) {
740 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000741 auto L = MDLocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000742 EXPECT_TRUE(L->isTemporary());
743 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000744}
745
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000746typedef MetadataTest GenericDebugNodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000747
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000748TEST_F(GenericDebugNodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000749 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000750 auto *Empty = MDNode::get(Context, None);
751 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000752 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000753 EXPECT_EQ(15u, N->getTag());
754 EXPECT_EQ(2u, N->getNumOperands());
755 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000756 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000757 EXPECT_EQ(1u, N->getNumDwarfOperands());
758 EXPECT_EQ(Empty, N->getDwarfOperand(0));
759 EXPECT_EQ(Empty, N->getOperand(1));
760 ASSERT_TRUE(N->isUniqued());
761
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000762 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000763
764 N->replaceOperandWith(1, nullptr);
765 EXPECT_EQ(15u, N->getTag());
766 EXPECT_EQ(Header, N->getHeader());
767 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
768 ASSERT_TRUE(N->isUniqued());
769
770 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000771 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000772
773 N->replaceDwarfOperandWith(0, Empty);
774 EXPECT_EQ(15u, N->getTag());
775 EXPECT_EQ(Header, N->getHeader());
776 EXPECT_EQ(Empty, N->getDwarfOperand(0));
777 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000778 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000779
780 TempGenericDebugNode Temp = N->clone();
781 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000782}
783
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000784TEST_F(GenericDebugNodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000785 // Canonicalize !"" to null.
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000786 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
787 EXPECT_EQ(StringRef(), N->getHeader());
788 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000789}
790
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000791typedef MetadataTest MDSubrangeTest;
792
793TEST_F(MDSubrangeTest, get) {
794 auto *N = MDSubrange::get(Context, 5, 7);
795 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
796 EXPECT_EQ(5, N->getCount());
797 EXPECT_EQ(7, N->getLo());
798 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
799 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000800
801 TempMDSubrange Temp = N->clone();
802 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000803}
804
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000805TEST_F(MDSubrangeTest, getEmptyArray) {
806 auto *N = MDSubrange::get(Context, -1, 0);
807 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
808 EXPECT_EQ(-1, N->getCount());
809 EXPECT_EQ(0, N->getLo());
810 EXPECT_EQ(N, MDSubrange::get(Context, -1, 0));
811}
812
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000813typedef MetadataTest MDEnumeratorTest;
814
815TEST_F(MDEnumeratorTest, get) {
816 auto *N = MDEnumerator::get(Context, 7, "name");
817 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
818 EXPECT_EQ(7, N->getValue());
819 EXPECT_EQ("name", N->getName());
820 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
821
822 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
823 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000824
825 TempMDEnumerator Temp = N->clone();
826 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000827}
828
829typedef MetadataTest MDBasicTypeTest;
830
831TEST_F(MDBasicTypeTest, get) {
832 auto *N =
833 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
834 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
835 EXPECT_EQ("special", N->getName());
836 EXPECT_EQ(33u, N->getSizeInBits());
837 EXPECT_EQ(26u, N->getAlignInBits());
838 EXPECT_EQ(7u, N->getEncoding());
839 EXPECT_EQ(0u, N->getLine());
840 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
841 26, 7));
842
843 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
844 "special", 33, 26, 7));
845 EXPECT_NE(N,
846 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
847 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
848 26, 7));
849 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
850 25, 7));
851 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
852 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000853
854 TempMDBasicType Temp = N->clone();
855 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000856}
857
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000858TEST_F(MDBasicTypeTest, getWithLargeValues) {
859 auto *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
860 UINT64_MAX, UINT64_MAX - 1, 7);
861 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
862 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
863}
864
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000865TEST_F(MDBasicTypeTest, getUnspecified) {
866 auto *N =
867 MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
868 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
869 EXPECT_EQ("unspecified", N->getName());
870 EXPECT_EQ(0u, N->getSizeInBits());
871 EXPECT_EQ(0u, N->getAlignInBits());
872 EXPECT_EQ(0u, N->getEncoding());
873 EXPECT_EQ(0u, N->getLine());
874}
875
876typedef MetadataTest MDTypeTest;
877
878TEST_F(MDTypeTest, clone) {
879 // Check that MDType has a specialized clone that returns TempMDType.
880 MDType *N = MDBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
881 dwarf::DW_ATE_signed);
882
883 TempMDType Temp = N->clone();
884 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
885}
886
887TEST_F(MDTypeTest, setFlags) {
888 // void (void)
889 Metadata *TypesOps[] = {nullptr};
890 Metadata *Types = MDTuple::get(Context, TypesOps);
891
892 MDType *D = MDSubroutineType::getDistinct(Context, 0u, Types);
893 EXPECT_EQ(0u, D->getFlags());
894 D->setFlags(DIDescriptor::FlagRValueReference);
895 EXPECT_EQ(DIDescriptor::FlagRValueReference, D->getFlags());
896 D->setFlags(0u);
897 EXPECT_EQ(0u, D->getFlags());
898
899 TempMDType T = MDSubroutineType::getTemporary(Context, 0u, Types);
900 EXPECT_EQ(0u, T->getFlags());
901 T->setFlags(DIDescriptor::FlagRValueReference);
902 EXPECT_EQ(DIDescriptor::FlagRValueReference, T->getFlags());
903 T->setFlags(0u);
904 EXPECT_EQ(0u, T->getFlags());
905}
906
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000907typedef MetadataTest MDDerivedTypeTest;
908
909TEST_F(MDDerivedTypeTest, get) {
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000910 MDFile *File = getFile();
911 MDScope *Scope = getSubprogram();
912 MDType *BaseType = getBasicType("basic");
913 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000914
915 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
916 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
917 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
918 EXPECT_EQ("something", N->getName());
919 EXPECT_EQ(File, N->getFile());
920 EXPECT_EQ(1u, N->getLine());
921 EXPECT_EQ(Scope, N->getScope());
922 EXPECT_EQ(BaseType, N->getBaseType());
923 EXPECT_EQ(2u, N->getSizeInBits());
924 EXPECT_EQ(3u, N->getAlignInBits());
925 EXPECT_EQ(4u, N->getOffsetInBits());
926 EXPECT_EQ(5u, N->getFlags());
927 EXPECT_EQ(ExtraData, N->getExtraData());
928 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
929 "something", File, 1, Scope, BaseType, 2, 3,
930 4, 5, ExtraData));
931
932 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
933 "something", File, 1, Scope, BaseType, 2, 3,
934 4, 5, ExtraData));
935 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
936 File, 1, Scope, BaseType, 2, 3, 4, 5,
937 ExtraData));
938 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000939 "something", getFile(), 1, Scope, BaseType, 2,
940 3, 4, 5, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000941 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
942 "something", File, 2, Scope, BaseType, 2, 3,
943 4, 5, ExtraData));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000944 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
945 "something", File, 1, getSubprogram(),
946 BaseType, 2, 3, 4, 5, ExtraData));
947 EXPECT_NE(N, MDDerivedType::get(
948 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
949 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000950 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
951 "something", File, 1, Scope, BaseType, 3, 3,
952 4, 5, ExtraData));
953 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
954 "something", File, 1, Scope, BaseType, 2, 2,
955 4, 5, ExtraData));
956 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
957 "something", File, 1, Scope, BaseType, 2, 3,
958 5, 5, ExtraData));
959 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
960 "something", File, 1, Scope, BaseType, 2, 3,
961 4, 4, ExtraData));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000962 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
963 "something", File, 1, Scope, BaseType, 2, 3,
964 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000965
966 TempMDDerivedType Temp = N->clone();
967 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000968}
969
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000970TEST_F(MDDerivedTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000971 MDFile *File = getFile();
972 MDScope *Scope = getSubprogram();
973 MDType *BaseType = getBasicType("basic");
974 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000975
976 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
977 File, 1, Scope, BaseType, UINT64_MAX,
978 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
979 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
980 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
981 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
982}
983
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000984typedef MetadataTest MDCompositeTypeTest;
985
986TEST_F(MDCompositeTypeTest, get) {
987 unsigned Tag = dwarf::DW_TAG_structure_type;
988 StringRef Name = "some name";
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000989 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000990 unsigned Line = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000991 MDScope *Scope = getSubprogram();
992 MDType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000993 uint64_t SizeInBits = 2;
994 uint64_t AlignInBits = 3;
995 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000996 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000997 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000998 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000999 MDType *VTableHolder = getCompositeType();
1000 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001001 StringRef Identifier = "some id";
1002
1003 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1004 BaseType, SizeInBits, AlignInBits,
1005 OffsetInBits, Flags, Elements, RuntimeLang,
1006 VTableHolder, TemplateParams, Identifier);
1007 EXPECT_EQ(Tag, N->getTag());
1008 EXPECT_EQ(Name, N->getName());
1009 EXPECT_EQ(File, N->getFile());
1010 EXPECT_EQ(Line, N->getLine());
1011 EXPECT_EQ(Scope, N->getScope());
1012 EXPECT_EQ(BaseType, N->getBaseType());
1013 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1014 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1015 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1016 EXPECT_EQ(Flags, N->getFlags());
1017 EXPECT_EQ(Elements, N->getElements());
1018 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1019 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1020 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1021 EXPECT_EQ(Identifier, N->getIdentifier());
1022
1023 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1024 BaseType, SizeInBits, AlignInBits,
1025 OffsetInBits, Flags, Elements, RuntimeLang,
1026 VTableHolder, TemplateParams, Identifier));
1027
1028 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1029 BaseType, SizeInBits, AlignInBits,
1030 OffsetInBits, Flags, Elements, RuntimeLang,
1031 VTableHolder, TemplateParams, Identifier));
1032 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
1033 BaseType, SizeInBits, AlignInBits,
1034 OffsetInBits, Flags, Elements, RuntimeLang,
1035 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001036 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001037 BaseType, SizeInBits, AlignInBits,
1038 OffsetInBits, Flags, Elements, RuntimeLang,
1039 VTableHolder, TemplateParams, Identifier));
1040 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1041 BaseType, SizeInBits, AlignInBits,
1042 OffsetInBits, Flags, Elements, RuntimeLang,
1043 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001044 EXPECT_NE(N, MDCompositeType::get(
1045 Context, Tag, Name, File, Line, getSubprogram(), BaseType,
1046 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1047 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001048 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
1049 SizeInBits, AlignInBits, OffsetInBits,
1050 Flags, Elements, RuntimeLang, VTableHolder,
1051 TemplateParams, Identifier));
1052 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1053 BaseType, SizeInBits + 1, AlignInBits,
1054 OffsetInBits, Flags, Elements, RuntimeLang,
1055 VTableHolder, TemplateParams, Identifier));
1056 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1057 BaseType, SizeInBits, AlignInBits + 1,
1058 OffsetInBits, Flags, Elements, RuntimeLang,
1059 VTableHolder, TemplateParams, Identifier));
1060 EXPECT_NE(N, MDCompositeType::get(
1061 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1062 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1063 VTableHolder, TemplateParams, Identifier));
1064 EXPECT_NE(N, MDCompositeType::get(
1065 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1066 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1067 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001068 EXPECT_NE(N, MDCompositeType::get(
1069 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1070 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1071 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001072 EXPECT_NE(N, MDCompositeType::get(
1073 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1074 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1075 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001076 EXPECT_NE(N, MDCompositeType::get(
1077 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1078 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1079 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001080 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1081 BaseType, SizeInBits, AlignInBits,
1082 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001083 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001084 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1085 BaseType, SizeInBits, AlignInBits,
1086 OffsetInBits, Flags, Elements, RuntimeLang,
1087 VTableHolder, TemplateParams, "other"));
1088
1089 // Be sure that missing identifiers get null pointers.
1090 EXPECT_FALSE(MDCompositeType::get(
1091 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1092 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1093 VTableHolder, TemplateParams, "")->getRawIdentifier());
1094 EXPECT_FALSE(MDCompositeType::get(
1095 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1096 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1097 VTableHolder, TemplateParams)->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001098
1099 TempMDCompositeType Temp = N->clone();
1100 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001101}
1102
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001103TEST_F(MDCompositeTypeTest, getWithLargeValues) {
1104 unsigned Tag = dwarf::DW_TAG_structure_type;
1105 StringRef Name = "some name";
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001106 MDFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001107 unsigned Line = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001108 MDScope *Scope = getSubprogram();
1109 MDType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001110 uint64_t SizeInBits = UINT64_MAX;
1111 uint64_t AlignInBits = UINT64_MAX - 1;
1112 uint64_t OffsetInBits = UINT64_MAX - 2;
1113 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001114 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001115 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001116 MDType *VTableHolder = getCompositeType();
1117 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001118 StringRef Identifier = "some id";
1119
1120 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1121 BaseType, SizeInBits, AlignInBits,
1122 OffsetInBits, Flags, Elements, RuntimeLang,
1123 VTableHolder, TemplateParams, Identifier);
1124 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1125 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1126 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1127}
1128
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001129TEST_F(MDCompositeTypeTest, replaceOperands) {
1130 unsigned Tag = dwarf::DW_TAG_structure_type;
1131 StringRef Name = "some name";
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001132 MDFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001133 unsigned Line = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001134 MDScope *Scope = getSubprogram();
1135 MDType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001136 uint64_t SizeInBits = 2;
1137 uint64_t AlignInBits = 3;
1138 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001139 unsigned Flags = 5;
1140 unsigned RuntimeLang = 6;
1141 StringRef Identifier = "some id";
1142
1143 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
1144 BaseType, SizeInBits, AlignInBits,
1145 OffsetInBits, Flags, nullptr, RuntimeLang,
1146 nullptr, nullptr, Identifier);
1147
1148 auto *Elements = MDTuple::getDistinct(Context, None);
1149 EXPECT_EQ(nullptr, N->getElements());
1150 N->replaceElements(Elements);
1151 EXPECT_EQ(Elements, N->getElements());
1152 N->replaceElements(nullptr);
1153 EXPECT_EQ(nullptr, N->getElements());
1154
1155 auto *VTableHolder = MDTuple::getDistinct(Context, None);
1156 EXPECT_EQ(nullptr, N->getVTableHolder());
1157 N->replaceVTableHolder(VTableHolder);
1158 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1159 N->replaceVTableHolder(nullptr);
1160 EXPECT_EQ(nullptr, N->getVTableHolder());
1161
1162 auto *TemplateParams = MDTuple::getDistinct(Context, None);
1163 EXPECT_EQ(nullptr, N->getTemplateParams());
1164 N->replaceTemplateParams(TemplateParams);
1165 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1166 N->replaceTemplateParams(nullptr);
1167 EXPECT_EQ(nullptr, N->getTemplateParams());
1168}
1169
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001170typedef MetadataTest MDSubroutineTypeTest;
1171
1172TEST_F(MDSubroutineTypeTest, get) {
1173 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001174 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001175
1176 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
1177 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1178 EXPECT_EQ(Flags, N->getFlags());
1179 EXPECT_EQ(TypeArray, N->getTypeArray());
1180 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
1181
1182 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001183 EXPECT_NE(N, MDSubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001184
1185 TempMDSubroutineType Temp = N->clone();
1186 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001187
1188 // Test always-empty operands.
1189 EXPECT_EQ(nullptr, N->getScope());
1190 EXPECT_EQ(nullptr, N->getFile());
1191 EXPECT_EQ("", N->getName());
1192 EXPECT_EQ(nullptr, N->getBaseType());
1193 EXPECT_EQ(nullptr, N->getVTableHolder());
1194 EXPECT_EQ(nullptr, N->getTemplateParams());
1195 EXPECT_EQ("", N->getIdentifier());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001196}
1197
1198typedef MetadataTest MDFileTest;
1199
1200TEST_F(MDFileTest, get) {
1201 StringRef Filename = "file";
1202 StringRef Directory = "dir";
1203 auto *N = MDFile::get(Context, Filename, Directory);
1204
1205 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1206 EXPECT_EQ(Filename, N->getFilename());
1207 EXPECT_EQ(Directory, N->getDirectory());
1208 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
1209
1210 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
1211 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001212
1213 TempMDFile Temp = N->clone();
1214 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001215}
1216
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001217TEST_F(MDFileTest, ScopeGetFile) {
1218 // Ensure that MDScope::getFile() returns itself.
1219 MDScope *N = MDFile::get(Context, "file", "dir");
1220 EXPECT_EQ(N, N->getFile());
1221}
1222
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001223typedef MetadataTest MDCompileUnitTest;
1224
1225TEST_F(MDCompileUnitTest, get) {
1226 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001227 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001228 StringRef Producer = "some producer";
1229 bool IsOptimized = false;
1230 StringRef Flags = "flag after flag";
1231 unsigned RuntimeVersion = 2;
1232 StringRef SplitDebugFilename = "another/file";
1233 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001234 MDTuple *EnumTypes = getTuple();
1235 MDTuple *RetainedTypes = getTuple();
1236 MDTuple *Subprograms = getTuple();
1237 MDTuple *GlobalVariables = getTuple();
1238 MDTuple *ImportedEntities = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001239 auto *N = MDCompileUnit::get(
1240 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1241 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1242 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1243
1244 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1245 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1246 EXPECT_EQ(File, N->getFile());
1247 EXPECT_EQ(Producer, N->getProducer());
1248 EXPECT_EQ(IsOptimized, N->isOptimized());
1249 EXPECT_EQ(Flags, N->getFlags());
1250 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1251 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1252 EXPECT_EQ(EmissionKind, N->getEmissionKind());
1253 EXPECT_EQ(EnumTypes, N->getEnumTypes());
1254 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
1255 EXPECT_EQ(Subprograms, N->getSubprograms());
1256 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1257 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
1258 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1259 IsOptimized, Flags, RuntimeVersion,
1260 SplitDebugFilename, EmissionKind, EnumTypes,
1261 RetainedTypes, Subprograms, GlobalVariables,
1262 ImportedEntities));
1263
1264 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
1265 IsOptimized, Flags, RuntimeVersion,
1266 SplitDebugFilename, EmissionKind, EnumTypes,
1267 RetainedTypes, Subprograms, GlobalVariables,
1268 ImportedEntities));
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001269 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, getFile(), Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001270 IsOptimized, Flags, RuntimeVersion,
1271 SplitDebugFilename, EmissionKind, EnumTypes,
1272 RetainedTypes, Subprograms, GlobalVariables,
1273 ImportedEntities));
1274 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
1275 IsOptimized, Flags, RuntimeVersion,
1276 SplitDebugFilename, EmissionKind, EnumTypes,
1277 RetainedTypes, Subprograms, GlobalVariables,
1278 ImportedEntities));
1279 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1280 !IsOptimized, Flags, RuntimeVersion,
1281 SplitDebugFilename, EmissionKind, EnumTypes,
1282 RetainedTypes, Subprograms, GlobalVariables,
1283 ImportedEntities));
1284 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1285 IsOptimized, "other", RuntimeVersion,
1286 SplitDebugFilename, EmissionKind, EnumTypes,
1287 RetainedTypes, Subprograms, GlobalVariables,
1288 ImportedEntities));
1289 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1290 IsOptimized, Flags, RuntimeVersion + 1,
1291 SplitDebugFilename, EmissionKind, EnumTypes,
1292 RetainedTypes, Subprograms, GlobalVariables,
1293 ImportedEntities));
1294 EXPECT_NE(N,
1295 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1296 IsOptimized, Flags, RuntimeVersion, "other",
1297 EmissionKind, EnumTypes, RetainedTypes,
1298 Subprograms, GlobalVariables, ImportedEntities));
1299 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1300 IsOptimized, Flags, RuntimeVersion,
1301 SplitDebugFilename, EmissionKind + 1,
1302 EnumTypes, RetainedTypes, Subprograms,
1303 GlobalVariables, ImportedEntities));
1304 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1305 IsOptimized, Flags, RuntimeVersion,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001306 SplitDebugFilename, EmissionKind, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001307 RetainedTypes, Subprograms, GlobalVariables,
1308 ImportedEntities));
1309 EXPECT_NE(N, MDCompileUnit::get(
1310 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1311 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001312 getTuple(), Subprograms, GlobalVariables, ImportedEntities));
1313 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
1314 IsOptimized, Flags, RuntimeVersion,
1315 SplitDebugFilename, EmissionKind, EnumTypes,
1316 RetainedTypes, getTuple(), GlobalVariables,
1317 ImportedEntities));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001318 EXPECT_NE(N, MDCompileUnit::get(
1319 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1320 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001321 RetainedTypes, Subprograms, getTuple(), ImportedEntities));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001322 EXPECT_NE(N, MDCompileUnit::get(
1323 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1324 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001325 RetainedTypes, Subprograms, GlobalVariables, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001326
1327 TempMDCompileUnit Temp = N->clone();
1328 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001329}
1330
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001331TEST_F(MDCompileUnitTest, replaceArrays) {
1332 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001333 MDFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001334 StringRef Producer = "some producer";
1335 bool IsOptimized = false;
1336 StringRef Flags = "flag after flag";
1337 unsigned RuntimeVersion = 2;
1338 StringRef SplitDebugFilename = "another/file";
1339 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001340 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1341 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1342 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001343 auto *N = MDCompileUnit::get(
1344 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1345 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1346 RetainedTypes, nullptr, nullptr, ImportedEntities);
1347
1348 auto *Subprograms = MDTuple::getDistinct(Context, None);
1349 EXPECT_EQ(nullptr, N->getSubprograms());
1350 N->replaceSubprograms(Subprograms);
1351 EXPECT_EQ(Subprograms, N->getSubprograms());
1352 N->replaceSubprograms(nullptr);
1353 EXPECT_EQ(nullptr, N->getSubprograms());
1354
1355 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1356 EXPECT_EQ(nullptr, N->getGlobalVariables());
1357 N->replaceGlobalVariables(GlobalVariables);
1358 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
1359 N->replaceGlobalVariables(nullptr);
1360 EXPECT_EQ(nullptr, N->getGlobalVariables());
1361}
1362
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001363typedef MetadataTest MDSubprogramTest;
1364
1365TEST_F(MDSubprogramTest, get) {
1366 Metadata *Scope = MDTuple::getDistinct(Context, None);
1367 StringRef Name = "name";
1368 StringRef LinkageName = "linkage";
1369 Metadata *File = MDTuple::getDistinct(Context, None);
1370 unsigned Line = 2;
1371 Metadata *Type = MDTuple::getDistinct(Context, None);
1372 bool IsLocalToUnit = false;
1373 bool IsDefinition = true;
1374 unsigned ScopeLine = 3;
1375 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1376 unsigned Virtuality = 4;
1377 unsigned VirtualIndex = 5;
1378 unsigned Flags = 6;
1379 bool IsOptimized = false;
1380 Metadata *Function = MDTuple::getDistinct(Context, None);
1381 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1382 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1383 Metadata *Variables = MDTuple::getDistinct(Context, None);
1384
1385 auto *N = MDSubprogram::get(
1386 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1387 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1388 IsOptimized, Function, TemplateParams, Declaration, Variables);
1389
1390 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1391 EXPECT_EQ(Scope, N->getScope());
1392 EXPECT_EQ(Name, N->getName());
1393 EXPECT_EQ(LinkageName, N->getLinkageName());
1394 EXPECT_EQ(File, N->getFile());
1395 EXPECT_EQ(Line, N->getLine());
1396 EXPECT_EQ(Type, N->getType());
1397 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1398 EXPECT_EQ(IsDefinition, N->isDefinition());
1399 EXPECT_EQ(ScopeLine, N->getScopeLine());
1400 EXPECT_EQ(ContainingType, N->getContainingType());
1401 EXPECT_EQ(Virtuality, N->getVirtuality());
1402 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1403 EXPECT_EQ(Flags, N->getFlags());
1404 EXPECT_EQ(IsOptimized, N->isOptimized());
1405 EXPECT_EQ(Function, N->getFunction());
1406 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1407 EXPECT_EQ(Declaration, N->getDeclaration());
1408 EXPECT_EQ(Variables, N->getVariables());
1409 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1410 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1411 ContainingType, Virtuality, VirtualIndex,
1412 Flags, IsOptimized, Function, TemplateParams,
1413 Declaration, Variables));
1414
1415 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1416 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1417 ContainingType, Virtuality, VirtualIndex,
1418 Flags, IsOptimized, Function, TemplateParams,
1419 Declaration, Variables));
1420 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1421 Line, Type, IsLocalToUnit, IsDefinition,
1422 ScopeLine, ContainingType, Virtuality,
1423 VirtualIndex, Flags, IsOptimized, Function,
1424 TemplateParams, Declaration, Variables));
1425 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1426 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1427 ContainingType, Virtuality, VirtualIndex,
1428 Flags, IsOptimized, Function, TemplateParams,
1429 Declaration, Variables));
1430 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1431 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1432 ContainingType, Virtuality, VirtualIndex,
1433 Flags, IsOptimized, Function, TemplateParams,
1434 Declaration, Variables));
1435 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1436 Line + 1, Type, IsLocalToUnit, IsDefinition,
1437 ScopeLine, ContainingType, Virtuality,
1438 VirtualIndex, Flags, IsOptimized, Function,
1439 TemplateParams, Declaration, Variables));
1440 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1441 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1442 ContainingType, Virtuality, VirtualIndex,
1443 Flags, IsOptimized, Function, TemplateParams,
1444 Declaration, Variables));
1445 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1446 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1447 ContainingType, Virtuality, VirtualIndex,
1448 Flags, IsOptimized, Function, TemplateParams,
1449 Declaration, Variables));
1450 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1451 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1452 ContainingType, Virtuality, VirtualIndex,
1453 Flags, IsOptimized, Function, TemplateParams,
1454 Declaration, Variables));
1455 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1456 Type, IsLocalToUnit, IsDefinition,
1457 ScopeLine + 1, ContainingType, Virtuality,
1458 VirtualIndex, Flags, IsOptimized, Function,
1459 TemplateParams, Declaration, Variables));
1460 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1461 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1462 Type, Virtuality, VirtualIndex, Flags,
1463 IsOptimized, Function, TemplateParams,
1464 Declaration, Variables));
1465 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1466 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1467 ContainingType, Virtuality + 1, VirtualIndex,
1468 Flags, IsOptimized, Function, TemplateParams,
1469 Declaration, Variables));
1470 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1471 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1472 ContainingType, Virtuality, VirtualIndex + 1,
1473 Flags, IsOptimized, Function, TemplateParams,
1474 Declaration, Variables));
1475 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1476 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1477 ContainingType, Virtuality, VirtualIndex,
1478 ~Flags, IsOptimized, Function, TemplateParams,
1479 Declaration, Variables));
1480 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1481 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1482 ContainingType, Virtuality, VirtualIndex,
1483 Flags, !IsOptimized, Function, TemplateParams,
1484 Declaration, Variables));
1485 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1486 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1487 ContainingType, Virtuality, VirtualIndex,
1488 Flags, IsOptimized, Type, TemplateParams,
1489 Declaration, Variables));
1490 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1491 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1492 ContainingType, Virtuality, VirtualIndex,
1493 Flags, IsOptimized, Function, Type,
1494 Declaration, Variables));
1495 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1496 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1497 ContainingType, Virtuality, VirtualIndex,
1498 Flags, IsOptimized, Function, TemplateParams,
1499 Type, Variables));
1500 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1501 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1502 ContainingType, Virtuality, VirtualIndex,
1503 Flags, IsOptimized, Function, TemplateParams,
1504 Declaration, Type));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001505
1506 TempMDSubprogram Temp = N->clone();
1507 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001508}
1509
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001510TEST_F(MDSubprogramTest, replaceFunction) {
1511 Metadata *Scope = MDTuple::getDistinct(Context, None);
1512 StringRef Name = "name";
1513 StringRef LinkageName = "linkage";
1514 Metadata *File = MDTuple::getDistinct(Context, None);
1515 unsigned Line = 2;
1516 Metadata *Type = MDTuple::getDistinct(Context, None);
1517 bool IsLocalToUnit = false;
1518 bool IsDefinition = true;
1519 unsigned ScopeLine = 3;
1520 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1521 unsigned Virtuality = 4;
1522 unsigned VirtualIndex = 5;
1523 unsigned Flags = 6;
1524 bool IsOptimized = false;
1525 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1526 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1527 Metadata *Variables = MDTuple::getDistinct(Context, None);
1528
1529 auto *N = MDSubprogram::get(
1530 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1531 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1532 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1533
1534 EXPECT_EQ(nullptr, N->getFunction());
1535
1536 std::unique_ptr<Function> F(
1537 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1538 GlobalValue::ExternalLinkage));
1539 N->replaceFunction(F.get());
1540 EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction());
1541
1542 N->replaceFunction(nullptr);
1543 EXPECT_EQ(nullptr, N->getFunction());
1544}
1545
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001546typedef MetadataTest MDLexicalBlockTest;
1547
1548TEST_F(MDLexicalBlockTest, get) {
1549 Metadata *Scope = MDTuple::getDistinct(Context, None);
1550 Metadata *File = MDTuple::getDistinct(Context, None);
1551 unsigned Line = 5;
1552 unsigned Column = 8;
1553
1554 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1555
1556 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1557 EXPECT_EQ(Scope, N->getScope());
1558 EXPECT_EQ(File, N->getFile());
1559 EXPECT_EQ(Line, N->getLine());
1560 EXPECT_EQ(Column, N->getColumn());
1561 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1562
1563 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1564 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1565 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1566 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001567
1568 TempMDLexicalBlock Temp = N->clone();
1569 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001570}
1571
1572typedef MetadataTest MDLexicalBlockFileTest;
1573
1574TEST_F(MDLexicalBlockFileTest, get) {
1575 Metadata *Scope = MDTuple::getDistinct(Context, None);
1576 Metadata *File = MDTuple::getDistinct(Context, None);
1577 unsigned Discriminator = 5;
1578
1579 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1580
1581 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1582 EXPECT_EQ(Scope, N->getScope());
1583 EXPECT_EQ(File, N->getFile());
1584 EXPECT_EQ(Discriminator, N->getDiscriminator());
1585 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1586
1587 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1588 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1589 EXPECT_NE(N,
1590 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001591
1592 TempMDLexicalBlockFile 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 MDNamespaceTest;
1597
1598TEST_F(MDNamespaceTest, get) {
1599 Metadata *Scope = MDTuple::getDistinct(Context, None);
1600 Metadata *File = MDTuple::getDistinct(Context, None);
1601 StringRef Name = "namespace";
1602 unsigned Line = 5;
1603
1604 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1605
1606 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1607 EXPECT_EQ(Scope, N->getScope());
1608 EXPECT_EQ(File, N->getFile());
1609 EXPECT_EQ(Name, N->getName());
1610 EXPECT_EQ(Line, N->getLine());
1611 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1612
1613 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1614 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1615 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1616 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001617
1618 TempMDNamespace Temp = N->clone();
1619 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001620}
1621
1622typedef MetadataTest MDTemplateTypeParameterTest;
1623
1624TEST_F(MDTemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001625 StringRef Name = "template";
1626 Metadata *Type = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001627 Metadata *Other = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001628
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001629 auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001630
1631 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001632 EXPECT_EQ(Name, N->getName());
1633 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001634 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001635
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001636 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, "other", Type));
1637 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Name, Other));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001638
1639 TempMDTemplateTypeParameter Temp = N->clone();
1640 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001641}
1642
1643typedef MetadataTest MDTemplateValueParameterTest;
1644
1645TEST_F(MDTemplateValueParameterTest, get) {
1646 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001647 StringRef Name = "template";
1648 Metadata *Type = MDTuple::getDistinct(Context, None);
1649 Metadata *Value = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001650 Metadata *Other = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001651
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001652 auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001653 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001654 EXPECT_EQ(Name, N->getName());
1655 EXPECT_EQ(Type, N->getType());
1656 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001657 EXPECT_EQ(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001658
1659 EXPECT_NE(N, MDTemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001660 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1661 Type, Value));
1662 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, "other", Type,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001663 Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001664 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Other,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001665 Value));
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001666 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Name, Type, Other));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001667
1668 TempMDTemplateValueParameter Temp = N->clone();
1669 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001670}
1671
1672typedef MetadataTest MDGlobalVariableTest;
1673
1674TEST_F(MDGlobalVariableTest, get) {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001675 MDScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001676 StringRef Name = "name";
1677 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001678 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001679 unsigned Line = 5;
1680 Metadata *Type = MDTuple::getDistinct(Context, None);
1681 bool IsLocalToUnit = false;
1682 bool IsDefinition = true;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001683 ConstantAsMetadata *Variable = getConstantAsMetadata();
1684 MDDerivedType *StaticDataMemberDeclaration = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001685
1686 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1687 Type, IsLocalToUnit, IsDefinition, Variable,
1688 StaticDataMemberDeclaration);
1689 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1690 EXPECT_EQ(Scope, N->getScope());
1691 EXPECT_EQ(Name, N->getName());
1692 EXPECT_EQ(LinkageName, N->getLinkageName());
1693 EXPECT_EQ(File, N->getFile());
1694 EXPECT_EQ(Line, N->getLine());
1695 EXPECT_EQ(Type, N->getType());
1696 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1697 EXPECT_EQ(IsDefinition, N->isDefinition());
1698 EXPECT_EQ(Variable, N->getVariable());
1699 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1700 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1701 Line, Type, IsLocalToUnit, IsDefinition,
1702 Variable, StaticDataMemberDeclaration));
1703
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001704 EXPECT_NE(N,
1705 MDGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
1706 File, Line, Type, IsLocalToUnit, IsDefinition,
1707 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001708 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1709 Line, Type, IsLocalToUnit, IsDefinition,
1710 Variable, StaticDataMemberDeclaration));
1711 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1712 Type, IsLocalToUnit, IsDefinition,
1713 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001714 EXPECT_NE(N,
1715 MDGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1716 Line, Type, IsLocalToUnit, IsDefinition,
1717 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001718 EXPECT_NE(N,
1719 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1720 Line + 1, Type, IsLocalToUnit, IsDefinition,
1721 Variable, StaticDataMemberDeclaration));
1722 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1723 Line, Scope, IsLocalToUnit, IsDefinition,
1724 Variable, StaticDataMemberDeclaration));
1725 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1726 Line, Type, !IsLocalToUnit, IsDefinition,
1727 Variable, StaticDataMemberDeclaration));
1728 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1729 Line, Type, IsLocalToUnit, !IsDefinition,
1730 Variable, StaticDataMemberDeclaration));
1731 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1732 Line, Type, IsLocalToUnit, IsDefinition,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001733 getConstantAsMetadata(),
1734 StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001735 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1736 Line, Type, IsLocalToUnit, IsDefinition,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001737 Variable, getDerivedType()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001738
1739 TempMDGlobalVariable Temp = N->clone();
1740 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001741}
1742
1743typedef MetadataTest MDLocalVariableTest;
1744
1745TEST_F(MDLocalVariableTest, get) {
1746 unsigned Tag = dwarf::DW_TAG_arg_variable;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001747 MDLocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001748 StringRef Name = "name";
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001749 MDFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001750 unsigned Line = 5;
1751 Metadata *Type = MDTuple::getDistinct(Context, None);
1752 unsigned Arg = 6;
1753 unsigned Flags = 7;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001754 MDLocation *InlinedAt =
1755 MDLocation::getDistinct(Context, 10, 20, getSubprogram());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001756
1757 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1758 Arg, Flags, InlinedAt);
1759 EXPECT_EQ(Tag, N->getTag());
1760 EXPECT_EQ(Scope, N->getScope());
1761 EXPECT_EQ(Name, N->getName());
1762 EXPECT_EQ(File, N->getFile());
1763 EXPECT_EQ(Line, N->getLine());
1764 EXPECT_EQ(Type, N->getType());
1765 EXPECT_EQ(Arg, N->getArg());
1766 EXPECT_EQ(Flags, N->getFlags());
1767 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1768 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1769 Arg, Flags, InlinedAt));
1770
1771 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1772 Name, File, Line, Type, Arg, Flags,
1773 InlinedAt));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001774 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, getSubprogram(), Name, File,
1775 Line, Type, Arg, Flags, InlinedAt));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001776 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1777 Type, Arg, Flags, InlinedAt));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001778 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001779 Type, Arg, Flags, InlinedAt));
1780 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1781 Type, Arg, Flags, InlinedAt));
1782 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1783 Scope, Arg, Flags, InlinedAt));
1784 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1785 Arg + 1, Flags, InlinedAt));
1786 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1787 Arg, ~Flags, InlinedAt));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001788 EXPECT_NE(N, MDLocalVariable::get(
1789 Context, Tag, Scope, Name, File, Line, Type, Arg, Flags,
1790 MDLocation::getDistinct(Context, 10, 20, getSubprogram())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001791
1792 TempMDLocalVariable Temp = N->clone();
1793 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001794
1795 auto *Inlined = N->withoutInline();
1796 EXPECT_NE(N, Inlined);
1797 EXPECT_EQ(N->getTag(), Inlined->getTag());
1798 EXPECT_EQ(N->getScope(), Inlined->getScope());
1799 EXPECT_EQ(N->getName(), Inlined->getName());
1800 EXPECT_EQ(N->getFile(), Inlined->getFile());
1801 EXPECT_EQ(N->getLine(), Inlined->getLine());
1802 EXPECT_EQ(N->getType(), Inlined->getType());
1803 EXPECT_EQ(N->getArg(), Inlined->getArg());
1804 EXPECT_EQ(N->getFlags(), Inlined->getFlags());
1805 EXPECT_EQ(nullptr, Inlined->getInlinedAt());
1806 EXPECT_EQ(N, Inlined->withInline(cast<MDLocation>(InlinedAt)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001807}
1808
1809typedef MetadataTest MDExpressionTest;
1810
1811TEST_F(MDExpressionTest, get) {
1812 uint64_t Elements[] = {2, 6, 9, 78, 0};
1813 auto *N = MDExpression::get(Context, Elements);
1814 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1815 EXPECT_EQ(N, MDExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001816
1817 EXPECT_EQ(5u, N->getNumElements());
1818 EXPECT_EQ(2u, N->getElement(0));
1819 EXPECT_EQ(6u, N->getElement(1));
1820 EXPECT_EQ(9u, N->getElement(2));
1821 EXPECT_EQ(78u, N->getElement(3));
1822 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001823
1824 TempMDExpression Temp = N->clone();
1825 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001826}
1827
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001828TEST_F(MDExpressionTest, isValid) {
1829#define EXPECT_VALID(...) \
1830 do { \
1831 uint64_t Elements[] = {__VA_ARGS__}; \
1832 EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \
1833 } while (false)
1834#define EXPECT_INVALID(...) \
1835 do { \
1836 uint64_t Elements[] = {__VA_ARGS__}; \
1837 EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \
1838 } while (false)
1839
1840 // Empty expression should be valid.
1841 EXPECT_TRUE(MDExpression::get(Context, None));
1842
1843 // Valid constructions.
1844 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1845 EXPECT_VALID(dwarf::DW_OP_deref);
1846 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1847 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1848 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1849 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1850 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1851
1852 // Invalid constructions.
1853 EXPECT_INVALID(~0u);
1854 EXPECT_INVALID(dwarf::DW_OP_plus);
1855 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1856 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1857 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1858 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1859
1860#undef EXPECT_VALID
1861#undef EXPECT_INVALID
1862}
1863
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001864typedef MetadataTest MDObjCPropertyTest;
1865
1866TEST_F(MDObjCPropertyTest, get) {
1867 StringRef Name = "name";
1868 Metadata *File = MDTuple::getDistinct(Context, None);
1869 unsigned Line = 5;
1870 StringRef GetterName = "getter";
1871 StringRef SetterName = "setter";
1872 unsigned Attributes = 7;
1873 Metadata *Type = MDTuple::getDistinct(Context, None);
1874
1875 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1876 SetterName, Attributes, Type);
1877
1878 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1879 EXPECT_EQ(Name, N->getName());
1880 EXPECT_EQ(File, N->getFile());
1881 EXPECT_EQ(Line, N->getLine());
1882 EXPECT_EQ(GetterName, N->getGetterName());
1883 EXPECT_EQ(SetterName, N->getSetterName());
1884 EXPECT_EQ(Attributes, N->getAttributes());
1885 EXPECT_EQ(Type, N->getType());
1886 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1887 SetterName, Attributes, Type));
1888
1889 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1890 SetterName, Attributes, Type));
1891 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1892 SetterName, Attributes, Type));
1893 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1894 SetterName, Attributes, Type));
1895 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1896 SetterName, Attributes, Type));
1897 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1898 "other", Attributes, Type));
1899 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1900 SetterName, Attributes + 1, Type));
1901 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1902 SetterName, Attributes, File));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001903
1904 TempMDObjCProperty Temp = N->clone();
1905 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001906}
1907
1908typedef MetadataTest MDImportedEntityTest;
1909
1910TEST_F(MDImportedEntityTest, get) {
1911 unsigned Tag = dwarf::DW_TAG_imported_module;
1912 Metadata *Scope = MDTuple::getDistinct(Context, None);
1913 Metadata *Entity = MDTuple::getDistinct(Context, None);
1914 unsigned Line = 5;
1915 StringRef Name = "name";
1916
1917 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1918
1919 EXPECT_EQ(Tag, N->getTag());
1920 EXPECT_EQ(Scope, N->getScope());
1921 EXPECT_EQ(Entity, N->getEntity());
1922 EXPECT_EQ(Line, N->getLine());
1923 EXPECT_EQ(Name, N->getName());
1924 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1925
1926 EXPECT_NE(N,
1927 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1928 Scope, Entity, Line, Name));
1929 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1930 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1931 EXPECT_NE(N,
1932 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1933 EXPECT_NE(N,
1934 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001935
1936 TempMDImportedEntity Temp = N->clone();
1937 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001938}
1939
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001940typedef MetadataTest MetadataAsValueTest;
1941
1942TEST_F(MetadataAsValueTest, MDNode) {
1943 MDNode *N = MDNode::get(Context, None);
1944 auto *V = MetadataAsValue::get(Context, N);
1945 EXPECT_TRUE(V->getType()->isMetadataTy());
1946 EXPECT_EQ(N, V->getMetadata());
1947
1948 auto *V2 = MetadataAsValue::get(Context, N);
1949 EXPECT_EQ(V, V2);
1950}
1951
1952TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1953 MDNode *N = MDNode::get(Context, None);
1954 Metadata *Ops[] = {N};
1955 MDNode *N2 = MDNode::get(Context, Ops);
1956 auto *V = MetadataAsValue::get(Context, N2);
1957 EXPECT_TRUE(V->getType()->isMetadataTy());
1958 EXPECT_EQ(N2, V->getMetadata());
1959
1960 auto *V2 = MetadataAsValue::get(Context, N2);
1961 EXPECT_EQ(V, V2);
1962
1963 auto *V3 = MetadataAsValue::get(Context, N);
1964 EXPECT_TRUE(V3->getType()->isMetadataTy());
1965 EXPECT_NE(V, V3);
1966 EXPECT_EQ(N, V3->getMetadata());
1967}
1968
1969TEST_F(MetadataAsValueTest, MDNodeConstant) {
1970 auto *C = ConstantInt::getTrue(Context);
1971 auto *MD = ConstantAsMetadata::get(C);
1972 Metadata *Ops[] = {MD};
1973 auto *N = MDNode::get(Context, Ops);
1974
1975 auto *V = MetadataAsValue::get(Context, MD);
1976 EXPECT_TRUE(V->getType()->isMetadataTy());
1977 EXPECT_EQ(MD, V->getMetadata());
1978
1979 auto *V2 = MetadataAsValue::get(Context, N);
1980 EXPECT_EQ(MD, V2->getMetadata());
1981 EXPECT_EQ(V, V2);
1982}
1983
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001984typedef MetadataTest ValueAsMetadataTest;
1985
1986TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1987 Type *Ty = Type::getInt1PtrTy(Context);
1988 std::unique_ptr<GlobalVariable> GV0(
1989 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1990 auto *MD = ValueAsMetadata::get(GV0.get());
1991 EXPECT_TRUE(MD->getValue() == GV0.get());
1992 ASSERT_TRUE(GV0->use_empty());
1993
1994 std::unique_ptr<GlobalVariable> GV1(
1995 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1996 GV0->replaceAllUsesWith(GV1.get());
1997 EXPECT_TRUE(MD->getValue() == GV1.get());
1998}
1999
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002000TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2001 // Create a constant.
2002 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2003 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2004
2005 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002006 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002007
2008 // 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 +00002009 Metadata *Ops1[] = {CI, CI, Temp.get()};
2010 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002011
2012 auto *N1 = MDTuple::get(Context, Ops1);
2013 auto *N2 = MDTuple::get(Context, Ops2);
2014 ASSERT_NE(N1, N2);
2015
2016 // Tell metadata that the constant is getting deleted.
2017 //
2018 // After this, N1 will be invalid, so don't touch it.
2019 ValueAsMetadata::handleDeletion(CI->getValue());
2020 EXPECT_EQ(nullptr, N2->getOperand(0));
2021 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002022 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002023
2024 // Clean up Temp for teardown.
2025 Temp->replaceAllUsesWith(nullptr);
2026}
2027
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002028typedef MetadataTest TrackingMDRefTest;
2029
2030TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2031 Type *Ty = Type::getInt1PtrTy(Context);
2032 std::unique_ptr<GlobalVariable> GV0(
2033 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2034 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2035 EXPECT_TRUE(MD->getValue() == GV0.get());
2036 ASSERT_TRUE(GV0->use_empty());
2037
2038 std::unique_ptr<GlobalVariable> GV1(
2039 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2040 GV0->replaceAllUsesWith(GV1.get());
2041 EXPECT_TRUE(MD->getValue() == GV1.get());
2042
2043 // Reset it, so we don't inadvertently test deletion.
2044 MD.reset();
2045}
2046
2047TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2048 Type *Ty = Type::getInt1PtrTy(Context);
2049 std::unique_ptr<GlobalVariable> GV(
2050 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2051 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2052 EXPECT_TRUE(MD->getValue() == GV.get());
2053 ASSERT_TRUE(GV->use_empty());
2054
2055 GV.reset();
2056 EXPECT_TRUE(!MD);
2057}
2058
Devang Patel0924b332009-07-30 00:03:41 +00002059TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002060 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002061 ConstantAsMetadata *C =
2062 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2063 ConstantAsMetadata *C2 =
2064 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002065
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002066 Metadata *const V = C;
2067 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002068 MDNode *n = MDNode::get(Context, V);
2069 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002070
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002071 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002072 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002073 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2074 NMD->addOperand(n);
2075 NMD->addOperand(n2);
2076
Chris Lattnerbe354a62009-08-23 04:47:35 +00002077 std::string Str;
2078 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002079 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002080 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002081 oss.str().c_str());
2082}
Nick Lewycky49f89192009-04-04 07:22:01 +00002083}