blob: 883a5823100c3b6958fdd90fe0bc6645fbd3f397 [file] [log] [blame]
Duncan P. N. Exon Smith0784a4d2015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky21cc4462009-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
Chandler Carruth3c0d6072017-06-06 11:06:56 +000010#include "llvm/IR/Metadata.h"
Duncan P. N. Exon Smith698be082015-01-13 00:46:34 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000012#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +000013#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithca8d3bf2015-02-02 18:53:21 +000014#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smitha9d82a52015-02-18 20:32:57 +000015#include "llvm/IR/Function.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Instructions.h"
17#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +000019#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Duncan P. N. Exon Smithef477f32015-04-24 21:53:27 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000023#include "gtest/gtest.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000024using namespace llvm;
25
26namespace {
27
Duncan P. N. Exon Smith641414a2015-01-19 19:02:06 +000028TEST(ContextAndReplaceableUsesTest, FromContext) {
29 LLVMContext Context;
30 ContextAndReplaceableUses CRU(Context);
31 EXPECT_EQ(&Context, &CRU.getContext());
32 EXPECT_FALSE(CRU.hasReplaceableUses());
33 EXPECT_FALSE(CRU.getReplaceableUses());
34}
35
36TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
37 LLVMContext Context;
38 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
39 EXPECT_EQ(&Context, &CRU.getContext());
40 EXPECT_TRUE(CRU.hasReplaceableUses());
41 EXPECT_TRUE(CRU.getReplaceableUses());
42}
43
44TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
45 LLVMContext Context;
46 ContextAndReplaceableUses CRU(Context);
47 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
48 EXPECT_EQ(&Context, &CRU.getContext());
49 EXPECT_TRUE(CRU.hasReplaceableUses());
50 EXPECT_TRUE(CRU.getReplaceableUses());
51}
52
53TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
54 LLVMContext Context;
55 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
56 auto *Ptr = ReplaceableUses.get();
57 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
58 ReplaceableUses = CRU.takeReplaceableUses();
59 EXPECT_EQ(&Context, &CRU.getContext());
60 EXPECT_FALSE(CRU.hasReplaceableUses());
61 EXPECT_FALSE(CRU.getReplaceableUses());
62 EXPECT_EQ(Ptr, ReplaceableUses.get());
63}
64
Jeffrey Yasskine5790a42010-03-04 23:24:19 +000065class MetadataTest : public testing::Test {
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +000066public:
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +000067 MetadataTest() : M("test", Context), Counter(0) {}
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +000068
Jeffrey Yasskine5790a42010-03-04 23:24:19 +000069protected:
70 LLVMContext Context;
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +000071 Module M;
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +000072 int Counter;
73
Duncan P. N. Exon Smith2ebd1ef2014-12-16 07:09:37 +000074 MDNode *getNode() { return MDNode::get(Context, None); }
75 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
76 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
77 Metadata *MDs[] = {MD1, MD2};
78 return MDNode::get(Context, MDs);
79 }
Duncan P. N. Exon Smithc4eafd22015-03-26 22:05:04 +000080
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +000081 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000082 DISubroutineType *getSubroutineType() {
Leny Kholodov01dd3d92016-09-06 17:03:02 +000083 return DISubroutineType::getDistinct(Context, DINode::FlagZero, 0,
84 getNode(nullptr));
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +000085 }
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000086 DISubprogram *getSubprogram() {
Paul Robinsoneaa73532018-11-19 18:29:28 +000087 return DISubprogram::getDistinct(
88 Context, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0,
89 DINode::FlagZero, DISubprogram::SPFlagZero, nullptr);
Duncan P. N. Exon Smithc4eafd22015-03-26 22:05:04 +000090 }
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000091 DIFile *getFile() {
92 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +000093 }
Adrian Prantl4eeaa0d2016-04-15 15:57:41 +000094 DICompileUnit *getUnit() {
Peter Collingbourne76221cb2017-09-12 21:50:41 +000095 return DICompileUnit::getDistinct(
96 Context, 1, getFile(), "clang", false, "-g", 2, "",
97 DICompileUnit::FullDebug, getTuple(), getTuple(), getTuple(),
David Blaikiecf8a4a52018-08-16 21:29:55 +000098 getTuple(), getTuple(), 0, true, false,
David Blaikieecc582a2018-11-13 20:08:10 +000099 DICompileUnit::DebugNameTableKind::Default, false);
Adrian Prantl4eeaa0d2016-04-15 15:57:41 +0000100 }
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +0000101 DIType *getBasicType(StringRef Name) {
102 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +0000103 }
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +0000104 DIType *getDerivedType() {
Leny Kholodov01dd3d92016-09-06 17:03:02 +0000105 return DIDerivedType::getDistinct(
106 Context, dwarf::DW_TAG_pointer_type, "", nullptr, 0, nullptr,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +0000107 getBasicType("basictype"), 1, 2, 0, None, DINode::FlagZero);
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +0000108 }
Duncan P. N. Exon Smithe2e64122015-04-11 20:27:40 +0000109 Constant *getConstant() {
110 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
111 }
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +0000112 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smithe2e64122015-04-11 20:27:40 +0000113 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +0000114 }
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +0000115 DIType *getCompositeType() {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000116 return DICompositeType::getDistinct(
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +0000117 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
Leny Kholodovd9478f82016-09-06 10:46:28 +0000118 32, 32, 0, DINode::FlagZero, nullptr, 0, nullptr, nullptr, "");
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +0000119 }
Duncan P. N. Exon Smithe2e64122015-04-11 20:27:40 +0000120 Function *getFunction(StringRef Name) {
121 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +0000122 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
123 }
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000124};
125typedef MetadataTest MDStringTest;
Owen Anderson5d0bf1b2009-07-31 21:38:10 +0000126
Nick Lewycky21cc4462009-04-04 07:22:01 +0000127// Test that construction of MDString with different value produces different
128// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000129TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky21cc4462009-04-04 07:22:01 +0000130 char x[3] = { 'f', 0, 'A' };
Owen Anderson5d0bf1b2009-07-31 21:38:10 +0000131 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000132 x[2] = 'B';
Owen Anderson5d0bf1b2009-07-31 21:38:10 +0000133 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000134 EXPECT_NE(s1, s2);
135}
136
137// Test that creation of MDStrings with the same string contents produces the
138// same MDString object, even with different pointers.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000139TEST_F(MDStringTest, CreateSame) {
Nick Lewycky21cc4462009-04-04 07:22:01 +0000140 char x[4] = { 'a', 'b', 'c', 'X' };
141 char y[4] = { 'a', 'b', 'c', 'Y' };
142
Owen Anderson5d0bf1b2009-07-31 21:38:10 +0000143 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
144 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000145 EXPECT_EQ(s1, s2);
146}
147
148// Test that MDString prints out the string we fed it.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000149TEST_F(MDStringTest, PrintingSimple) {
Florian Hahn80809352018-11-25 19:38:02 +0000150 char str[14] = "testing 1 2 3";
151 MDString *s = MDString::get(Context, StringRef(&str[0], 13));
152 strncpy(str, "aaaaaaaaaaaaa", 14);
Nick Lewycky21cc4462009-04-04 07:22:01 +0000153
Chris Lattner0c47a412009-08-23 04:47:35 +0000154 std::string Str;
155 raw_string_ostream oss(Str);
Nick Lewycky21cc4462009-04-04 07:22:01 +0000156 s->print(oss);
Duncan P. N. Exon Smith02a8d1b2014-12-16 07:40:31 +0000157 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky21cc4462009-04-04 07:22:01 +0000158}
159
160// Test printing of MDString with non-printable characters.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000161TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskincda2a142011-08-30 20:53:29 +0000162 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson5d0bf1b2009-07-31 21:38:10 +0000163 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattner0c47a412009-08-23 04:47:35 +0000164 std::string Str;
165 raw_string_ostream oss(Str);
Nick Lewycky21cc4462009-04-04 07:22:01 +0000166 s->print(oss);
Duncan P. N. Exon Smith02a8d1b2014-12-16 07:40:31 +0000167 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky21cc4462009-04-04 07:22:01 +0000168}
169
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000170typedef MetadataTest MDNodeTest;
171
Nick Lewycky21cc4462009-04-04 07:22:01 +0000172// Test the two constructors, and containing other Constants.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000173TEST_F(MDNodeTest, Simple) {
Nick Lewycky21cc4462009-04-04 07:22:01 +0000174 char x[3] = { 'a', 'b', 'c' };
175 char y[3] = { '1', '2', '3' };
176
Owen Anderson5d0bf1b2009-07-31 21:38:10 +0000177 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
178 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Mehdi Amini8be77072016-04-14 21:59:01 +0000179 ConstantAsMetadata *CI =
180 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000181
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000182 std::vector<Metadata *> V;
Nick Lewycky21cc4462009-04-04 07:22:01 +0000183 V.push_back(s1);
184 V.push_back(CI);
185 V.push_back(s2);
186
Jay Foadec9186bc2011-04-21 19:59:31 +0000187 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000188 Metadata *const c1 = n1;
Jay Foadec9186bc2011-04-21 19:59:31 +0000189 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000190 Metadata *const c2 = n2;
Jay Foadec9186bc2011-04-21 19:59:31 +0000191 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands4000afe2012-03-31 08:20:11 +0000192 MDNode *n4 = MDNode::getIfExists(Context, V);
193 MDNode *n5 = MDNode::getIfExists(Context, c1);
194 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky21cc4462009-04-04 07:22:01 +0000195 EXPECT_NE(n1, n2);
Devang Patel5f4ac842009-09-03 01:39:20 +0000196 EXPECT_EQ(n1, n3);
Duncan Sands4000afe2012-03-31 08:20:11 +0000197 EXPECT_EQ(n4, n1);
198 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000199 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky21cc4462009-04-04 07:22:01 +0000200
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000201 EXPECT_EQ(3u, n1->getNumOperands());
202 EXPECT_EQ(s1, n1->getOperand(0));
203 EXPECT_EQ(CI, n1->getOperand(1));
204 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000205
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000206 EXPECT_EQ(1u, n2->getNumOperands());
207 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000208}
Nick Lewyckycb337992009-05-10 20:57:05 +0000209
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000210TEST_F(MDNodeTest, Delete) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000211 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
212 Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
Nick Lewyckycb337992009-05-10 20:57:05 +0000213
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000214 Metadata *const V = LocalAsMetadata::get(I);
Jay Foadec9186bc2011-04-21 19:59:31 +0000215 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000216 TrackingMDRef wvh(n);
Nick Lewyckycb337992009-05-10 20:57:05 +0000217
218 EXPECT_EQ(n, wvh);
219
Reid Kleckner816047d2017-05-18 17:24:10 +0000220 I->deleteValue();
Nick Lewyckycb337992009-05-10 20:57:05 +0000221}
Devang Patelfa7c4dc2009-07-30 00:03:41 +0000222
Duncan P. N. Exon Smith7280d8c2014-12-07 19:52:06 +0000223TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith9e2c0f92014-12-16 07:45:05 +0000224 // !0 = !{!0}
225 // !1 = !{!0}
Duncan P. N. Exon Smith7280d8c2014-12-07 19:52:06 +0000226 {
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000227 auto Temp = MDNode::getTemporary(Context, None);
228 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smith7280d8c2014-12-07 19:52:06 +0000229 MDNode *Self = MDNode::get(Context, Args);
230 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smith7280d8c2014-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.
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
Duncan P. N. Exon Smith9e2c0f92014-12-16 07:45:05 +0000242 // !0 = !{!0, !{}}
243 // !1 = !{!0, !{}}
Duncan P. N. Exon Smith7280d8c2014-12-07 19:52:06 +0000244 {
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000245 auto Temp = MDNode::getTemporary(Context, None);
246 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smith7280d8c2014-12-07 19:52:06 +0000247 MDNode *Self = MDNode::get(Context, Args);
248 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smith7280d8c2014-12-07 19:52:06 +0000249 ASSERT_EQ(Self, Self->getOperand(0));
250
251 // Self-references should be distinct, so MDNode::get() should grab a
252 // uniqued node that references Self, not Self itself.
253 Args[0] = Self;
254 MDNode *Ref1 = MDNode::get(Context, Args);
255 MDNode *Ref2 = MDNode::get(Context, Args);
256 EXPECT_NE(Self, Ref1);
257 EXPECT_EQ(Ref1, Ref2);
258 }
259}
260
Duncan P. N. Exon Smith2ebd1ef2014-12-16 07:09:37 +0000261TEST_F(MDNodeTest, Print) {
262 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
263 MDString *S = MDString::get(Context, "foo");
264 MDNode *N0 = getNode();
265 MDNode *N1 = getNode(N0);
266 MDNode *N2 = getNode(N0, N1);
267
268 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
269 MDNode *N = MDNode::get(Context, Args);
270
271 std::string Expected;
272 {
273 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000274 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smith2ebd1ef2014-12-16 07:09:37 +0000275 C->printAsOperand(OS);
276 OS << ", ";
Duncan P. N. Exon Smith02a8d1b2014-12-16 07:40:31 +0000277 S->printAsOperand(OS);
Duncan P. N. Exon Smith2ebd1ef2014-12-16 07:09:37 +0000278 OS << ", null";
279 MDNode *Nodes[] = {N0, N1, N2};
280 for (auto *Node : Nodes)
281 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith344ea292015-02-25 22:46:38 +0000282 OS << "}";
Duncan P. N. Exon Smith2ebd1ef2014-12-16 07:09:37 +0000283 }
284
285 std::string Actual;
286 {
287 raw_string_ostream OS(Actual);
288 N->print(OS);
289 }
290
291 EXPECT_EQ(Expected, Actual);
292}
293
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000294#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
295 do { \
296 std::string Actual_; \
297 raw_string_ostream OS(Actual_); \
298 PRINT; \
299 OS.flush(); \
300 std::string Expected_(EXPECTED); \
301 EXPECT_EQ(Expected_, Actual_); \
302 } while (false)
303
Duncan P. N. Exon Smithbee74a32015-03-16 21:21:10 +0000304TEST_F(MDNodeTest, PrintTemporary) {
305 MDNode *Arg = getNode();
306 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
307 MDNode *N = getNode(Temp.get());
308 Module M("test", Context);
309 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
310 NMD->addOperand(N);
311
312 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
313 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
314 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
315
316 // Cleanup.
317 Temp->replaceAllUsesWith(Arg);
318}
319
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000320TEST_F(MDNodeTest, PrintFromModule) {
321 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
322 MDString *S = MDString::get(Context, "foo");
323 MDNode *N0 = getNode();
324 MDNode *N1 = getNode(N0);
325 MDNode *N2 = getNode(N0, N1);
326
327 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
328 MDNode *N = MDNode::get(Context, Args);
329 Module M("test", Context);
330 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
331 NMD->addOperand(N);
332
333 std::string Expected;
334 {
335 raw_string_ostream OS(Expected);
336 OS << "!0 = !{";
337 C->printAsOperand(OS);
338 OS << ", ";
339 S->printAsOperand(OS);
340 OS << ", null, !1, !2, !3}";
341 }
342
343 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
344}
345
346TEST_F(MDNodeTest, PrintFromFunction) {
347 Module M("test", Context);
348 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
349 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
350 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
351 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
352 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
353 auto *R0 = ReturnInst::Create(Context, BB0);
354 auto *R1 = ReturnInst::Create(Context, BB1);
355 auto *N0 = MDNode::getDistinct(Context, None);
356 auto *N1 = MDNode::getDistinct(Context, None);
357 R0->setMetadata("md", N0);
358 R1->setMetadata("md", N1);
359
360 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
361 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000362
363 ModuleSlotTracker MST(&M);
364 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
365 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000366}
367
368TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
369 Module M("test", Context);
370
371 auto *Intrinsic =
372 Function::Create(FunctionType::get(Type::getVoidTy(Context),
373 Type::getMetadataTy(Context), false),
374 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
375
376 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
377 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
378 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
379 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
380 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
381 auto *N0 = MDNode::getDistinct(Context, None);
382 auto *N1 = MDNode::getDistinct(Context, None);
383 auto *MAV0 = MetadataAsValue::get(Context, N0);
384 auto *MAV1 = MetadataAsValue::get(Context, N1);
385 CallInst::Create(Intrinsic, MAV0, "", BB0);
386 CallInst::Create(Intrinsic, MAV1, "", BB1);
387
388 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
389 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
390 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
391 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
392 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
393 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +0000394
395 ModuleSlotTracker MST(&M);
396 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
397 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
398 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
399 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
400 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
401 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000402}
David Stenberg11ef6e12018-11-02 11:46:24 +0000403
404TEST_F(MDNodeTest, PrintWithDroppedCallOperand) {
405 Module M("test", Context);
406
407 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
408 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
409 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
410 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
411
412 CallInst *CI0 = CallInst::Create(F1, "", BB0);
413 CI0->dropAllReferences();
414
415 auto *R0 = ReturnInst::Create(Context, BB0);
416 auto *N0 = MDNode::getDistinct(Context, None);
417 R0->setMetadata("md", N0);
418
419 // Printing the metadata node would previously result in a failed assertion
420 // due to the call instruction's dropped function operand.
421 ModuleSlotTracker MST(&M);
422 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
423}
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000424#undef EXPECT_PRINTER_EQ
425
Duncan P. N. Exon Smith97d1c072015-01-05 23:31:54 +0000426TEST_F(MDNodeTest, NullOperand) {
427 // metadata !{}
428 MDNode *Empty = MDNode::get(Context, None);
429
430 // metadata !{metadata !{}}
431 Metadata *Ops[] = {Empty};
432 MDNode *N = MDNode::get(Context, Ops);
433 ASSERT_EQ(Empty, N->getOperand(0));
434
435 // metadata !{metadata !{}} => metadata !{null}
436 N->replaceOperandWith(0, nullptr);
437 ASSERT_EQ(nullptr, N->getOperand(0));
438
439 // metadata !{null}
440 Ops[0] = nullptr;
441 MDNode *NullOp = MDNode::get(Context, Ops);
442 ASSERT_EQ(nullptr, NullOp->getOperand(0));
443 EXPECT_EQ(N, NullOp);
444}
445
Duncan P. N. Exon Smithee06e122015-01-07 21:35:38 +0000446TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
447 // !{}
448 MDNode *Empty = MDNode::get(Context, None);
449 ASSERT_TRUE(Empty->isResolved());
450 EXPECT_FALSE(Empty->isDistinct());
451
452 // !{!{}}
453 Metadata *Wrapped1Ops[] = {Empty};
454 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
455 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
456 ASSERT_TRUE(Wrapped1->isResolved());
457 EXPECT_FALSE(Wrapped1->isDistinct());
458
459 // !{!{!{}}}
460 Metadata *Wrapped2Ops[] = {Wrapped1};
461 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
462 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
463 ASSERT_TRUE(Wrapped2->isResolved());
464 EXPECT_FALSE(Wrapped2->isDistinct());
465
466 // !{!{!{}}} => !{!{}}
467 Wrapped2->replaceOperandWith(0, Empty);
468 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
469 EXPECT_TRUE(Wrapped2->isDistinct());
470 EXPECT_FALSE(Wrapped1->isDistinct());
471}
472
Duncan P. N. Exon Smith8fa25a12016-08-03 18:19:43 +0000473TEST_F(MDNodeTest, UniquedOnDeletedOperand) {
474 // temp !{}
475 TempMDTuple T = MDTuple::getTemporary(Context, None);
476
477 // !{temp !{}}
478 Metadata *Ops[] = {T.get()};
479 MDTuple *N = MDTuple::get(Context, Ops);
480
481 // !{temp !{}} => !{null}
482 T.reset();
483 ASSERT_TRUE(N->isUniqued());
484 Metadata *NullOps[] = {nullptr};
485 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
486}
487
488TEST_F(MDNodeTest, DistinctOnDeletedValueOperand) {
489 // i1* @GV
490 Type *Ty = Type::getInt1PtrTy(Context);
491 std::unique_ptr<GlobalVariable> GV(
492 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
493 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
494
495 // !{i1* @GV}
496 Metadata *Ops[] = {Op};
497 MDTuple *N = MDTuple::get(Context, Ops);
498
499 // !{i1* @GV} => !{null}
500 GV.reset();
501 ASSERT_TRUE(N->isDistinct());
502 ASSERT_EQ(nullptr, N->getOperand(0));
503 Metadata *NullOps[] = {nullptr};
504 ASSERT_NE(N, MDTuple::get(Context, NullOps));
505}
506
Duncan P. N. Exon Smith727176d2015-01-07 22:24:46 +0000507TEST_F(MDNodeTest, getDistinct) {
508 // !{}
509 MDNode *Empty = MDNode::get(Context, None);
510 ASSERT_TRUE(Empty->isResolved());
511 ASSERT_FALSE(Empty->isDistinct());
512 ASSERT_EQ(Empty, MDNode::get(Context, None));
513
514 // distinct !{}
515 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
516 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
517 EXPECT_TRUE(Distinct1->isResolved());
518 EXPECT_TRUE(Distinct2->isDistinct());
519 EXPECT_NE(Empty, Distinct1);
520 EXPECT_NE(Empty, Distinct2);
521 EXPECT_NE(Distinct1, Distinct2);
522
523 // !{}
524 ASSERT_EQ(Empty, MDNode::get(Context, None));
525}
526
Duncan P. N. Exon Smith1d72e182015-01-19 18:45:35 +0000527TEST_F(MDNodeTest, isUniqued) {
528 MDNode *U = MDTuple::get(Context, None);
529 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000530 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith1d72e182015-01-19 18:45:35 +0000531 EXPECT_TRUE(U->isUniqued());
532 EXPECT_FALSE(D->isUniqued());
533 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smith1d72e182015-01-19 18:45:35 +0000534}
535
536TEST_F(MDNodeTest, isDistinct) {
537 MDNode *U = MDTuple::get(Context, None);
538 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000539 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith1d72e182015-01-19 18:45:35 +0000540 EXPECT_FALSE(U->isDistinct());
541 EXPECT_TRUE(D->isDistinct());
542 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smith1d72e182015-01-19 18:45:35 +0000543}
544
545TEST_F(MDNodeTest, isTemporary) {
546 MDNode *U = MDTuple::get(Context, None);
547 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000548 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith1d72e182015-01-19 18:45:35 +0000549 EXPECT_FALSE(U->isTemporary());
550 EXPECT_FALSE(D->isTemporary());
551 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smith28184c12015-01-12 18:41:26 +0000552}
553
Duncan P. N. Exon Smith727176d2015-01-07 22:24:46 +0000554TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
555 // temporary !{}
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000556 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith727176d2015-01-07 22:24:46 +0000557 ASSERT_FALSE(Temp->isResolved());
558
559 // distinct !{temporary !{}}
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000560 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith727176d2015-01-07 22:24:46 +0000561 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
562 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000563 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith727176d2015-01-07 22:24:46 +0000564
565 // temporary !{} => !{}
566 MDNode *Empty = MDNode::get(Context, None);
567 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith727176d2015-01-07 22:24:46 +0000568 EXPECT_EQ(Empty, Distinct->getOperand(0));
569}
570
Duncan P. N. Exon Smith728315a2015-01-12 19:22:04 +0000571TEST_F(MDNodeTest, handleChangedOperandRecursion) {
572 // !0 = !{}
573 MDNode *N0 = MDNode::get(Context, None);
574
575 // !1 = !{!3, null}
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000576 auto Temp3 = MDTuple::getTemporary(Context, None);
577 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith728315a2015-01-12 19:22:04 +0000578 MDNode *N1 = MDNode::get(Context, Ops1);
579
580 // !2 = !{!3, !0}
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000581 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith728315a2015-01-12 19:22:04 +0000582 MDNode *N2 = MDNode::get(Context, Ops2);
583
584 // !3 = !{!2}
585 Metadata *Ops3[] = {N2};
586 MDNode *N3 = MDNode::get(Context, Ops3);
587 Temp3->replaceAllUsesWith(N3);
588
589 // !4 = !{!1}
590 Metadata *Ops4[] = {N1};
591 MDNode *N4 = MDNode::get(Context, Ops4);
592
593 // Confirm that the cycle prevented RAUW from getting dropped.
594 EXPECT_TRUE(N0->isResolved());
595 EXPECT_FALSE(N1->isResolved());
596 EXPECT_FALSE(N2->isResolved());
597 EXPECT_FALSE(N3->isResolved());
598 EXPECT_FALSE(N4->isResolved());
599
600 // Create a couple of distinct nodes to observe what's going on.
601 //
602 // !5 = distinct !{!2}
603 // !6 = distinct !{!3}
604 Metadata *Ops5[] = {N2};
605 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
606 Metadata *Ops6[] = {N3};
607 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
608
609 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
610 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
611 // references !3, this can cause a re-entry of handleChangedOperand() when !3
612 // is not ready for it.
613 //
614 // !2->replaceOperandWith(1, nullptr)
615 // !2: !{!3, !0} => !{!3, null}
616 // !2->replaceAllUsesWith(!1)
617 // !3: !{!2] => !{!1}
618 // !3->replaceAllUsesWith(!4)
619 N2->replaceOperandWith(1, nullptr);
620
621 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
622 // under us. Just check that the other nodes are sane.
623 //
624 // !1 = !{!4, null}
625 // !4 = !{!1}
626 // !5 = distinct !{!1}
627 // !6 = distinct !{!4}
628 EXPECT_EQ(N4, N1->getOperand(0));
629 EXPECT_EQ(N1, N4->getOperand(0));
630 EXPECT_EQ(N1, N5->getOperand(0));
631 EXPECT_EQ(N4, N6->getOperand(0));
632}
633
Duncan P. N. Exon Smith698be082015-01-13 00:46:34 +0000634TEST_F(MDNodeTest, replaceResolvedOperand) {
635 // Check code for replacing one resolved operand with another. If doing this
636 // directly (via replaceOperandWith()) becomes illegal, change the operand to
637 // a global value that gets RAUW'ed.
638 //
639 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000640 auto Temp = MDTuple::getTemporary(Context, None);
641 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith698be082015-01-13 00:46:34 +0000642
NAKAMURA Takumi16a6d382015-01-13 08:13:46 +0000643 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith698be082015-01-13 00:46:34 +0000644 MDNode *N = MDTuple::get(Context, Ops);
645 EXPECT_EQ(nullptr, N->getOperand(0));
646 ASSERT_FALSE(N->isResolved());
647
648 // Check code for replacing resolved nodes.
649 N->replaceOperandWith(0, Empty);
650 EXPECT_EQ(Empty, N->getOperand(0));
651
652 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +0000653 N->replaceOperandWith(0, Temp.get());
654 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith698be082015-01-13 00:46:34 +0000655
656 // Remove the references to Temp; required for teardown.
657 Temp->replaceAllUsesWith(nullptr);
658}
659
Duncan P. N. Exon Smith37c7ccc2015-01-19 22:24:52 +0000660TEST_F(MDNodeTest, replaceWithUniqued) {
661 auto *Empty = MDTuple::get(Context, None);
662 MDTuple *FirstUniqued;
663 {
664 Metadata *Ops[] = {Empty};
665 auto Temp = MDTuple::getTemporary(Context, Ops);
666 EXPECT_TRUE(Temp->isTemporary());
667
668 // Don't expect a collision.
669 auto *Current = Temp.get();
670 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
671 EXPECT_TRUE(FirstUniqued->isUniqued());
672 EXPECT_TRUE(FirstUniqued->isResolved());
673 EXPECT_EQ(Current, FirstUniqued);
674 }
675 {
676 Metadata *Ops[] = {Empty};
677 auto Temp = MDTuple::getTemporary(Context, Ops);
678 EXPECT_TRUE(Temp->isTemporary());
679
680 // Should collide with Uniqued above this time.
681 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
682 EXPECT_TRUE(Uniqued->isUniqued());
683 EXPECT_TRUE(Uniqued->isResolved());
684 EXPECT_EQ(FirstUniqued, Uniqued);
685 }
686 {
687 auto Unresolved = MDTuple::getTemporary(Context, None);
688 Metadata *Ops[] = {Unresolved.get()};
689 auto Temp = MDTuple::getTemporary(Context, Ops);
690 EXPECT_TRUE(Temp->isTemporary());
691
692 // Shouldn't be resolved.
693 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
694 EXPECT_TRUE(Uniqued->isUniqued());
695 EXPECT_FALSE(Uniqued->isResolved());
696
697 // Should be a different node.
698 EXPECT_NE(FirstUniqued, Uniqued);
699
700 // Should resolve when we update its node (note: be careful to avoid a
701 // collision with any other nodes above).
702 Uniqued->replaceOperandWith(0, nullptr);
703 EXPECT_TRUE(Uniqued->isResolved());
704 }
705}
706
Duncan P. N. Exon Smith34dea0f2015-03-31 21:05:06 +0000707TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithf067a682015-03-31 20:50:50 +0000708 // temp !{}
709 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
710 EXPECT_FALSE(Op->isResolved());
711
712 // temp !{temp !{}}
713 Metadata *Ops[] = {Op};
714 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
715 EXPECT_FALSE(N->isResolved());
716
717 // temp !{temp !{}} => !{temp !{}}
718 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
719 EXPECT_FALSE(N->isResolved());
720
721 // !{temp !{}} => !{!{}}
722 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
723 EXPECT_TRUE(Op->isResolved());
724 EXPECT_TRUE(N->isResolved());
725}
726
Duncan P. N. Exon Smith8fa25a12016-08-03 18:19:43 +0000727TEST_F(MDNodeTest, replaceWithUniquedDeletedOperand) {
Duncan P. N. Exon Smithf067a682015-03-31 20:50:50 +0000728 // i1* @GV
729 Type *Ty = Type::getInt1PtrTy(Context);
730 std::unique_ptr<GlobalVariable> GV(
731 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
732 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
733
734 // temp !{i1* @GV}
735 Metadata *Ops[] = {Op};
736 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
737
738 // temp !{i1* @GV} => !{i1* @GV}
739 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
740 ASSERT_TRUE(N->isUniqued());
741
742 // !{i1* @GV} => !{null}
743 GV.reset();
Duncan P. N. Exon Smith8fa25a12016-08-03 18:19:43 +0000744 ASSERT_TRUE(N->isDistinct());
745 ASSERT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smithf067a682015-03-31 20:50:50 +0000746 Metadata *NullOps[] = {nullptr};
Duncan P. N. Exon Smith8fa25a12016-08-03 18:19:43 +0000747 ASSERT_NE(N, MDTuple::get(Context, NullOps));
748}
749
750TEST_F(MDNodeTest, replaceWithUniquedChangedOperand) {
751 // i1* @GV
752 Type *Ty = Type::getInt1PtrTy(Context);
753 std::unique_ptr<GlobalVariable> GV(
754 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
755 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
756
757 // temp !{i1* @GV}
758 Metadata *Ops[] = {Op};
759 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
760
761 // temp !{i1* @GV} => !{i1* @GV}
762 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
763 ASSERT_TRUE(N->isUniqued());
764
765 // !{i1* @GV} => !{i1* @GV2}
766 std::unique_ptr<GlobalVariable> GV2(
767 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
768 GV->replaceAllUsesWith(GV2.get());
769 ASSERT_TRUE(N->isUniqued());
770 Metadata *NullOps[] = {ConstantAsMetadata::get(GV2.get())};
Duncan P. N. Exon Smithf067a682015-03-31 20:50:50 +0000771 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
772}
773
Duncan P. N. Exon Smith37c7ccc2015-01-19 22:24:52 +0000774TEST_F(MDNodeTest, replaceWithDistinct) {
775 {
776 auto *Empty = MDTuple::get(Context, None);
777 Metadata *Ops[] = {Empty};
778 auto Temp = MDTuple::getTemporary(Context, Ops);
779 EXPECT_TRUE(Temp->isTemporary());
780
781 // Don't expect a collision.
782 auto *Current = Temp.get();
783 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
784 EXPECT_TRUE(Distinct->isDistinct());
785 EXPECT_TRUE(Distinct->isResolved());
786 EXPECT_EQ(Current, Distinct);
787 }
788 {
789 auto Unresolved = MDTuple::getTemporary(Context, None);
790 Metadata *Ops[] = {Unresolved.get()};
791 auto Temp = MDTuple::getTemporary(Context, Ops);
792 EXPECT_TRUE(Temp->isTemporary());
793
794 // Don't expect a collision.
795 auto *Current = Temp.get();
796 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
797 EXPECT_TRUE(Distinct->isDistinct());
798 EXPECT_TRUE(Distinct->isResolved());
799 EXPECT_EQ(Current, Distinct);
800
801 // Cleanup; required for teardown.
802 Unresolved->replaceAllUsesWith(nullptr);
803 }
804}
805
Duncan P. N. Exon Smith027898a2015-02-10 19:13:46 +0000806TEST_F(MDNodeTest, replaceWithPermanent) {
807 Metadata *Ops[] = {nullptr};
808 auto Temp = MDTuple::getTemporary(Context, Ops);
809 auto *T = Temp.get();
810
811 // U is a normal, uniqued node that references T.
812 auto *U = MDTuple::get(Context, T);
813 EXPECT_TRUE(U->isUniqued());
814
815 // Make Temp self-referencing.
816 Temp->replaceOperandWith(0, T);
817
818 // Try to uniquify Temp. This should, despite the name in the API, give a
819 // 'distinct' node, since self-references aren't allowed to be uniqued.
820 //
821 // Since it's distinct, N should have the same address as when it was a
822 // temporary (i.e., be equal to T not U).
823 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
824 EXPECT_EQ(N, T);
825 EXPECT_TRUE(N->isDistinct());
826
827 // U should be the canonical unique node with N as the argument.
828 EXPECT_EQ(U, MDTuple::get(Context, N));
829 EXPECT_TRUE(U->isUniqued());
830
831 // This temporary should collide with U when replaced, but it should still be
832 // uniqued.
833 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
834 EXPECT_TRUE(U->isUniqued());
835
836 // This temporary should become a new uniqued node.
837 auto Temp2 = MDTuple::getTemporary(Context, U);
838 auto *V = Temp2.get();
839 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
840 EXPECT_TRUE(V->isUniqued());
841 EXPECT_EQ(U, V->getOperand(0));
842}
843
Duncan P. N. Exon Smith00334612015-01-22 21:36:45 +0000844TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
845 TrackingMDRef Ref;
846 EXPECT_EQ(nullptr, Ref.get());
847 {
848 auto Temp = MDTuple::getTemporary(Context, None);
849 Ref.reset(Temp.get());
850 EXPECT_EQ(Temp.get(), Ref.get());
851 }
852 EXPECT_EQ(nullptr, Ref.get());
853}
854
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000855typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000856
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000857TEST_F(DILocationTest, Overflow) {
858 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000859 {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000860 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000861 EXPECT_EQ(2u, L->getLine());
862 EXPECT_EQ(7u, L->getColumn());
863 }
Duncan P. N. Exon Smithfb7514f2015-01-16 17:33:08 +0000864 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000865 {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000866 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smith8713d992015-02-06 22:50:13 +0000867 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithfb7514f2015-01-16 17:33:08 +0000868 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000869 }
870 {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000871 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smith8713d992015-02-06 22:50:13 +0000872 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000873 EXPECT_EQ(0u, L->getColumn());
874 }
875 {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000876 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smith8713d992015-02-06 22:50:13 +0000877 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000878 EXPECT_EQ(0u, L->getColumn());
879 }
880}
881
Adrian Prantl59c0c362018-08-24 23:30:57 +0000882TEST_F(DILocationTest, Merge) {
883 DISubprogram *N = getSubprogram();
884 DIScope *S = DILexicalBlock::get(Context, N, getFile(), 3, 4);
885
886 {
887 // Identical.
888 auto *A = DILocation::get(Context, 2, 7, N);
889 auto *B = DILocation::get(Context, 2, 7, N);
890 auto *M = DILocation::getMergedLocation(A, B);
891 EXPECT_EQ(2u, M->getLine());
892 EXPECT_EQ(7u, M->getColumn());
893 EXPECT_EQ(N, M->getScope());
894 }
895
896 {
897 // Identical, different scopes.
898 auto *A = DILocation::get(Context, 2, 7, N);
899 auto *B = DILocation::get(Context, 2, 7, S);
900 auto *M = DILocation::getMergedLocation(A, B);
901 EXPECT_EQ(0u, M->getLine()); // FIXME: Should this be 2?
902 EXPECT_EQ(0u, M->getColumn()); // FIXME: Should this be 7?
903 EXPECT_EQ(N, M->getScope());
904 }
905
906 {
907 // Different lines, same scopes.
908 auto *A = DILocation::get(Context, 1, 6, N);
909 auto *B = DILocation::get(Context, 2, 7, N);
910 auto *M = DILocation::getMergedLocation(A, B);
911 EXPECT_EQ(0u, M->getLine());
912 EXPECT_EQ(0u, M->getColumn());
913 EXPECT_EQ(N, M->getScope());
914 }
915
916 {
917 // Twisty locations, all different, same function.
918 auto *A = DILocation::get(Context, 1, 6, N);
919 auto *B = DILocation::get(Context, 2, 7, S);
920 auto *M = DILocation::getMergedLocation(A, B);
921 EXPECT_EQ(0u, M->getLine());
922 EXPECT_EQ(0u, M->getColumn());
923 EXPECT_EQ(N, M->getScope());
924 }
925
926 {
927 // Different function, same inlined-at.
928 auto *F = getFile();
929 auto *SP1 = DISubprogram::getDistinct(Context, F, "a", "a", F, 0, nullptr,
Paul Robinsoneaa73532018-11-19 18:29:28 +0000930 0, nullptr, 0, 0, DINode::FlagZero,
931 DISubprogram::SPFlagZero, nullptr);
Adrian Prantl59c0c362018-08-24 23:30:57 +0000932 auto *SP2 = DISubprogram::getDistinct(Context, F, "b", "b", F, 0, nullptr,
Paul Robinsoneaa73532018-11-19 18:29:28 +0000933 0, nullptr, 0, 0, DINode::FlagZero,
934 DISubprogram::SPFlagZero, nullptr);
Adrian Prantl59c0c362018-08-24 23:30:57 +0000935
936 auto *I = DILocation::get(Context, 2, 7, N);
937 auto *A = DILocation::get(Context, 1, 6, SP1, I);
938 auto *B = DILocation::get(Context, 2, 7, SP2, I);
939 auto *M = DILocation::getMergedLocation(A, B);
940 EXPECT_EQ(0u, M->getLine());
941 EXPECT_EQ(0u, M->getColumn());
942 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
943 EXPECT_EQ(I, M->getInlinedAt());
944 }
945
946 {
947 // Completely different.
948 auto *I = DILocation::get(Context, 2, 7, N);
949 auto *A = DILocation::get(Context, 1, 6, S, I);
950 auto *B = DILocation::get(Context, 2, 7, getSubprogram());
951 auto *M = DILocation::getMergedLocation(A, B);
952 EXPECT_EQ(0u, M->getLine());
953 EXPECT_EQ(0u, M->getColumn());
954 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
955 EXPECT_EQ(S, M->getScope());
956 EXPECT_EQ(nullptr, M->getInlinedAt());
957 }
958}
959
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000960TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smithc4eafd22015-03-26 22:05:04 +0000961 MDNode *N = getSubprogram();
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000962 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000963 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000964 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000965 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000966 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +0000967}
968
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000969TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smitha7852bf2015-01-19 20:37:44 +0000970 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000971 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smitha7852bf2015-01-19 20:37:44 +0000972 EXPECT_TRUE(L->isTemporary());
973 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smitha7852bf2015-01-19 20:37:44 +0000974}
975
Teresa Johnsonfda49fd2015-12-07 15:05:44 +0000976TEST_F(DILocationTest, cloneTemporary) {
977 MDNode *N = MDNode::get(Context, None);
978 auto L = DILocation::getTemporary(Context, 2, 7, N);
979 EXPECT_TRUE(L->isTemporary());
980 auto L2 = L->clone();
981 EXPECT_TRUE(L2->isTemporary());
982}
983
Mircea Trofin001ab102018-12-21 22:48:50 +0000984TEST_F(DILocationTest, discriminatorEncoding) {
985 EXPECT_EQ(0U, DILocation::encodeDiscriminator(0, 0, 0).getValue());
986
987 // Encode base discriminator as a component: lsb is 0, then the value.
988 // The other components are all absent, so we leave all the other bits 0.
989 EXPECT_EQ(2U, DILocation::encodeDiscriminator(1, 0, 0).getValue());
990
991 // Base discriminator component is empty, so lsb is 1. Next component is not
992 // empty, so its lsb is 0, then its value (1). Next component is empty.
993 // So the bit pattern is 101.
994 EXPECT_EQ(5U, DILocation::encodeDiscriminator(0, 1, 0).getValue());
995
996 // First 2 components are empty, so the bit pattern is 11. Then the
997 // next component - ending up with 1011.
998 EXPECT_EQ(0xbU, DILocation::encodeDiscriminator(0, 0, 1).getValue());
999
1000 // The bit pattern for the first 2 components is 11. The next bit is 0,
1001 // because the last component is not empty. We have 29 bits usable for
1002 // encoding, but we cap it at 12 bits uniformously for all components. We
1003 // encode the last component over 14 bits.
1004 EXPECT_EQ(0xfffbU, DILocation::encodeDiscriminator(0, 0, 0xfff).getValue());
1005
1006 EXPECT_EQ(0x102U, DILocation::encodeDiscriminator(1, 1, 0).getValue());
1007
1008 EXPECT_EQ(0x13eU, DILocation::encodeDiscriminator(0x1f, 1, 0).getValue());
1009
1010 EXPECT_EQ(0x87feU, DILocation::encodeDiscriminator(0x1ff, 1, 0).getValue());
1011
1012 EXPECT_EQ(0x1f3eU, DILocation::encodeDiscriminator(0x1f, 0x1f, 0).getValue());
1013
1014 EXPECT_EQ(0x3ff3eU,
1015 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0).getValue());
1016
1017 EXPECT_EQ(0x1ff87feU,
1018 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 0).getValue());
1019
1020 EXPECT_EQ(0xfff9f3eU,
1021 DILocation::encodeDiscriminator(0x1f, 0x1f, 0xfff).getValue());
1022
1023 EXPECT_EQ(0xffc3ff3eU,
1024 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0x1ff).getValue());
1025
1026 EXPECT_EQ(0xffcf87feU,
1027 DILocation::encodeDiscriminator(0x1ff, 0x1f, 0x1ff).getValue());
1028
1029 EXPECT_EQ(0xe1ff87feU,
1030 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 7).getValue());
1031}
1032
1033TEST_F(DILocationTest, discriminatorEncodingNegativeTests) {
1034 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
1035 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1000, 0, 0));
1036 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0x1000, 0));
1037 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
1038 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1ff, 0x1ff, 8));
1039 EXPECT_EQ(None,
1040 DILocation::encodeDiscriminator(std::numeric_limits<uint32_t>::max(),
1041 std::numeric_limits<uint32_t>::max(),
1042 0));
1043}
1044
1045TEST_F(DILocationTest, discriminatorSpecialCases) {
1046 // We don't test getCopyIdentifier here because the only way
1047 // to set it is by constructing an encoded discriminator using
1048 // encodeDiscriminator, which is already tested.
1049 auto L1 = DILocation::get(Context, 1, 2, getSubprogram());
1050 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1051 EXPECT_EQ(1U, L1->getDuplicationFactor());
1052
1053 auto L2 = L1->setBaseDiscriminator(1).getValue();
1054 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1055 EXPECT_EQ(1U, L1->getDuplicationFactor());
1056
1057 EXPECT_EQ(1U, L2->getBaseDiscriminator());
1058 EXPECT_EQ(1U, L2->getDuplicationFactor());
1059
1060 auto L3 = L2->cloneWithDuplicationFactor(2).getValue();
1061 EXPECT_EQ(1U, L3->getBaseDiscriminator());
1062 EXPECT_EQ(2U, L3->getDuplicationFactor());
1063
1064 auto L4 = L3->cloneWithDuplicationFactor(4).getValue();
1065 EXPECT_EQ(1U, L4->getBaseDiscriminator());
1066 EXPECT_EQ(8U, L4->getDuplicationFactor());
1067
1068 auto L5 = L4->setBaseDiscriminator(2).getValue();
1069 EXPECT_EQ(2U, L5->getBaseDiscriminator());
1070 EXPECT_EQ(1U, L5->getDuplicationFactor());
1071
1072 // Check extreme cases
1073 auto L6 = L1->setBaseDiscriminator(0xfff).getValue();
1074 EXPECT_EQ(0xfffU, L6->getBaseDiscriminator());
1075 EXPECT_EQ(
1076 0xfffU,
1077 L6->cloneWithDuplicationFactor(0xfff).getValue()->getDuplicationFactor());
1078
1079 // Check we return None for unencodable cases.
1080 EXPECT_EQ(None, L4->setBaseDiscriminator(0x1000));
1081 EXPECT_EQ(None, L4->cloneWithDuplicationFactor(0x1000));
1082}
1083
1084
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001085typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001086
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001087TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smitha791aea2015-01-22 23:10:55 +00001088 StringRef Header = "header";
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001089 auto *Empty = MDNode::get(Context, None);
1090 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001091 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001092 EXPECT_EQ(15u, N->getTag());
1093 EXPECT_EQ(2u, N->getNumOperands());
1094 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smitha791aea2015-01-22 23:10:55 +00001095 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001096 EXPECT_EQ(1u, N->getNumDwarfOperands());
1097 EXPECT_EQ(Empty, N->getDwarfOperand(0));
1098 EXPECT_EQ(Empty, N->getOperand(1));
1099 ASSERT_TRUE(N->isUniqued());
1100
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001101 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001102
1103 N->replaceOperandWith(1, nullptr);
1104 EXPECT_EQ(15u, N->getTag());
1105 EXPECT_EQ(Header, N->getHeader());
1106 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
1107 ASSERT_TRUE(N->isUniqued());
1108
1109 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001110 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001111
1112 N->replaceDwarfOperandWith(0, Empty);
1113 EXPECT_EQ(15u, N->getTag());
1114 EXPECT_EQ(Header, N->getHeader());
1115 EXPECT_EQ(Empty, N->getDwarfOperand(0));
1116 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001117 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001118
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001119 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001120 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001121}
1122
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001123TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith62475af2015-01-20 00:58:46 +00001124 // Canonicalize !"" to null.
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001125 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smitha791aea2015-01-22 23:10:55 +00001126 EXPECT_EQ(StringRef(), N->getHeader());
1127 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith62475af2015-01-20 00:58:46 +00001128}
1129
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001130typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001131
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001132TEST_F(DISubrangeTest, get) {
1133 auto *N = DISubrange::get(Context, 5, 7);
Sander de Smalen959cee72018-01-24 09:56:07 +00001134 auto Count = N->getCount();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001135 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
Sander de Smalen959cee72018-01-24 09:56:07 +00001136 ASSERT_TRUE(Count);
1137 ASSERT_TRUE(Count.is<ConstantInt*>());
1138 EXPECT_EQ(5, Count.get<ConstantInt*>()->getSExtValue());
Duncan P. N. Exon Smith329f8212015-04-07 00:39:59 +00001139 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001140 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
1141 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001142
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001143 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001144 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001145}
1146
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001147TEST_F(DISubrangeTest, getEmptyArray) {
1148 auto *N = DISubrange::get(Context, -1, 0);
Sander de Smalen959cee72018-01-24 09:56:07 +00001149 auto Count = N->getCount();
Duncan P. N. Exon Smithc2c5e482015-02-18 23:17:51 +00001150 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
Sander de Smalen959cee72018-01-24 09:56:07 +00001151 ASSERT_TRUE(Count);
1152 ASSERT_TRUE(Count.is<ConstantInt*>());
1153 EXPECT_EQ(-1, Count.get<ConstantInt*>()->getSExtValue());
Duncan P. N. Exon Smith329f8212015-04-07 00:39:59 +00001154 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001155 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smithc2c5e482015-02-18 23:17:51 +00001156}
1157
Sander de Smalen959cee72018-01-24 09:56:07 +00001158TEST_F(DISubrangeTest, getVariableCount) {
1159 DILocalScope *Scope = getSubprogram();
1160 DIFile *File = getFile();
1161 DIType *Type = getDerivedType();
1162 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
1163 auto *VlaExpr = DILocalVariable::get(Context, Scope, "vla_expr", File, 8,
1164 Type, 2, Flags, 8);
1165
1166 auto *N = DISubrange::get(Context, VlaExpr, 0);
1167 auto Count = N->getCount();
1168 ASSERT_TRUE(Count);
1169 ASSERT_TRUE(Count.is<DIVariable*>());
1170 EXPECT_EQ(VlaExpr, Count.get<DIVariable*>());
1171 ASSERT_TRUE(isa<DIVariable>(N->getRawCountNode()));
1172 EXPECT_EQ(0, N->getLowerBound());
1173 EXPECT_EQ("vla_expr", Count.get<DIVariable*>()->getName());
1174 EXPECT_EQ(N, DISubrange::get(Context, VlaExpr, 0));
1175}
1176
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001177typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001178
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001179TEST_F(DIEnumeratorTest, get) {
Momchil Velikov0c69bf42018-02-12 16:10:09 +00001180 auto *N = DIEnumerator::get(Context, 7, false, "name");
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001181 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
1182 EXPECT_EQ(7, N->getValue());
Momchil Velikov9e8a4252018-02-14 13:11:56 +00001183 EXPECT_FALSE(N->isUnsigned());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001184 EXPECT_EQ("name", N->getName());
Momchil Velikov0c69bf42018-02-12 16:10:09 +00001185 EXPECT_EQ(N, DIEnumerator::get(Context, 7, false, "name"));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001186
Momchil Velikov0c69bf42018-02-12 16:10:09 +00001187 EXPECT_NE(N, DIEnumerator::get(Context, 7, true, "name"));
1188 EXPECT_NE(N, DIEnumerator::get(Context, 8, false, "name"));
1189 EXPECT_NE(N, DIEnumerator::get(Context, 7, false, "nam"));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001190
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001191 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001192 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001193}
1194
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001195typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001196
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001197TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001198 auto *N =
Adrian Prantlc4d19092018-08-14 19:35:34 +00001199 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7,
1200 DINode::FlagZero);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001201 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
1202 EXPECT_EQ("special", N->getName());
1203 EXPECT_EQ(33u, N->getSizeInBits());
1204 EXPECT_EQ(26u, N->getAlignInBits());
1205 EXPECT_EQ(7u, N->getEncoding());
1206 EXPECT_EQ(0u, N->getLine());
Adrian Prantlc4d19092018-08-14 19:35:34 +00001207 EXPECT_EQ(DINode::FlagZero, N->getFlags());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001208 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Adrian Prantlc4d19092018-08-14 19:35:34 +00001209 26, 7, DINode::FlagZero));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001210
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001211 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Adrian Prantlc4d19092018-08-14 19:35:34 +00001212 "special", 33, 26, 7, DINode::FlagZero));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001213 EXPECT_NE(N,
Adrian Prantlc4d19092018-08-14 19:35:34 +00001214 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7,
1215 DINode::FlagZero));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001216 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Adrian Prantlc4d19092018-08-14 19:35:34 +00001217 26, 7, DINode::FlagZero));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001218 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Adrian Prantlc4d19092018-08-14 19:35:34 +00001219 25, 7, DINode::FlagZero));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001220 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Adrian Prantlc4d19092018-08-14 19:35:34 +00001221 26, 6, DINode::FlagZero));
1222 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
1223 26, 7, DINode::FlagBigEndian));
1224 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
1225 26, 7, DINode::FlagLittleEndian));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001226
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001227 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001228 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001229}
1230
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001231TEST_F(DIBasicTypeTest, getWithLargeValues) {
1232 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Adrian Prantlc4d19092018-08-14 19:35:34 +00001233 UINT64_MAX, UINT32_MAX - 1, 7, DINode::FlagZero);
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001234 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk58be60c2016-10-18 14:31:22 +00001235 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001236}
1237
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001238TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001239 auto *N =
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001240 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001241 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
1242 EXPECT_EQ("unspecified", N->getName());
1243 EXPECT_EQ(0u, N->getSizeInBits());
1244 EXPECT_EQ(0u, N->getAlignInBits());
1245 EXPECT_EQ(0u, N->getEncoding());
1246 EXPECT_EQ(0u, N->getLine());
Adrian Prantlc4d19092018-08-14 19:35:34 +00001247 EXPECT_EQ(DINode::FlagZero, N->getFlags());
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001248}
1249
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001250typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001251
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001252TEST_F(DITypeTest, clone) {
1253 // Check that DIType has a specialized clone that returns TempDIType.
1254 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Adrian Prantlc4d19092018-08-14 19:35:34 +00001255 dwarf::DW_ATE_signed, DINode::FlagZero);
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001256
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001257 TempDIType Temp = N->clone();
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001258 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1259}
1260
Roman Tereshina82bdae2018-06-01 23:15:09 +00001261TEST_F(DITypeTest, cloneWithFlags) {
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001262 // void (void)
1263 Metadata *TypesOps[] = {nullptr};
1264 Metadata *Types = MDTuple::get(Context, TypesOps);
1265
Leny Kholodov01dd3d92016-09-06 17:03:02 +00001266 DIType *D =
1267 DISubroutineType::getDistinct(Context, DINode::FlagZero, 0, Types);
Leny Kholodovd9478f82016-09-06 10:46:28 +00001268 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Roman Tereshina82bdae2018-06-01 23:15:09 +00001269 TempDIType D2 = D->cloneWithFlags(DINode::FlagRValueReference);
1270 EXPECT_EQ(DINode::FlagRValueReference, D2->getFlags());
Leny Kholodovd9478f82016-09-06 10:46:28 +00001271 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001272
Leny Kholodov01dd3d92016-09-06 17:03:02 +00001273 TempDIType T =
1274 DISubroutineType::getTemporary(Context, DINode::FlagZero, 0, Types);
Leny Kholodovd9478f82016-09-06 10:46:28 +00001275 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Roman Tereshina82bdae2018-06-01 23:15:09 +00001276 TempDIType T2 = T->cloneWithFlags(DINode::FlagRValueReference);
1277 EXPECT_EQ(DINode::FlagRValueReference, T2->getFlags());
Leny Kholodovd9478f82016-09-06 10:46:28 +00001278 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Duncan P. N. Exon Smith853cd262015-03-03 16:45:34 +00001279}
1280
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001281typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001282
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001283TEST_F(DIDerivedTypeTest, get) {
1284 DIFile *File = getFile();
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001285 DIScope *Scope = getSubprogram();
1286 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001287 MDTuple *ExtraData = getTuple();
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001288 unsigned DWARFAddressSpace = 8;
Leny Kholodovd9478f82016-09-06 10:46:28 +00001289 DINode::DIFlags Flags5 = static_cast<DINode::DIFlags>(5);
1290 DINode::DIFlags Flags4 = static_cast<DINode::DIFlags>(4);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001291
Leny Kholodov01dd3d92016-09-06 17:03:02 +00001292 auto *N =
1293 DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", File,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001294 1, Scope, BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
1295 ExtraData);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001296 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
1297 EXPECT_EQ("something", N->getName());
1298 EXPECT_EQ(File, N->getFile());
1299 EXPECT_EQ(1u, N->getLine());
1300 EXPECT_EQ(Scope, N->getScope());
1301 EXPECT_EQ(BaseType, N->getBaseType());
1302 EXPECT_EQ(2u, N->getSizeInBits());
1303 EXPECT_EQ(3u, N->getAlignInBits());
1304 EXPECT_EQ(4u, N->getOffsetInBits());
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001305 EXPECT_EQ(DWARFAddressSpace, N->getDWARFAddressSpace().getValue());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001306 EXPECT_EQ(5u, N->getFlags());
1307 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001308 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001309 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001310 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001311
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001312 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001313 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001314 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001315 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001316 File, 1, Scope, BaseType, 2, 3,
1317 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001318 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001319 "something", getFile(), 1, Scope, BaseType, 2,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001320 3, 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001321 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001322 "something", File, 2, Scope, BaseType, 2, 3,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001323 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001324 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001325 "something", File, 1, getSubprogram(),
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001326 BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
1327 ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001328 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001329 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001330 Scope, getBasicType("basic2"), 2, 3, 4, DWARFAddressSpace,
1331 Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001332 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001333 "something", File, 1, Scope, BaseType, 3, 3,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001334 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001335 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001336 "something", File, 1, Scope, BaseType, 2, 2,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001337 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001338 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001339 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001340 5, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001341 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001342 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001343 4, DWARFAddressSpace + 1, Flags5, ExtraData));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001344 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001345 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001346 4, DWARFAddressSpace, Flags4, ExtraData));
1347 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1348 "something", File, 1, Scope, BaseType, 2, 3,
1349 4, DWARFAddressSpace, Flags5, getTuple()));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001350
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001351 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001352 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001353}
1354
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001355TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1356 DIFile *File = getFile();
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001357 DIScope *Scope = getSubprogram();
1358 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001359 MDTuple *ExtraData = getTuple();
Leny Kholodovd9478f82016-09-06 10:46:28 +00001360 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001361
Leny Kholodov01dd3d92016-09-06 17:03:02 +00001362 auto *N = DIDerivedType::get(
1363 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, Scope,
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001364 BaseType, UINT64_MAX, UINT32_MAX - 1, UINT64_MAX - 2, UINT32_MAX - 3,
1365 Flags, ExtraData);
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001366 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk58be60c2016-10-18 14:31:22 +00001367 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001368 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001369 EXPECT_EQ(UINT32_MAX - 3, N->getDWARFAddressSpace().getValue());
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001370}
1371
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001372typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001373
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001374TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001375 unsigned Tag = dwarf::DW_TAG_structure_type;
1376 StringRef Name = "some name";
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001377 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001378 unsigned Line = 1;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001379 DIScope *Scope = getSubprogram();
1380 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001381 uint64_t SizeInBits = 2;
Victor Leschuk58be60c2016-10-18 14:31:22 +00001382 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001383 uint64_t OffsetInBits = 4;
Leny Kholodovd9478f82016-09-06 10:46:28 +00001384 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001385 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001386 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001387 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001388 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001389 StringRef Identifier = "some id";
1390
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001391 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001392 BaseType, SizeInBits, AlignInBits,
1393 OffsetInBits, Flags, Elements, RuntimeLang,
1394 VTableHolder, TemplateParams, Identifier);
1395 EXPECT_EQ(Tag, N->getTag());
1396 EXPECT_EQ(Name, N->getName());
1397 EXPECT_EQ(File, N->getFile());
1398 EXPECT_EQ(Line, N->getLine());
1399 EXPECT_EQ(Scope, N->getScope());
1400 EXPECT_EQ(BaseType, N->getBaseType());
1401 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1402 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1403 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1404 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001405 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001406 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1407 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001408 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001409 EXPECT_EQ(Identifier, N->getIdentifier());
1410
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001411 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001412 BaseType, SizeInBits, AlignInBits,
1413 OffsetInBits, Flags, Elements, RuntimeLang,
1414 VTableHolder, TemplateParams, Identifier));
1415
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001416 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001417 BaseType, SizeInBits, AlignInBits,
1418 OffsetInBits, Flags, Elements, RuntimeLang,
1419 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001420 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001421 BaseType, SizeInBits, AlignInBits,
1422 OffsetInBits, Flags, Elements, RuntimeLang,
1423 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001424 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001425 BaseType, SizeInBits, AlignInBits,
1426 OffsetInBits, Flags, Elements, RuntimeLang,
1427 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001428 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001429 BaseType, SizeInBits, AlignInBits,
1430 OffsetInBits, Flags, Elements, RuntimeLang,
1431 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001432 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001433 Context, Tag, Name, File, Line, getSubprogram(), BaseType,
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001434 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1435 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001436 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smithe009b6f2015-04-06 19:03:45 +00001437 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1438 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1439 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001440 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001441 BaseType, SizeInBits + 1, AlignInBits,
1442 OffsetInBits, Flags, Elements, RuntimeLang,
1443 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001444 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001445 BaseType, SizeInBits, AlignInBits + 1,
1446 OffsetInBits, Flags, Elements, RuntimeLang,
1447 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001448 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001449 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1450 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1451 VTableHolder, TemplateParams, Identifier));
Leny Kholodovd9478f82016-09-06 10:46:28 +00001452 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001453 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001454 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
Leny Kholodovd9478f82016-09-06 10:46:28 +00001455 AlignInBits, OffsetInBits, FlagsPOne, Elements, RuntimeLang,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001456 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001457 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001458 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1459 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1460 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001461 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001462 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1463 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1464 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001465 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001466 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1467 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1468 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001469 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001470 BaseType, SizeInBits, AlignInBits,
1471 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001472 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001473 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001474 BaseType, SizeInBits, AlignInBits,
1475 OffsetInBits, Flags, Elements, RuntimeLang,
1476 VTableHolder, TemplateParams, "other"));
1477
1478 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001479 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1480 BaseType, SizeInBits, AlignInBits,
1481 OffsetInBits, Flags, Elements, RuntimeLang,
1482 VTableHolder, TemplateParams, "")
1483 ->getRawIdentifier());
1484 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1485 BaseType, SizeInBits, AlignInBits,
1486 OffsetInBits, Flags, Elements, RuntimeLang,
1487 VTableHolder, TemplateParams)
1488 ->getRawIdentifier());
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001489
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001490 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001491 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001492}
1493
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001494TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001495 unsigned Tag = dwarf::DW_TAG_structure_type;
1496 StringRef Name = "some name";
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001497 DIFile *File = getFile();
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001498 unsigned Line = 1;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001499 DIScope *Scope = getSubprogram();
1500 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001501 uint64_t SizeInBits = UINT64_MAX;
Victor Leschuk58be60c2016-10-18 14:31:22 +00001502 uint32_t AlignInBits = UINT32_MAX - 1;
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001503 uint64_t OffsetInBits = UINT64_MAX - 2;
Leny Kholodovd9478f82016-09-06 10:46:28 +00001504 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001505 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001506 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001507 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001508 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001509 StringRef Identifier = "some id";
1510
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001511 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001512 BaseType, SizeInBits, AlignInBits,
1513 OffsetInBits, Flags, Elements, RuntimeLang,
1514 VTableHolder, TemplateParams, Identifier);
1515 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1516 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1517 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1518}
1519
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001520TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001521 unsigned Tag = dwarf::DW_TAG_structure_type;
1522 StringRef Name = "some name";
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001523 DIFile *File = getFile();
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001524 unsigned Line = 1;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001525 DIScope *Scope = getSubprogram();
1526 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001527 uint64_t SizeInBits = 2;
Victor Leschuk58be60c2016-10-18 14:31:22 +00001528 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smith8a76ab62015-02-19 23:56:07 +00001529 uint64_t OffsetInBits = 4;
Leny Kholodovd9478f82016-09-06 10:46:28 +00001530 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001531 unsigned RuntimeLang = 6;
1532 StringRef Identifier = "some id";
1533
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001534 auto *N = DICompositeType::get(
1535 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1536 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001537
1538 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001539 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001540 N->replaceElements(Elements);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001541 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001542 N->replaceElements(nullptr);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001543 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001544
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001545 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001546 EXPECT_EQ(nullptr, N->getVTableHolder());
1547 N->replaceVTableHolder(VTableHolder);
1548 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Adrian Prantld6200f62017-11-08 22:04:43 +00001549 // As an extension, the containing type can be anything. This is
1550 // used by Rust to associate vtables with their concrete type.
1551 DIType *BasicType = getBasicType("basic");
1552 N->replaceVTableHolder(BasicType);
1553 EXPECT_EQ(BasicType, N->getVTableHolder());
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001554 N->replaceVTableHolder(nullptr);
1555 EXPECT_EQ(nullptr, N->getVTableHolder());
1556
1557 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001558 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001559 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001560 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001561 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001562 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smith668aca92015-02-18 20:47:52 +00001563}
1564
Adrian Prantl04aa6502018-02-06 23:45:59 +00001565TEST_F(DICompositeTypeTest, variant_part) {
1566 unsigned Tag = dwarf::DW_TAG_variant_part;
1567 StringRef Name = "some name";
1568 DIFile *File = getFile();
1569 unsigned Line = 1;
1570 DIScope *Scope = getSubprogram();
1571 DIType *BaseType = getCompositeType();
1572 uint64_t SizeInBits = 2;
1573 uint32_t AlignInBits = 3;
1574 uint64_t OffsetInBits = 4;
1575 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
1576 unsigned RuntimeLang = 6;
1577 StringRef Identifier = "some id";
1578 DIDerivedType *Discriminator = cast<DIDerivedType>(getDerivedType());
1579 DIDerivedType *Discriminator2 = cast<DIDerivedType>(getDerivedType());
1580
1581 EXPECT_NE(Discriminator, Discriminator2);
1582
1583 auto *N = DICompositeType::get(
1584 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1585 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1586 Discriminator);
1587
1588 // Test the hashing.
1589 auto *Same = DICompositeType::get(
1590 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1591 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1592 Discriminator);
1593 auto *Other = DICompositeType::get(
1594 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1595 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1596 Discriminator2);
1597 auto *NoDisc = DICompositeType::get(
1598 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1599 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1600 nullptr);
1601
1602 EXPECT_EQ(N, Same);
1603 EXPECT_NE(Same, Other);
1604 EXPECT_NE(Same, NoDisc);
1605 EXPECT_NE(Other, NoDisc);
1606
1607 EXPECT_EQ(N->getDiscriminator(), Discriminator);
1608}
1609
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001610typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001611
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001612TEST_F(DISubroutineTypeTest, get) {
Leny Kholodovd9478f82016-09-06 10:46:28 +00001613 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(1);
1614 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001615 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001616
Reid Kleckner3d3aca22016-06-08 20:34:29 +00001617 auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001618 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1619 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001620 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Reid Kleckner3d3aca22016-06-08 20:34:29 +00001621 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001622
Leny Kholodovd9478f82016-09-06 10:46:28 +00001623 EXPECT_NE(N, DISubroutineType::get(Context, FlagsPOne, 0, TypeArray));
Reid Kleckner3d3aca22016-06-08 20:34:29 +00001624 EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple()));
1625
1626 // Test the hashing of calling conventions.
1627 auto *Fast = DISubroutineType::get(
1628 Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray);
1629 auto *Std = DISubroutineType::get(Context, Flags,
1630 dwarf::DW_CC_BORLAND_stdcall, TypeArray);
1631 EXPECT_EQ(Fast,
1632 DISubroutineType::get(Context, Flags,
1633 dwarf::DW_CC_BORLAND_msfastcall, TypeArray));
1634 EXPECT_EQ(Std, DISubroutineType::get(
1635 Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray));
1636
1637 EXPECT_NE(N, Fast);
1638 EXPECT_NE(N, Std);
1639 EXPECT_NE(Fast, Std);
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001640
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001641 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001642 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithaeb422f2015-02-19 23:25:21 +00001643
1644 // Test always-empty operands.
1645 EXPECT_EQ(nullptr, N->getScope());
1646 EXPECT_EQ(nullptr, N->getFile());
1647 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001648}
1649
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001650typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001651
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001652TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001653 StringRef Filename = "file";
1654 StringRef Directory = "dir";
Scott Linder48632522018-02-12 19:45:54 +00001655 DIFile::ChecksumKind CSKind = DIFile::ChecksumKind::CSK_MD5;
1656 StringRef ChecksumString = "000102030405060708090a0b0c0d0e0f";
1657 DIFile::ChecksumInfo<StringRef> Checksum(CSKind, ChecksumString);
Scott Linder5e4b5152018-02-23 23:01:06 +00001658 StringRef Source = "source";
1659 auto *N = DIFile::get(Context, Filename, Directory, Checksum, Source);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001660
1661 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1662 EXPECT_EQ(Filename, N->getFilename());
1663 EXPECT_EQ(Directory, N->getDirectory());
Amjad Aboud4e2e80b2016-12-25 10:12:09 +00001664 EXPECT_EQ(Checksum, N->getChecksum());
Scott Linder5e4b5152018-02-23 23:01:06 +00001665 EXPECT_EQ(Source, N->getSource());
1666 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory, Checksum, Source));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001667
Scott Linder5e4b5152018-02-23 23:01:06 +00001668 EXPECT_NE(N, DIFile::get(Context, "other", Directory, Checksum, Source));
1669 EXPECT_NE(N, DIFile::get(Context, Filename, "other", Checksum, Source));
Scott Linder48632522018-02-12 19:45:54 +00001670 DIFile::ChecksumInfo<StringRef> OtherChecksum(DIFile::ChecksumKind::CSK_SHA1, ChecksumString);
Reid Kleckner3796a452017-09-19 18:14:45 +00001671 EXPECT_NE(
Scott Linder48632522018-02-12 19:45:54 +00001672 N, DIFile::get(Context, Filename, Directory, OtherChecksum));
Scott Linder5e4b5152018-02-23 23:01:06 +00001673 StringRef OtherSource = "other";
1674 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum, OtherSource));
1675 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum));
Amjad Aboud4e2e80b2016-12-25 10:12:09 +00001676 EXPECT_NE(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001677
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001678 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001679 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001680}
1681
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001682TEST_F(DIFileTest, ScopeGetFile) {
1683 // Ensure that DIScope::getFile() returns itself.
1684 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith5bcf1302015-02-28 21:47:02 +00001685 EXPECT_EQ(N, N->getFile());
1686}
1687
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001688typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001689
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001690TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001691 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001692 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001693 StringRef Producer = "some producer";
1694 bool IsOptimized = false;
1695 StringRef Flags = "flag after flag";
1696 unsigned RuntimeVersion = 2;
1697 StringRef SplitDebugFilename = "another/file";
Adrian Prantl39bb84a2016-03-31 23:56:58 +00001698 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001699 MDTuple *EnumTypes = getTuple();
1700 MDTuple *RetainedTypes = getTuple();
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001701 MDTuple *GlobalVariables = getTuple();
1702 MDTuple *ImportedEntities = getTuple();
Adrian Prantl5df84992015-09-22 23:42:47 +00001703 uint64_t DWOId = 0x10000000c0ffee;
Amjad Aboud7db39802015-12-10 12:56:35 +00001704 MDTuple *Macros = getTuple();
Duncan P. N. Exon Smithc61bc482015-08-03 17:26:41 +00001705 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001706 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1707 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Dehao Chenfe462302017-02-01 22:45:09 +00001708 RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true,
David Blaikieecc582a2018-11-13 20:08:10 +00001709 false, DICompileUnit::DebugNameTableKind::Default, false);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001710
1711 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1712 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1713 EXPECT_EQ(File, N->getFile());
1714 EXPECT_EQ(Producer, N->getProducer());
1715 EXPECT_EQ(IsOptimized, N->isOptimized());
1716 EXPECT_EQ(Flags, N->getFlags());
1717 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1718 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1719 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001720 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1721 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001722 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1723 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Amjad Aboud7db39802015-12-10 12:56:35 +00001724 EXPECT_EQ(Macros, N->getMacros().get());
Adrian Prantl849c7602015-05-21 20:37:30 +00001725 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001726
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001727 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smithc61bc482015-08-03 17:26:41 +00001728 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1729 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1730 EXPECT_EQ(File, Temp->getFile());
1731 EXPECT_EQ(Producer, Temp->getProducer());
1732 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1733 EXPECT_EQ(Flags, Temp->getFlags());
1734 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1735 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1736 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1737 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1738 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
Duncan P. N. Exon Smithc61bc482015-08-03 17:26:41 +00001739 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1740 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
Amjad Aboud7db39802015-12-10 12:56:35 +00001741 EXPECT_EQ(Macros, Temp->getMacros().get());
Duncan P. N. Exon Smithc61bc482015-08-03 17:26:41 +00001742 EXPECT_EQ(DWOId, Temp->getDWOId());
1743
1744 auto *TempAddress = Temp.get();
1745 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1746 EXPECT_TRUE(Clone->isDistinct());
1747 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001748}
1749
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001750TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith70ee0382015-02-18 20:36:09 +00001751 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001752 DIFile *File = getFile();
Duncan P. N. Exon Smith70ee0382015-02-18 20:36:09 +00001753 StringRef Producer = "some producer";
1754 bool IsOptimized = false;
1755 StringRef Flags = "flag after flag";
1756 unsigned RuntimeVersion = 2;
1757 StringRef SplitDebugFilename = "another/file";
Adrian Prantl39bb84a2016-03-31 23:56:58 +00001758 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001759 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1760 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1761 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl849c7602015-05-21 20:37:30 +00001762 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smithc61bc482015-08-03 17:26:41 +00001763 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith70ee0382015-02-18 20:36:09 +00001764 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1765 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Peter Collingbourne76221cb2017-09-12 21:50:41 +00001766 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true, false,
David Blaikieecc582a2018-11-13 20:08:10 +00001767 DICompileUnit::DebugNameTableKind::Default, false);
Duncan P. N. Exon Smith70ee0382015-02-18 20:36:09 +00001768
1769 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001770 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith70ee0382015-02-18 20:36:09 +00001771 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001772 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith70ee0382015-02-18 20:36:09 +00001773 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001774 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Amjad Aboud7db39802015-12-10 12:56:35 +00001775
1776 auto *Macros = MDTuple::getDistinct(Context, None);
1777 EXPECT_EQ(nullptr, N->getMacros().get());
1778 N->replaceMacros(Macros);
1779 EXPECT_EQ(Macros, N->getMacros().get());
1780 N->replaceMacros(nullptr);
1781 EXPECT_EQ(nullptr, N->getMacros().get());
Duncan P. N. Exon Smith70ee0382015-02-18 20:36:09 +00001782}
1783
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001784typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001785
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001786TEST_F(DISubprogramTest, get) {
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001787 DIScope *Scope = getCompositeType();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001788 StringRef Name = "name";
1789 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001790 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001791 unsigned Line = 2;
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001792 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001793 bool IsLocalToUnit = false;
1794 bool IsDefinition = true;
1795 unsigned ScopeLine = 3;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00001796 DIType *ContainingType = getCompositeType();
Davide Italiano01497312016-04-13 20:17:42 +00001797 unsigned Virtuality = 2;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001798 unsigned VirtualIndex = 5;
Reid Klecknerbd79db22016-07-01 02:41:21 +00001799 int ThisAdjustment = -3;
Leny Kholodovd9478f82016-09-06 10:46:28 +00001800 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(6);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001801 bool IsOptimized = false;
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +00001802 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001803 DISubprogram *Declaration = getSubprogram();
Shiva Chena8a13bc2018-05-09 02:40:45 +00001804 MDTuple *RetainedNodes = getTuple();
Adrian Prantl1bf62972017-04-26 22:56:44 +00001805 MDTuple *ThrownTypes = getTuple();
Adrian Prantl4eeaa0d2016-04-15 15:57:41 +00001806 DICompileUnit *Unit = getUnit();
Paul Robinsoneaa73532018-11-19 18:29:28 +00001807 DISubprogram::DISPFlags SPFlags =
1808 static_cast<DISubprogram::DISPFlags>(Virtuality);
1809 assert(!IsLocalToUnit && IsDefinition && !IsOptimized &&
1810 "bools and SPFlags have to match");
1811 SPFlags |= DISubprogram::SPFlagDefinition;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001812
Adrian Prantl1bf62972017-04-26 22:56:44 +00001813 auto *N = DISubprogram::get(
Paul Robinsoneaa73532018-11-19 18:29:28 +00001814 Context, Scope, Name, LinkageName, File, Line, Type, ScopeLine,
1815 ContainingType, VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit,
1816 TemplateParams, Declaration, RetainedNodes, ThrownTypes);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001817
1818 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1819 EXPECT_EQ(Scope, N->getScope());
1820 EXPECT_EQ(Name, N->getName());
1821 EXPECT_EQ(LinkageName, N->getLinkageName());
1822 EXPECT_EQ(File, N->getFile());
1823 EXPECT_EQ(Line, N->getLine());
1824 EXPECT_EQ(Type, N->getType());
1825 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1826 EXPECT_EQ(IsDefinition, N->isDefinition());
1827 EXPECT_EQ(ScopeLine, N->getScopeLine());
1828 EXPECT_EQ(ContainingType, N->getContainingType());
1829 EXPECT_EQ(Virtuality, N->getVirtuality());
1830 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
Reid Klecknerbd79db22016-07-01 02:41:21 +00001831 EXPECT_EQ(ThisAdjustment, N->getThisAdjustment());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001832 EXPECT_EQ(Flags, N->getFlags());
1833 EXPECT_EQ(IsOptimized, N->isOptimized());
Adrian Prantl4eeaa0d2016-04-15 15:57:41 +00001834 EXPECT_EQ(Unit, N->getUnit());
Duncan P. N. Exon Smith2e251152015-04-07 16:50:39 +00001835 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001836 EXPECT_EQ(Declaration, N->getDeclaration());
Shiva Chena8a13bc2018-05-09 02:40:45 +00001837 EXPECT_EQ(RetainedNodes, N->getRetainedNodes().get());
Adrian Prantl1bf62972017-04-26 22:56:44 +00001838 EXPECT_EQ(ThrownTypes, N->getThrownTypes().get());
Paul Robinsoneaa73532018-11-19 18:29:28 +00001839 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1840 Type, ScopeLine, ContainingType, VirtualIndex,
1841 ThisAdjustment, Flags, SPFlags, Unit,
1842 TemplateParams, Declaration, RetainedNodes,
1843 ThrownTypes));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001844
Paul Robinsoneaa73532018-11-19 18:29:28 +00001845 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
1846 File, Line, Type, ScopeLine, ContainingType,
1847 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1848 Unit, TemplateParams, Declaration,
1849 RetainedNodes, ThrownTypes));
1850 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
1851 Line, Type, ScopeLine, ContainingType,
1852 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1853 Unit, TemplateParams, Declaration,
1854 RetainedNodes, ThrownTypes));
1855 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
1856 Type, ScopeLine, ContainingType, VirtualIndex,
1857 ThisAdjustment, Flags, SPFlags, Unit,
Shiva Chena8a13bc2018-05-09 02:40:45 +00001858 TemplateParams, Declaration, RetainedNodes,
Adrian Prantl1bf62972017-04-26 22:56:44 +00001859 ThrownTypes));
Paul Robinsoneaa73532018-11-19 18:29:28 +00001860 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
1861 Line, Type, ScopeLine, ContainingType,
1862 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1863 Unit, TemplateParams, Declaration,
1864 RetainedNodes, ThrownTypes));
1865 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
1866 Line + 1, Type, ScopeLine, ContainingType,
1867 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1868 Unit, TemplateParams, Declaration,
1869 RetainedNodes, ThrownTypes));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001870 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Paul Robinsoneaa73532018-11-19 18:29:28 +00001871 getSubroutineType(), ScopeLine, ContainingType,
1872 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1873 Unit, TemplateParams, Declaration,
1874 RetainedNodes, ThrownTypes));
Adrian Prantl1bf62972017-04-26 22:56:44 +00001875 EXPECT_NE(N, DISubprogram::get(
1876 Context, Scope, Name, LinkageName, File, Line, Type,
Paul Robinsoneaa73532018-11-19 18:29:28 +00001877 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1878 Flags, SPFlags ^ DISubprogram::SPFlagLocalToUnit, Unit,
1879 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
1880 EXPECT_NE(N, DISubprogram::get(
1881 Context, Scope, Name, LinkageName, File, Line, Type,
1882 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1883 Flags, SPFlags ^ DISubprogram::SPFlagDefinition, Unit,
1884 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Adrian Prantl4eeaa0d2016-04-15 15:57:41 +00001885 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Paul Robinsoneaa73532018-11-19 18:29:28 +00001886 Type, ScopeLine + 1, ContainingType,
1887 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1888 Unit, TemplateParams, Declaration,
1889 RetainedNodes, ThrownTypes));
1890 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1891 Type, ScopeLine, getCompositeType(),
1892 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1893 Unit, TemplateParams, Declaration,
1894 RetainedNodes, ThrownTypes));
1895 EXPECT_NE(N, DISubprogram::get(
1896 Context, Scope, Name, LinkageName, File, Line, Type,
1897 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1898 Flags, SPFlags ^ DISubprogram::SPFlagVirtual, Unit,
1899 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
1900 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1901 Type, ScopeLine, ContainingType,
1902 VirtualIndex + 1, ThisAdjustment, Flags,
1903 SPFlags, Unit, TemplateParams, Declaration,
1904 RetainedNodes, ThrownTypes));
1905 EXPECT_NE(N, DISubprogram::get(
1906 Context, Scope, Name, LinkageName, File, Line, Type,
1907 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1908 Flags, SPFlags ^ DISubprogram::SPFlagOptimized, Unit,
1909 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
1910 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1911 Type, ScopeLine, ContainingType, VirtualIndex,
1912 ThisAdjustment, Flags, SPFlags, nullptr,
1913 TemplateParams, Declaration, RetainedNodes,
1914 ThrownTypes));
1915 EXPECT_NE(N,
1916 DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1917 Type, ScopeLine, ContainingType, VirtualIndex,
1918 ThisAdjustment, Flags, SPFlags, Unit, getTuple(),
1919 Declaration, RetainedNodes, ThrownTypes));
1920 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1921 Type, ScopeLine, ContainingType, VirtualIndex,
1922 ThisAdjustment, Flags, SPFlags, Unit,
Shiva Chena8a13bc2018-05-09 02:40:45 +00001923 TemplateParams, getSubprogram(), RetainedNodes,
Adrian Prantl1bf62972017-04-26 22:56:44 +00001924 ThrownTypes));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001925 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Paul Robinsoneaa73532018-11-19 18:29:28 +00001926 Type, ScopeLine, ContainingType, VirtualIndex,
1927 ThisAdjustment, Flags, SPFlags, Unit,
Reid Klecknerbd79db22016-07-01 02:41:21 +00001928 TemplateParams, Declaration, getTuple()));
Paul Robinsoneaa73532018-11-19 18:29:28 +00001929 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1930 Type, ScopeLine, ContainingType, VirtualIndex,
1931 ThisAdjustment, Flags, SPFlags, Unit,
1932 TemplateParams, Declaration, RetainedNodes,
1933 getTuple()));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001934
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001935 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001936 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001937}
1938
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001939typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001940
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001941TEST_F(DILexicalBlockTest, get) {
1942 DILocalScope *Scope = getSubprogram();
1943 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001944 unsigned Line = 5;
1945 unsigned Column = 8;
1946
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001947 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001948
1949 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1950 EXPECT_EQ(Scope, N->getScope());
1951 EXPECT_EQ(File, N->getFile());
1952 EXPECT_EQ(Line, N->getLine());
1953 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001954 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001955
Duncan P. N. Exon Smithd3ec0ca2015-03-30 16:37:48 +00001956 EXPECT_NE(N,
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001957 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1958 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1959 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1960 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001961
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001962 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00001963 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001964}
1965
Duncan P. N. Exon Smith091adc72015-08-28 22:58:50 +00001966TEST_F(DILexicalBlockTest, Overflow) {
1967 DISubprogram *SP = getSubprogram();
1968 DIFile *F = getFile();
1969 {
1970 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1971 EXPECT_EQ(2u, LB->getLine());
1972 EXPECT_EQ(7u, LB->getColumn());
1973 }
1974 unsigned U16 = 1u << 16;
1975 {
1976 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1977 EXPECT_EQ(UINT32_MAX, LB->getLine());
1978 EXPECT_EQ(U16 - 1, LB->getColumn());
1979 }
1980 {
1981 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1982 EXPECT_EQ(UINT32_MAX, LB->getLine());
1983 EXPECT_EQ(0u, LB->getColumn());
1984 }
1985 {
1986 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1987 EXPECT_EQ(UINT32_MAX, LB->getLine());
1988 EXPECT_EQ(0u, LB->getColumn());
1989 }
1990}
1991
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001992typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001993
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001994TEST_F(DILexicalBlockFileTest, get) {
1995 DILocalScope *Scope = getSubprogram();
1996 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001997 unsigned Discriminator = 5;
1998
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001999 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002000
2001 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
2002 EXPECT_EQ(Scope, N->getScope());
2003 EXPECT_EQ(File, N->getFile());
2004 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002005 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002006
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002007 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smithd3ec0ca2015-03-30 16:37:48 +00002008 Discriminator));
2009 EXPECT_NE(N,
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002010 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002011 EXPECT_NE(N,
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002012 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002013
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002014 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002015 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002016}
2017
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002018typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002019
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002020TEST_F(DINamespaceTest, get) {
2021 DIScope *Scope = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002022 StringRef Name = "namespace";
Adrian Prantl60a7c432016-11-03 19:42:02 +00002023 bool ExportSymbols = true;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002024
Adrian Prantl841400b2017-04-28 22:25:46 +00002025 auto *N = DINamespace::get(Context, Scope, Name, ExportSymbols);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002026
2027 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
2028 EXPECT_EQ(Scope, N->getScope());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002029 EXPECT_EQ(Name, N->getName());
Adrian Prantl841400b2017-04-28 22:25:46 +00002030 EXPECT_EQ(N, DINamespace::get(Context, Scope, Name, ExportSymbols));
2031 EXPECT_NE(N, DINamespace::get(Context, getFile(), Name, ExportSymbols));
2032 EXPECT_NE(N, DINamespace::get(Context, Scope, "other", ExportSymbols));
2033 EXPECT_NE(N, DINamespace::get(Context, Scope, Name, !ExportSymbols));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002034
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002035 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002036 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002037}
2038
Adrian Prantl71776472015-06-29 23:03:47 +00002039typedef MetadataTest DIModuleTest;
2040
2041TEST_F(DIModuleTest, get) {
2042 DIScope *Scope = getFile();
2043 StringRef Name = "module";
2044 StringRef ConfigMacro = "-DNDEBUG";
2045 StringRef Includes = "-I.";
2046 StringRef Sysroot = "/";
2047
2048 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
2049
2050 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
2051 EXPECT_EQ(Scope, N->getScope());
2052 EXPECT_EQ(Name, N->getName());
2053 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
2054 EXPECT_EQ(Includes, N->getIncludePath());
2055 EXPECT_EQ(Sysroot, N->getISysRoot());
2056 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
2057 ConfigMacro, Includes, Sysroot));
2058 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
2059 ConfigMacro, Includes, Sysroot));
2060 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
2061 ConfigMacro, Includes, Sysroot));
2062 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
2063 "other", Includes, Sysroot));
2064 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
2065 ConfigMacro, "other", Sysroot));
2066 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
2067 ConfigMacro, Includes, "other"));
2068
2069 TempDIModule Temp = N->clone();
2070 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2071}
2072
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002073typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002074
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002075TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002076 StringRef Name = "template";
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00002077 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002078
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002079 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002080
2081 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002082 EXPECT_EQ(Name, N->getName());
2083 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002084 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002085
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002086 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002087 EXPECT_NE(N,
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002088 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002089
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002090 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002091 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002092}
2093
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002094typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002095
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002096TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002097 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002098 StringRef Name = "template";
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00002099 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002100 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002101
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002102 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002103 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002104 EXPECT_EQ(Name, N->getName());
2105 EXPECT_EQ(Type, N->getType());
2106 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002107 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002108
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002109 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smitheac950e2015-02-19 00:37:21 +00002110 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
2111 Type, Value));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002112 EXPECT_NE(N,
2113 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
2114 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002115 getBasicType("other"), Value));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002116 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002117 getConstantAsMetadata()));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002118
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002119 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002120 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002121}
2122
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002123typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002124
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002125TEST_F(DIGlobalVariableTest, get) {
2126 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002127 StringRef Name = "name";
2128 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002129 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002130 unsigned Line = 5;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00002131 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002132 bool IsLocalToUnit = false;
2133 bool IsDefinition = true;
Matthew Vossfff44e62018-10-03 18:44:53 +00002134 MDTuple *templateParams = getTuple();
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002135 DIDerivedType *StaticDataMemberDeclaration =
2136 cast<DIDerivedType>(getDerivedType());
Matthew Vossfff44e62018-10-03 18:44:53 +00002137
Victor Leschuk7614f6d2016-10-26 21:32:29 +00002138 uint32_t AlignInBits = 8;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002139
Matthew Vossfff44e62018-10-03 18:44:53 +00002140 auto *N = DIGlobalVariable::get(
2141 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2142 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
2143
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002144 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
2145 EXPECT_EQ(Scope, N->getScope());
2146 EXPECT_EQ(Name, N->getName());
2147 EXPECT_EQ(LinkageName, N->getLinkageName());
2148 EXPECT_EQ(File, N->getFile());
2149 EXPECT_EQ(Line, N->getLine());
2150 EXPECT_EQ(Type, N->getType());
2151 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
2152 EXPECT_EQ(IsDefinition, N->isDefinition());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002153 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Matthew Vossfff44e62018-10-03 18:44:53 +00002154 EXPECT_EQ(templateParams, N->getTemplateParams());
Victor Leschuke69c4592016-10-20 00:13:12 +00002155 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002156 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002157 Line, Type, IsLocalToUnit, IsDefinition,
Matthew Vossfff44e62018-10-03 18:44:53 +00002158 StaticDataMemberDeclaration,
2159 templateParams, AlignInBits));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002160
Matthew Vossfff44e62018-10-03 18:44:53 +00002161 EXPECT_NE(N, DIGlobalVariable::get(
2162 Context, getSubprogram(), Name, LinkageName, File, Line,
2163 Type, IsLocalToUnit, IsDefinition,
2164 StaticDataMemberDeclaration, templateParams, AlignInBits));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002165 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002166 Line, Type, IsLocalToUnit, IsDefinition,
Matthew Vossfff44e62018-10-03 18:44:53 +00002167 StaticDataMemberDeclaration,
2168 templateParams, AlignInBits));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002169 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Adrian Prantl7b500b42016-12-20 02:09:43 +00002170 Type, IsLocalToUnit, IsDefinition,
Matthew Vossfff44e62018-10-03 18:44:53 +00002171 StaticDataMemberDeclaration,
2172 templateParams, AlignInBits));
2173 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName,
2174 getFile(), Line, Type, IsLocalToUnit,
2175 IsDefinition, StaticDataMemberDeclaration,
2176 templateParams, AlignInBits));
2177 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2178 Line + 1, Type, IsLocalToUnit,
2179 IsDefinition, StaticDataMemberDeclaration,
2180 templateParams, AlignInBits));
2181 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2182 Line, getDerivedType(), IsLocalToUnit,
2183 IsDefinition, StaticDataMemberDeclaration,
2184 templateParams, AlignInBits));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002185 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002186 Line, Type, !IsLocalToUnit, IsDefinition,
Matthew Vossfff44e62018-10-03 18:44:53 +00002187 StaticDataMemberDeclaration,
2188 templateParams, AlignInBits));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002189 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002190 Line, Type, IsLocalToUnit, !IsDefinition,
Matthew Vossfff44e62018-10-03 18:44:53 +00002191 StaticDataMemberDeclaration,
2192 templateParams, AlignInBits));
Adrian Prantl7b500b42016-12-20 02:09:43 +00002193 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2194 Line, Type, IsLocalToUnit, IsDefinition,
2195 cast<DIDerivedType>(getDerivedType()),
Matthew Vossfff44e62018-10-03 18:44:53 +00002196 templateParams, AlignInBits));
2197 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2198 Line, Type, IsLocalToUnit, IsDefinition,
2199 StaticDataMemberDeclaration, nullptr,
Victor Leschuke69c4592016-10-20 00:13:12 +00002200 AlignInBits));
Peter Collingbourne5420de32016-09-13 01:12:59 +00002201 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2202 Line, Type, IsLocalToUnit, IsDefinition,
Adrian Prantl7b500b42016-12-20 02:09:43 +00002203 StaticDataMemberDeclaration,
Matthew Vossfff44e62018-10-03 18:44:53 +00002204 templateParams, (AlignInBits << 1)));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002205
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002206 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002207 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002208}
2209
Adrian Prantl7b500b42016-12-20 02:09:43 +00002210typedef MetadataTest DIGlobalVariableExpressionTest;
2211
2212TEST_F(DIGlobalVariableExpressionTest, get) {
2213 DIScope *Scope = getSubprogram();
2214 StringRef Name = "name";
2215 StringRef LinkageName = "linkage";
2216 DIFile *File = getFile();
2217 unsigned Line = 5;
2218 DIType *Type = getDerivedType();
2219 bool IsLocalToUnit = false;
2220 bool IsDefinition = true;
Matthew Vossfff44e62018-10-03 18:44:53 +00002221 MDTuple *templateParams = getTuple();
Adrian Prantl7b500b42016-12-20 02:09:43 +00002222 auto *Expr = DIExpression::get(Context, {1, 2});
2223 auto *Expr2 = DIExpression::get(Context, {1, 2, 3});
2224 DIDerivedType *StaticDataMemberDeclaration =
2225 cast<DIDerivedType>(getDerivedType());
2226 uint32_t AlignInBits = 8;
2227
Matthew Vossfff44e62018-10-03 18:44:53 +00002228 auto *Var = DIGlobalVariable::get(
2229 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2230 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
2231 auto *Var2 = DIGlobalVariable::get(
2232 Context, Scope, "other", LinkageName, File, Line, Type, IsLocalToUnit,
2233 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
Adrian Prantl7b500b42016-12-20 02:09:43 +00002234 auto *N = DIGlobalVariableExpression::get(Context, Var, Expr);
2235
2236 EXPECT_EQ(Var, N->getVariable());
2237 EXPECT_EQ(Expr, N->getExpression());
2238 EXPECT_EQ(N, DIGlobalVariableExpression::get(Context, Var, Expr));
2239 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var2, Expr));
2240 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var, Expr2));
2241
2242 TempDIGlobalVariableExpression Temp = N->clone();
2243 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2244}
2245
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002246typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002247
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002248TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002249 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002250 StringRef Name = "name";
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002251 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002252 unsigned Line = 5;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00002253 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002254 unsigned Arg = 6;
Leny Kholodovd9478f82016-09-06 10:46:28 +00002255 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
Victor Leschuk7614f6d2016-10-26 21:32:29 +00002256 uint32_t AlignInBits = 8;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002257
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002258 auto *N =
Victor Leschuke69c4592016-10-20 00:13:12 +00002259 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags,
2260 AlignInBits);
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002261 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002262 EXPECT_EQ(Scope, N->getScope());
2263 EXPECT_EQ(Name, N->getName());
2264 EXPECT_EQ(File, N->getFile());
2265 EXPECT_EQ(Line, N->getLine());
2266 EXPECT_EQ(Type, N->getType());
2267 EXPECT_EQ(Arg, N->getArg());
2268 EXPECT_EQ(Flags, N->getFlags());
Victor Leschuke69c4592016-10-20 00:13:12 +00002269 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002270 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
Victor Leschuke69c4592016-10-20 00:13:12 +00002271 Flags, AlignInBits));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002272
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002273 EXPECT_FALSE(
Victor Leschuke69c4592016-10-20 00:13:12 +00002274 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags,
2275 AlignInBits)->isParameter());
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002276 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Victor Leschuke69c4592016-10-20 00:13:12 +00002277 Type, Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002278 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
Victor Leschuke69c4592016-10-20 00:13:12 +00002279 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002280 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
Victor Leschuke69c4592016-10-20 00:13:12 +00002281 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002282 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
Victor Leschuke69c4592016-10-20 00:13:12 +00002283 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002284 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Victor Leschuke69c4592016-10-20 00:13:12 +00002285 getDerivedType(), Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002286 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Victor Leschuke69c4592016-10-20 00:13:12 +00002287 Arg + 1, Flags, AlignInBits));
2288 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
2289 Arg, Flags, (AlignInBits << 1)));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002290
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002291 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002292 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002293}
2294
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002295TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002296 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuke69c4592016-10-20 00:13:12 +00002297 0, nullptr, 255, DINode::FlagZero, 0)
Duncan P. N. Exon Smithfef48362015-04-28 01:07:33 +00002298 ->getArg());
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002299 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuke69c4592016-10-20 00:13:12 +00002300 0, nullptr, 256, DINode::FlagZero, 0)
Duncan P. N. Exon Smithfef48362015-04-28 01:07:33 +00002301 ->getArg());
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002302 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuke69c4592016-10-20 00:13:12 +00002303 0, nullptr, 257, DINode::FlagZero, 0)
Duncan P. N. Exon Smithfef48362015-04-28 01:07:33 +00002304 ->getArg());
2305 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002306 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuke69c4592016-10-20 00:13:12 +00002307 0, nullptr, Max, DINode::FlagZero, 0)
Duncan P. N. Exon Smithfef48362015-04-28 01:07:33 +00002308 ->getArg());
2309}
2310
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002311typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002312
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002313TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002314 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002315 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002316 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002317 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdb7dea02015-02-10 01:36:46 +00002318
2319 EXPECT_EQ(5u, N->getNumElements());
2320 EXPECT_EQ(2u, N->getElement(0));
2321 EXPECT_EQ(6u, N->getElement(1));
2322 EXPECT_EQ(9u, N->getElement(2));
2323 EXPECT_EQ(78u, N->getElement(3));
2324 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002325
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002326 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002327 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Adrian Prantl0375b232017-12-08 21:58:18 +00002328
2329 // Test DIExpression::prepend().
2330 uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
2331 auto *N0 = DIExpression::get(Context, Elts0);
Vedant Kumar081d2112018-07-26 20:56:53 +00002332 auto *N0WithPrependedOps = DIExpression::prepend(N0, true, 64, true, true);
Adrian Prantl0375b232017-12-08 21:58:18 +00002333 uint64_t Elts1[] = {dwarf::DW_OP_deref,
2334 dwarf::DW_OP_plus_uconst, 64,
2335 dwarf::DW_OP_deref,
2336 dwarf::DW_OP_stack_value,
2337 dwarf::DW_OP_LLVM_fragment, 0, 32};
2338 auto *N1 = DIExpression::get(Context, Elts1);
Vedant Kumar081d2112018-07-26 20:56:53 +00002339 EXPECT_EQ(N0WithPrependedOps, N1);
2340
2341 // Test DIExpression::append().
2342 uint64_t Elts2[] = {dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 64,
2343 dwarf::DW_OP_deref, dwarf::DW_OP_stack_value};
2344 auto *N2 = DIExpression::append(N0, Elts2);
2345 EXPECT_EQ(N0WithPrependedOps, N2);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002346}
2347
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002348TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith74734852015-02-13 01:07:46 +00002349#define EXPECT_VALID(...) \
2350 do { \
2351 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002352 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith74734852015-02-13 01:07:46 +00002353 } while (false)
2354#define EXPECT_INVALID(...) \
2355 do { \
2356 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002357 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith74734852015-02-13 01:07:46 +00002358 } while (false)
2359
2360 // Empty expression should be valid.
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002361 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith74734852015-02-13 01:07:46 +00002362
2363 // Valid constructions.
Florian Hahn10ccfa62017-06-13 16:54:44 +00002364 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6);
Florian Hahn50963b32017-06-14 13:14:38 +00002365 EXPECT_VALID(dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus);
Duncan P. N. Exon Smith74734852015-02-13 01:07:46 +00002366 EXPECT_VALID(dwarf::DW_OP_deref);
Adrian Prantl460dd602016-12-05 18:04:47 +00002367 EXPECT_VALID(dwarf::DW_OP_LLVM_fragment, 3, 7);
Florian Hahn50963b32017-06-14 13:14:38 +00002368 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref);
2369 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6);
Adrian Prantl460dd602016-12-05 18:04:47 +00002370 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_LLVM_fragment, 3, 7);
Florian Hahn50963b32017-06-14 13:14:38 +00002371 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6,
Adrian Prantl460dd602016-12-05 18:04:47 +00002372 dwarf::DW_OP_LLVM_fragment, 3, 7);
Duncan P. N. Exon Smith74734852015-02-13 01:07:46 +00002373
2374 // Invalid constructions.
2375 EXPECT_INVALID(~0u);
Florian Hahn50963b32017-06-14 13:14:38 +00002376 EXPECT_INVALID(dwarf::DW_OP_plus, 0);
Florian Hahn10ccfa62017-06-13 16:54:44 +00002377 EXPECT_INVALID(dwarf::DW_OP_plus_uconst);
Adrian Prantl460dd602016-12-05 18:04:47 +00002378 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment);
2379 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3);
Florian Hahn50963b32017-06-14 13:14:38 +00002380 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_plus_uconst, 3);
Adrian Prantl460dd602016-12-05 18:04:47 +00002381 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_deref);
Duncan P. N. Exon Smith74734852015-02-13 01:07:46 +00002382
2383#undef EXPECT_VALID
2384#undef EXPECT_INVALID
2385}
2386
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002387typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002388
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002389TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002390 StringRef Name = "name";
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002391 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002392 unsigned Line = 5;
2393 StringRef GetterName = "getter";
2394 StringRef SetterName = "setter";
2395 unsigned Attributes = 7;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00002396 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002397
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002398 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002399 SetterName, Attributes, Type);
2400
2401 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
2402 EXPECT_EQ(Name, N->getName());
2403 EXPECT_EQ(File, N->getFile());
2404 EXPECT_EQ(Line, N->getLine());
2405 EXPECT_EQ(GetterName, N->getGetterName());
2406 EXPECT_EQ(SetterName, N->getSetterName());
2407 EXPECT_EQ(Attributes, N->getAttributes());
2408 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002409 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002410 SetterName, Attributes, Type));
2411
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002412 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002413 SetterName, Attributes, Type));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002414 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002415 SetterName, Attributes, Type));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002416 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002417 SetterName, Attributes, Type));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002418 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002419 SetterName, Attributes, Type));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002420 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002421 "other", Attributes, Type));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002422 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002423 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002424 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smithe009b6f2015-04-06 19:03:45 +00002425 SetterName, Attributes,
Adrian Prantl0f80e472015-06-15 23:18:03 +00002426 getBasicType("other")));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002427
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002428 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002429 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002430}
2431
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002432typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002433
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002434TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002435 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002436 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +00002437 DINode *Entity = getCompositeType();
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002438 DIFile *File = getFile();
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002439 unsigned Line = 5;
2440 StringRef Name = "name";
2441
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002442 auto *N =
2443 DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name);
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002444
2445 EXPECT_EQ(Tag, N->getTag());
2446 EXPECT_EQ(Scope, N->getScope());
2447 EXPECT_EQ(Entity, N->getEntity());
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002448 EXPECT_EQ(File, N->getFile());
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002449 EXPECT_EQ(Line, N->getLine());
2450 EXPECT_EQ(Name, N->getName());
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002451 EXPECT_EQ(
2452 N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002453
2454 EXPECT_NE(N,
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002455 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002456 Scope, Entity, File, Line, Name));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002457 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002458 File, Line, Name));
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002459 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002460 File, Line, Name));
2461 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, nullptr, Line,
2462 Name));
2463 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File,
2464 Line + 1, Name));
2465 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
2466 "other"));
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002467
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002468 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith15aa8ba2015-02-17 23:10:13 +00002469 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002470}
2471
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002472typedef MetadataTest MetadataAsValueTest;
2473
2474TEST_F(MetadataAsValueTest, MDNode) {
2475 MDNode *N = MDNode::get(Context, None);
2476 auto *V = MetadataAsValue::get(Context, N);
2477 EXPECT_TRUE(V->getType()->isMetadataTy());
2478 EXPECT_EQ(N, V->getMetadata());
2479
2480 auto *V2 = MetadataAsValue::get(Context, N);
2481 EXPECT_EQ(V, V2);
2482}
2483
2484TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2485 MDNode *N = MDNode::get(Context, None);
2486 Metadata *Ops[] = {N};
2487 MDNode *N2 = MDNode::get(Context, Ops);
2488 auto *V = MetadataAsValue::get(Context, N2);
2489 EXPECT_TRUE(V->getType()->isMetadataTy());
2490 EXPECT_EQ(N2, V->getMetadata());
2491
2492 auto *V2 = MetadataAsValue::get(Context, N2);
2493 EXPECT_EQ(V, V2);
2494
2495 auto *V3 = MetadataAsValue::get(Context, N);
2496 EXPECT_TRUE(V3->getType()->isMetadataTy());
2497 EXPECT_NE(V, V3);
2498 EXPECT_EQ(N, V3->getMetadata());
2499}
2500
2501TEST_F(MetadataAsValueTest, MDNodeConstant) {
2502 auto *C = ConstantInt::getTrue(Context);
2503 auto *MD = ConstantAsMetadata::get(C);
2504 Metadata *Ops[] = {MD};
2505 auto *N = MDNode::get(Context, Ops);
2506
2507 auto *V = MetadataAsValue::get(Context, MD);
2508 EXPECT_TRUE(V->getType()->isMetadataTy());
2509 EXPECT_EQ(MD, V->getMetadata());
2510
2511 auto *V2 = MetadataAsValue::get(Context, N);
2512 EXPECT_EQ(MD, V2->getMetadata());
2513 EXPECT_EQ(V, V2);
2514}
2515
Duncan P. N. Exon Smith85cbe572014-12-12 19:24:33 +00002516typedef MetadataTest ValueAsMetadataTest;
2517
2518TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2519 Type *Ty = Type::getInt1PtrTy(Context);
2520 std::unique_ptr<GlobalVariable> GV0(
2521 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2522 auto *MD = ValueAsMetadata::get(GV0.get());
2523 EXPECT_TRUE(MD->getValue() == GV0.get());
2524 ASSERT_TRUE(GV0->use_empty());
2525
2526 std::unique_ptr<GlobalVariable> GV1(
2527 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2528 GV0->replaceAllUsesWith(GV1.get());
2529 EXPECT_TRUE(MD->getValue() == GV1.get());
2530}
2531
Adrian Prantlc44fb842016-02-08 17:02:34 +00002532TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2533 // Create a constant.
Mehdi Amini8be77072016-04-14 21:59:01 +00002534 ConstantAsMetadata *CI =
2535 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Adrian Prantlc44fb842016-02-08 17:02:34 +00002536
Adrian Prantlc44fb842016-02-08 17:02:34 +00002537 auto Temp1 = MDTuple::getTemporary(Context, None);
Adrian Prantl4ebe0582016-02-08 19:13:15 +00002538 auto Temp2 = MDTuple::getTemporary(Context, {CI});
2539 auto *N = MDTuple::get(Context, {Temp1.get()});
Adrian Prantlc44fb842016-02-08 17:02:34 +00002540
2541 // Test replacing a temporary node with another temporary node.
2542 Temp1->replaceAllUsesWith(Temp2.get());
2543 EXPECT_EQ(N->getOperand(0), Temp2.get());
2544
2545 // Clean up Temp2 for teardown.
2546 Temp2->replaceAllUsesWith(nullptr);
2547}
2548
Duncan P. N. Exon Smith9e4a11f2015-01-14 19:56:10 +00002549TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2550 // Create a constant.
Mehdi Amini8be77072016-04-14 21:59:01 +00002551 ConstantAsMetadata *CI =
2552 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Duncan P. N. Exon Smith9e4a11f2015-01-14 19:56:10 +00002553
2554 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +00002555 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith9e4a11f2015-01-14 19:56:10 +00002556
2557 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +00002558 Metadata *Ops1[] = {CI, CI, Temp.get()};
2559 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith9e4a11f2015-01-14 19:56:10 +00002560
2561 auto *N1 = MDTuple::get(Context, Ops1);
2562 auto *N2 = MDTuple::get(Context, Ops2);
2563 ASSERT_NE(N1, N2);
2564
2565 // Tell metadata that the constant is getting deleted.
2566 //
2567 // After this, N1 will be invalid, so don't touch it.
2568 ValueAsMetadata::handleDeletion(CI->getValue());
2569 EXPECT_EQ(nullptr, N2->getOperand(0));
2570 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smithf9eaea72015-01-19 21:30:18 +00002571 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith9e4a11f2015-01-14 19:56:10 +00002572
2573 // Clean up Temp for teardown.
2574 Temp->replaceAllUsesWith(nullptr);
2575}
2576
Duncan P. N. Exon Smith85cbe572014-12-12 19:24:33 +00002577typedef MetadataTest TrackingMDRefTest;
2578
2579TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2580 Type *Ty = Type::getInt1PtrTy(Context);
2581 std::unique_ptr<GlobalVariable> GV0(
2582 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2583 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2584 EXPECT_TRUE(MD->getValue() == GV0.get());
2585 ASSERT_TRUE(GV0->use_empty());
2586
2587 std::unique_ptr<GlobalVariable> GV1(
2588 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2589 GV0->replaceAllUsesWith(GV1.get());
2590 EXPECT_TRUE(MD->getValue() == GV1.get());
2591
2592 // Reset it, so we don't inadvertently test deletion.
2593 MD.reset();
2594}
2595
2596TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2597 Type *Ty = Type::getInt1PtrTy(Context);
2598 std::unique_ptr<GlobalVariable> GV(
2599 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2600 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2601 EXPECT_TRUE(MD->getValue() == GV.get());
2602 ASSERT_TRUE(GV->use_empty());
2603
2604 GV.reset();
2605 EXPECT_TRUE(!MD);
2606}
2607
Devang Patelfa7c4dc2009-07-30 00:03:41 +00002608TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskine5790a42010-03-04 23:24:19 +00002609 LLVMContext Context;
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002610 ConstantAsMetadata *C =
2611 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2612 ConstantAsMetadata *C2 =
2613 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patelfa7c4dc2009-07-30 00:03:41 +00002614
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002615 Metadata *const V = C;
2616 Metadata *const V2 = C2;
Jay Foadec9186bc2011-04-21 19:59:31 +00002617 MDNode *n = MDNode::get(Context, V);
2618 MDNode *n2 = MDNode::get(Context, V2);
Devang Patelfa7c4dc2009-07-30 00:03:41 +00002619
Jeffrey Yasskin9d0b3dd2010-03-13 01:39:20 +00002620 Module M("MyModule", Context);
Devang Patelfa7c4dc2009-07-30 00:03:41 +00002621 const char *Name = "llvm.NMD1";
Dan Gohman17aa92c2010-07-21 23:38:33 +00002622 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2623 NMD->addOperand(n);
2624 NMD->addOperand(n2);
2625
Chris Lattner0c47a412009-08-23 04:47:35 +00002626 std::string Str;
2627 raw_string_ostream oss(Str);
Devang Patelfa7c4dc2009-07-30 00:03:41 +00002628 NMD->print(oss);
Chris Lattnerab2f2f12009-12-31 02:12:13 +00002629 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patelfa7c4dc2009-07-30 00:03:41 +00002630 oss.str().c_str());
2631}
Duncan P. N. Exon Smitheb79bb62015-04-24 21:51:02 +00002632
2633typedef MetadataTest FunctionAttachmentTest;
2634TEST_F(FunctionAttachmentTest, setMetadata) {
2635 Function *F = getFunction("foo");
2636 ASSERT_FALSE(F->hasMetadata());
2637 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2638 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2639 EXPECT_EQ(nullptr, F->getMetadata("other"));
2640
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002641 DISubprogram *SP1 = getSubprogram();
2642 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smitheb79bb62015-04-24 21:51:02 +00002643 ASSERT_NE(SP1, SP2);
2644
2645 F->setMetadata("dbg", SP1);
2646 EXPECT_TRUE(F->hasMetadata());
2647 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2648 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2649 EXPECT_EQ(nullptr, F->getMetadata("other"));
2650
2651 F->setMetadata(LLVMContext::MD_dbg, SP2);
2652 EXPECT_TRUE(F->hasMetadata());
2653 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2654 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2655 EXPECT_EQ(nullptr, F->getMetadata("other"));
2656
2657 F->setMetadata("dbg", nullptr);
2658 EXPECT_FALSE(F->hasMetadata());
2659 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2660 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2661 EXPECT_EQ(nullptr, F->getMetadata("other"));
2662
2663 MDTuple *T1 = getTuple();
2664 MDTuple *T2 = getTuple();
2665 ASSERT_NE(T1, T2);
2666
2667 F->setMetadata("other1", T1);
2668 F->setMetadata("other2", T2);
2669 EXPECT_TRUE(F->hasMetadata());
2670 EXPECT_EQ(T1, F->getMetadata("other1"));
2671 EXPECT_EQ(T2, F->getMetadata("other2"));
2672 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2673
2674 F->setMetadata("other1", T2);
2675 F->setMetadata("other2", T1);
2676 EXPECT_EQ(T2, F->getMetadata("other1"));
2677 EXPECT_EQ(T1, F->getMetadata("other2"));
2678
2679 F->setMetadata("other1", nullptr);
2680 F->setMetadata("other2", nullptr);
2681 EXPECT_FALSE(F->hasMetadata());
2682 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2683 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2684}
2685
2686TEST_F(FunctionAttachmentTest, getAll) {
2687 Function *F = getFunction("foo");
2688
2689 MDTuple *T1 = getTuple();
2690 MDTuple *T2 = getTuple();
2691 MDTuple *P = getTuple();
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002692 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smitheb79bb62015-04-24 21:51:02 +00002693
2694 F->setMetadata("other1", T2);
2695 F->setMetadata(LLVMContext::MD_dbg, SP);
2696 F->setMetadata("other2", T1);
2697 F->setMetadata(LLVMContext::MD_prof, P);
2698 F->setMetadata("other2", T2);
2699 F->setMetadata("other1", T1);
2700
2701 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2702 F->getAllMetadata(MDs);
2703 ASSERT_EQ(4u, MDs.size());
2704 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2705 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2706 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2707 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2708 EXPECT_EQ(SP, MDs[0].second);
2709 EXPECT_EQ(P, MDs[1].second);
2710 EXPECT_EQ(T1, MDs[2].second);
2711 EXPECT_EQ(T2, MDs[3].second);
2712}
2713
Duncan P. N. Exon Smithef477f32015-04-24 21:53:27 +00002714TEST_F(FunctionAttachmentTest, Verifier) {
2715 Function *F = getFunction("foo");
2716 F->setMetadata("attach", getTuple());
Peter Collingbourne052c9ee2016-06-06 23:21:27 +00002717 F->setIsMaterializable(true);
Peter Collingbourne052c9ee2016-06-06 23:21:27 +00002718
Peter Collingbourne99e2e272016-06-21 23:42:48 +00002719 // Confirm this is materializable.
2720 ASSERT_TRUE(F->isMaterializable());
2721
2722 // Materializable functions cannot have metadata attachments.
2723 EXPECT_TRUE(verifyFunction(*F));
2724
2725 // Function declarations can.
Peter Collingbourne052c9ee2016-06-06 23:21:27 +00002726 F->setIsMaterializable(false);
Peter Collingbourne99e2e272016-06-21 23:42:48 +00002727 EXPECT_FALSE(verifyModule(*F->getParent()));
2728 EXPECT_FALSE(verifyFunction(*F));
2729
2730 // So can definitions.
Duncan P. N. Exon Smithef477f32015-04-24 21:53:27 +00002731 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2732 EXPECT_FALSE(verifyModule(*F->getParent()));
2733 EXPECT_FALSE(verifyFunction(*F));
2734}
2735
Diego Novilloa3bccce2015-05-13 15:13:45 +00002736TEST_F(FunctionAttachmentTest, EntryCount) {
2737 Function *F = getFunction("foo");
2738 EXPECT_FALSE(F->getEntryCount().hasValue());
Easwaran Ramanfd335152018-01-17 22:24:23 +00002739 F->setEntryCount(12304, Function::PCT_Real);
2740 auto Count = F->getEntryCount();
2741 EXPECT_TRUE(Count.hasValue());
2742 EXPECT_EQ(12304u, Count.getCount());
2743 EXPECT_EQ(Function::PCT_Real, Count.getType());
2744
2745 // Repeat the same for synthetic counts.
2746 F = getFunction("bar");
2747 EXPECT_FALSE(F->getEntryCount().hasValue());
2748 F->setEntryCount(123, Function::PCT_Synthetic);
2749 Count = F->getEntryCount();
2750 EXPECT_TRUE(Count.hasValue());
2751 EXPECT_EQ(123u, Count.getCount());
2752 EXPECT_EQ(Function::PCT_Synthetic, Count.getType());
Diego Novilloa3bccce2015-05-13 15:13:45 +00002753}
2754
Duncan P. N. Exon Smith819d0a52015-08-28 21:55:35 +00002755TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2756 Function *F = getFunction("foo");
2757 DISubprogram *SP = getSubprogram();
2758 F->setSubprogram(SP);
2759
2760 // Note that the static_cast confirms that F->getSubprogram() actually
2761 // returns an DISubprogram.
2762 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2763 EXPECT_EQ(SP, F->getMetadata("dbg"));
2764 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2765}
2766
Duncan P. N. Exon Smith20c44662016-04-23 04:15:56 +00002767typedef MetadataTest DistinctMDOperandPlaceholderTest;
2768TEST_F(DistinctMDOperandPlaceholderTest, getID) {
2769 EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
2770}
2771
2772TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
2773 // Set up some placeholders.
2774 DistinctMDOperandPlaceholder PH0(7);
2775 DistinctMDOperandPlaceholder PH1(3);
2776 DistinctMDOperandPlaceholder PH2(0);
2777 Metadata *Ops[] = {&PH0, &PH1, &PH2};
2778 auto *D = MDTuple::getDistinct(Context, Ops);
2779 ASSERT_EQ(&PH0, D->getOperand(0));
2780 ASSERT_EQ(&PH1, D->getOperand(1));
2781 ASSERT_EQ(&PH2, D->getOperand(2));
2782
2783 // Replace them.
2784 auto *N0 = MDTuple::get(Context, None);
2785 auto *N1 = MDTuple::get(Context, N0);
2786 PH0.replaceUseWith(N0);
2787 PH1.replaceUseWith(N1);
2788 PH2.replaceUseWith(nullptr);
2789 EXPECT_EQ(N0, D->getOperand(0));
2790 EXPECT_EQ(N1, D->getOperand(1));
2791 EXPECT_EQ(nullptr, D->getOperand(2));
2792}
2793
2794TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
2795 // There is no user, but we can still call replace.
2796 DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None));
2797}
2798
Reid Klecknerf9841d42017-08-10 21:14:07 +00002799// Test various assertions in metadata tracking. Don't run these tests if gtest
2800// will use SEH to recover from them. Two of these tests get halfway through
2801// inserting metadata into DenseMaps for tracking purposes, and then they
2802// assert, and we attempt to destroy an LLVMContext with broken invariants,
2803// leading to infinite loops.
2804#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) && !defined(GTEST_HAS_SEH)
Duncan P. N. Exon Smith20c44662016-04-23 04:15:56 +00002805TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
2806 // This shouldn't crash.
2807 DistinctMDOperandPlaceholder PH(7);
2808 EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
2809 "Unexpected callback to owner");
2810}
2811
2812TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
2813 // This shouldn't crash.
2814 DistinctMDOperandPlaceholder PH(7);
2815 EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
2816}
2817
2818TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
2819 // This shouldn't crash.
2820 DistinctMDOperandPlaceholder PH(7);
2821 MDTuple::getDistinct(Context, &PH);
2822 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2823 "Placeholders can only be used once");
2824}
2825
2826TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
2827 // TrackingMDRef doesn't install an owner callback, so it can't be detected
2828 // as an invalid use. However, using a placeholder in a TrackingMDRef *and*
2829 // a distinct node isn't possible and we should assert.
2830 //
2831 // (There's no positive test for using TrackingMDRef because it's not a
2832 // useful thing to do.)
2833 {
2834 DistinctMDOperandPlaceholder PH(7);
2835 MDTuple::getDistinct(Context, &PH);
2836 EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
2837 }
2838 {
2839 DistinctMDOperandPlaceholder PH(7);
2840 TrackingMDRef Ref(&PH);
2841 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2842 "Placeholders can only be used once");
2843 }
2844}
2845#endif
2846
Duncan P. N. Exon Smithbb8cd122016-04-23 04:02:39 +00002847} // end namespace