blob: ad96789284875e58e6e77d27d00789371b3b7259 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000012#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000013#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000014#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +000019#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000023#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024using namespace llvm;
25
26namespace {
27
Duncan P. N. Exon Smith2711ca72015-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 Yasskinbd8a7592010-03-04 23:24:19 +000065class MetadataTest : public testing::Test {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000066public:
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000067 MetadataTest() : M("test", Context), Counter(0) {}
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000068
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000069protected:
70 LLVMContext Context;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000071 Module M;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000072 int Counter;
73
Duncan P. N. Exon Smithfee167f2014-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 Smith26489982015-03-26 22:05:04 +000080
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000081 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000082 DISubroutineType *getSubroutineType() {
Leny Kholodov40c62352016-09-06 17:03:02 +000083 return DISubroutineType::getDistinct(Context, DINode::FlagZero, 0,
84 getNode(nullptr));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000085 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000086 DISubprogram *getSubprogram() {
87 return DISubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
Leny Kholodov40c62352016-09-06 17:03:02 +000088 nullptr, false, false, 0, nullptr, 0, 0, 0,
89 DINode::FlagZero, false, nullptr);
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000090 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000091 DIFile *getFile() {
92 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000093 }
Adrian Prantl75819ae2016-04-15 15:57:41 +000094 DICompileUnit *getUnit() {
95 return DICompileUnit::getDistinct(Context, 1, getFile(), "clang", false,
96 "-g", 2, "", DICompileUnit::FullDebug,
97 getTuple(), getTuple(), getTuple(),
David Blaikiea01f2952016-08-24 18:29:49 +000098 getTuple(), getTuple(), 0, true);
Adrian Prantl75819ae2016-04-15 15:57:41 +000099 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000100 DIType *getBasicType(StringRef Name) {
101 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000102 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000103 DIType *getDerivedType() {
Leny Kholodov40c62352016-09-06 17:03:02 +0000104 return DIDerivedType::getDistinct(
105 Context, dwarf::DW_TAG_pointer_type, "", nullptr, 0, nullptr,
106 getBasicType("basictype"), 1, 2, 0, DINode::FlagZero);
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000107 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000108 Constant *getConstant() {
109 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
110 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000111 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000112 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000113 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000114 DIType *getCompositeType() {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000115 return DICompositeType::getDistinct(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000116 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000117 32, 32, 0, DINode::FlagZero, nullptr, 0, nullptr, nullptr, "");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000118 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000119 Function *getFunction(StringRef Name) {
120 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000121 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
122 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000123};
124typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000125
Nick Lewycky49f89192009-04-04 07:22:01 +0000126// Test that construction of MDString with different value produces different
127// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000128TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000129 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000130 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000131 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000132 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000133 EXPECT_NE(s1, s2);
134}
135
136// Test that creation of MDStrings with the same string contents produces the
137// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000138TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000139 char x[4] = { 'a', 'b', 'c', 'X' };
140 char y[4] = { 'a', 'b', 'c', 'Y' };
141
Owen Anderson23587322009-07-31 21:38:10 +0000142 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
143 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000144 EXPECT_EQ(s1, s2);
145}
146
147// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000148TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000149 char *str = new char[13];
150 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000151 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000152 strncpy(str, "aaaaaaaaaaaaa", 13);
153 delete[] str;
154
Chris Lattnerbe354a62009-08-23 04:47:35 +0000155 std::string Str;
156 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000157 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000158 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000159}
160
161// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000162TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000163 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000164 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000165 std::string Str;
166 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000167 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000168 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000169}
170
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000171typedef MetadataTest MDNodeTest;
172
Nick Lewycky49f89192009-04-04 07:22:01 +0000173// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000174TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000175 char x[3] = { 'a', 'b', 'c' };
176 char y[3] = { '1', '2', '3' };
177
Owen Anderson23587322009-07-31 21:38:10 +0000178 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
179 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Mehdi Amini03b42e42016-04-14 21:59:01 +0000180 ConstantAsMetadata *CI =
181 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000182
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000183 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000184 V.push_back(s1);
185 V.push_back(CI);
186 V.push_back(s2);
187
Jay Foad5514afe2011-04-21 19:59:31 +0000188 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000189 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000190 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000191 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000192 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000193 MDNode *n4 = MDNode::getIfExists(Context, V);
194 MDNode *n5 = MDNode::getIfExists(Context, c1);
195 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000196 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000197 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000198 EXPECT_EQ(n4, n1);
199 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000200 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000201
Chris Lattner9b493022009-12-31 01:22:29 +0000202 EXPECT_EQ(3u, n1->getNumOperands());
203 EXPECT_EQ(s1, n1->getOperand(0));
204 EXPECT_EQ(CI, n1->getOperand(1));
205 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000206
Chris Lattner9b493022009-12-31 01:22:29 +0000207 EXPECT_EQ(1u, n2->getNumOperands());
208 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000209}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000210
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000211TEST_F(MDNodeTest, Delete) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000212 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
213 Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000214
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000215 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000216 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000217 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000218
219 EXPECT_EQ(n, wvh);
220
221 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000222}
Devang Patel0924b332009-07-30 00:03:41 +0000223
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000224TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000225 // !0 = !{!0}
226 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000227 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000228 auto Temp = MDNode::getTemporary(Context, None);
229 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000230 MDNode *Self = MDNode::get(Context, Args);
231 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000232 ASSERT_EQ(Self, Self->getOperand(0));
233
234 // Self-references should be distinct, so MDNode::get() should grab a
235 // uniqued node that references Self, not Self.
236 Args[0] = Self;
237 MDNode *Ref1 = MDNode::get(Context, Args);
238 MDNode *Ref2 = MDNode::get(Context, Args);
239 EXPECT_NE(Self, Ref1);
240 EXPECT_EQ(Ref1, Ref2);
241 }
242
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000243 // !0 = !{!0, !{}}
244 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000245 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000246 auto Temp = MDNode::getTemporary(Context, None);
247 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000248 MDNode *Self = MDNode::get(Context, Args);
249 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000250 ASSERT_EQ(Self, Self->getOperand(0));
251
252 // Self-references should be distinct, so MDNode::get() should grab a
253 // uniqued node that references Self, not Self itself.
254 Args[0] = Self;
255 MDNode *Ref1 = MDNode::get(Context, Args);
256 MDNode *Ref2 = MDNode::get(Context, Args);
257 EXPECT_NE(Self, Ref1);
258 EXPECT_EQ(Ref1, Ref2);
259 }
260}
261
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000262TEST_F(MDNodeTest, Print) {
263 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
264 MDString *S = MDString::get(Context, "foo");
265 MDNode *N0 = getNode();
266 MDNode *N1 = getNode(N0);
267 MDNode *N2 = getNode(N0, N1);
268
269 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
270 MDNode *N = MDNode::get(Context, Args);
271
272 std::string Expected;
273 {
274 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000275 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000276 C->printAsOperand(OS);
277 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000278 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000279 OS << ", null";
280 MDNode *Nodes[] = {N0, N1, N2};
281 for (auto *Node : Nodes)
282 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000283 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000284 }
285
286 std::string Actual;
287 {
288 raw_string_ostream OS(Actual);
289 N->print(OS);
290 }
291
292 EXPECT_EQ(Expected, Actual);
293}
294
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000295#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
296 do { \
297 std::string Actual_; \
298 raw_string_ostream OS(Actual_); \
299 PRINT; \
300 OS.flush(); \
301 std::string Expected_(EXPECTED); \
302 EXPECT_EQ(Expected_, Actual_); \
303 } while (false)
304
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000305TEST_F(MDNodeTest, PrintTemporary) {
306 MDNode *Arg = getNode();
307 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
308 MDNode *N = getNode(Temp.get());
309 Module M("test", Context);
310 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
311 NMD->addOperand(N);
312
313 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
314 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
315 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
316
317 // Cleanup.
318 Temp->replaceAllUsesWith(Arg);
319}
320
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000321TEST_F(MDNodeTest, PrintFromModule) {
322 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
323 MDString *S = MDString::get(Context, "foo");
324 MDNode *N0 = getNode();
325 MDNode *N1 = getNode(N0);
326 MDNode *N2 = getNode(N0, N1);
327
328 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
329 MDNode *N = MDNode::get(Context, Args);
330 Module M("test", Context);
331 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
332 NMD->addOperand(N);
333
334 std::string Expected;
335 {
336 raw_string_ostream OS(Expected);
337 OS << "!0 = !{";
338 C->printAsOperand(OS);
339 OS << ", ";
340 S->printAsOperand(OS);
341 OS << ", null, !1, !2, !3}";
342 }
343
344 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
345}
346
347TEST_F(MDNodeTest, PrintFromFunction) {
348 Module M("test", Context);
349 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
350 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
351 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
352 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
353 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
354 auto *R0 = ReturnInst::Create(Context, BB0);
355 auto *R1 = ReturnInst::Create(Context, BB1);
356 auto *N0 = MDNode::getDistinct(Context, None);
357 auto *N1 = MDNode::getDistinct(Context, None);
358 R0->setMetadata("md", N0);
359 R1->setMetadata("md", N1);
360
361 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
362 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000363
364 ModuleSlotTracker MST(&M);
365 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
366 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000367}
368
369TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
370 Module M("test", Context);
371
372 auto *Intrinsic =
373 Function::Create(FunctionType::get(Type::getVoidTy(Context),
374 Type::getMetadataTy(Context), false),
375 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
376
377 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
378 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
379 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
380 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
381 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
382 auto *N0 = MDNode::getDistinct(Context, None);
383 auto *N1 = MDNode::getDistinct(Context, None);
384 auto *MAV0 = MetadataAsValue::get(Context, N0);
385 auto *MAV1 = MetadataAsValue::get(Context, N1);
386 CallInst::Create(Intrinsic, MAV0, "", BB0);
387 CallInst::Create(Intrinsic, MAV1, "", BB1);
388
389 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
390 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
391 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
392 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
393 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
394 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000395
396 ModuleSlotTracker MST(&M);
397 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
398 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
399 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
400 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
401 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
402 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000403}
404#undef EXPECT_PRINTER_EQ
405
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000406TEST_F(MDNodeTest, NullOperand) {
407 // metadata !{}
408 MDNode *Empty = MDNode::get(Context, None);
409
410 // metadata !{metadata !{}}
411 Metadata *Ops[] = {Empty};
412 MDNode *N = MDNode::get(Context, Ops);
413 ASSERT_EQ(Empty, N->getOperand(0));
414
415 // metadata !{metadata !{}} => metadata !{null}
416 N->replaceOperandWith(0, nullptr);
417 ASSERT_EQ(nullptr, N->getOperand(0));
418
419 // metadata !{null}
420 Ops[0] = nullptr;
421 MDNode *NullOp = MDNode::get(Context, Ops);
422 ASSERT_EQ(nullptr, NullOp->getOperand(0));
423 EXPECT_EQ(N, NullOp);
424}
425
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000426TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
427 // !{}
428 MDNode *Empty = MDNode::get(Context, None);
429 ASSERT_TRUE(Empty->isResolved());
430 EXPECT_FALSE(Empty->isDistinct());
431
432 // !{!{}}
433 Metadata *Wrapped1Ops[] = {Empty};
434 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
435 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
436 ASSERT_TRUE(Wrapped1->isResolved());
437 EXPECT_FALSE(Wrapped1->isDistinct());
438
439 // !{!{!{}}}
440 Metadata *Wrapped2Ops[] = {Wrapped1};
441 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
442 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
443 ASSERT_TRUE(Wrapped2->isResolved());
444 EXPECT_FALSE(Wrapped2->isDistinct());
445
446 // !{!{!{}}} => !{!{}}
447 Wrapped2->replaceOperandWith(0, Empty);
448 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
449 EXPECT_TRUE(Wrapped2->isDistinct());
450 EXPECT_FALSE(Wrapped1->isDistinct());
451}
452
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000453TEST_F(MDNodeTest, UniquedOnDeletedOperand) {
454 // temp !{}
455 TempMDTuple T = MDTuple::getTemporary(Context, None);
456
457 // !{temp !{}}
458 Metadata *Ops[] = {T.get()};
459 MDTuple *N = MDTuple::get(Context, Ops);
460
461 // !{temp !{}} => !{null}
462 T.reset();
463 ASSERT_TRUE(N->isUniqued());
464 Metadata *NullOps[] = {nullptr};
465 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
466}
467
468TEST_F(MDNodeTest, DistinctOnDeletedValueOperand) {
469 // i1* @GV
470 Type *Ty = Type::getInt1PtrTy(Context);
471 std::unique_ptr<GlobalVariable> GV(
472 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
473 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
474
475 // !{i1* @GV}
476 Metadata *Ops[] = {Op};
477 MDTuple *N = MDTuple::get(Context, Ops);
478
479 // !{i1* @GV} => !{null}
480 GV.reset();
481 ASSERT_TRUE(N->isDistinct());
482 ASSERT_EQ(nullptr, N->getOperand(0));
483 Metadata *NullOps[] = {nullptr};
484 ASSERT_NE(N, MDTuple::get(Context, NullOps));
485}
486
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000487TEST_F(MDNodeTest, getDistinct) {
488 // !{}
489 MDNode *Empty = MDNode::get(Context, None);
490 ASSERT_TRUE(Empty->isResolved());
491 ASSERT_FALSE(Empty->isDistinct());
492 ASSERT_EQ(Empty, MDNode::get(Context, None));
493
494 // distinct !{}
495 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
496 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
497 EXPECT_TRUE(Distinct1->isResolved());
498 EXPECT_TRUE(Distinct2->isDistinct());
499 EXPECT_NE(Empty, Distinct1);
500 EXPECT_NE(Empty, Distinct2);
501 EXPECT_NE(Distinct1, Distinct2);
502
503 // !{}
504 ASSERT_EQ(Empty, MDNode::get(Context, None));
505}
506
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000507TEST_F(MDNodeTest, isUniqued) {
508 MDNode *U = MDTuple::get(Context, None);
509 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000510 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000511 EXPECT_TRUE(U->isUniqued());
512 EXPECT_FALSE(D->isUniqued());
513 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000514}
515
516TEST_F(MDNodeTest, isDistinct) {
517 MDNode *U = MDTuple::get(Context, None);
518 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000519 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000520 EXPECT_FALSE(U->isDistinct());
521 EXPECT_TRUE(D->isDistinct());
522 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000523}
524
525TEST_F(MDNodeTest, isTemporary) {
526 MDNode *U = MDTuple::get(Context, None);
527 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000528 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000529 EXPECT_FALSE(U->isTemporary());
530 EXPECT_FALSE(D->isTemporary());
531 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000532}
533
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000534TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
535 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000536 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000537 ASSERT_FALSE(Temp->isResolved());
538
539 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000540 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000541 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
542 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000543 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000544
545 // temporary !{} => !{}
546 MDNode *Empty = MDNode::get(Context, None);
547 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000548 EXPECT_EQ(Empty, Distinct->getOperand(0));
549}
550
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000551TEST_F(MDNodeTest, handleChangedOperandRecursion) {
552 // !0 = !{}
553 MDNode *N0 = MDNode::get(Context, None);
554
555 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000556 auto Temp3 = MDTuple::getTemporary(Context, None);
557 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000558 MDNode *N1 = MDNode::get(Context, Ops1);
559
560 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000561 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000562 MDNode *N2 = MDNode::get(Context, Ops2);
563
564 // !3 = !{!2}
565 Metadata *Ops3[] = {N2};
566 MDNode *N3 = MDNode::get(Context, Ops3);
567 Temp3->replaceAllUsesWith(N3);
568
569 // !4 = !{!1}
570 Metadata *Ops4[] = {N1};
571 MDNode *N4 = MDNode::get(Context, Ops4);
572
573 // Confirm that the cycle prevented RAUW from getting dropped.
574 EXPECT_TRUE(N0->isResolved());
575 EXPECT_FALSE(N1->isResolved());
576 EXPECT_FALSE(N2->isResolved());
577 EXPECT_FALSE(N3->isResolved());
578 EXPECT_FALSE(N4->isResolved());
579
580 // Create a couple of distinct nodes to observe what's going on.
581 //
582 // !5 = distinct !{!2}
583 // !6 = distinct !{!3}
584 Metadata *Ops5[] = {N2};
585 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
586 Metadata *Ops6[] = {N3};
587 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
588
589 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
590 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
591 // references !3, this can cause a re-entry of handleChangedOperand() when !3
592 // is not ready for it.
593 //
594 // !2->replaceOperandWith(1, nullptr)
595 // !2: !{!3, !0} => !{!3, null}
596 // !2->replaceAllUsesWith(!1)
597 // !3: !{!2] => !{!1}
598 // !3->replaceAllUsesWith(!4)
599 N2->replaceOperandWith(1, nullptr);
600
601 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
602 // under us. Just check that the other nodes are sane.
603 //
604 // !1 = !{!4, null}
605 // !4 = !{!1}
606 // !5 = distinct !{!1}
607 // !6 = distinct !{!4}
608 EXPECT_EQ(N4, N1->getOperand(0));
609 EXPECT_EQ(N1, N4->getOperand(0));
610 EXPECT_EQ(N1, N5->getOperand(0));
611 EXPECT_EQ(N4, N6->getOperand(0));
612}
613
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000614TEST_F(MDNodeTest, replaceResolvedOperand) {
615 // Check code for replacing one resolved operand with another. If doing this
616 // directly (via replaceOperandWith()) becomes illegal, change the operand to
617 // a global value that gets RAUW'ed.
618 //
619 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000620 auto Temp = MDTuple::getTemporary(Context, None);
621 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000622
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000623 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000624 MDNode *N = MDTuple::get(Context, Ops);
625 EXPECT_EQ(nullptr, N->getOperand(0));
626 ASSERT_FALSE(N->isResolved());
627
628 // Check code for replacing resolved nodes.
629 N->replaceOperandWith(0, Empty);
630 EXPECT_EQ(Empty, N->getOperand(0));
631
632 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000633 N->replaceOperandWith(0, Temp.get());
634 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000635
636 // Remove the references to Temp; required for teardown.
637 Temp->replaceAllUsesWith(nullptr);
638}
639
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000640TEST_F(MDNodeTest, replaceWithUniqued) {
641 auto *Empty = MDTuple::get(Context, None);
642 MDTuple *FirstUniqued;
643 {
644 Metadata *Ops[] = {Empty};
645 auto Temp = MDTuple::getTemporary(Context, Ops);
646 EXPECT_TRUE(Temp->isTemporary());
647
648 // Don't expect a collision.
649 auto *Current = Temp.get();
650 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
651 EXPECT_TRUE(FirstUniqued->isUniqued());
652 EXPECT_TRUE(FirstUniqued->isResolved());
653 EXPECT_EQ(Current, FirstUniqued);
654 }
655 {
656 Metadata *Ops[] = {Empty};
657 auto Temp = MDTuple::getTemporary(Context, Ops);
658 EXPECT_TRUE(Temp->isTemporary());
659
660 // Should collide with Uniqued above this time.
661 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
662 EXPECT_TRUE(Uniqued->isUniqued());
663 EXPECT_TRUE(Uniqued->isResolved());
664 EXPECT_EQ(FirstUniqued, Uniqued);
665 }
666 {
667 auto Unresolved = MDTuple::getTemporary(Context, None);
668 Metadata *Ops[] = {Unresolved.get()};
669 auto Temp = MDTuple::getTemporary(Context, Ops);
670 EXPECT_TRUE(Temp->isTemporary());
671
672 // Shouldn't be resolved.
673 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
674 EXPECT_TRUE(Uniqued->isUniqued());
675 EXPECT_FALSE(Uniqued->isResolved());
676
677 // Should be a different node.
678 EXPECT_NE(FirstUniqued, Uniqued);
679
680 // Should resolve when we update its node (note: be careful to avoid a
681 // collision with any other nodes above).
682 Uniqued->replaceOperandWith(0, nullptr);
683 EXPECT_TRUE(Uniqued->isResolved());
684 }
685}
686
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000687TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000688 // temp !{}
689 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
690 EXPECT_FALSE(Op->isResolved());
691
692 // temp !{temp !{}}
693 Metadata *Ops[] = {Op};
694 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
695 EXPECT_FALSE(N->isResolved());
696
697 // temp !{temp !{}} => !{temp !{}}
698 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
699 EXPECT_FALSE(N->isResolved());
700
701 // !{temp !{}} => !{!{}}
702 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
703 EXPECT_TRUE(Op->isResolved());
704 EXPECT_TRUE(N->isResolved());
705}
706
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000707TEST_F(MDNodeTest, replaceWithUniquedDeletedOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000708 // i1* @GV
709 Type *Ty = Type::getInt1PtrTy(Context);
710 std::unique_ptr<GlobalVariable> GV(
711 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
712 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
713
714 // temp !{i1* @GV}
715 Metadata *Ops[] = {Op};
716 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
717
718 // temp !{i1* @GV} => !{i1* @GV}
719 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
720 ASSERT_TRUE(N->isUniqued());
721
722 // !{i1* @GV} => !{null}
723 GV.reset();
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000724 ASSERT_TRUE(N->isDistinct());
725 ASSERT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000726 Metadata *NullOps[] = {nullptr};
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000727 ASSERT_NE(N, MDTuple::get(Context, NullOps));
728}
729
730TEST_F(MDNodeTest, replaceWithUniquedChangedOperand) {
731 // i1* @GV
732 Type *Ty = Type::getInt1PtrTy(Context);
733 std::unique_ptr<GlobalVariable> GV(
734 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
735 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
736
737 // temp !{i1* @GV}
738 Metadata *Ops[] = {Op};
739 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
740
741 // temp !{i1* @GV} => !{i1* @GV}
742 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
743 ASSERT_TRUE(N->isUniqued());
744
745 // !{i1* @GV} => !{i1* @GV2}
746 std::unique_ptr<GlobalVariable> GV2(
747 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
748 GV->replaceAllUsesWith(GV2.get());
749 ASSERT_TRUE(N->isUniqued());
750 Metadata *NullOps[] = {ConstantAsMetadata::get(GV2.get())};
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000751 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
752}
753
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000754TEST_F(MDNodeTest, replaceWithDistinct) {
755 {
756 auto *Empty = MDTuple::get(Context, None);
757 Metadata *Ops[] = {Empty};
758 auto Temp = MDTuple::getTemporary(Context, Ops);
759 EXPECT_TRUE(Temp->isTemporary());
760
761 // Don't expect a collision.
762 auto *Current = Temp.get();
763 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
764 EXPECT_TRUE(Distinct->isDistinct());
765 EXPECT_TRUE(Distinct->isResolved());
766 EXPECT_EQ(Current, Distinct);
767 }
768 {
769 auto Unresolved = MDTuple::getTemporary(Context, None);
770 Metadata *Ops[] = {Unresolved.get()};
771 auto Temp = MDTuple::getTemporary(Context, Ops);
772 EXPECT_TRUE(Temp->isTemporary());
773
774 // Don't expect a collision.
775 auto *Current = Temp.get();
776 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
777 EXPECT_TRUE(Distinct->isDistinct());
778 EXPECT_TRUE(Distinct->isResolved());
779 EXPECT_EQ(Current, Distinct);
780
781 // Cleanup; required for teardown.
782 Unresolved->replaceAllUsesWith(nullptr);
783 }
784}
785
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000786TEST_F(MDNodeTest, replaceWithPermanent) {
787 Metadata *Ops[] = {nullptr};
788 auto Temp = MDTuple::getTemporary(Context, Ops);
789 auto *T = Temp.get();
790
791 // U is a normal, uniqued node that references T.
792 auto *U = MDTuple::get(Context, T);
793 EXPECT_TRUE(U->isUniqued());
794
795 // Make Temp self-referencing.
796 Temp->replaceOperandWith(0, T);
797
798 // Try to uniquify Temp. This should, despite the name in the API, give a
799 // 'distinct' node, since self-references aren't allowed to be uniqued.
800 //
801 // Since it's distinct, N should have the same address as when it was a
802 // temporary (i.e., be equal to T not U).
803 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
804 EXPECT_EQ(N, T);
805 EXPECT_TRUE(N->isDistinct());
806
807 // U should be the canonical unique node with N as the argument.
808 EXPECT_EQ(U, MDTuple::get(Context, N));
809 EXPECT_TRUE(U->isUniqued());
810
811 // This temporary should collide with U when replaced, but it should still be
812 // uniqued.
813 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
814 EXPECT_TRUE(U->isUniqued());
815
816 // This temporary should become a new uniqued node.
817 auto Temp2 = MDTuple::getTemporary(Context, U);
818 auto *V = Temp2.get();
819 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
820 EXPECT_TRUE(V->isUniqued());
821 EXPECT_EQ(U, V->getOperand(0));
822}
823
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000824TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
825 TrackingMDRef Ref;
826 EXPECT_EQ(nullptr, Ref.get());
827 {
828 auto Temp = MDTuple::getTemporary(Context, None);
829 Ref.reset(Temp.get());
830 EXPECT_EQ(Temp.get(), Ref.get());
831 }
832 EXPECT_EQ(nullptr, Ref.get());
833}
834
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000835typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000836
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000837TEST_F(DILocationTest, Overflow) {
838 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000839 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000840 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000841 EXPECT_EQ(2u, L->getLine());
842 EXPECT_EQ(7u, L->getColumn());
843 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000844 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000845 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000846 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000847 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000848 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000849 }
850 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000851 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000852 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000853 EXPECT_EQ(0u, L->getColumn());
854 }
855 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000856 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000857 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000858 EXPECT_EQ(0u, L->getColumn());
859 }
860}
861
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000862TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000863 MDNode *N = getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000864 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000865 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000866 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000867 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000868 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000869}
870
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000871TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000872 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000873 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000874 EXPECT_TRUE(L->isTemporary());
875 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000876}
877
Teresa Johnsond98152b62015-12-07 15:05:44 +0000878TEST_F(DILocationTest, cloneTemporary) {
879 MDNode *N = MDNode::get(Context, None);
880 auto L = DILocation::getTemporary(Context, 2, 7, N);
881 EXPECT_TRUE(L->isTemporary());
882 auto L2 = L->clone();
883 EXPECT_TRUE(L2->isTemporary());
884}
885
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000886typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000887
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000888TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000889 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000890 auto *Empty = MDNode::get(Context, None);
891 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000892 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000893 EXPECT_EQ(15u, N->getTag());
894 EXPECT_EQ(2u, N->getNumOperands());
895 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000896 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000897 EXPECT_EQ(1u, N->getNumDwarfOperands());
898 EXPECT_EQ(Empty, N->getDwarfOperand(0));
899 EXPECT_EQ(Empty, N->getOperand(1));
900 ASSERT_TRUE(N->isUniqued());
901
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000902 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000903
904 N->replaceOperandWith(1, nullptr);
905 EXPECT_EQ(15u, N->getTag());
906 EXPECT_EQ(Header, N->getHeader());
907 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
908 ASSERT_TRUE(N->isUniqued());
909
910 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000911 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000912
913 N->replaceDwarfOperandWith(0, Empty);
914 EXPECT_EQ(15u, N->getTag());
915 EXPECT_EQ(Header, N->getHeader());
916 EXPECT_EQ(Empty, N->getDwarfOperand(0));
917 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000918 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000919
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000920 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000921 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000922}
923
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000924TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000925 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000926 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000927 EXPECT_EQ(StringRef(), N->getHeader());
928 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000929}
930
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000931typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000932
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000933TEST_F(DISubrangeTest, get) {
934 auto *N = DISubrange::get(Context, 5, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000935 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
936 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000937 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000938 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
939 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000940
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000941 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000942 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000943}
944
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000945TEST_F(DISubrangeTest, getEmptyArray) {
946 auto *N = DISubrange::get(Context, -1, 0);
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000947 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
948 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000949 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000950 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000951}
952
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000953typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000954
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000955TEST_F(DIEnumeratorTest, get) {
956 auto *N = DIEnumerator::get(Context, 7, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000957 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
958 EXPECT_EQ(7, N->getValue());
959 EXPECT_EQ("name", N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000960 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000961
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000962 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
963 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000964
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000965 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000966 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000967}
968
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000969typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000970
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000971TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000972 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000973 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000974 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
975 EXPECT_EQ("special", N->getName());
976 EXPECT_EQ(33u, N->getSizeInBits());
977 EXPECT_EQ(26u, N->getAlignInBits());
978 EXPECT_EQ(7u, N->getEncoding());
979 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000980 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000981 26, 7));
982
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000983 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000984 "special", 33, 26, 7));
985 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000986 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
987 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000988 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000989 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000990 25, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000991 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000992 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000993
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000994 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000995 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000996}
997
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000998TEST_F(DIBasicTypeTest, getWithLargeValues) {
999 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Victor Leschuk197aa312016-10-18 14:31:22 +00001000 UINT64_MAX, UINT32_MAX - 1, 7);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001001 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk197aa312016-10-18 14:31:22 +00001002 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001003}
1004
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001005TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001006 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001007 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001008 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
1009 EXPECT_EQ("unspecified", N->getName());
1010 EXPECT_EQ(0u, N->getSizeInBits());
1011 EXPECT_EQ(0u, N->getAlignInBits());
1012 EXPECT_EQ(0u, N->getEncoding());
1013 EXPECT_EQ(0u, N->getLine());
1014}
1015
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001016typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001017
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001018TEST_F(DITypeTest, clone) {
1019 // Check that DIType has a specialized clone that returns TempDIType.
1020 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001021 dwarf::DW_ATE_signed);
1022
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001023 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001024 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1025}
1026
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001027TEST_F(DITypeTest, setFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001028 // void (void)
1029 Metadata *TypesOps[] = {nullptr};
1030 Metadata *Types = MDTuple::get(Context, TypesOps);
1031
Leny Kholodov40c62352016-09-06 17:03:02 +00001032 DIType *D =
1033 DISubroutineType::getDistinct(Context, DINode::FlagZero, 0, Types);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001034 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001035 D->setFlags(DINode::FlagRValueReference);
1036 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001037 D->setFlags(DINode::FlagZero);
1038 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001039
Leny Kholodov40c62352016-09-06 17:03:02 +00001040 TempDIType T =
1041 DISubroutineType::getTemporary(Context, DINode::FlagZero, 0, Types);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001042 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001043 T->setFlags(DINode::FlagRValueReference);
1044 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001045 T->setFlags(DINode::FlagZero);
1046 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001047}
1048
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001049typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001050
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001051TEST_F(DIDerivedTypeTest, get) {
1052 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001053 DIScope *Scope = getSubprogram();
1054 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001055 MDTuple *ExtraData = getTuple();
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001056 DINode::DIFlags Flags5 = static_cast<DINode::DIFlags>(5);
1057 DINode::DIFlags Flags4 = static_cast<DINode::DIFlags>(4);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001058
Leny Kholodov40c62352016-09-06 17:03:02 +00001059 auto *N =
1060 DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", File,
1061 1, Scope, BaseType, 2, 3, 4, Flags5, ExtraData);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001062 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
1063 EXPECT_EQ("something", N->getName());
1064 EXPECT_EQ(File, N->getFile());
1065 EXPECT_EQ(1u, N->getLine());
1066 EXPECT_EQ(Scope, N->getScope());
1067 EXPECT_EQ(BaseType, N->getBaseType());
1068 EXPECT_EQ(2u, N->getSizeInBits());
1069 EXPECT_EQ(3u, N->getAlignInBits());
1070 EXPECT_EQ(4u, N->getOffsetInBits());
1071 EXPECT_EQ(5u, N->getFlags());
1072 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001073 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001074 "something", File, 1, Scope, BaseType, 2, 3,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001075 4, Flags5, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001076
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001077 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001078 "something", File, 1, Scope, BaseType, 2, 3,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001079 4, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001080 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Leny Kholodov40c62352016-09-06 17:03:02 +00001081 File, 1, Scope, BaseType, 2, 3, 4, Flags5,
1082 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001083 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001084 "something", getFile(), 1, Scope, BaseType, 2,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001085 3, 4, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001086 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001087 "something", File, 2, Scope, BaseType, 2, 3,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001088 4, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001089 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001090 "something", File, 1, getSubprogram(),
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001091 BaseType, 2, 3, 4, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001092 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001093 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001094 Scope, getBasicType("basic2"), 2, 3, 4, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001095 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001096 "something", File, 1, Scope, BaseType, 3, 3,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001097 4, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001098 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001099 "something", File, 1, Scope, BaseType, 2, 2,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001100 4, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001101 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001102 "something", File, 1, Scope, BaseType, 2, 3,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001103 5, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001104 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001105 "something", File, 1, Scope, BaseType, 2, 3,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001106 4, Flags4, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001107 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001108 "something", File, 1, Scope, BaseType, 2, 3,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001109 4, Flags5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001110
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001111 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001112 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001113}
1114
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001115TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1116 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001117 DIScope *Scope = getSubprogram();
1118 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001119 MDTuple *ExtraData = getTuple();
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001120 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001121
Leny Kholodov40c62352016-09-06 17:03:02 +00001122 auto *N = DIDerivedType::get(
1123 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, Scope,
Victor Leschuk197aa312016-10-18 14:31:22 +00001124 BaseType, UINT64_MAX, UINT32_MAX - 1, UINT64_MAX - 2, Flags, ExtraData);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001125 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk197aa312016-10-18 14:31:22 +00001126 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001127 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1128}
1129
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001130typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001131
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001132TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001133 unsigned Tag = dwarf::DW_TAG_structure_type;
1134 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001135 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001136 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001137 DIScope *Scope = getSubprogram();
1138 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001139 uint64_t SizeInBits = 2;
Victor Leschuk197aa312016-10-18 14:31:22 +00001140 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001141 uint64_t OffsetInBits = 4;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001142 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001143 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001144 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001145 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001146 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001147 StringRef Identifier = "some id";
1148
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001149 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001150 BaseType, SizeInBits, AlignInBits,
1151 OffsetInBits, Flags, Elements, RuntimeLang,
1152 VTableHolder, TemplateParams, Identifier);
1153 EXPECT_EQ(Tag, N->getTag());
1154 EXPECT_EQ(Name, N->getName());
1155 EXPECT_EQ(File, N->getFile());
1156 EXPECT_EQ(Line, N->getLine());
1157 EXPECT_EQ(Scope, N->getScope());
1158 EXPECT_EQ(BaseType, N->getBaseType());
1159 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1160 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1161 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1162 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001163 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001164 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1165 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001166 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001167 EXPECT_EQ(Identifier, N->getIdentifier());
1168
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001169 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001170 BaseType, SizeInBits, AlignInBits,
1171 OffsetInBits, Flags, Elements, RuntimeLang,
1172 VTableHolder, TemplateParams, Identifier));
1173
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001174 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001175 BaseType, SizeInBits, AlignInBits,
1176 OffsetInBits, Flags, Elements, RuntimeLang,
1177 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001178 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001179 BaseType, SizeInBits, AlignInBits,
1180 OffsetInBits, Flags, Elements, RuntimeLang,
1181 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001182 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001183 BaseType, SizeInBits, AlignInBits,
1184 OffsetInBits, Flags, Elements, RuntimeLang,
1185 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001186 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001187 BaseType, SizeInBits, AlignInBits,
1188 OffsetInBits, Flags, Elements, RuntimeLang,
1189 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001190 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001191 Context, Tag, Name, File, Line, getSubprogram(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001192 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1193 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001194 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001195 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1196 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1197 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001198 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001199 BaseType, SizeInBits + 1, AlignInBits,
1200 OffsetInBits, Flags, Elements, RuntimeLang,
1201 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001202 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001203 BaseType, SizeInBits, AlignInBits + 1,
1204 OffsetInBits, Flags, Elements, RuntimeLang,
1205 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001206 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001207 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1208 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1209 VTableHolder, TemplateParams, Identifier));
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001210 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001211 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001212 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001213 AlignInBits, OffsetInBits, FlagsPOne, Elements, RuntimeLang,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001214 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001215 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001216 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1217 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1218 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001219 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001220 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1221 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1222 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001223 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001224 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1225 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1226 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001227 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001228 BaseType, SizeInBits, AlignInBits,
1229 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001230 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001231 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001232 BaseType, SizeInBits, AlignInBits,
1233 OffsetInBits, Flags, Elements, RuntimeLang,
1234 VTableHolder, TemplateParams, "other"));
1235
1236 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001237 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1238 BaseType, SizeInBits, AlignInBits,
1239 OffsetInBits, Flags, Elements, RuntimeLang,
1240 VTableHolder, TemplateParams, "")
1241 ->getRawIdentifier());
1242 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1243 BaseType, SizeInBits, AlignInBits,
1244 OffsetInBits, Flags, Elements, RuntimeLang,
1245 VTableHolder, TemplateParams)
1246 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001247
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001248 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001249 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001250}
1251
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001252TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001253 unsigned Tag = dwarf::DW_TAG_structure_type;
1254 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001255 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001256 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001257 DIScope *Scope = getSubprogram();
1258 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001259 uint64_t SizeInBits = UINT64_MAX;
Victor Leschuk197aa312016-10-18 14:31:22 +00001260 uint32_t AlignInBits = UINT32_MAX - 1;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001261 uint64_t OffsetInBits = UINT64_MAX - 2;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001262 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001263 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001264 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001265 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001266 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001267 StringRef Identifier = "some id";
1268
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001269 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001270 BaseType, SizeInBits, AlignInBits,
1271 OffsetInBits, Flags, Elements, RuntimeLang,
1272 VTableHolder, TemplateParams, Identifier);
1273 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1274 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1275 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1276}
1277
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001278TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001279 unsigned Tag = dwarf::DW_TAG_structure_type;
1280 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001281 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001282 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001283 DIScope *Scope = getSubprogram();
1284 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001285 uint64_t SizeInBits = 2;
Victor Leschuk197aa312016-10-18 14:31:22 +00001286 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001287 uint64_t OffsetInBits = 4;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001288 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001289 unsigned RuntimeLang = 6;
1290 StringRef Identifier = "some id";
1291
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001292 auto *N = DICompositeType::get(
1293 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1294 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001295
1296 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001297 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001298 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001299 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001300 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001301 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001302
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001303 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001304 EXPECT_EQ(nullptr, N->getVTableHolder());
1305 N->replaceVTableHolder(VTableHolder);
1306 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1307 N->replaceVTableHolder(nullptr);
1308 EXPECT_EQ(nullptr, N->getVTableHolder());
1309
1310 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001311 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001312 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001313 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001314 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001315 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001316}
1317
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001318typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001319
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001320TEST_F(DISubroutineTypeTest, get) {
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001321 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(1);
1322 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001323 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001324
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001325 auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001326 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1327 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001328 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001329 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001330
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001331 EXPECT_NE(N, DISubroutineType::get(Context, FlagsPOne, 0, TypeArray));
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001332 EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple()));
1333
1334 // Test the hashing of calling conventions.
1335 auto *Fast = DISubroutineType::get(
1336 Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray);
1337 auto *Std = DISubroutineType::get(Context, Flags,
1338 dwarf::DW_CC_BORLAND_stdcall, TypeArray);
1339 EXPECT_EQ(Fast,
1340 DISubroutineType::get(Context, Flags,
1341 dwarf::DW_CC_BORLAND_msfastcall, TypeArray));
1342 EXPECT_EQ(Std, DISubroutineType::get(
1343 Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray));
1344
1345 EXPECT_NE(N, Fast);
1346 EXPECT_NE(N, Std);
1347 EXPECT_NE(Fast, Std);
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001348
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001349 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001350 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001351
1352 // Test always-empty operands.
1353 EXPECT_EQ(nullptr, N->getScope());
1354 EXPECT_EQ(nullptr, N->getFile());
1355 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001356}
1357
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001358typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001359
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001360TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001361 StringRef Filename = "file";
1362 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001363 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001364
1365 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1366 EXPECT_EQ(Filename, N->getFilename());
1367 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001368 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001369
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001370 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1371 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001372
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001373 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001374 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001375}
1376
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001377TEST_F(DIFileTest, ScopeGetFile) {
1378 // Ensure that DIScope::getFile() returns itself.
1379 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001380 EXPECT_EQ(N, N->getFile());
1381}
1382
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001383typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001384
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001385TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001386 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001387 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001388 StringRef Producer = "some producer";
1389 bool IsOptimized = false;
1390 StringRef Flags = "flag after flag";
1391 unsigned RuntimeVersion = 2;
1392 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001393 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001394 MDTuple *EnumTypes = getTuple();
1395 MDTuple *RetainedTypes = getTuple();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001396 MDTuple *GlobalVariables = getTuple();
1397 MDTuple *ImportedEntities = getTuple();
Adrian Prantle3b49e02015-09-22 23:42:47 +00001398 uint64_t DWOId = 0x10000000c0ffee;
Amjad Abouda9bcf162015-12-10 12:56:35 +00001399 MDTuple *Macros = getTuple();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001400 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001401 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1402 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
David Blaikiea01f2952016-08-24 18:29:49 +00001403 RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001404
1405 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1406 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1407 EXPECT_EQ(File, N->getFile());
1408 EXPECT_EQ(Producer, N->getProducer());
1409 EXPECT_EQ(IsOptimized, N->isOptimized());
1410 EXPECT_EQ(Flags, N->getFlags());
1411 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1412 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1413 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001414 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1415 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001416 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1417 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001418 EXPECT_EQ(Macros, N->getMacros().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001419 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001420
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001421 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001422 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1423 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1424 EXPECT_EQ(File, Temp->getFile());
1425 EXPECT_EQ(Producer, Temp->getProducer());
1426 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1427 EXPECT_EQ(Flags, Temp->getFlags());
1428 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1429 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1430 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1431 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1432 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001433 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1434 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001435 EXPECT_EQ(Macros, Temp->getMacros().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001436 EXPECT_EQ(DWOId, Temp->getDWOId());
1437
1438 auto *TempAddress = Temp.get();
1439 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1440 EXPECT_TRUE(Clone->isDistinct());
1441 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001442}
1443
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001444TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001445 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001446 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001447 StringRef Producer = "some producer";
1448 bool IsOptimized = false;
1449 StringRef Flags = "flag after flag";
1450 unsigned RuntimeVersion = 2;
1451 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001452 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001453 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1454 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1455 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001456 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001457 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001458 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1459 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
David Blaikiea01f2952016-08-24 18:29:49 +00001460 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001461
1462 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001463 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001464 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001465 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001466 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001467 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001468
1469 auto *Macros = MDTuple::getDistinct(Context, None);
1470 EXPECT_EQ(nullptr, N->getMacros().get());
1471 N->replaceMacros(Macros);
1472 EXPECT_EQ(Macros, N->getMacros().get());
1473 N->replaceMacros(nullptr);
1474 EXPECT_EQ(nullptr, N->getMacros().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001475}
1476
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001477typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001478
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001479TEST_F(DISubprogramTest, get) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001480 DIScope *Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001481 StringRef Name = "name";
1482 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001483 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001484 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001485 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001486 bool IsLocalToUnit = false;
1487 bool IsDefinition = true;
1488 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001489 DIType *ContainingType = getCompositeType();
Davide Italiano236e7442016-04-13 20:17:42 +00001490 unsigned Virtuality = 2;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001491 unsigned VirtualIndex = 5;
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001492 int ThisAdjustment = -3;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001493 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(6);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001494 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001495 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001496 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001497 MDTuple *Variables = getTuple();
Adrian Prantl75819ae2016-04-15 15:57:41 +00001498 DICompileUnit *Unit = getUnit();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001499
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001500 auto *N = DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1501 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1502 ContainingType, Virtuality, VirtualIndex,
1503 ThisAdjustment, Flags, IsOptimized, Unit,
1504 TemplateParams, Declaration, Variables);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001505
1506 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1507 EXPECT_EQ(Scope, N->getScope());
1508 EXPECT_EQ(Name, N->getName());
1509 EXPECT_EQ(LinkageName, N->getLinkageName());
1510 EXPECT_EQ(File, N->getFile());
1511 EXPECT_EQ(Line, N->getLine());
1512 EXPECT_EQ(Type, N->getType());
1513 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1514 EXPECT_EQ(IsDefinition, N->isDefinition());
1515 EXPECT_EQ(ScopeLine, N->getScopeLine());
1516 EXPECT_EQ(ContainingType, N->getContainingType());
1517 EXPECT_EQ(Virtuality, N->getVirtuality());
1518 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001519 EXPECT_EQ(ThisAdjustment, N->getThisAdjustment());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001520 EXPECT_EQ(Flags, N->getFlags());
1521 EXPECT_EQ(IsOptimized, N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001522 EXPECT_EQ(Unit, N->getUnit());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001523 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001524 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001525 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001526 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001527 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1528 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001529 ThisAdjustment, Flags, IsOptimized, Unit,
1530 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001531
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001532 EXPECT_NE(N, DISubprogram::get(
1533 Context, getCompositeType(), Name, LinkageName, File, Line,
1534 Type, IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1535 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1536 Unit, TemplateParams, Declaration, Variables));
1537 EXPECT_NE(N, DISubprogram::get(
1538 Context, Scope, "other", LinkageName, File, Line, Type,
1539 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1540 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1541 Unit, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001542 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001543 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1544 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001545 ThisAdjustment, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001546 TemplateParams, Declaration, Variables));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001547 EXPECT_NE(N, DISubprogram::get(
1548 Context, Scope, Name, LinkageName, getFile(), Line, Type,
1549 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1550 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1551 Unit, TemplateParams, Declaration, Variables));
1552 EXPECT_NE(N, DISubprogram::get(
1553 Context, Scope, Name, LinkageName, File, Line + 1, Type,
1554 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1555 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1556 Unit, TemplateParams, Declaration, Variables));
1557 EXPECT_NE(N,
1558 DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1559 getSubroutineType(), IsLocalToUnit, IsDefinition,
1560 ScopeLine, ContainingType, Virtuality,
1561 VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1562 Unit, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001563 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001564 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1565 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001566 ThisAdjustment, Flags, IsOptimized, Unit,
1567 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001568 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001569 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1570 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001571 ThisAdjustment, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001572 TemplateParams, Declaration, Variables));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001573 EXPECT_NE(N, DISubprogram::get(
1574 Context, Scope, Name, LinkageName, File, Line, Type,
1575 IsLocalToUnit, IsDefinition, ScopeLine + 1, ContainingType,
1576 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1577 Unit, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001578 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001579 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001580 getCompositeType(), Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001581 ThisAdjustment, Flags, IsOptimized, Unit,
1582 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001583 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001584 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1585 ContainingType, Virtuality + 1, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001586 ThisAdjustment, Flags, IsOptimized, Unit,
1587 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001588 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001589 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1590 ContainingType, Virtuality, VirtualIndex + 1,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001591 ThisAdjustment, Flags, IsOptimized, Unit,
1592 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001593 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001594 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1595 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001596 ThisAdjustment, Flags, !IsOptimized, Unit,
1597 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001598 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001599 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1600 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001601 ThisAdjustment, Flags, IsOptimized, nullptr,
1602 TemplateParams, Declaration, Variables));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001603 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1604 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1605 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001606 ThisAdjustment, Flags, IsOptimized, Unit,
1607 getTuple(), Declaration, Variables));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001608 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1609 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1610 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001611 ThisAdjustment, Flags, IsOptimized, Unit,
1612 TemplateParams, getSubprogram(), Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001613 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001614 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1615 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001616 ThisAdjustment, Flags, IsOptimized, Unit,
1617 TemplateParams, Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001618
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001619 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001620 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001621}
1622
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001623typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001624
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001625TEST_F(DILexicalBlockTest, get) {
1626 DILocalScope *Scope = getSubprogram();
1627 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001628 unsigned Line = 5;
1629 unsigned Column = 8;
1630
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001631 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001632
1633 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1634 EXPECT_EQ(Scope, N->getScope());
1635 EXPECT_EQ(File, N->getFile());
1636 EXPECT_EQ(Line, N->getLine());
1637 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001638 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001639
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001640 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001641 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1642 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1643 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1644 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001645
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001646 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001647 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001648}
1649
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +00001650TEST_F(DILexicalBlockTest, Overflow) {
1651 DISubprogram *SP = getSubprogram();
1652 DIFile *F = getFile();
1653 {
1654 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1655 EXPECT_EQ(2u, LB->getLine());
1656 EXPECT_EQ(7u, LB->getColumn());
1657 }
1658 unsigned U16 = 1u << 16;
1659 {
1660 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1661 EXPECT_EQ(UINT32_MAX, LB->getLine());
1662 EXPECT_EQ(U16 - 1, LB->getColumn());
1663 }
1664 {
1665 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1666 EXPECT_EQ(UINT32_MAX, LB->getLine());
1667 EXPECT_EQ(0u, LB->getColumn());
1668 }
1669 {
1670 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1671 EXPECT_EQ(UINT32_MAX, LB->getLine());
1672 EXPECT_EQ(0u, LB->getColumn());
1673 }
1674}
1675
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001676typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001677
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001678TEST_F(DILexicalBlockFileTest, get) {
1679 DILocalScope *Scope = getSubprogram();
1680 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001681 unsigned Discriminator = 5;
1682
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001683 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001684
1685 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1686 EXPECT_EQ(Scope, N->getScope());
1687 EXPECT_EQ(File, N->getFile());
1688 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001689 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001690
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001691 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001692 Discriminator));
1693 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001694 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001695 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001696 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001697
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001698 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001699 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001700}
1701
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001702typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001703
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001704TEST_F(DINamespaceTest, get) {
1705 DIScope *Scope = getFile();
1706 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001707 StringRef Name = "namespace";
1708 unsigned Line = 5;
1709
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001710 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001711
1712 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1713 EXPECT_EQ(Scope, N->getScope());
1714 EXPECT_EQ(File, N->getFile());
1715 EXPECT_EQ(Name, N->getName());
1716 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001717 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001718
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001719 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1720 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1721 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1722 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001723
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001724 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001725 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001726}
1727
Adrian Prantlab1243f2015-06-29 23:03:47 +00001728typedef MetadataTest DIModuleTest;
1729
1730TEST_F(DIModuleTest, get) {
1731 DIScope *Scope = getFile();
1732 StringRef Name = "module";
1733 StringRef ConfigMacro = "-DNDEBUG";
1734 StringRef Includes = "-I.";
1735 StringRef Sysroot = "/";
1736
1737 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1738
1739 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1740 EXPECT_EQ(Scope, N->getScope());
1741 EXPECT_EQ(Name, N->getName());
1742 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1743 EXPECT_EQ(Includes, N->getIncludePath());
1744 EXPECT_EQ(Sysroot, N->getISysRoot());
1745 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1746 ConfigMacro, Includes, Sysroot));
1747 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1748 ConfigMacro, Includes, Sysroot));
1749 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1750 ConfigMacro, Includes, Sysroot));
1751 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1752 "other", Includes, Sysroot));
1753 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1754 ConfigMacro, "other", Sysroot));
1755 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1756 ConfigMacro, Includes, "other"));
1757
1758 TempDIModule Temp = N->clone();
1759 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1760}
1761
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001762typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001763
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001764TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001765 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001766 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001767
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001768 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001769
1770 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001771 EXPECT_EQ(Name, N->getName());
1772 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001773 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001774
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001775 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001776 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001777 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001778
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001779 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001780 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001781}
1782
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001783typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001784
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001785TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001786 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001787 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001788 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001789 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001790
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001791 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001792 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001793 EXPECT_EQ(Name, N->getName());
1794 EXPECT_EQ(Type, N->getType());
1795 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001796 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001797
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001799 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1800 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001801 EXPECT_NE(N,
1802 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1803 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001804 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001805 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001806 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001807
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001808 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001809 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001810}
1811
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001812typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001813
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001814TEST_F(DIGlobalVariableTest, get) {
1815 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001816 StringRef Name = "name";
1817 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001818 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001819 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001820 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001821 bool IsLocalToUnit = false;
1822 bool IsDefinition = true;
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001823 auto *Expr = DIExpression::get(Context, {1, 2});
1824 auto *Expr2 = DIExpression::get(Context, {1, 2, 3});
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001825 DIDerivedType *StaticDataMemberDeclaration =
1826 cast<DIDerivedType>(getDerivedType());
Victor Leschuk2ede1262016-10-20 00:13:12 +00001827 uint64_t AlignInBits = 8;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001828
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001829 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001830 Type, IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001831 Expr, StaticDataMemberDeclaration,
1832 AlignInBits);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001833 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1834 EXPECT_EQ(Scope, N->getScope());
1835 EXPECT_EQ(Name, N->getName());
1836 EXPECT_EQ(LinkageName, N->getLinkageName());
1837 EXPECT_EQ(File, N->getFile());
1838 EXPECT_EQ(Line, N->getLine());
1839 EXPECT_EQ(Type, N->getType());
1840 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1841 EXPECT_EQ(IsDefinition, N->isDefinition());
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001842 EXPECT_EQ(Expr, N->getExpr());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001843 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Victor Leschuk2ede1262016-10-20 00:13:12 +00001844 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001845 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001846 Line, Type, IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001847 Expr, StaticDataMemberDeclaration,
1848 AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001849
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001850 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001851 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001852 File, Line, Type, IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001853 Expr, StaticDataMemberDeclaration,
1854 AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001855 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001856 Line, Type, IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001857 Expr, StaticDataMemberDeclaration,
1858 AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001859 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001860 Type, IsLocalToUnit, IsDefinition, Expr,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001861 StaticDataMemberDeclaration,
1862 AlignInBits));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001863 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001864 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001865 Line, Type, IsLocalToUnit, IsDefinition, Expr,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001866 StaticDataMemberDeclaration,
1867 AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001868 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001869 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001870 Line + 1, Type, IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001871 Expr, StaticDataMemberDeclaration,
1872 AlignInBits));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001873 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001874 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001875 getDerivedType(), IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001876 Expr, StaticDataMemberDeclaration,
1877 AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001878 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001879 Line, Type, !IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001880 Expr, StaticDataMemberDeclaration,
1881 AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001882 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001883 Line, Type, IsLocalToUnit, !IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001884 Expr, StaticDataMemberDeclaration,
1885 AlignInBits));
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001886 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1887 Line, Type, IsLocalToUnit, IsDefinition,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001888 Expr2, StaticDataMemberDeclaration,
1889 AlignInBits));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001890 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001891 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001892 Type, IsLocalToUnit, IsDefinition, Expr,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001893 cast<DIDerivedType>(getDerivedType()),
1894 AlignInBits));
1895 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1896 Line, Type, IsLocalToUnit, IsDefinition,
1897 Expr, StaticDataMemberDeclaration,
1898 (AlignInBits << 1)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001899
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001900 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001901 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001902}
1903
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001904typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001905
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001906TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001907 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001908 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001909 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001910 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001911 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001912 unsigned Arg = 6;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001913 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
Victor Leschuk2ede1262016-10-20 00:13:12 +00001914 uint64_t AlignInBits = 8;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001915
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001916 auto *N =
Victor Leschuk2ede1262016-10-20 00:13:12 +00001917 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags,
1918 AlignInBits);
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001919 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001920 EXPECT_EQ(Scope, N->getScope());
1921 EXPECT_EQ(Name, N->getName());
1922 EXPECT_EQ(File, N->getFile());
1923 EXPECT_EQ(Line, N->getLine());
1924 EXPECT_EQ(Type, N->getType());
1925 EXPECT_EQ(Arg, N->getArg());
1926 EXPECT_EQ(Flags, N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00001927 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001928 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001929 Flags, AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001930
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001931 EXPECT_FALSE(
Victor Leschuk2ede1262016-10-20 00:13:12 +00001932 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags,
1933 AlignInBits)->isParameter());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001934 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001935 Type, Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001936 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001937 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001938 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001939 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001940 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001941 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001942 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001943 getDerivedType(), Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001944 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001945 Arg + 1, Flags, AlignInBits));
1946 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
1947 Arg, Flags, (AlignInBits << 1)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001948
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001949 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001950 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001951}
1952
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001953TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001954 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00001955 0, nullptr, 255, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001956 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001957 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00001958 0, nullptr, 256, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001959 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001960 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00001961 0, nullptr, 257, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001962 ->getArg());
1963 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001964 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00001965 0, nullptr, Max, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001966 ->getArg());
1967}
1968
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001969typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001970
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001971TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001972 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001973 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001974 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001975 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001976
1977 EXPECT_EQ(5u, N->getNumElements());
1978 EXPECT_EQ(2u, N->getElement(0));
1979 EXPECT_EQ(6u, N->getElement(1));
1980 EXPECT_EQ(9u, N->getElement(2));
1981 EXPECT_EQ(78u, N->getElement(3));
1982 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001983
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001984 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001985 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001986}
1987
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001988TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001989#define EXPECT_VALID(...) \
1990 do { \
1991 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001992 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001993 } while (false)
1994#define EXPECT_INVALID(...) \
1995 do { \
1996 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001997 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001998 } while (false)
1999
2000 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002001 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002002
2003 // Valid constructions.
2004 EXPECT_VALID(dwarf::DW_OP_plus, 6);
2005 EXPECT_VALID(dwarf::DW_OP_deref);
2006 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
2007 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
2008 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
2009 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
2010 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
2011
2012 // Invalid constructions.
2013 EXPECT_INVALID(~0u);
2014 EXPECT_INVALID(dwarf::DW_OP_plus);
2015 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
2016 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
2017 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
2018 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
2019
2020#undef EXPECT_VALID
2021#undef EXPECT_INVALID
2022}
2023
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002024typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002025
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002026TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002027 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002028 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002029 unsigned Line = 5;
2030 StringRef GetterName = "getter";
2031 StringRef SetterName = "setter";
2032 unsigned Attributes = 7;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002033 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002034
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002035 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002036 SetterName, Attributes, Type);
2037
2038 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
2039 EXPECT_EQ(Name, N->getName());
2040 EXPECT_EQ(File, N->getFile());
2041 EXPECT_EQ(Line, N->getLine());
2042 EXPECT_EQ(GetterName, N->getGetterName());
2043 EXPECT_EQ(SetterName, N->getSetterName());
2044 EXPECT_EQ(Attributes, N->getAttributes());
2045 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002046 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002047 SetterName, Attributes, Type));
2048
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002049 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002050 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002051 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002052 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002053 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002054 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002055 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002056 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002057 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002058 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002059 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002060 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002061 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002062 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00002063 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002064
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002065 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002066 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002067}
2068
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002069typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002070
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002071TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002072 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002073 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002074 DINode *Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002075 unsigned Line = 5;
2076 StringRef Name = "name";
2077
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002078 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002079
2080 EXPECT_EQ(Tag, N->getTag());
2081 EXPECT_EQ(Scope, N->getScope());
2082 EXPECT_EQ(Entity, N->getEntity());
2083 EXPECT_EQ(Line, N->getLine());
2084 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002085 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002086
2087 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002088 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002089 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002090 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002091 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002092 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002093 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002094 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002095 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002096 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002097 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002098
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002099 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002100 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002101}
2102
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002103typedef MetadataTest MetadataAsValueTest;
2104
2105TEST_F(MetadataAsValueTest, MDNode) {
2106 MDNode *N = MDNode::get(Context, None);
2107 auto *V = MetadataAsValue::get(Context, N);
2108 EXPECT_TRUE(V->getType()->isMetadataTy());
2109 EXPECT_EQ(N, V->getMetadata());
2110
2111 auto *V2 = MetadataAsValue::get(Context, N);
2112 EXPECT_EQ(V, V2);
2113}
2114
2115TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2116 MDNode *N = MDNode::get(Context, None);
2117 Metadata *Ops[] = {N};
2118 MDNode *N2 = MDNode::get(Context, Ops);
2119 auto *V = MetadataAsValue::get(Context, N2);
2120 EXPECT_TRUE(V->getType()->isMetadataTy());
2121 EXPECT_EQ(N2, V->getMetadata());
2122
2123 auto *V2 = MetadataAsValue::get(Context, N2);
2124 EXPECT_EQ(V, V2);
2125
2126 auto *V3 = MetadataAsValue::get(Context, N);
2127 EXPECT_TRUE(V3->getType()->isMetadataTy());
2128 EXPECT_NE(V, V3);
2129 EXPECT_EQ(N, V3->getMetadata());
2130}
2131
2132TEST_F(MetadataAsValueTest, MDNodeConstant) {
2133 auto *C = ConstantInt::getTrue(Context);
2134 auto *MD = ConstantAsMetadata::get(C);
2135 Metadata *Ops[] = {MD};
2136 auto *N = MDNode::get(Context, Ops);
2137
2138 auto *V = MetadataAsValue::get(Context, MD);
2139 EXPECT_TRUE(V->getType()->isMetadataTy());
2140 EXPECT_EQ(MD, V->getMetadata());
2141
2142 auto *V2 = MetadataAsValue::get(Context, N);
2143 EXPECT_EQ(MD, V2->getMetadata());
2144 EXPECT_EQ(V, V2);
2145}
2146
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002147typedef MetadataTest ValueAsMetadataTest;
2148
2149TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2150 Type *Ty = Type::getInt1PtrTy(Context);
2151 std::unique_ptr<GlobalVariable> GV0(
2152 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2153 auto *MD = ValueAsMetadata::get(GV0.get());
2154 EXPECT_TRUE(MD->getValue() == GV0.get());
2155 ASSERT_TRUE(GV0->use_empty());
2156
2157 std::unique_ptr<GlobalVariable> GV1(
2158 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2159 GV0->replaceAllUsesWith(GV1.get());
2160 EXPECT_TRUE(MD->getValue() == GV1.get());
2161}
2162
Adrian Prantlcbec1602016-02-08 17:02:34 +00002163TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2164 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002165 ConstantAsMetadata *CI =
2166 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Adrian Prantlcbec1602016-02-08 17:02:34 +00002167
Adrian Prantlcbec1602016-02-08 17:02:34 +00002168 auto Temp1 = MDTuple::getTemporary(Context, None);
Adrian Prantl817c47b2016-02-08 19:13:15 +00002169 auto Temp2 = MDTuple::getTemporary(Context, {CI});
2170 auto *N = MDTuple::get(Context, {Temp1.get()});
Adrian Prantlcbec1602016-02-08 17:02:34 +00002171
2172 // Test replacing a temporary node with another temporary node.
2173 Temp1->replaceAllUsesWith(Temp2.get());
2174 EXPECT_EQ(N->getOperand(0), Temp2.get());
2175
2176 // Clean up Temp2 for teardown.
2177 Temp2->replaceAllUsesWith(nullptr);
2178}
2179
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002180TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2181 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002182 ConstantAsMetadata *CI =
2183 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002184
2185 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002186 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002187
2188 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002189 Metadata *Ops1[] = {CI, CI, Temp.get()};
2190 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002191
2192 auto *N1 = MDTuple::get(Context, Ops1);
2193 auto *N2 = MDTuple::get(Context, Ops2);
2194 ASSERT_NE(N1, N2);
2195
2196 // Tell metadata that the constant is getting deleted.
2197 //
2198 // After this, N1 will be invalid, so don't touch it.
2199 ValueAsMetadata::handleDeletion(CI->getValue());
2200 EXPECT_EQ(nullptr, N2->getOperand(0));
2201 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002202 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002203
2204 // Clean up Temp for teardown.
2205 Temp->replaceAllUsesWith(nullptr);
2206}
2207
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002208typedef MetadataTest TrackingMDRefTest;
2209
2210TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2211 Type *Ty = Type::getInt1PtrTy(Context);
2212 std::unique_ptr<GlobalVariable> GV0(
2213 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2214 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2215 EXPECT_TRUE(MD->getValue() == GV0.get());
2216 ASSERT_TRUE(GV0->use_empty());
2217
2218 std::unique_ptr<GlobalVariable> GV1(
2219 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2220 GV0->replaceAllUsesWith(GV1.get());
2221 EXPECT_TRUE(MD->getValue() == GV1.get());
2222
2223 // Reset it, so we don't inadvertently test deletion.
2224 MD.reset();
2225}
2226
2227TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2228 Type *Ty = Type::getInt1PtrTy(Context);
2229 std::unique_ptr<GlobalVariable> GV(
2230 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2231 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2232 EXPECT_TRUE(MD->getValue() == GV.get());
2233 ASSERT_TRUE(GV->use_empty());
2234
2235 GV.reset();
2236 EXPECT_TRUE(!MD);
2237}
2238
Devang Patel0924b332009-07-30 00:03:41 +00002239TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002240 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002241 ConstantAsMetadata *C =
2242 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2243 ConstantAsMetadata *C2 =
2244 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002245
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002246 Metadata *const V = C;
2247 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002248 MDNode *n = MDNode::get(Context, V);
2249 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002250
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002251 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002252 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002253 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2254 NMD->addOperand(n);
2255 NMD->addOperand(n2);
2256
Chris Lattnerbe354a62009-08-23 04:47:35 +00002257 std::string Str;
2258 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002259 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002260 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002261 oss.str().c_str());
2262}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002263
2264typedef MetadataTest FunctionAttachmentTest;
2265TEST_F(FunctionAttachmentTest, setMetadata) {
2266 Function *F = getFunction("foo");
2267 ASSERT_FALSE(F->hasMetadata());
2268 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2269 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2270 EXPECT_EQ(nullptr, F->getMetadata("other"));
2271
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002272 DISubprogram *SP1 = getSubprogram();
2273 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002274 ASSERT_NE(SP1, SP2);
2275
2276 F->setMetadata("dbg", SP1);
2277 EXPECT_TRUE(F->hasMetadata());
2278 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2279 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2280 EXPECT_EQ(nullptr, F->getMetadata("other"));
2281
2282 F->setMetadata(LLVMContext::MD_dbg, SP2);
2283 EXPECT_TRUE(F->hasMetadata());
2284 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2285 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2286 EXPECT_EQ(nullptr, F->getMetadata("other"));
2287
2288 F->setMetadata("dbg", nullptr);
2289 EXPECT_FALSE(F->hasMetadata());
2290 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2291 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2292 EXPECT_EQ(nullptr, F->getMetadata("other"));
2293
2294 MDTuple *T1 = getTuple();
2295 MDTuple *T2 = getTuple();
2296 ASSERT_NE(T1, T2);
2297
2298 F->setMetadata("other1", T1);
2299 F->setMetadata("other2", T2);
2300 EXPECT_TRUE(F->hasMetadata());
2301 EXPECT_EQ(T1, F->getMetadata("other1"));
2302 EXPECT_EQ(T2, F->getMetadata("other2"));
2303 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2304
2305 F->setMetadata("other1", T2);
2306 F->setMetadata("other2", T1);
2307 EXPECT_EQ(T2, F->getMetadata("other1"));
2308 EXPECT_EQ(T1, F->getMetadata("other2"));
2309
2310 F->setMetadata("other1", nullptr);
2311 F->setMetadata("other2", nullptr);
2312 EXPECT_FALSE(F->hasMetadata());
2313 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2314 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2315}
2316
2317TEST_F(FunctionAttachmentTest, getAll) {
2318 Function *F = getFunction("foo");
2319
2320 MDTuple *T1 = getTuple();
2321 MDTuple *T2 = getTuple();
2322 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002323 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002324
2325 F->setMetadata("other1", T2);
2326 F->setMetadata(LLVMContext::MD_dbg, SP);
2327 F->setMetadata("other2", T1);
2328 F->setMetadata(LLVMContext::MD_prof, P);
2329 F->setMetadata("other2", T2);
2330 F->setMetadata("other1", T1);
2331
2332 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2333 F->getAllMetadata(MDs);
2334 ASSERT_EQ(4u, MDs.size());
2335 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2336 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2337 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2338 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2339 EXPECT_EQ(SP, MDs[0].second);
2340 EXPECT_EQ(P, MDs[1].second);
2341 EXPECT_EQ(T1, MDs[2].second);
2342 EXPECT_EQ(T2, MDs[3].second);
2343}
2344
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002345TEST_F(FunctionAttachmentTest, Verifier) {
2346 Function *F = getFunction("foo");
2347 F->setMetadata("attach", getTuple());
Peter Collingbournebb738172016-06-06 23:21:27 +00002348 F->setIsMaterializable(true);
Peter Collingbournebb738172016-06-06 23:21:27 +00002349
Peter Collingbourne21521892016-06-21 23:42:48 +00002350 // Confirm this is materializable.
2351 ASSERT_TRUE(F->isMaterializable());
2352
2353 // Materializable functions cannot have metadata attachments.
2354 EXPECT_TRUE(verifyFunction(*F));
2355
2356 // Function declarations can.
Peter Collingbournebb738172016-06-06 23:21:27 +00002357 F->setIsMaterializable(false);
Peter Collingbourne21521892016-06-21 23:42:48 +00002358 EXPECT_FALSE(verifyModule(*F->getParent()));
2359 EXPECT_FALSE(verifyFunction(*F));
2360
2361 // So can definitions.
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002362 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2363 EXPECT_FALSE(verifyModule(*F->getParent()));
2364 EXPECT_FALSE(verifyFunction(*F));
2365}
2366
Diego Novillo2567f3d2015-05-13 15:13:45 +00002367TEST_F(FunctionAttachmentTest, EntryCount) {
2368 Function *F = getFunction("foo");
2369 EXPECT_FALSE(F->getEntryCount().hasValue());
2370 F->setEntryCount(12304);
2371 EXPECT_TRUE(F->getEntryCount().hasValue());
2372 EXPECT_EQ(12304u, *F->getEntryCount());
2373}
2374
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002375TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2376 Function *F = getFunction("foo");
2377 DISubprogram *SP = getSubprogram();
2378 F->setSubprogram(SP);
2379
2380 // Note that the static_cast confirms that F->getSubprogram() actually
2381 // returns an DISubprogram.
2382 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2383 EXPECT_EQ(SP, F->getMetadata("dbg"));
2384 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2385}
2386
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002387typedef MetadataTest DistinctMDOperandPlaceholderTest;
2388TEST_F(DistinctMDOperandPlaceholderTest, getID) {
2389 EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
2390}
2391
2392TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
2393 // Set up some placeholders.
2394 DistinctMDOperandPlaceholder PH0(7);
2395 DistinctMDOperandPlaceholder PH1(3);
2396 DistinctMDOperandPlaceholder PH2(0);
2397 Metadata *Ops[] = {&PH0, &PH1, &PH2};
2398 auto *D = MDTuple::getDistinct(Context, Ops);
2399 ASSERT_EQ(&PH0, D->getOperand(0));
2400 ASSERT_EQ(&PH1, D->getOperand(1));
2401 ASSERT_EQ(&PH2, D->getOperand(2));
2402
2403 // Replace them.
2404 auto *N0 = MDTuple::get(Context, None);
2405 auto *N1 = MDTuple::get(Context, N0);
2406 PH0.replaceUseWith(N0);
2407 PH1.replaceUseWith(N1);
2408 PH2.replaceUseWith(nullptr);
2409 EXPECT_EQ(N0, D->getOperand(0));
2410 EXPECT_EQ(N1, D->getOperand(1));
2411 EXPECT_EQ(nullptr, D->getOperand(2));
2412}
2413
2414TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
2415 // There is no user, but we can still call replace.
2416 DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None));
2417}
2418
Duncan P. N. Exon Smith004eb552016-04-23 04:34:11 +00002419#ifndef NDEBUG
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002420#ifdef GTEST_HAS_DEATH_TEST
2421TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
2422 // This shouldn't crash.
2423 DistinctMDOperandPlaceholder PH(7);
2424 EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
2425 "Unexpected callback to owner");
2426}
2427
2428TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
2429 // This shouldn't crash.
2430 DistinctMDOperandPlaceholder PH(7);
2431 EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
2432}
2433
2434TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
2435 // This shouldn't crash.
2436 DistinctMDOperandPlaceholder PH(7);
2437 MDTuple::getDistinct(Context, &PH);
2438 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2439 "Placeholders can only be used once");
2440}
2441
2442TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
2443 // TrackingMDRef doesn't install an owner callback, so it can't be detected
2444 // as an invalid use. However, using a placeholder in a TrackingMDRef *and*
2445 // a distinct node isn't possible and we should assert.
2446 //
2447 // (There's no positive test for using TrackingMDRef because it's not a
2448 // useful thing to do.)
2449 {
2450 DistinctMDOperandPlaceholder PH(7);
2451 MDTuple::getDistinct(Context, &PH);
2452 EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
2453 }
2454 {
2455 DistinctMDOperandPlaceholder PH(7);
2456 TrackingMDRef Ref(&PH);
2457 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2458 "Placeholders can only be used once");
2459 }
2460}
2461#endif
Duncan P. N. Exon Smith004eb552016-04-23 04:34:11 +00002462#endif
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002463
Duncan P. N. Exon Smith2923a432016-04-23 04:02:39 +00002464} // end namespace