blob: 12256bd23663914e301c88d585eb0be1c0d6b396 [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
Chandler Carruth9a67b072017-06-06 11:06:56 +000010#include "llvm/IR/Metadata.h"
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000013#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000014#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000015#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Instructions.h"
17#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
Duncan P. N. Exon 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() {
Peter Collingbourneb52e2362017-09-12 21:50:41 +000095 return DICompileUnit::getDistinct(
96 Context, 1, getFile(), "clang", false, "-g", 2, "",
97 DICompileUnit::FullDebug, getTuple(), getTuple(), getTuple(),
98 getTuple(), getTuple(), 0, true, false, false);
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,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000106 getBasicType("basictype"), 1, 2, 0, None, 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
Reid Kleckner96ab8722017-05-18 17:24:10 +0000221 I->deleteValue();
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);
Sander de Smalenfdf40912018-01-24 09:56:07 +0000935 auto Count = N->getCount();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000936 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
Sander de Smalenfdf40912018-01-24 09:56:07 +0000937 ASSERT_TRUE(Count);
938 ASSERT_TRUE(Count.is<ConstantInt*>());
939 EXPECT_EQ(5, Count.get<ConstantInt*>()->getSExtValue());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000940 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000941 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
942 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000943
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000944 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000945 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000946}
947
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000948TEST_F(DISubrangeTest, getEmptyArray) {
949 auto *N = DISubrange::get(Context, -1, 0);
Sander de Smalenfdf40912018-01-24 09:56:07 +0000950 auto Count = N->getCount();
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000951 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
Sander de Smalenfdf40912018-01-24 09:56:07 +0000952 ASSERT_TRUE(Count);
953 ASSERT_TRUE(Count.is<ConstantInt*>());
954 EXPECT_EQ(-1, Count.get<ConstantInt*>()->getSExtValue());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000955 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000956 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000957}
958
Sander de Smalenfdf40912018-01-24 09:56:07 +0000959TEST_F(DISubrangeTest, getVariableCount) {
960 DILocalScope *Scope = getSubprogram();
961 DIFile *File = getFile();
962 DIType *Type = getDerivedType();
963 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
964 auto *VlaExpr = DILocalVariable::get(Context, Scope, "vla_expr", File, 8,
965 Type, 2, Flags, 8);
966
967 auto *N = DISubrange::get(Context, VlaExpr, 0);
968 auto Count = N->getCount();
969 ASSERT_TRUE(Count);
970 ASSERT_TRUE(Count.is<DIVariable*>());
971 EXPECT_EQ(VlaExpr, Count.get<DIVariable*>());
972 ASSERT_TRUE(isa<DIVariable>(N->getRawCountNode()));
973 EXPECT_EQ(0, N->getLowerBound());
974 EXPECT_EQ("vla_expr", Count.get<DIVariable*>()->getName());
975 EXPECT_EQ(N, DISubrange::get(Context, VlaExpr, 0));
976}
977
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000978typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000979
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000980TEST_F(DIEnumeratorTest, get) {
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000981 auto *N = DIEnumerator::get(Context, 7, false, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000982 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
983 EXPECT_EQ(7, N->getValue());
Momchil Velikovaf6312a2018-02-14 13:11:56 +0000984 EXPECT_FALSE(N->isUnsigned());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000985 EXPECT_EQ("name", N->getName());
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000986 EXPECT_EQ(N, DIEnumerator::get(Context, 7, false, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000987
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000988 EXPECT_NE(N, DIEnumerator::get(Context, 7, true, "name"));
989 EXPECT_NE(N, DIEnumerator::get(Context, 8, false, "name"));
990 EXPECT_NE(N, DIEnumerator::get(Context, 7, false, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000991
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000992 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000993 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000994}
995
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000996typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000997
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000998TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000999 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001000 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001001 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
1002 EXPECT_EQ("special", N->getName());
1003 EXPECT_EQ(33u, N->getSizeInBits());
1004 EXPECT_EQ(26u, N->getAlignInBits());
1005 EXPECT_EQ(7u, N->getEncoding());
1006 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001007 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001008 26, 7));
1009
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001010 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001011 "special", 33, 26, 7));
1012 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001013 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
1014 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001015 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001016 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001017 25, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001018 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001019 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001020
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001021 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001022 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001023}
1024
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001025TEST_F(DIBasicTypeTest, getWithLargeValues) {
1026 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Victor Leschuk197aa312016-10-18 14:31:22 +00001027 UINT64_MAX, UINT32_MAX - 1, 7);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001028 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk197aa312016-10-18 14:31:22 +00001029 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001030}
1031
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001032TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001033 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001034 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001035 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
1036 EXPECT_EQ("unspecified", N->getName());
1037 EXPECT_EQ(0u, N->getSizeInBits());
1038 EXPECT_EQ(0u, N->getAlignInBits());
1039 EXPECT_EQ(0u, N->getEncoding());
1040 EXPECT_EQ(0u, N->getLine());
1041}
1042
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001043typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001044
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001045TEST_F(DITypeTest, clone) {
1046 // Check that DIType has a specialized clone that returns TempDIType.
1047 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001048 dwarf::DW_ATE_signed);
1049
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001050 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001051 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1052}
1053
Roman Tereshincf88ffa2018-06-01 23:15:09 +00001054TEST_F(DITypeTest, cloneWithFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001055 // void (void)
1056 Metadata *TypesOps[] = {nullptr};
1057 Metadata *Types = MDTuple::get(Context, TypesOps);
1058
Leny Kholodov40c62352016-09-06 17:03:02 +00001059 DIType *D =
1060 DISubroutineType::getDistinct(Context, DINode::FlagZero, 0, Types);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001061 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Roman Tereshincf88ffa2018-06-01 23:15:09 +00001062 TempDIType D2 = D->cloneWithFlags(DINode::FlagRValueReference);
1063 EXPECT_EQ(DINode::FlagRValueReference, D2->getFlags());
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001064 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001065
Leny Kholodov40c62352016-09-06 17:03:02 +00001066 TempDIType T =
1067 DISubroutineType::getTemporary(Context, DINode::FlagZero, 0, Types);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001068 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Roman Tereshincf88ffa2018-06-01 23:15:09 +00001069 TempDIType T2 = T->cloneWithFlags(DINode::FlagRValueReference);
1070 EXPECT_EQ(DINode::FlagRValueReference, T2->getFlags());
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001071 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001072}
1073
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001074typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001075
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001076TEST_F(DIDerivedTypeTest, get) {
1077 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001078 DIScope *Scope = getSubprogram();
1079 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001080 MDTuple *ExtraData = getTuple();
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001081 unsigned DWARFAddressSpace = 8;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001082 DINode::DIFlags Flags5 = static_cast<DINode::DIFlags>(5);
1083 DINode::DIFlags Flags4 = static_cast<DINode::DIFlags>(4);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001084
Leny Kholodov40c62352016-09-06 17:03:02 +00001085 auto *N =
1086 DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", File,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001087 1, Scope, BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
1088 ExtraData);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001089 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
1090 EXPECT_EQ("something", N->getName());
1091 EXPECT_EQ(File, N->getFile());
1092 EXPECT_EQ(1u, N->getLine());
1093 EXPECT_EQ(Scope, N->getScope());
1094 EXPECT_EQ(BaseType, N->getBaseType());
1095 EXPECT_EQ(2u, N->getSizeInBits());
1096 EXPECT_EQ(3u, N->getAlignInBits());
1097 EXPECT_EQ(4u, N->getOffsetInBits());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001098 EXPECT_EQ(DWARFAddressSpace, N->getDWARFAddressSpace().getValue());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001099 EXPECT_EQ(5u, N->getFlags());
1100 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001101 EXPECT_EQ(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,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001103 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001104
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001105 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001106 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001107 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001108 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001109 File, 1, Scope, BaseType, 2, 3,
1110 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001111 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001112 "something", getFile(), 1, Scope, BaseType, 2,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001113 3, 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001114 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001115 "something", File, 2, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001116 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001117 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001118 "something", File, 1, getSubprogram(),
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001119 BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
1120 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001121 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001122 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001123 Scope, getBasicType("basic2"), 2, 3, 4, DWARFAddressSpace,
1124 Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001125 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001126 "something", File, 1, Scope, BaseType, 3, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001127 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001128 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001129 "something", File, 1, Scope, BaseType, 2, 2,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001130 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001131 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001132 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001133 5, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001134 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001135 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001136 4, DWARFAddressSpace + 1, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001137 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001138 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001139 4, DWARFAddressSpace, Flags4, ExtraData));
1140 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1141 "something", File, 1, Scope, BaseType, 2, 3,
1142 4, DWARFAddressSpace, Flags5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001143
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001144 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001145 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001146}
1147
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001148TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1149 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001150 DIScope *Scope = getSubprogram();
1151 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001152 MDTuple *ExtraData = getTuple();
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001153 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001154
Leny Kholodov40c62352016-09-06 17:03:02 +00001155 auto *N = DIDerivedType::get(
1156 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, Scope,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001157 BaseType, UINT64_MAX, UINT32_MAX - 1, UINT64_MAX - 2, UINT32_MAX - 3,
1158 Flags, ExtraData);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001159 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk197aa312016-10-18 14:31:22 +00001160 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001161 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001162 EXPECT_EQ(UINT32_MAX - 3, N->getDWARFAddressSpace().getValue());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001163}
1164
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001165typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001166
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001167TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001168 unsigned Tag = dwarf::DW_TAG_structure_type;
1169 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001170 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001171 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001172 DIScope *Scope = getSubprogram();
1173 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001174 uint64_t SizeInBits = 2;
Victor Leschuk197aa312016-10-18 14:31:22 +00001175 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001176 uint64_t OffsetInBits = 4;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001177 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001178 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001179 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001180 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001181 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001182 StringRef Identifier = "some id";
1183
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001184 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001185 BaseType, SizeInBits, AlignInBits,
1186 OffsetInBits, Flags, Elements, RuntimeLang,
1187 VTableHolder, TemplateParams, Identifier);
1188 EXPECT_EQ(Tag, N->getTag());
1189 EXPECT_EQ(Name, N->getName());
1190 EXPECT_EQ(File, N->getFile());
1191 EXPECT_EQ(Line, N->getLine());
1192 EXPECT_EQ(Scope, N->getScope());
1193 EXPECT_EQ(BaseType, N->getBaseType());
1194 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1195 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1196 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1197 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001198 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001199 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1200 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001201 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001202 EXPECT_EQ(Identifier, N->getIdentifier());
1203
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001204 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001205 BaseType, SizeInBits, AlignInBits,
1206 OffsetInBits, Flags, Elements, RuntimeLang,
1207 VTableHolder, TemplateParams, Identifier));
1208
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001209 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001210 BaseType, SizeInBits, AlignInBits,
1211 OffsetInBits, Flags, Elements, RuntimeLang,
1212 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001213 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001214 BaseType, SizeInBits, AlignInBits,
1215 OffsetInBits, Flags, Elements, RuntimeLang,
1216 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001217 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001218 BaseType, SizeInBits, AlignInBits,
1219 OffsetInBits, Flags, Elements, RuntimeLang,
1220 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001221 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001222 BaseType, SizeInBits, AlignInBits,
1223 OffsetInBits, Flags, Elements, RuntimeLang,
1224 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001225 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001226 Context, Tag, Name, File, Line, getSubprogram(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001227 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1228 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001229 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001230 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1231 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1232 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001233 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001234 BaseType, SizeInBits + 1, AlignInBits,
1235 OffsetInBits, Flags, Elements, RuntimeLang,
1236 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001237 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001238 BaseType, SizeInBits, AlignInBits + 1,
1239 OffsetInBits, Flags, Elements, RuntimeLang,
1240 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001241 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001242 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1243 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1244 VTableHolder, TemplateParams, Identifier));
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001245 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001246 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001247 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001248 AlignInBits, OffsetInBits, FlagsPOne, Elements, RuntimeLang,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001249 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001250 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001251 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1252 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1253 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001254 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001255 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1256 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1257 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001258 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001259 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1260 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1261 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001262 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001263 BaseType, SizeInBits, AlignInBits,
1264 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001265 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001266 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001267 BaseType, SizeInBits, AlignInBits,
1268 OffsetInBits, Flags, Elements, RuntimeLang,
1269 VTableHolder, TemplateParams, "other"));
1270
1271 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001272 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1273 BaseType, SizeInBits, AlignInBits,
1274 OffsetInBits, Flags, Elements, RuntimeLang,
1275 VTableHolder, TemplateParams, "")
1276 ->getRawIdentifier());
1277 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1278 BaseType, SizeInBits, AlignInBits,
1279 OffsetInBits, Flags, Elements, RuntimeLang,
1280 VTableHolder, TemplateParams)
1281 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001282
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001283 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001284 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001285}
1286
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001287TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001288 unsigned Tag = dwarf::DW_TAG_structure_type;
1289 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001290 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001291 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001292 DIScope *Scope = getSubprogram();
1293 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001294 uint64_t SizeInBits = UINT64_MAX;
Victor Leschuk197aa312016-10-18 14:31:22 +00001295 uint32_t AlignInBits = UINT32_MAX - 1;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001296 uint64_t OffsetInBits = UINT64_MAX - 2;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001297 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001298 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001299 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001300 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001301 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001302 StringRef Identifier = "some id";
1303
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001304 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001305 BaseType, SizeInBits, AlignInBits,
1306 OffsetInBits, Flags, Elements, RuntimeLang,
1307 VTableHolder, TemplateParams, Identifier);
1308 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1309 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1310 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1311}
1312
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001313TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001314 unsigned Tag = dwarf::DW_TAG_structure_type;
1315 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001316 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001317 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001318 DIScope *Scope = getSubprogram();
1319 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001320 uint64_t SizeInBits = 2;
Victor Leschuk197aa312016-10-18 14:31:22 +00001321 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001322 uint64_t OffsetInBits = 4;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001323 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001324 unsigned RuntimeLang = 6;
1325 StringRef Identifier = "some id";
1326
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001327 auto *N = DICompositeType::get(
1328 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1329 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001330
1331 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001332 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001333 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001334 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001335 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001336 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001337
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001338 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001339 EXPECT_EQ(nullptr, N->getVTableHolder());
1340 N->replaceVTableHolder(VTableHolder);
1341 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Adrian Prantla8e56452017-11-08 22:04:43 +00001342 // As an extension, the containing type can be anything. This is
1343 // used by Rust to associate vtables with their concrete type.
1344 DIType *BasicType = getBasicType("basic");
1345 N->replaceVTableHolder(BasicType);
1346 EXPECT_EQ(BasicType, N->getVTableHolder());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001347 N->replaceVTableHolder(nullptr);
1348 EXPECT_EQ(nullptr, N->getVTableHolder());
1349
1350 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001351 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001352 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001353 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001354 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001355 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001356}
1357
Adrian Prantl8c599212018-02-06 23:45:59 +00001358TEST_F(DICompositeTypeTest, variant_part) {
1359 unsigned Tag = dwarf::DW_TAG_variant_part;
1360 StringRef Name = "some name";
1361 DIFile *File = getFile();
1362 unsigned Line = 1;
1363 DIScope *Scope = getSubprogram();
1364 DIType *BaseType = getCompositeType();
1365 uint64_t SizeInBits = 2;
1366 uint32_t AlignInBits = 3;
1367 uint64_t OffsetInBits = 4;
1368 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
1369 unsigned RuntimeLang = 6;
1370 StringRef Identifier = "some id";
1371 DIDerivedType *Discriminator = cast<DIDerivedType>(getDerivedType());
1372 DIDerivedType *Discriminator2 = cast<DIDerivedType>(getDerivedType());
1373
1374 EXPECT_NE(Discriminator, Discriminator2);
1375
1376 auto *N = DICompositeType::get(
1377 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1378 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1379 Discriminator);
1380
1381 // Test the hashing.
1382 auto *Same = DICompositeType::get(
1383 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1384 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1385 Discriminator);
1386 auto *Other = DICompositeType::get(
1387 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1388 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1389 Discriminator2);
1390 auto *NoDisc = DICompositeType::get(
1391 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1392 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1393 nullptr);
1394
1395 EXPECT_EQ(N, Same);
1396 EXPECT_NE(Same, Other);
1397 EXPECT_NE(Same, NoDisc);
1398 EXPECT_NE(Other, NoDisc);
1399
1400 EXPECT_EQ(N->getDiscriminator(), Discriminator);
1401}
1402
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001403typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001404
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001405TEST_F(DISubroutineTypeTest, get) {
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001406 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(1);
1407 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001408 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001409
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001410 auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001411 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1412 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001413 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001414 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001415
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001416 EXPECT_NE(N, DISubroutineType::get(Context, FlagsPOne, 0, TypeArray));
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001417 EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple()));
1418
1419 // Test the hashing of calling conventions.
1420 auto *Fast = DISubroutineType::get(
1421 Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray);
1422 auto *Std = DISubroutineType::get(Context, Flags,
1423 dwarf::DW_CC_BORLAND_stdcall, TypeArray);
1424 EXPECT_EQ(Fast,
1425 DISubroutineType::get(Context, Flags,
1426 dwarf::DW_CC_BORLAND_msfastcall, TypeArray));
1427 EXPECT_EQ(Std, DISubroutineType::get(
1428 Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray));
1429
1430 EXPECT_NE(N, Fast);
1431 EXPECT_NE(N, Std);
1432 EXPECT_NE(Fast, Std);
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001433
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001434 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001435 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001436
1437 // Test always-empty operands.
1438 EXPECT_EQ(nullptr, N->getScope());
1439 EXPECT_EQ(nullptr, N->getFile());
1440 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001441}
1442
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001443typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001444
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001445TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001446 StringRef Filename = "file";
1447 StringRef Directory = "dir";
Scott Linder71603842018-02-12 19:45:54 +00001448 DIFile::ChecksumKind CSKind = DIFile::ChecksumKind::CSK_MD5;
1449 StringRef ChecksumString = "000102030405060708090a0b0c0d0e0f";
1450 DIFile::ChecksumInfo<StringRef> Checksum(CSKind, ChecksumString);
Scott Linder16c7bda2018-02-23 23:01:06 +00001451 StringRef Source = "source";
1452 auto *N = DIFile::get(Context, Filename, Directory, Checksum, Source);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001453
1454 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1455 EXPECT_EQ(Filename, N->getFilename());
1456 EXPECT_EQ(Directory, N->getDirectory());
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001457 EXPECT_EQ(Checksum, N->getChecksum());
Scott Linder16c7bda2018-02-23 23:01:06 +00001458 EXPECT_EQ(Source, N->getSource());
1459 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory, Checksum, Source));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001460
Scott Linder16c7bda2018-02-23 23:01:06 +00001461 EXPECT_NE(N, DIFile::get(Context, "other", Directory, Checksum, Source));
1462 EXPECT_NE(N, DIFile::get(Context, Filename, "other", Checksum, Source));
Scott Linder71603842018-02-12 19:45:54 +00001463 DIFile::ChecksumInfo<StringRef> OtherChecksum(DIFile::ChecksumKind::CSK_SHA1, ChecksumString);
Reid Kleckner26fa1bf2017-09-19 18:14:45 +00001464 EXPECT_NE(
Scott Linder71603842018-02-12 19:45:54 +00001465 N, DIFile::get(Context, Filename, Directory, OtherChecksum));
Scott Linder16c7bda2018-02-23 23:01:06 +00001466 StringRef OtherSource = "other";
1467 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum, OtherSource));
1468 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum));
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001469 EXPECT_NE(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001470
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001471 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001472 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001473}
1474
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001475TEST_F(DIFileTest, ScopeGetFile) {
1476 // Ensure that DIScope::getFile() returns itself.
1477 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001478 EXPECT_EQ(N, N->getFile());
1479}
1480
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001481typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001482
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001483TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001484 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001485 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001486 StringRef Producer = "some producer";
1487 bool IsOptimized = false;
1488 StringRef Flags = "flag after flag";
1489 unsigned RuntimeVersion = 2;
1490 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001491 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001492 MDTuple *EnumTypes = getTuple();
1493 MDTuple *RetainedTypes = getTuple();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001494 MDTuple *GlobalVariables = getTuple();
1495 MDTuple *ImportedEntities = getTuple();
Adrian Prantle3b49e02015-09-22 23:42:47 +00001496 uint64_t DWOId = 0x10000000c0ffee;
Amjad Abouda9bcf162015-12-10 12:56:35 +00001497 MDTuple *Macros = getTuple();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001498 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001499 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1500 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Dehao Chen0944a8c2017-02-01 22:45:09 +00001501 RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true,
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001502 false, false);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001503
1504 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1505 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1506 EXPECT_EQ(File, N->getFile());
1507 EXPECT_EQ(Producer, N->getProducer());
1508 EXPECT_EQ(IsOptimized, N->isOptimized());
1509 EXPECT_EQ(Flags, N->getFlags());
1510 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1511 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1512 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001513 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1514 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001515 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1516 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001517 EXPECT_EQ(Macros, N->getMacros().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001518 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001519
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001520 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001521 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1522 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1523 EXPECT_EQ(File, Temp->getFile());
1524 EXPECT_EQ(Producer, Temp->getProducer());
1525 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1526 EXPECT_EQ(Flags, Temp->getFlags());
1527 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1528 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1529 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1530 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1531 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001532 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1533 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001534 EXPECT_EQ(Macros, Temp->getMacros().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001535 EXPECT_EQ(DWOId, Temp->getDWOId());
1536
1537 auto *TempAddress = Temp.get();
1538 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1539 EXPECT_TRUE(Clone->isDistinct());
1540 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001541}
1542
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001543TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001544 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001545 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001546 StringRef Producer = "some producer";
1547 bool IsOptimized = false;
1548 StringRef Flags = "flag after flag";
1549 unsigned RuntimeVersion = 2;
1550 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001551 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001552 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1553 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1554 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001555 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001556 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001557 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1558 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001559 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true, false,
1560 false);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001561
1562 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001563 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001564 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001565 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001566 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001567 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001568
1569 auto *Macros = MDTuple::getDistinct(Context, None);
1570 EXPECT_EQ(nullptr, N->getMacros().get());
1571 N->replaceMacros(Macros);
1572 EXPECT_EQ(Macros, N->getMacros().get());
1573 N->replaceMacros(nullptr);
1574 EXPECT_EQ(nullptr, N->getMacros().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001575}
1576
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001577typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001578
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001579TEST_F(DISubprogramTest, get) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001580 DIScope *Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001581 StringRef Name = "name";
1582 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001583 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001584 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001585 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001586 bool IsLocalToUnit = false;
1587 bool IsDefinition = true;
1588 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001589 DIType *ContainingType = getCompositeType();
Davide Italiano236e7442016-04-13 20:17:42 +00001590 unsigned Virtuality = 2;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001591 unsigned VirtualIndex = 5;
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001592 int ThisAdjustment = -3;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001593 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(6);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001594 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001595 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001596 DISubprogram *Declaration = getSubprogram();
Shiva Chen2c864552018-05-09 02:40:45 +00001597 MDTuple *RetainedNodes = getTuple();
Adrian Prantl1d12b882017-04-26 22:56:44 +00001598 MDTuple *ThrownTypes = getTuple();
Adrian Prantl75819ae2016-04-15 15:57:41 +00001599 DICompileUnit *Unit = getUnit();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001600
Adrian Prantl1d12b882017-04-26 22:56:44 +00001601 auto *N = DISubprogram::get(
1602 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1603 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex,
1604 ThisAdjustment, Flags, IsOptimized, Unit, TemplateParams, Declaration,
Shiva Chen2c864552018-05-09 02:40:45 +00001605 RetainedNodes, ThrownTypes);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001606
1607 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1608 EXPECT_EQ(Scope, N->getScope());
1609 EXPECT_EQ(Name, N->getName());
1610 EXPECT_EQ(LinkageName, N->getLinkageName());
1611 EXPECT_EQ(File, N->getFile());
1612 EXPECT_EQ(Line, N->getLine());
1613 EXPECT_EQ(Type, N->getType());
1614 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1615 EXPECT_EQ(IsDefinition, N->isDefinition());
1616 EXPECT_EQ(ScopeLine, N->getScopeLine());
1617 EXPECT_EQ(ContainingType, N->getContainingType());
1618 EXPECT_EQ(Virtuality, N->getVirtuality());
1619 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001620 EXPECT_EQ(ThisAdjustment, N->getThisAdjustment());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001621 EXPECT_EQ(Flags, N->getFlags());
1622 EXPECT_EQ(IsOptimized, N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001623 EXPECT_EQ(Unit, N->getUnit());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001624 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001625 EXPECT_EQ(Declaration, N->getDeclaration());
Shiva Chen2c864552018-05-09 02:40:45 +00001626 EXPECT_EQ(RetainedNodes, N->getRetainedNodes().get());
Adrian Prantl1d12b882017-04-26 22:56:44 +00001627 EXPECT_EQ(ThrownTypes, N->getThrownTypes().get());
1628 EXPECT_EQ(N, DISubprogram::get(
1629 Context, Scope, Name, LinkageName, File, Line, Type,
1630 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1631 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001632 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001633
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001634 EXPECT_NE(N, DISubprogram::get(
1635 Context, getCompositeType(), Name, LinkageName, File, Line,
1636 Type, IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1637 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001638 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001639 EXPECT_NE(N, DISubprogram::get(
1640 Context, Scope, "other", LinkageName, File, Line, Type,
1641 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1642 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001643 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Adrian Prantl1d12b882017-04-26 22:56:44 +00001644 EXPECT_NE(N, DISubprogram::get(
1645 Context, Scope, Name, "other", File, Line, Type,
1646 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1647 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001648 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001649 EXPECT_NE(N, DISubprogram::get(
1650 Context, Scope, Name, LinkageName, getFile(), Line, Type,
1651 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1652 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001653 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001654 EXPECT_NE(N, DISubprogram::get(
1655 Context, Scope, Name, LinkageName, File, Line + 1, Type,
1656 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1657 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001658 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001659 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001660 getSubroutineType(), IsLocalToUnit,
1661 IsDefinition, ScopeLine, ContainingType,
1662 Virtuality, VirtualIndex, ThisAdjustment,
1663 Flags, IsOptimized, Unit, TemplateParams,
Shiva Chen2c864552018-05-09 02:40:45 +00001664 Declaration, RetainedNodes, ThrownTypes));
Adrian Prantl1d12b882017-04-26 22:56:44 +00001665 EXPECT_NE(N, DISubprogram::get(
1666 Context, Scope, Name, LinkageName, File, Line, Type,
1667 !IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1668 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001669 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Adrian Prantl1d12b882017-04-26 22:56:44 +00001670 EXPECT_NE(N, DISubprogram::get(
1671 Context, Scope, Name, LinkageName, File, Line, Type,
1672 IsLocalToUnit, !IsDefinition, ScopeLine, ContainingType,
1673 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001674 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001675 EXPECT_NE(N, DISubprogram::get(
1676 Context, Scope, Name, LinkageName, File, Line, Type,
1677 IsLocalToUnit, IsDefinition, ScopeLine + 1, ContainingType,
1678 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001679 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Adrian Prantl1d12b882017-04-26 22:56:44 +00001680 EXPECT_NE(N, DISubprogram::get(
1681 Context, Scope, Name, LinkageName, File, Line, Type,
1682 IsLocalToUnit, IsDefinition, ScopeLine, getCompositeType(),
1683 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001684 Unit, TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001685 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001686 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1687 ContainingType, Virtuality + 1, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001688 ThisAdjustment, Flags, IsOptimized, Unit,
Shiva Chen2c864552018-05-09 02:40:45 +00001689 TemplateParams, Declaration, RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001690 ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001691 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001692 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1693 ContainingType, Virtuality, VirtualIndex + 1,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001694 ThisAdjustment, Flags, IsOptimized, Unit,
Shiva Chen2c864552018-05-09 02:40:45 +00001695 TemplateParams, Declaration, RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001696 ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001697 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001698 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1699 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001700 ThisAdjustment, Flags, !IsOptimized, Unit,
Shiva Chen2c864552018-05-09 02:40:45 +00001701 TemplateParams, Declaration, RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001702 ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001703 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001704 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1705 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001706 ThisAdjustment, Flags, IsOptimized, nullptr,
Shiva Chen2c864552018-05-09 02:40:45 +00001707 TemplateParams, Declaration, RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001708 ThrownTypes));
1709 EXPECT_NE(N, DISubprogram::get(
1710 Context, Scope, Name, LinkageName, File, Line, Type,
1711 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1712 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001713 Unit, getTuple(), Declaration, RetainedNodes, ThrownTypes));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001714 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1715 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1716 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001717 ThisAdjustment, Flags, IsOptimized, Unit,
Shiva Chen2c864552018-05-09 02:40:45 +00001718 TemplateParams, getSubprogram(), RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001719 ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001720 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001721 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1722 ContainingType, Virtuality, VirtualIndex,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001723 ThisAdjustment, Flags, IsOptimized, Unit,
1724 TemplateParams, Declaration, getTuple()));
Adrian Prantl1d12b882017-04-26 22:56:44 +00001725 EXPECT_NE(N, DISubprogram::get(
1726 Context, Scope, Name, LinkageName, File, Line, Type,
1727 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1728 Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
Shiva Chen2c864552018-05-09 02:40:45 +00001729 Unit, TemplateParams, Declaration, RetainedNodes, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001730
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001731 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001732 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001733}
1734
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001735typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001736
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001737TEST_F(DILexicalBlockTest, get) {
1738 DILocalScope *Scope = getSubprogram();
1739 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001740 unsigned Line = 5;
1741 unsigned Column = 8;
1742
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001743 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001744
1745 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1746 EXPECT_EQ(Scope, N->getScope());
1747 EXPECT_EQ(File, N->getFile());
1748 EXPECT_EQ(Line, N->getLine());
1749 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001750 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001751
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001752 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001753 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1754 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1755 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1756 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001757
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001758 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001759 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001760}
1761
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +00001762TEST_F(DILexicalBlockTest, Overflow) {
1763 DISubprogram *SP = getSubprogram();
1764 DIFile *F = getFile();
1765 {
1766 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1767 EXPECT_EQ(2u, LB->getLine());
1768 EXPECT_EQ(7u, LB->getColumn());
1769 }
1770 unsigned U16 = 1u << 16;
1771 {
1772 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1773 EXPECT_EQ(UINT32_MAX, LB->getLine());
1774 EXPECT_EQ(U16 - 1, LB->getColumn());
1775 }
1776 {
1777 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1778 EXPECT_EQ(UINT32_MAX, LB->getLine());
1779 EXPECT_EQ(0u, LB->getColumn());
1780 }
1781 {
1782 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1783 EXPECT_EQ(UINT32_MAX, LB->getLine());
1784 EXPECT_EQ(0u, LB->getColumn());
1785 }
1786}
1787
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001788typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001789
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001790TEST_F(DILexicalBlockFileTest, get) {
1791 DILocalScope *Scope = getSubprogram();
1792 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001793 unsigned Discriminator = 5;
1794
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001795 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001796
1797 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1798 EXPECT_EQ(Scope, N->getScope());
1799 EXPECT_EQ(File, N->getFile());
1800 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001801 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001802
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001803 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001804 Discriminator));
1805 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001806 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001807 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001808 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001809
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001810 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001811 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001812}
1813
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001814typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001815
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001816TEST_F(DINamespaceTest, get) {
1817 DIScope *Scope = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001818 StringRef Name = "namespace";
Adrian Prantldbfda632016-11-03 19:42:02 +00001819 bool ExportSymbols = true;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001820
Adrian Prantlfed4f392017-04-28 22:25:46 +00001821 auto *N = DINamespace::get(Context, Scope, Name, ExportSymbols);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001822
1823 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1824 EXPECT_EQ(Scope, N->getScope());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001825 EXPECT_EQ(Name, N->getName());
Adrian Prantlfed4f392017-04-28 22:25:46 +00001826 EXPECT_EQ(N, DINamespace::get(Context, Scope, Name, ExportSymbols));
1827 EXPECT_NE(N, DINamespace::get(Context, getFile(), Name, ExportSymbols));
1828 EXPECT_NE(N, DINamespace::get(Context, Scope, "other", ExportSymbols));
1829 EXPECT_NE(N, DINamespace::get(Context, Scope, Name, !ExportSymbols));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001830
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001831 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001832 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001833}
1834
Adrian Prantlab1243f2015-06-29 23:03:47 +00001835typedef MetadataTest DIModuleTest;
1836
1837TEST_F(DIModuleTest, get) {
1838 DIScope *Scope = getFile();
1839 StringRef Name = "module";
1840 StringRef ConfigMacro = "-DNDEBUG";
1841 StringRef Includes = "-I.";
1842 StringRef Sysroot = "/";
1843
1844 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1845
1846 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1847 EXPECT_EQ(Scope, N->getScope());
1848 EXPECT_EQ(Name, N->getName());
1849 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1850 EXPECT_EQ(Includes, N->getIncludePath());
1851 EXPECT_EQ(Sysroot, N->getISysRoot());
1852 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1853 ConfigMacro, Includes, Sysroot));
1854 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1855 ConfigMacro, Includes, Sysroot));
1856 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1857 ConfigMacro, Includes, Sysroot));
1858 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1859 "other", Includes, Sysroot));
1860 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1861 ConfigMacro, "other", Sysroot));
1862 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1863 ConfigMacro, Includes, "other"));
1864
1865 TempDIModule Temp = N->clone();
1866 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1867}
1868
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001869typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001870
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001871TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001872 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001873 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001874
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001875 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001876
1877 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001878 EXPECT_EQ(Name, N->getName());
1879 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001880 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001881
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001882 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001883 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001884 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001885
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001886 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001887 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001888}
1889
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001890typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001891
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001892TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001893 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001894 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001895 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001896 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001897
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001898 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001899 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001900 EXPECT_EQ(Name, N->getName());
1901 EXPECT_EQ(Type, N->getType());
1902 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001903 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001904
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001905 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001906 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1907 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001908 EXPECT_NE(N,
1909 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1910 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001911 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001912 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001913 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001914
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001915 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001916 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001917}
1918
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001919typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001920
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001921TEST_F(DIGlobalVariableTest, get) {
1922 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001923 StringRef Name = "name";
1924 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001925 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001926 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001927 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001928 bool IsLocalToUnit = false;
1929 bool IsDefinition = true;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001930 DIDerivedType *StaticDataMemberDeclaration =
1931 cast<DIDerivedType>(getDerivedType());
Victor Leschuka37660c2016-10-26 21:32:29 +00001932 uint32_t AlignInBits = 8;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001933
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001934 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001935 Type, IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001936 StaticDataMemberDeclaration, AlignInBits);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001937 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1938 EXPECT_EQ(Scope, N->getScope());
1939 EXPECT_EQ(Name, N->getName());
1940 EXPECT_EQ(LinkageName, N->getLinkageName());
1941 EXPECT_EQ(File, N->getFile());
1942 EXPECT_EQ(Line, N->getLine());
1943 EXPECT_EQ(Type, N->getType());
1944 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1945 EXPECT_EQ(IsDefinition, N->isDefinition());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001946 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Victor Leschuk2ede1262016-10-20 00:13:12 +00001947 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001948 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001949 Line, Type, IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001950 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001951
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001952 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001953 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001954 File, Line, Type, IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001955 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001956 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001957 Line, Type, IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001958 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001959 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001960 Type, IsLocalToUnit, IsDefinition,
1961 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001962 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001963 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001964 Line, Type, IsLocalToUnit, IsDefinition,
1965 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001966 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001967 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001968 Line + 1, Type, IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001969 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001970 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001971 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001972 getDerivedType(), IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001973 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001974 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001975 Line, Type, !IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001976 StaticDataMemberDeclaration, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001977 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001978 Line, Type, IsLocalToUnit, !IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001979 StaticDataMemberDeclaration, AlignInBits));
1980 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1981 Line, Type, IsLocalToUnit, IsDefinition,
1982 cast<DIDerivedType>(getDerivedType()),
Victor Leschuk2ede1262016-10-20 00:13:12 +00001983 AlignInBits));
Peter Collingbourned4135bb2016-09-13 01:12:59 +00001984 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1985 Line, Type, IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001986 StaticDataMemberDeclaration,
Victor Leschuk2ede1262016-10-20 00:13:12 +00001987 (AlignInBits << 1)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001988
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001989 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001990 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001991}
1992
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001993typedef MetadataTest DIGlobalVariableExpressionTest;
1994
1995TEST_F(DIGlobalVariableExpressionTest, get) {
1996 DIScope *Scope = getSubprogram();
1997 StringRef Name = "name";
1998 StringRef LinkageName = "linkage";
1999 DIFile *File = getFile();
2000 unsigned Line = 5;
2001 DIType *Type = getDerivedType();
2002 bool IsLocalToUnit = false;
2003 bool IsDefinition = true;
2004 auto *Expr = DIExpression::get(Context, {1, 2});
2005 auto *Expr2 = DIExpression::get(Context, {1, 2, 3});
2006 DIDerivedType *StaticDataMemberDeclaration =
2007 cast<DIDerivedType>(getDerivedType());
2008 uint32_t AlignInBits = 8;
2009
2010 auto *Var = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2011 Line, Type, IsLocalToUnit, IsDefinition,
2012 StaticDataMemberDeclaration, AlignInBits);
2013 auto *Var2 = DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
2014 Line, Type, IsLocalToUnit, IsDefinition,
2015 StaticDataMemberDeclaration, AlignInBits);
2016 auto *N = DIGlobalVariableExpression::get(Context, Var, Expr);
2017
2018 EXPECT_EQ(Var, N->getVariable());
2019 EXPECT_EQ(Expr, N->getExpression());
2020 EXPECT_EQ(N, DIGlobalVariableExpression::get(Context, Var, Expr));
2021 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var2, Expr));
2022 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var, Expr2));
2023
2024 TempDIGlobalVariableExpression Temp = N->clone();
2025 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2026}
2027
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002028typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002029
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002030TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002031 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002032 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002033 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002034 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002035 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002036 unsigned Arg = 6;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00002037 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
Victor Leschuka37660c2016-10-26 21:32:29 +00002038 uint32_t AlignInBits = 8;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002039
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002040 auto *N =
Victor Leschuk2ede1262016-10-20 00:13:12 +00002041 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags,
2042 AlignInBits);
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002043 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002044 EXPECT_EQ(Scope, N->getScope());
2045 EXPECT_EQ(Name, N->getName());
2046 EXPECT_EQ(File, N->getFile());
2047 EXPECT_EQ(Line, N->getLine());
2048 EXPECT_EQ(Type, N->getType());
2049 EXPECT_EQ(Arg, N->getArg());
2050 EXPECT_EQ(Flags, N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002051 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002052 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002053 Flags, AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002054
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002055 EXPECT_FALSE(
Victor Leschuk2ede1262016-10-20 00:13:12 +00002056 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags,
2057 AlignInBits)->isParameter());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002058 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002059 Type, Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002060 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002061 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002062 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002063 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002064 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002065 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002066 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002067 getDerivedType(), Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002068 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002069 Arg + 1, Flags, AlignInBits));
2070 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
2071 Arg, Flags, (AlignInBits << 1)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002072
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002073 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002074 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002075}
2076
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002077TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002078 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002079 0, nullptr, 255, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002080 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002081 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002082 0, nullptr, 256, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002083 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002084 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002085 0, nullptr, 257, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002086 ->getArg());
2087 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002088 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002089 0, nullptr, Max, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002090 ->getArg());
2091}
2092
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002093typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002094
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002095TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002096 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002097 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002098 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002099 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00002100
2101 EXPECT_EQ(5u, N->getNumElements());
2102 EXPECT_EQ(2u, N->getElement(0));
2103 EXPECT_EQ(6u, N->getElement(1));
2104 EXPECT_EQ(9u, N->getElement(2));
2105 EXPECT_EQ(78u, N->getElement(3));
2106 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002107
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002108 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002109 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Adrian Prantld1317012017-12-08 21:58:18 +00002110
2111 // Test DIExpression::prepend().
2112 uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
2113 auto *N0 = DIExpression::get(Context, Elts0);
2114 N0 = DIExpression::prepend(N0, true, 64, true, true);
2115 uint64_t Elts1[] = {dwarf::DW_OP_deref,
2116 dwarf::DW_OP_plus_uconst, 64,
2117 dwarf::DW_OP_deref,
2118 dwarf::DW_OP_stack_value,
2119 dwarf::DW_OP_LLVM_fragment, 0, 32};
2120 auto *N1 = DIExpression::get(Context, Elts1);
2121 EXPECT_EQ(N0, N1);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002122}
2123
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002124TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002125#define EXPECT_VALID(...) \
2126 do { \
2127 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002128 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002129 } while (false)
2130#define EXPECT_INVALID(...) \
2131 do { \
2132 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002133 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002134 } while (false)
2135
2136 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002137 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002138
2139 // Valid constructions.
Florian Hahnc9c403c2017-06-13 16:54:44 +00002140 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6);
Florian Hahnffc498d2017-06-14 13:14:38 +00002141 EXPECT_VALID(dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus);
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002142 EXPECT_VALID(dwarf::DW_OP_deref);
Adrian Prantl941fa752016-12-05 18:04:47 +00002143 EXPECT_VALID(dwarf::DW_OP_LLVM_fragment, 3, 7);
Florian Hahnffc498d2017-06-14 13:14:38 +00002144 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref);
2145 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6);
Adrian Prantl941fa752016-12-05 18:04:47 +00002146 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_LLVM_fragment, 3, 7);
Florian Hahnffc498d2017-06-14 13:14:38 +00002147 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6,
Adrian Prantl941fa752016-12-05 18:04:47 +00002148 dwarf::DW_OP_LLVM_fragment, 3, 7);
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002149
2150 // Invalid constructions.
2151 EXPECT_INVALID(~0u);
Florian Hahnffc498d2017-06-14 13:14:38 +00002152 EXPECT_INVALID(dwarf::DW_OP_plus, 0);
Florian Hahnc9c403c2017-06-13 16:54:44 +00002153 EXPECT_INVALID(dwarf::DW_OP_plus_uconst);
Adrian Prantl941fa752016-12-05 18:04:47 +00002154 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment);
2155 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3);
Florian Hahnffc498d2017-06-14 13:14:38 +00002156 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_plus_uconst, 3);
Adrian Prantl941fa752016-12-05 18:04:47 +00002157 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_deref);
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002158
2159#undef EXPECT_VALID
2160#undef EXPECT_INVALID
2161}
2162
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002163typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002164
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002165TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002166 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002167 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002168 unsigned Line = 5;
2169 StringRef GetterName = "getter";
2170 StringRef SetterName = "setter";
2171 unsigned Attributes = 7;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002172 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002173
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002174 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002175 SetterName, Attributes, Type);
2176
2177 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
2178 EXPECT_EQ(Name, N->getName());
2179 EXPECT_EQ(File, N->getFile());
2180 EXPECT_EQ(Line, N->getLine());
2181 EXPECT_EQ(GetterName, N->getGetterName());
2182 EXPECT_EQ(SetterName, N->getSetterName());
2183 EXPECT_EQ(Attributes, N->getAttributes());
2184 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002185 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002186 SetterName, Attributes, Type));
2187
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002188 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002189 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002190 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002191 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002192 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002193 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002194 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002195 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002196 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002197 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002198 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002199 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002200 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002201 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00002202 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002203
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002204 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002205 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002206}
2207
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002208typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002209
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002210TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002211 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002212 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002213 DINode *Entity = getCompositeType();
Adrian Prantld63bfd22017-07-19 00:09:54 +00002214 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002215 unsigned Line = 5;
2216 StringRef Name = "name";
2217
Adrian Prantld63bfd22017-07-19 00:09:54 +00002218 auto *N =
2219 DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002220
2221 EXPECT_EQ(Tag, N->getTag());
2222 EXPECT_EQ(Scope, N->getScope());
2223 EXPECT_EQ(Entity, N->getEntity());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002224 EXPECT_EQ(File, N->getFile());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002225 EXPECT_EQ(Line, N->getLine());
2226 EXPECT_EQ(Name, N->getName());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002227 EXPECT_EQ(
2228 N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002229
2230 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002231 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Adrian Prantld63bfd22017-07-19 00:09:54 +00002232 Scope, Entity, File, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002233 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Adrian Prantld63bfd22017-07-19 00:09:54 +00002234 File, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002235 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Adrian Prantld63bfd22017-07-19 00:09:54 +00002236 File, Line, Name));
2237 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, nullptr, Line,
2238 Name));
2239 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File,
2240 Line + 1, Name));
2241 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
2242 "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002243
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002244 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002245 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002246}
2247
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002248typedef MetadataTest MetadataAsValueTest;
2249
2250TEST_F(MetadataAsValueTest, MDNode) {
2251 MDNode *N = MDNode::get(Context, None);
2252 auto *V = MetadataAsValue::get(Context, N);
2253 EXPECT_TRUE(V->getType()->isMetadataTy());
2254 EXPECT_EQ(N, V->getMetadata());
2255
2256 auto *V2 = MetadataAsValue::get(Context, N);
2257 EXPECT_EQ(V, V2);
2258}
2259
2260TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2261 MDNode *N = MDNode::get(Context, None);
2262 Metadata *Ops[] = {N};
2263 MDNode *N2 = MDNode::get(Context, Ops);
2264 auto *V = MetadataAsValue::get(Context, N2);
2265 EXPECT_TRUE(V->getType()->isMetadataTy());
2266 EXPECT_EQ(N2, V->getMetadata());
2267
2268 auto *V2 = MetadataAsValue::get(Context, N2);
2269 EXPECT_EQ(V, V2);
2270
2271 auto *V3 = MetadataAsValue::get(Context, N);
2272 EXPECT_TRUE(V3->getType()->isMetadataTy());
2273 EXPECT_NE(V, V3);
2274 EXPECT_EQ(N, V3->getMetadata());
2275}
2276
2277TEST_F(MetadataAsValueTest, MDNodeConstant) {
2278 auto *C = ConstantInt::getTrue(Context);
2279 auto *MD = ConstantAsMetadata::get(C);
2280 Metadata *Ops[] = {MD};
2281 auto *N = MDNode::get(Context, Ops);
2282
2283 auto *V = MetadataAsValue::get(Context, MD);
2284 EXPECT_TRUE(V->getType()->isMetadataTy());
2285 EXPECT_EQ(MD, V->getMetadata());
2286
2287 auto *V2 = MetadataAsValue::get(Context, N);
2288 EXPECT_EQ(MD, V2->getMetadata());
2289 EXPECT_EQ(V, V2);
2290}
2291
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002292typedef MetadataTest ValueAsMetadataTest;
2293
2294TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2295 Type *Ty = Type::getInt1PtrTy(Context);
2296 std::unique_ptr<GlobalVariable> GV0(
2297 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2298 auto *MD = ValueAsMetadata::get(GV0.get());
2299 EXPECT_TRUE(MD->getValue() == GV0.get());
2300 ASSERT_TRUE(GV0->use_empty());
2301
2302 std::unique_ptr<GlobalVariable> GV1(
2303 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2304 GV0->replaceAllUsesWith(GV1.get());
2305 EXPECT_TRUE(MD->getValue() == GV1.get());
2306}
2307
Adrian Prantlcbec1602016-02-08 17:02:34 +00002308TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2309 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002310 ConstantAsMetadata *CI =
2311 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Adrian Prantlcbec1602016-02-08 17:02:34 +00002312
Adrian Prantlcbec1602016-02-08 17:02:34 +00002313 auto Temp1 = MDTuple::getTemporary(Context, None);
Adrian Prantl817c47b2016-02-08 19:13:15 +00002314 auto Temp2 = MDTuple::getTemporary(Context, {CI});
2315 auto *N = MDTuple::get(Context, {Temp1.get()});
Adrian Prantlcbec1602016-02-08 17:02:34 +00002316
2317 // Test replacing a temporary node with another temporary node.
2318 Temp1->replaceAllUsesWith(Temp2.get());
2319 EXPECT_EQ(N->getOperand(0), Temp2.get());
2320
2321 // Clean up Temp2 for teardown.
2322 Temp2->replaceAllUsesWith(nullptr);
2323}
2324
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002325TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2326 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002327 ConstantAsMetadata *CI =
2328 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002329
2330 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002331 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002332
2333 // 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 +00002334 Metadata *Ops1[] = {CI, CI, Temp.get()};
2335 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002336
2337 auto *N1 = MDTuple::get(Context, Ops1);
2338 auto *N2 = MDTuple::get(Context, Ops2);
2339 ASSERT_NE(N1, N2);
2340
2341 // Tell metadata that the constant is getting deleted.
2342 //
2343 // After this, N1 will be invalid, so don't touch it.
2344 ValueAsMetadata::handleDeletion(CI->getValue());
2345 EXPECT_EQ(nullptr, N2->getOperand(0));
2346 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002347 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002348
2349 // Clean up Temp for teardown.
2350 Temp->replaceAllUsesWith(nullptr);
2351}
2352
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002353typedef MetadataTest TrackingMDRefTest;
2354
2355TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2356 Type *Ty = Type::getInt1PtrTy(Context);
2357 std::unique_ptr<GlobalVariable> GV0(
2358 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2359 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2360 EXPECT_TRUE(MD->getValue() == GV0.get());
2361 ASSERT_TRUE(GV0->use_empty());
2362
2363 std::unique_ptr<GlobalVariable> GV1(
2364 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2365 GV0->replaceAllUsesWith(GV1.get());
2366 EXPECT_TRUE(MD->getValue() == GV1.get());
2367
2368 // Reset it, so we don't inadvertently test deletion.
2369 MD.reset();
2370}
2371
2372TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2373 Type *Ty = Type::getInt1PtrTy(Context);
2374 std::unique_ptr<GlobalVariable> GV(
2375 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2376 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2377 EXPECT_TRUE(MD->getValue() == GV.get());
2378 ASSERT_TRUE(GV->use_empty());
2379
2380 GV.reset();
2381 EXPECT_TRUE(!MD);
2382}
2383
Devang Patel0924b332009-07-30 00:03:41 +00002384TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002385 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002386 ConstantAsMetadata *C =
2387 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2388 ConstantAsMetadata *C2 =
2389 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002390
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002391 Metadata *const V = C;
2392 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002393 MDNode *n = MDNode::get(Context, V);
2394 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002395
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002396 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002397 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002398 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2399 NMD->addOperand(n);
2400 NMD->addOperand(n2);
2401
Chris Lattnerbe354a62009-08-23 04:47:35 +00002402 std::string Str;
2403 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002404 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002405 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002406 oss.str().c_str());
2407}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002408
2409typedef MetadataTest FunctionAttachmentTest;
2410TEST_F(FunctionAttachmentTest, setMetadata) {
2411 Function *F = getFunction("foo");
2412 ASSERT_FALSE(F->hasMetadata());
2413 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2414 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2415 EXPECT_EQ(nullptr, F->getMetadata("other"));
2416
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002417 DISubprogram *SP1 = getSubprogram();
2418 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002419 ASSERT_NE(SP1, SP2);
2420
2421 F->setMetadata("dbg", SP1);
2422 EXPECT_TRUE(F->hasMetadata());
2423 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2424 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2425 EXPECT_EQ(nullptr, F->getMetadata("other"));
2426
2427 F->setMetadata(LLVMContext::MD_dbg, SP2);
2428 EXPECT_TRUE(F->hasMetadata());
2429 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2430 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2431 EXPECT_EQ(nullptr, F->getMetadata("other"));
2432
2433 F->setMetadata("dbg", nullptr);
2434 EXPECT_FALSE(F->hasMetadata());
2435 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2436 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2437 EXPECT_EQ(nullptr, F->getMetadata("other"));
2438
2439 MDTuple *T1 = getTuple();
2440 MDTuple *T2 = getTuple();
2441 ASSERT_NE(T1, T2);
2442
2443 F->setMetadata("other1", T1);
2444 F->setMetadata("other2", T2);
2445 EXPECT_TRUE(F->hasMetadata());
2446 EXPECT_EQ(T1, F->getMetadata("other1"));
2447 EXPECT_EQ(T2, F->getMetadata("other2"));
2448 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2449
2450 F->setMetadata("other1", T2);
2451 F->setMetadata("other2", T1);
2452 EXPECT_EQ(T2, F->getMetadata("other1"));
2453 EXPECT_EQ(T1, F->getMetadata("other2"));
2454
2455 F->setMetadata("other1", nullptr);
2456 F->setMetadata("other2", nullptr);
2457 EXPECT_FALSE(F->hasMetadata());
2458 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2459 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2460}
2461
2462TEST_F(FunctionAttachmentTest, getAll) {
2463 Function *F = getFunction("foo");
2464
2465 MDTuple *T1 = getTuple();
2466 MDTuple *T2 = getTuple();
2467 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002468 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002469
2470 F->setMetadata("other1", T2);
2471 F->setMetadata(LLVMContext::MD_dbg, SP);
2472 F->setMetadata("other2", T1);
2473 F->setMetadata(LLVMContext::MD_prof, P);
2474 F->setMetadata("other2", T2);
2475 F->setMetadata("other1", T1);
2476
2477 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2478 F->getAllMetadata(MDs);
2479 ASSERT_EQ(4u, MDs.size());
2480 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2481 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2482 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2483 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2484 EXPECT_EQ(SP, MDs[0].second);
2485 EXPECT_EQ(P, MDs[1].second);
2486 EXPECT_EQ(T1, MDs[2].second);
2487 EXPECT_EQ(T2, MDs[3].second);
2488}
2489
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002490TEST_F(FunctionAttachmentTest, Verifier) {
2491 Function *F = getFunction("foo");
2492 F->setMetadata("attach", getTuple());
Peter Collingbournebb738172016-06-06 23:21:27 +00002493 F->setIsMaterializable(true);
Peter Collingbournebb738172016-06-06 23:21:27 +00002494
Peter Collingbourne21521892016-06-21 23:42:48 +00002495 // Confirm this is materializable.
2496 ASSERT_TRUE(F->isMaterializable());
2497
2498 // Materializable functions cannot have metadata attachments.
2499 EXPECT_TRUE(verifyFunction(*F));
2500
2501 // Function declarations can.
Peter Collingbournebb738172016-06-06 23:21:27 +00002502 F->setIsMaterializable(false);
Peter Collingbourne21521892016-06-21 23:42:48 +00002503 EXPECT_FALSE(verifyModule(*F->getParent()));
2504 EXPECT_FALSE(verifyFunction(*F));
2505
2506 // So can definitions.
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002507 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2508 EXPECT_FALSE(verifyModule(*F->getParent()));
2509 EXPECT_FALSE(verifyFunction(*F));
2510}
2511
Diego Novillo2567f3d2015-05-13 15:13:45 +00002512TEST_F(FunctionAttachmentTest, EntryCount) {
2513 Function *F = getFunction("foo");
2514 EXPECT_FALSE(F->getEntryCount().hasValue());
Easwaran Ramane5b8de22018-01-17 22:24:23 +00002515 F->setEntryCount(12304, Function::PCT_Real);
2516 auto Count = F->getEntryCount();
2517 EXPECT_TRUE(Count.hasValue());
2518 EXPECT_EQ(12304u, Count.getCount());
2519 EXPECT_EQ(Function::PCT_Real, Count.getType());
2520
2521 // Repeat the same for synthetic counts.
2522 F = getFunction("bar");
2523 EXPECT_FALSE(F->getEntryCount().hasValue());
2524 F->setEntryCount(123, Function::PCT_Synthetic);
2525 Count = F->getEntryCount();
2526 EXPECT_TRUE(Count.hasValue());
2527 EXPECT_EQ(123u, Count.getCount());
2528 EXPECT_EQ(Function::PCT_Synthetic, Count.getType());
Diego Novillo2567f3d2015-05-13 15:13:45 +00002529}
2530
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002531TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2532 Function *F = getFunction("foo");
2533 DISubprogram *SP = getSubprogram();
2534 F->setSubprogram(SP);
2535
2536 // Note that the static_cast confirms that F->getSubprogram() actually
2537 // returns an DISubprogram.
2538 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2539 EXPECT_EQ(SP, F->getMetadata("dbg"));
2540 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2541}
2542
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002543typedef MetadataTest DistinctMDOperandPlaceholderTest;
2544TEST_F(DistinctMDOperandPlaceholderTest, getID) {
2545 EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
2546}
2547
2548TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
2549 // Set up some placeholders.
2550 DistinctMDOperandPlaceholder PH0(7);
2551 DistinctMDOperandPlaceholder PH1(3);
2552 DistinctMDOperandPlaceholder PH2(0);
2553 Metadata *Ops[] = {&PH0, &PH1, &PH2};
2554 auto *D = MDTuple::getDistinct(Context, Ops);
2555 ASSERT_EQ(&PH0, D->getOperand(0));
2556 ASSERT_EQ(&PH1, D->getOperand(1));
2557 ASSERT_EQ(&PH2, D->getOperand(2));
2558
2559 // Replace them.
2560 auto *N0 = MDTuple::get(Context, None);
2561 auto *N1 = MDTuple::get(Context, N0);
2562 PH0.replaceUseWith(N0);
2563 PH1.replaceUseWith(N1);
2564 PH2.replaceUseWith(nullptr);
2565 EXPECT_EQ(N0, D->getOperand(0));
2566 EXPECT_EQ(N1, D->getOperand(1));
2567 EXPECT_EQ(nullptr, D->getOperand(2));
2568}
2569
2570TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
2571 // There is no user, but we can still call replace.
2572 DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None));
2573}
2574
Reid Kleckner6be1ed02017-08-10 21:14:07 +00002575// Test various assertions in metadata tracking. Don't run these tests if gtest
2576// will use SEH to recover from them. Two of these tests get halfway through
2577// inserting metadata into DenseMaps for tracking purposes, and then they
2578// assert, and we attempt to destroy an LLVMContext with broken invariants,
2579// leading to infinite loops.
2580#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) && !defined(GTEST_HAS_SEH)
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002581TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
2582 // This shouldn't crash.
2583 DistinctMDOperandPlaceholder PH(7);
2584 EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
2585 "Unexpected callback to owner");
2586}
2587
2588TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
2589 // This shouldn't crash.
2590 DistinctMDOperandPlaceholder PH(7);
2591 EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
2592}
2593
2594TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
2595 // This shouldn't crash.
2596 DistinctMDOperandPlaceholder PH(7);
2597 MDTuple::getDistinct(Context, &PH);
2598 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2599 "Placeholders can only be used once");
2600}
2601
2602TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
2603 // TrackingMDRef doesn't install an owner callback, so it can't be detected
2604 // as an invalid use. However, using a placeholder in a TrackingMDRef *and*
2605 // a distinct node isn't possible and we should assert.
2606 //
2607 // (There's no positive test for using TrackingMDRef because it's not a
2608 // useful thing to do.)
2609 {
2610 DistinctMDOperandPlaceholder PH(7);
2611 MDTuple::getDistinct(Context, &PH);
2612 EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
2613 }
2614 {
2615 DistinctMDOperandPlaceholder PH(7);
2616 TrackingMDRef Ref(&PH);
2617 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2618 "Placeholders can only be used once");
2619 }
2620}
2621#endif
2622
Duncan P. N. Exon Smith2923a432016-04-23 04:02:39 +00002623} // end namespace