blob: baba3e5bc5253fff1fcce881df42364354f01e47 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000012#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000013#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000014#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
19#include "llvm/IR/Type.h"
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +000020#include "llvm/IR/Verifier.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000021#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000022#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000023using namespace llvm;
24
25namespace {
26
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000027TEST(ContextAndReplaceableUsesTest, FromContext) {
28 LLVMContext Context;
29 ContextAndReplaceableUses CRU(Context);
30 EXPECT_EQ(&Context, &CRU.getContext());
31 EXPECT_FALSE(CRU.hasReplaceableUses());
32 EXPECT_FALSE(CRU.getReplaceableUses());
33}
34
35TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
36 LLVMContext Context;
37 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
38 EXPECT_EQ(&Context, &CRU.getContext());
39 EXPECT_TRUE(CRU.hasReplaceableUses());
40 EXPECT_TRUE(CRU.getReplaceableUses());
41}
42
43TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
44 LLVMContext Context;
45 ContextAndReplaceableUses CRU(Context);
46 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
47 EXPECT_EQ(&Context, &CRU.getContext());
48 EXPECT_TRUE(CRU.hasReplaceableUses());
49 EXPECT_TRUE(CRU.getReplaceableUses());
50}
51
52TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
53 LLVMContext Context;
54 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
55 auto *Ptr = ReplaceableUses.get();
56 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
57 ReplaceableUses = CRU.takeReplaceableUses();
58 EXPECT_EQ(&Context, &CRU.getContext());
59 EXPECT_FALSE(CRU.hasReplaceableUses());
60 EXPECT_FALSE(CRU.getReplaceableUses());
61 EXPECT_EQ(Ptr, ReplaceableUses.get());
62}
63
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000064class MetadataTest : public testing::Test {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000065public:
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000066 MetadataTest() : M("test", Context), Counter(0) {}
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000067
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000068protected:
69 LLVMContext Context;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000070 Module M;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000071 int Counter;
72
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000073 MDNode *getNode() { return MDNode::get(Context, None); }
74 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
75 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
76 Metadata *MDs[] = {MD1, MD2};
77 return MDNode::get(Context, MDs);
78 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000079
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000080 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000081 DISubroutineType *getSubroutineType() {
82 return DISubroutineType::getDistinct(Context, 0, getNode(nullptr));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000083 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000084 DISubprogram *getSubprogram() {
85 return DISubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000086 nullptr, false, false, 0, nullptr, 0, 0, 0,
87 0);
88 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000089 DIScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
90 DIFile *getFile() {
91 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000092 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000093 DITypeRef getBasicType(StringRef Name) {
94 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +000095 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000096 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000097 DITypeRef getDerivedType() {
98 return DIDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000099 nullptr, 0, nullptr,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000100 getBasicType("basictype"), 1, 2, 0, 0)
101 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000102 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000103 Constant *getConstant() {
104 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
105 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000106 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000107 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000108 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000109 DITypeRef getCompositeType() {
110 return DICompositeType::getDistinct(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000111 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
112 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
113 ->getRef();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000114 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000115 Function *getFunction(StringRef Name) {
116 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000117 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
118 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000119};
120typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000121
Nick Lewycky49f89192009-04-04 07:22:01 +0000122// Test that construction of MDString with different value produces different
123// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000124TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000125 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000126 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000127 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000128 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000129 EXPECT_NE(s1, s2);
130}
131
132// Test that creation of MDStrings with the same string contents produces the
133// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000134TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000135 char x[4] = { 'a', 'b', 'c', 'X' };
136 char y[4] = { 'a', 'b', 'c', 'Y' };
137
Owen Anderson23587322009-07-31 21:38:10 +0000138 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
139 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000140 EXPECT_EQ(s1, s2);
141}
142
143// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000144TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000145 char *str = new char[13];
146 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000147 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000148 strncpy(str, "aaaaaaaaaaaaa", 13);
149 delete[] str;
150
Chris Lattnerbe354a62009-08-23 04:47:35 +0000151 std::string Str;
152 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000153 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000154 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000155}
156
157// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000158TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000159 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000160 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000161 std::string Str;
162 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000163 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000164 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000165}
166
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000167typedef MetadataTest MDNodeTest;
168
Nick Lewycky49f89192009-04-04 07:22:01 +0000169// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000170TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000171 char x[3] = { 'a', 'b', 'c' };
172 char y[3] = { '1', '2', '3' };
173
Owen Anderson23587322009-07-31 21:38:10 +0000174 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
175 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000176 ConstantAsMetadata *CI = ConstantAsMetadata::get(
177 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000178
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000179 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000180 V.push_back(s1);
181 V.push_back(CI);
182 V.push_back(s2);
183
Jay Foad5514afe2011-04-21 19:59:31 +0000184 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000185 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000186 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000187 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000188 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000189 MDNode *n4 = MDNode::getIfExists(Context, V);
190 MDNode *n5 = MDNode::getIfExists(Context, c1);
191 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000192 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000193 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000194 EXPECT_EQ(n4, n1);
195 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000196 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000197
Chris Lattner9b493022009-12-31 01:22:29 +0000198 EXPECT_EQ(3u, n1->getNumOperands());
199 EXPECT_EQ(s1, n1->getOperand(0));
200 EXPECT_EQ(CI, n1->getOperand(1));
201 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000202
Chris Lattner9b493022009-12-31 01:22:29 +0000203 EXPECT_EQ(1u, n2->getNumOperands());
204 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000205}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000206
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000207TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000208 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
209 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000210
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000211 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000212 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000213 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000214
215 EXPECT_EQ(n, wvh);
216
217 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000218}
Devang Patel0924b332009-07-30 00:03:41 +0000219
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000220TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000221 // !0 = !{!0}
222 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000223 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000224 auto Temp = MDNode::getTemporary(Context, None);
225 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000226 MDNode *Self = MDNode::get(Context, Args);
227 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000228 ASSERT_EQ(Self, Self->getOperand(0));
229
230 // Self-references should be distinct, so MDNode::get() should grab a
231 // uniqued node that references Self, not Self.
232 Args[0] = Self;
233 MDNode *Ref1 = MDNode::get(Context, Args);
234 MDNode *Ref2 = MDNode::get(Context, Args);
235 EXPECT_NE(Self, Ref1);
236 EXPECT_EQ(Ref1, Ref2);
237 }
238
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000239 // !0 = !{!0, !{}}
240 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000241 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000242 auto Temp = MDNode::getTemporary(Context, None);
243 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000244 MDNode *Self = MDNode::get(Context, Args);
245 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000246 ASSERT_EQ(Self, Self->getOperand(0));
247
248 // Self-references should be distinct, so MDNode::get() should grab a
249 // uniqued node that references Self, not Self itself.
250 Args[0] = Self;
251 MDNode *Ref1 = MDNode::get(Context, Args);
252 MDNode *Ref2 = MDNode::get(Context, Args);
253 EXPECT_NE(Self, Ref1);
254 EXPECT_EQ(Ref1, Ref2);
255 }
256}
257
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000258TEST_F(MDNodeTest, Print) {
259 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
260 MDString *S = MDString::get(Context, "foo");
261 MDNode *N0 = getNode();
262 MDNode *N1 = getNode(N0);
263 MDNode *N2 = getNode(N0, N1);
264
265 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
266 MDNode *N = MDNode::get(Context, Args);
267
268 std::string Expected;
269 {
270 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000271 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000272 C->printAsOperand(OS);
273 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000274 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000275 OS << ", null";
276 MDNode *Nodes[] = {N0, N1, N2};
277 for (auto *Node : Nodes)
278 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000279 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000280 }
281
282 std::string Actual;
283 {
284 raw_string_ostream OS(Actual);
285 N->print(OS);
286 }
287
288 EXPECT_EQ(Expected, Actual);
289}
290
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000291#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
292 do { \
293 std::string Actual_; \
294 raw_string_ostream OS(Actual_); \
295 PRINT; \
296 OS.flush(); \
297 std::string Expected_(EXPECTED); \
298 EXPECT_EQ(Expected_, Actual_); \
299 } while (false)
300
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000301TEST_F(MDNodeTest, PrintTemporary) {
302 MDNode *Arg = getNode();
303 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
304 MDNode *N = getNode(Temp.get());
305 Module M("test", Context);
306 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
307 NMD->addOperand(N);
308
309 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
310 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
311 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
312
313 // Cleanup.
314 Temp->replaceAllUsesWith(Arg);
315}
316
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000317TEST_F(MDNodeTest, PrintFromModule) {
318 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
319 MDString *S = MDString::get(Context, "foo");
320 MDNode *N0 = getNode();
321 MDNode *N1 = getNode(N0);
322 MDNode *N2 = getNode(N0, N1);
323
324 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
325 MDNode *N = MDNode::get(Context, Args);
326 Module M("test", Context);
327 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
328 NMD->addOperand(N);
329
330 std::string Expected;
331 {
332 raw_string_ostream OS(Expected);
333 OS << "!0 = !{";
334 C->printAsOperand(OS);
335 OS << ", ";
336 S->printAsOperand(OS);
337 OS << ", null, !1, !2, !3}";
338 }
339
340 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
341}
342
343TEST_F(MDNodeTest, PrintFromFunction) {
344 Module M("test", Context);
345 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
346 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
347 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
348 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
349 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
350 auto *R0 = ReturnInst::Create(Context, BB0);
351 auto *R1 = ReturnInst::Create(Context, BB1);
352 auto *N0 = MDNode::getDistinct(Context, None);
353 auto *N1 = MDNode::getDistinct(Context, None);
354 R0->setMetadata("md", N0);
355 R1->setMetadata("md", N1);
356
357 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
358 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
359}
360
361TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
362 Module M("test", Context);
363
364 auto *Intrinsic =
365 Function::Create(FunctionType::get(Type::getVoidTy(Context),
366 Type::getMetadataTy(Context), false),
367 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
368
369 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
370 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
371 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
372 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
373 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
374 auto *N0 = MDNode::getDistinct(Context, None);
375 auto *N1 = MDNode::getDistinct(Context, None);
376 auto *MAV0 = MetadataAsValue::get(Context, N0);
377 auto *MAV1 = MetadataAsValue::get(Context, N1);
378 CallInst::Create(Intrinsic, MAV0, "", BB0);
379 CallInst::Create(Intrinsic, MAV1, "", BB1);
380
381 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
382 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
383 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
384 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
385 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
386 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
387}
388#undef EXPECT_PRINTER_EQ
389
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000390TEST_F(MDNodeTest, NullOperand) {
391 // metadata !{}
392 MDNode *Empty = MDNode::get(Context, None);
393
394 // metadata !{metadata !{}}
395 Metadata *Ops[] = {Empty};
396 MDNode *N = MDNode::get(Context, Ops);
397 ASSERT_EQ(Empty, N->getOperand(0));
398
399 // metadata !{metadata !{}} => metadata !{null}
400 N->replaceOperandWith(0, nullptr);
401 ASSERT_EQ(nullptr, N->getOperand(0));
402
403 // metadata !{null}
404 Ops[0] = nullptr;
405 MDNode *NullOp = MDNode::get(Context, Ops);
406 ASSERT_EQ(nullptr, NullOp->getOperand(0));
407 EXPECT_EQ(N, NullOp);
408}
409
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000410TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
411 // !{}
412 MDNode *Empty = MDNode::get(Context, None);
413 ASSERT_TRUE(Empty->isResolved());
414 EXPECT_FALSE(Empty->isDistinct());
415
416 // !{!{}}
417 Metadata *Wrapped1Ops[] = {Empty};
418 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
419 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
420 ASSERT_TRUE(Wrapped1->isResolved());
421 EXPECT_FALSE(Wrapped1->isDistinct());
422
423 // !{!{!{}}}
424 Metadata *Wrapped2Ops[] = {Wrapped1};
425 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
426 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
427 ASSERT_TRUE(Wrapped2->isResolved());
428 EXPECT_FALSE(Wrapped2->isDistinct());
429
430 // !{!{!{}}} => !{!{}}
431 Wrapped2->replaceOperandWith(0, Empty);
432 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
433 EXPECT_TRUE(Wrapped2->isDistinct());
434 EXPECT_FALSE(Wrapped1->isDistinct());
435}
436
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000437TEST_F(MDNodeTest, getDistinct) {
438 // !{}
439 MDNode *Empty = MDNode::get(Context, None);
440 ASSERT_TRUE(Empty->isResolved());
441 ASSERT_FALSE(Empty->isDistinct());
442 ASSERT_EQ(Empty, MDNode::get(Context, None));
443
444 // distinct !{}
445 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
446 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
447 EXPECT_TRUE(Distinct1->isResolved());
448 EXPECT_TRUE(Distinct2->isDistinct());
449 EXPECT_NE(Empty, Distinct1);
450 EXPECT_NE(Empty, Distinct2);
451 EXPECT_NE(Distinct1, Distinct2);
452
453 // !{}
454 ASSERT_EQ(Empty, MDNode::get(Context, None));
455}
456
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000457TEST_F(MDNodeTest, isUniqued) {
458 MDNode *U = MDTuple::get(Context, None);
459 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000460 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000461 EXPECT_TRUE(U->isUniqued());
462 EXPECT_FALSE(D->isUniqued());
463 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000464}
465
466TEST_F(MDNodeTest, isDistinct) {
467 MDNode *U = MDTuple::get(Context, None);
468 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000469 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000470 EXPECT_FALSE(U->isDistinct());
471 EXPECT_TRUE(D->isDistinct());
472 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000473}
474
475TEST_F(MDNodeTest, isTemporary) {
476 MDNode *U = MDTuple::get(Context, None);
477 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000478 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000479 EXPECT_FALSE(U->isTemporary());
480 EXPECT_FALSE(D->isTemporary());
481 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000482}
483
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000484TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
485 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000486 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000487 ASSERT_FALSE(Temp->isResolved());
488
489 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000490 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000491 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
492 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000493 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000494
495 // temporary !{} => !{}
496 MDNode *Empty = MDNode::get(Context, None);
497 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000498 EXPECT_EQ(Empty, Distinct->getOperand(0));
499}
500
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000501TEST_F(MDNodeTest, handleChangedOperandRecursion) {
502 // !0 = !{}
503 MDNode *N0 = MDNode::get(Context, None);
504
505 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000506 auto Temp3 = MDTuple::getTemporary(Context, None);
507 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000508 MDNode *N1 = MDNode::get(Context, Ops1);
509
510 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000511 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000512 MDNode *N2 = MDNode::get(Context, Ops2);
513
514 // !3 = !{!2}
515 Metadata *Ops3[] = {N2};
516 MDNode *N3 = MDNode::get(Context, Ops3);
517 Temp3->replaceAllUsesWith(N3);
518
519 // !4 = !{!1}
520 Metadata *Ops4[] = {N1};
521 MDNode *N4 = MDNode::get(Context, Ops4);
522
523 // Confirm that the cycle prevented RAUW from getting dropped.
524 EXPECT_TRUE(N0->isResolved());
525 EXPECT_FALSE(N1->isResolved());
526 EXPECT_FALSE(N2->isResolved());
527 EXPECT_FALSE(N3->isResolved());
528 EXPECT_FALSE(N4->isResolved());
529
530 // Create a couple of distinct nodes to observe what's going on.
531 //
532 // !5 = distinct !{!2}
533 // !6 = distinct !{!3}
534 Metadata *Ops5[] = {N2};
535 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
536 Metadata *Ops6[] = {N3};
537 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
538
539 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
540 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
541 // references !3, this can cause a re-entry of handleChangedOperand() when !3
542 // is not ready for it.
543 //
544 // !2->replaceOperandWith(1, nullptr)
545 // !2: !{!3, !0} => !{!3, null}
546 // !2->replaceAllUsesWith(!1)
547 // !3: !{!2] => !{!1}
548 // !3->replaceAllUsesWith(!4)
549 N2->replaceOperandWith(1, nullptr);
550
551 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
552 // under us. Just check that the other nodes are sane.
553 //
554 // !1 = !{!4, null}
555 // !4 = !{!1}
556 // !5 = distinct !{!1}
557 // !6 = distinct !{!4}
558 EXPECT_EQ(N4, N1->getOperand(0));
559 EXPECT_EQ(N1, N4->getOperand(0));
560 EXPECT_EQ(N1, N5->getOperand(0));
561 EXPECT_EQ(N4, N6->getOperand(0));
562}
563
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000564TEST_F(MDNodeTest, replaceResolvedOperand) {
565 // Check code for replacing one resolved operand with another. If doing this
566 // directly (via replaceOperandWith()) becomes illegal, change the operand to
567 // a global value that gets RAUW'ed.
568 //
569 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000570 auto Temp = MDTuple::getTemporary(Context, None);
571 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000572
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000573 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000574 MDNode *N = MDTuple::get(Context, Ops);
575 EXPECT_EQ(nullptr, N->getOperand(0));
576 ASSERT_FALSE(N->isResolved());
577
578 // Check code for replacing resolved nodes.
579 N->replaceOperandWith(0, Empty);
580 EXPECT_EQ(Empty, N->getOperand(0));
581
582 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000583 N->replaceOperandWith(0, Temp.get());
584 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000585
586 // Remove the references to Temp; required for teardown.
587 Temp->replaceAllUsesWith(nullptr);
588}
589
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000590TEST_F(MDNodeTest, replaceWithUniqued) {
591 auto *Empty = MDTuple::get(Context, None);
592 MDTuple *FirstUniqued;
593 {
594 Metadata *Ops[] = {Empty};
595 auto Temp = MDTuple::getTemporary(Context, Ops);
596 EXPECT_TRUE(Temp->isTemporary());
597
598 // Don't expect a collision.
599 auto *Current = Temp.get();
600 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
601 EXPECT_TRUE(FirstUniqued->isUniqued());
602 EXPECT_TRUE(FirstUniqued->isResolved());
603 EXPECT_EQ(Current, FirstUniqued);
604 }
605 {
606 Metadata *Ops[] = {Empty};
607 auto Temp = MDTuple::getTemporary(Context, Ops);
608 EXPECT_TRUE(Temp->isTemporary());
609
610 // Should collide with Uniqued above this time.
611 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
612 EXPECT_TRUE(Uniqued->isUniqued());
613 EXPECT_TRUE(Uniqued->isResolved());
614 EXPECT_EQ(FirstUniqued, Uniqued);
615 }
616 {
617 auto Unresolved = MDTuple::getTemporary(Context, None);
618 Metadata *Ops[] = {Unresolved.get()};
619 auto Temp = MDTuple::getTemporary(Context, Ops);
620 EXPECT_TRUE(Temp->isTemporary());
621
622 // Shouldn't be resolved.
623 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
624 EXPECT_TRUE(Uniqued->isUniqued());
625 EXPECT_FALSE(Uniqued->isResolved());
626
627 // Should be a different node.
628 EXPECT_NE(FirstUniqued, Uniqued);
629
630 // Should resolve when we update its node (note: be careful to avoid a
631 // collision with any other nodes above).
632 Uniqued->replaceOperandWith(0, nullptr);
633 EXPECT_TRUE(Uniqued->isResolved());
634 }
635}
636
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000637TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000638 // temp !{}
639 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
640 EXPECT_FALSE(Op->isResolved());
641
642 // temp !{temp !{}}
643 Metadata *Ops[] = {Op};
644 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
645 EXPECT_FALSE(N->isResolved());
646
647 // temp !{temp !{}} => !{temp !{}}
648 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
649 EXPECT_FALSE(N->isResolved());
650
651 // !{temp !{}} => !{!{}}
652 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
653 EXPECT_TRUE(Op->isResolved());
654 EXPECT_TRUE(N->isResolved());
655}
656
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000657TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000658 // i1* @GV
659 Type *Ty = Type::getInt1PtrTy(Context);
660 std::unique_ptr<GlobalVariable> GV(
661 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
662 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
663
664 // temp !{i1* @GV}
665 Metadata *Ops[] = {Op};
666 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
667
668 // temp !{i1* @GV} => !{i1* @GV}
669 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
670 ASSERT_TRUE(N->isUniqued());
671
672 // !{i1* @GV} => !{null}
673 GV.reset();
674 ASSERT_TRUE(N->isUniqued());
675 Metadata *NullOps[] = {nullptr};
676 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
677}
678
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000679TEST_F(MDNodeTest, replaceWithDistinct) {
680 {
681 auto *Empty = MDTuple::get(Context, None);
682 Metadata *Ops[] = {Empty};
683 auto Temp = MDTuple::getTemporary(Context, Ops);
684 EXPECT_TRUE(Temp->isTemporary());
685
686 // Don't expect a collision.
687 auto *Current = Temp.get();
688 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
689 EXPECT_TRUE(Distinct->isDistinct());
690 EXPECT_TRUE(Distinct->isResolved());
691 EXPECT_EQ(Current, Distinct);
692 }
693 {
694 auto Unresolved = MDTuple::getTemporary(Context, None);
695 Metadata *Ops[] = {Unresolved.get()};
696 auto Temp = MDTuple::getTemporary(Context, Ops);
697 EXPECT_TRUE(Temp->isTemporary());
698
699 // Don't expect a collision.
700 auto *Current = Temp.get();
701 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
702 EXPECT_TRUE(Distinct->isDistinct());
703 EXPECT_TRUE(Distinct->isResolved());
704 EXPECT_EQ(Current, Distinct);
705
706 // Cleanup; required for teardown.
707 Unresolved->replaceAllUsesWith(nullptr);
708 }
709}
710
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000711TEST_F(MDNodeTest, replaceWithPermanent) {
712 Metadata *Ops[] = {nullptr};
713 auto Temp = MDTuple::getTemporary(Context, Ops);
714 auto *T = Temp.get();
715
716 // U is a normal, uniqued node that references T.
717 auto *U = MDTuple::get(Context, T);
718 EXPECT_TRUE(U->isUniqued());
719
720 // Make Temp self-referencing.
721 Temp->replaceOperandWith(0, T);
722
723 // Try to uniquify Temp. This should, despite the name in the API, give a
724 // 'distinct' node, since self-references aren't allowed to be uniqued.
725 //
726 // Since it's distinct, N should have the same address as when it was a
727 // temporary (i.e., be equal to T not U).
728 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
729 EXPECT_EQ(N, T);
730 EXPECT_TRUE(N->isDistinct());
731
732 // U should be the canonical unique node with N as the argument.
733 EXPECT_EQ(U, MDTuple::get(Context, N));
734 EXPECT_TRUE(U->isUniqued());
735
736 // This temporary should collide with U when replaced, but it should still be
737 // uniqued.
738 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
739 EXPECT_TRUE(U->isUniqued());
740
741 // This temporary should become a new uniqued node.
742 auto Temp2 = MDTuple::getTemporary(Context, U);
743 auto *V = Temp2.get();
744 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
745 EXPECT_TRUE(V->isUniqued());
746 EXPECT_EQ(U, V->getOperand(0));
747}
748
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000749TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
750 TrackingMDRef Ref;
751 EXPECT_EQ(nullptr, Ref.get());
752 {
753 auto Temp = MDTuple::getTemporary(Context, None);
754 Ref.reset(Temp.get());
755 EXPECT_EQ(Temp.get(), Ref.get());
756 }
757 EXPECT_EQ(nullptr, Ref.get());
758}
759
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000760typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000761
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000762TEST_F(DILocationTest, Overflow) {
763 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000764 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000765 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000766 EXPECT_EQ(2u, L->getLine());
767 EXPECT_EQ(7u, L->getColumn());
768 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000769 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000770 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000771 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000772 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000773 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000774 }
775 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000776 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000777 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000778 EXPECT_EQ(0u, L->getColumn());
779 }
780 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000781 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000782 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000783 EXPECT_EQ(0u, L->getColumn());
784 }
785}
786
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000787TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000788 MDNode *N = getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000789 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000790 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000791 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000792 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000793 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000794}
795
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000796TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000797 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000798 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000799 EXPECT_TRUE(L->isTemporary());
800 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000801}
802
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000803typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000804
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000805TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000806 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000807 auto *Empty = MDNode::get(Context, None);
808 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000809 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000810 EXPECT_EQ(15u, N->getTag());
811 EXPECT_EQ(2u, N->getNumOperands());
812 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000813 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000814 EXPECT_EQ(1u, N->getNumDwarfOperands());
815 EXPECT_EQ(Empty, N->getDwarfOperand(0));
816 EXPECT_EQ(Empty, N->getOperand(1));
817 ASSERT_TRUE(N->isUniqued());
818
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000819 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000820
821 N->replaceOperandWith(1, nullptr);
822 EXPECT_EQ(15u, N->getTag());
823 EXPECT_EQ(Header, N->getHeader());
824 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
825 ASSERT_TRUE(N->isUniqued());
826
827 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000828 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000829
830 N->replaceDwarfOperandWith(0, Empty);
831 EXPECT_EQ(15u, N->getTag());
832 EXPECT_EQ(Header, N->getHeader());
833 EXPECT_EQ(Empty, N->getDwarfOperand(0));
834 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000835 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000836
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000837 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000838 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000839}
840
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000841TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000842 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000843 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000844 EXPECT_EQ(StringRef(), N->getHeader());
845 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000846}
847
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000848typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000849
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000850TEST_F(DISubrangeTest, get) {
851 auto *N = DISubrange::get(Context, 5, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000852 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
853 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000854 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000855 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
856 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000857
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000858 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000859 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000860}
861
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000862TEST_F(DISubrangeTest, getEmptyArray) {
863 auto *N = DISubrange::get(Context, -1, 0);
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000864 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
865 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000866 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000867 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000868}
869
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000870typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000871
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000872TEST_F(DIEnumeratorTest, get) {
873 auto *N = DIEnumerator::get(Context, 7, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000874 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
875 EXPECT_EQ(7, N->getValue());
876 EXPECT_EQ("name", N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000877 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000878
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000879 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
880 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000881
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000882 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000883 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000884}
885
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000886typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000887
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000888TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000889 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000890 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000891 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
892 EXPECT_EQ("special", N->getName());
893 EXPECT_EQ(33u, N->getSizeInBits());
894 EXPECT_EQ(26u, N->getAlignInBits());
895 EXPECT_EQ(7u, N->getEncoding());
896 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000897 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000898 26, 7));
899
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000900 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000901 "special", 33, 26, 7));
902 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000903 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
904 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000905 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000906 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000907 25, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000908 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000909 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000910
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000911 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000912 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000913}
914
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000915TEST_F(DIBasicTypeTest, getWithLargeValues) {
916 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000917 UINT64_MAX, UINT64_MAX - 1, 7);
918 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
919 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
920}
921
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000922TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000923 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000924 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000925 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
926 EXPECT_EQ("unspecified", N->getName());
927 EXPECT_EQ(0u, N->getSizeInBits());
928 EXPECT_EQ(0u, N->getAlignInBits());
929 EXPECT_EQ(0u, N->getEncoding());
930 EXPECT_EQ(0u, N->getLine());
931}
932
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000933typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000934
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000935TEST_F(DITypeTest, clone) {
936 // Check that DIType has a specialized clone that returns TempDIType.
937 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000938 dwarf::DW_ATE_signed);
939
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000940 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000941 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
942}
943
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000944TEST_F(DITypeTest, setFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000945 // void (void)
946 Metadata *TypesOps[] = {nullptr};
947 Metadata *Types = MDTuple::get(Context, TypesOps);
948
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000949 DIType *D = DISubroutineType::getDistinct(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000950 EXPECT_EQ(0u, D->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000951 D->setFlags(DINode::FlagRValueReference);
952 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000953 D->setFlags(0u);
954 EXPECT_EQ(0u, D->getFlags());
955
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000956 TempDIType T = DISubroutineType::getTemporary(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000957 EXPECT_EQ(0u, T->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000958 T->setFlags(DINode::FlagRValueReference);
959 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000960 T->setFlags(0u);
961 EXPECT_EQ(0u, T->getFlags());
962}
963
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000964typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000965
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000966TEST_F(DIDerivedTypeTest, get) {
967 DIFile *File = getFile();
968 DIScopeRef Scope = getSubprogramRef();
969 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000970 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000971
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000972 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000973 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
974 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
975 EXPECT_EQ("something", N->getName());
976 EXPECT_EQ(File, N->getFile());
977 EXPECT_EQ(1u, N->getLine());
978 EXPECT_EQ(Scope, N->getScope());
979 EXPECT_EQ(BaseType, N->getBaseType());
980 EXPECT_EQ(2u, N->getSizeInBits());
981 EXPECT_EQ(3u, N->getAlignInBits());
982 EXPECT_EQ(4u, N->getOffsetInBits());
983 EXPECT_EQ(5u, N->getFlags());
984 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000985 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000986 "something", File, 1, Scope, BaseType, 2, 3,
987 4, 5, ExtraData));
988
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000989 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000990 "something", File, 1, Scope, BaseType, 2, 3,
991 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000992 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000993 File, 1, Scope, BaseType, 2, 3, 4, 5,
994 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000995 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000996 "something", getFile(), 1, Scope, BaseType, 2,
997 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000998 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000999 "something", File, 2, Scope, BaseType, 2, 3,
1000 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001001 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001002 "something", File, 1, getSubprogramRef(),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001003 BaseType, 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001004 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001005 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1006 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001007 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001008 "something", File, 1, Scope, BaseType, 3, 3,
1009 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001010 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001011 "something", File, 1, Scope, BaseType, 2, 2,
1012 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001013 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001014 "something", File, 1, Scope, BaseType, 2, 3,
1015 5, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001016 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001017 "something", File, 1, Scope, BaseType, 2, 3,
1018 4, 4, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001019 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001020 "something", File, 1, Scope, BaseType, 2, 3,
1021 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001022
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001023 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001024 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001025}
1026
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001027TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1028 DIFile *File = getFile();
1029 DIScopeRef Scope = getSubprogramRef();
1030 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001031 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001032
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001033 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001034 File, 1, Scope, BaseType, UINT64_MAX,
1035 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1036 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1037 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1038 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1039}
1040
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001041typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001042
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001043TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001044 unsigned Tag = dwarf::DW_TAG_structure_type;
1045 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001046 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001047 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001048 DIScopeRef Scope = getSubprogramRef();
1049 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001050 uint64_t SizeInBits = 2;
1051 uint64_t AlignInBits = 3;
1052 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001053 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001054 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001055 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001056 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001057 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001058 StringRef Identifier = "some id";
1059
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001060 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001061 BaseType, SizeInBits, AlignInBits,
1062 OffsetInBits, Flags, Elements, RuntimeLang,
1063 VTableHolder, TemplateParams, Identifier);
1064 EXPECT_EQ(Tag, N->getTag());
1065 EXPECT_EQ(Name, N->getName());
1066 EXPECT_EQ(File, N->getFile());
1067 EXPECT_EQ(Line, N->getLine());
1068 EXPECT_EQ(Scope, N->getScope());
1069 EXPECT_EQ(BaseType, N->getBaseType());
1070 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1071 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1072 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1073 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001074 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001075 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1076 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001077 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001078 EXPECT_EQ(Identifier, N->getIdentifier());
1079
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001080 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001081 BaseType, SizeInBits, AlignInBits,
1082 OffsetInBits, Flags, Elements, RuntimeLang,
1083 VTableHolder, TemplateParams, Identifier));
1084
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001085 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001086 BaseType, SizeInBits, AlignInBits,
1087 OffsetInBits, Flags, Elements, RuntimeLang,
1088 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001089 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001090 BaseType, SizeInBits, AlignInBits,
1091 OffsetInBits, Flags, Elements, RuntimeLang,
1092 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001093 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001094 BaseType, SizeInBits, AlignInBits,
1095 OffsetInBits, Flags, Elements, RuntimeLang,
1096 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001097 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001098 BaseType, SizeInBits, AlignInBits,
1099 OffsetInBits, Flags, Elements, RuntimeLang,
1100 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001101 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001102 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001103 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1104 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001105 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001106 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1107 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1108 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001109 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001110 BaseType, SizeInBits + 1, AlignInBits,
1111 OffsetInBits, Flags, Elements, RuntimeLang,
1112 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001113 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001114 BaseType, SizeInBits, AlignInBits + 1,
1115 OffsetInBits, Flags, Elements, RuntimeLang,
1116 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001117 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001118 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1119 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1120 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001121 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001122 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1123 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1124 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001125 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001126 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1127 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1128 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001129 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001130 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1131 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1132 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001133 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001134 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1135 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1136 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001137 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001138 BaseType, SizeInBits, AlignInBits,
1139 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001140 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001141 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001142 BaseType, SizeInBits, AlignInBits,
1143 OffsetInBits, Flags, Elements, RuntimeLang,
1144 VTableHolder, TemplateParams, "other"));
1145
1146 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001147 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1148 BaseType, SizeInBits, AlignInBits,
1149 OffsetInBits, Flags, Elements, RuntimeLang,
1150 VTableHolder, TemplateParams, "")
1151 ->getRawIdentifier());
1152 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1153 BaseType, SizeInBits, AlignInBits,
1154 OffsetInBits, Flags, Elements, RuntimeLang,
1155 VTableHolder, TemplateParams)
1156 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001157
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001158 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001159 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001160}
1161
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001162TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001163 unsigned Tag = dwarf::DW_TAG_structure_type;
1164 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001165 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001166 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001167 DIScopeRef Scope = getSubprogramRef();
1168 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001169 uint64_t SizeInBits = UINT64_MAX;
1170 uint64_t AlignInBits = UINT64_MAX - 1;
1171 uint64_t OffsetInBits = UINT64_MAX - 2;
1172 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001173 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001174 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001175 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001176 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001177 StringRef Identifier = "some id";
1178
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001179 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001180 BaseType, SizeInBits, AlignInBits,
1181 OffsetInBits, Flags, Elements, RuntimeLang,
1182 VTableHolder, TemplateParams, Identifier);
1183 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1184 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1185 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1186}
1187
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001188TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001189 unsigned Tag = dwarf::DW_TAG_structure_type;
1190 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001191 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001192 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001193 DIScopeRef Scope = getSubprogramRef();
1194 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001195 uint64_t SizeInBits = 2;
1196 uint64_t AlignInBits = 3;
1197 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001198 unsigned Flags = 5;
1199 unsigned RuntimeLang = 6;
1200 StringRef Identifier = "some id";
1201
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001202 auto *N = DICompositeType::get(
1203 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1204 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001205
1206 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001207 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001208 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001209 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001210 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001211 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001212
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001213 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001214 EXPECT_EQ(nullptr, N->getVTableHolder());
1215 N->replaceVTableHolder(VTableHolder);
1216 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1217 N->replaceVTableHolder(nullptr);
1218 EXPECT_EQ(nullptr, N->getVTableHolder());
1219
1220 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001221 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001222 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001223 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001224 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001225 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001226}
1227
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001228typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001229
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001230TEST_F(DISubroutineTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001231 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001232 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001233
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001234 auto *N = DISubroutineType::get(Context, Flags, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001235 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1236 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001237 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001238 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001239
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001240 EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, TypeArray));
1241 EXPECT_NE(N, DISubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001242
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001243 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001244 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001245
1246 // Test always-empty operands.
1247 EXPECT_EQ(nullptr, N->getScope());
1248 EXPECT_EQ(nullptr, N->getFile());
1249 EXPECT_EQ("", N->getName());
1250 EXPECT_EQ(nullptr, N->getBaseType());
1251 EXPECT_EQ(nullptr, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001252 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001253 EXPECT_EQ("", N->getIdentifier());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001254}
1255
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001256typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001257
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001258TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001259 StringRef Filename = "file";
1260 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001261 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001262
1263 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1264 EXPECT_EQ(Filename, N->getFilename());
1265 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001266 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001267
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001268 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1269 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001270
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001271 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001272 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001273}
1274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001275TEST_F(DIFileTest, ScopeGetFile) {
1276 // Ensure that DIScope::getFile() returns itself.
1277 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001278 EXPECT_EQ(N, N->getFile());
1279}
1280
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001281typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001282
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001283TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001284 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001285 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001286 StringRef Producer = "some producer";
1287 bool IsOptimized = false;
1288 StringRef Flags = "flag after flag";
1289 unsigned RuntimeVersion = 2;
1290 StringRef SplitDebugFilename = "another/file";
1291 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001292 MDTuple *EnumTypes = getTuple();
1293 MDTuple *RetainedTypes = getTuple();
1294 MDTuple *Subprograms = getTuple();
1295 MDTuple *GlobalVariables = getTuple();
1296 MDTuple *ImportedEntities = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001297 auto *N = DICompileUnit::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001298 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1299 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1300 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
1301
1302 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1303 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1304 EXPECT_EQ(File, N->getFile());
1305 EXPECT_EQ(Producer, N->getProducer());
1306 EXPECT_EQ(IsOptimized, N->isOptimized());
1307 EXPECT_EQ(Flags, N->getFlags());
1308 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1309 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1310 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001311 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1312 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1313 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1314 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1315 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001316 EXPECT_EQ(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001317 IsOptimized, Flags, RuntimeVersion,
1318 SplitDebugFilename, EmissionKind, EnumTypes,
1319 RetainedTypes, Subprograms, GlobalVariables,
1320 ImportedEntities));
1321
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001322 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage + 1, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001323 IsOptimized, Flags, RuntimeVersion,
1324 SplitDebugFilename, EmissionKind, EnumTypes,
1325 RetainedTypes, Subprograms, GlobalVariables,
1326 ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001327 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, getFile(), Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001328 IsOptimized, Flags, RuntimeVersion,
1329 SplitDebugFilename, EmissionKind, EnumTypes,
1330 RetainedTypes, Subprograms, GlobalVariables,
1331 ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001332 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001333 IsOptimized, Flags, RuntimeVersion,
1334 SplitDebugFilename, EmissionKind, EnumTypes,
1335 RetainedTypes, Subprograms, GlobalVariables,
1336 ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001337 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001338 !IsOptimized, Flags, RuntimeVersion,
1339 SplitDebugFilename, EmissionKind, EnumTypes,
1340 RetainedTypes, Subprograms, GlobalVariables,
1341 ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001342 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001343 IsOptimized, "other", RuntimeVersion,
1344 SplitDebugFilename, EmissionKind, EnumTypes,
1345 RetainedTypes, Subprograms, GlobalVariables,
1346 ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001347 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001348 IsOptimized, Flags, RuntimeVersion + 1,
1349 SplitDebugFilename, EmissionKind, EnumTypes,
1350 RetainedTypes, Subprograms, GlobalVariables,
1351 ImportedEntities));
1352 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001353 DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001354 IsOptimized, Flags, RuntimeVersion, "other",
1355 EmissionKind, EnumTypes, RetainedTypes,
1356 Subprograms, GlobalVariables, ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001357 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001358 IsOptimized, Flags, RuntimeVersion,
1359 SplitDebugFilename, EmissionKind + 1,
1360 EnumTypes, RetainedTypes, Subprograms,
1361 GlobalVariables, ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001362 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001363 IsOptimized, Flags, RuntimeVersion,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001364 SplitDebugFilename, EmissionKind, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001365 RetainedTypes, Subprograms, GlobalVariables,
1366 ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001367 EXPECT_NE(N, DICompileUnit::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001368 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1369 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001370 getTuple(), Subprograms, GlobalVariables, ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001371 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001372 IsOptimized, Flags, RuntimeVersion,
1373 SplitDebugFilename, EmissionKind, EnumTypes,
1374 RetainedTypes, getTuple(), GlobalVariables,
1375 ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001376 EXPECT_NE(N, DICompileUnit::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001377 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1378 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001379 RetainedTypes, Subprograms, getTuple(), ImportedEntities));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001380 EXPECT_NE(N, DICompileUnit::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001381 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1382 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001383 RetainedTypes, Subprograms, GlobalVariables, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001384
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001385 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001386 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001387}
1388
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001389TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001390 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001391 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001392 StringRef Producer = "some producer";
1393 bool IsOptimized = false;
1394 StringRef Flags = "flag after flag";
1395 unsigned RuntimeVersion = 2;
1396 StringRef SplitDebugFilename = "another/file";
1397 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001398 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1399 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1400 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001401 auto *N = DICompileUnit::get(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001402 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1403 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1404 RetainedTypes, nullptr, nullptr, ImportedEntities);
1405
1406 auto *Subprograms = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001407 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001408 N->replaceSubprograms(Subprograms);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001409 EXPECT_EQ(Subprograms, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001410 N->replaceSubprograms(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001411 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001412
1413 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001414 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001415 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001416 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001417 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001418 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001419}
1420
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001421typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001422
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001423TEST_F(DISubprogramTest, get) {
1424 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001425 StringRef Name = "name";
1426 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001427 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001428 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001429 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001430 bool IsLocalToUnit = false;
1431 bool IsDefinition = true;
1432 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001433 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001434 unsigned Virtuality = 4;
1435 unsigned VirtualIndex = 5;
1436 unsigned Flags = 6;
1437 bool IsOptimized = false;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001438 llvm::Function *Function = getFunction("foo");
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001439 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001440 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001441 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001442
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001443 auto *N = DISubprogram::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001444 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1445 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1446 IsOptimized, Function, TemplateParams, Declaration, Variables);
1447
1448 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1449 EXPECT_EQ(Scope, N->getScope());
1450 EXPECT_EQ(Name, N->getName());
1451 EXPECT_EQ(LinkageName, N->getLinkageName());
1452 EXPECT_EQ(File, N->getFile());
1453 EXPECT_EQ(Line, N->getLine());
1454 EXPECT_EQ(Type, N->getType());
1455 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1456 EXPECT_EQ(IsDefinition, N->isDefinition());
1457 EXPECT_EQ(ScopeLine, N->getScopeLine());
1458 EXPECT_EQ(ContainingType, N->getContainingType());
1459 EXPECT_EQ(Virtuality, N->getVirtuality());
1460 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1461 EXPECT_EQ(Flags, N->getFlags());
1462 EXPECT_EQ(IsOptimized, N->isOptimized());
1463 EXPECT_EQ(Function, N->getFunction());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001464 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001465 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001466 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001467 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001468 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1469 ContainingType, Virtuality, VirtualIndex,
1470 Flags, IsOptimized, Function, TemplateParams,
1471 Declaration, Variables));
1472
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001473 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001474 File, Line, Type, IsLocalToUnit, IsDefinition,
1475 ScopeLine, ContainingType, Virtuality,
1476 VirtualIndex, Flags, IsOptimized, Function,
1477 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001478 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001479 Line, Type, IsLocalToUnit, IsDefinition,
1480 ScopeLine, ContainingType, Virtuality,
1481 VirtualIndex, Flags, IsOptimized, Function,
1482 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001483 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001484 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1485 ContainingType, Virtuality, VirtualIndex,
1486 Flags, IsOptimized, Function, TemplateParams,
1487 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001488 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001489 Line, Type, IsLocalToUnit, IsDefinition,
1490 ScopeLine, ContainingType, Virtuality,
1491 VirtualIndex, Flags, IsOptimized, Function,
1492 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001493 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001494 Line + 1, Type, IsLocalToUnit, IsDefinition,
1495 ScopeLine, ContainingType, Virtuality,
1496 VirtualIndex, Flags, IsOptimized, Function,
1497 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001498 EXPECT_NE(N, DISubprogram::get(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001499 Context, Scope, Name, LinkageName, File, Line,
1500 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine,
1501 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1502 Function, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001503 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001504 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1505 ContainingType, Virtuality, VirtualIndex,
1506 Flags, IsOptimized, Function, TemplateParams,
1507 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001508 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001509 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1510 ContainingType, Virtuality, VirtualIndex,
1511 Flags, IsOptimized, Function, TemplateParams,
1512 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001513 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001514 Type, IsLocalToUnit, IsDefinition,
1515 ScopeLine + 1, ContainingType, Virtuality,
1516 VirtualIndex, Flags, IsOptimized, Function,
1517 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001518 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001519 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001520 getCompositeType(), Virtuality, VirtualIndex,
1521 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001522 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001523 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001524 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1525 ContainingType, Virtuality + 1, VirtualIndex,
1526 Flags, IsOptimized, Function, TemplateParams,
1527 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001528 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001529 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1530 ContainingType, Virtuality, VirtualIndex + 1,
1531 Flags, IsOptimized, Function, TemplateParams,
1532 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001533 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001534 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1535 ContainingType, Virtuality, VirtualIndex,
1536 ~Flags, IsOptimized, Function, TemplateParams,
1537 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001538 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001539 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1540 ContainingType, Virtuality, VirtualIndex,
1541 Flags, !IsOptimized, Function, TemplateParams,
1542 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001543 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001544 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1545 ContainingType, Virtuality, VirtualIndex,
1546 Flags, IsOptimized, getFunction("bar"),
1547 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001548 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001549 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1550 ContainingType, Virtuality, VirtualIndex,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001551 Flags, IsOptimized, Function, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001552 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001553 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001554 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1555 ContainingType, Virtuality, VirtualIndex,
1556 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001557 getSubprogram(), Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001558 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001559 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1560 ContainingType, Virtuality, VirtualIndex,
1561 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001562 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001563
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001564 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001565 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001566}
1567
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001568TEST_F(DISubprogramTest, replaceFunction) {
1569 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001570 StringRef Name = "name";
1571 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001572 DIFile *File = getFile();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001573 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001574 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001575 bool IsLocalToUnit = false;
1576 bool IsDefinition = true;
1577 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001578 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001579 unsigned Virtuality = 4;
1580 unsigned VirtualIndex = 5;
1581 unsigned Flags = 6;
1582 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001583 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001584 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001585 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001586
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001587 auto *N = DISubprogram::get(
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001588 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1589 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1590 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1591
1592 EXPECT_EQ(nullptr, N->getFunction());
1593
1594 std::unique_ptr<Function> F(
1595 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1596 GlobalValue::ExternalLinkage));
1597 N->replaceFunction(F.get());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001598 EXPECT_EQ(F.get(), N->getFunction());
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001599
1600 N->replaceFunction(nullptr);
1601 EXPECT_EQ(nullptr, N->getFunction());
1602}
1603
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001604typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001605
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001606TEST_F(DILexicalBlockTest, get) {
1607 DILocalScope *Scope = getSubprogram();
1608 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001609 unsigned Line = 5;
1610 unsigned Column = 8;
1611
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001612 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001613
1614 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1615 EXPECT_EQ(Scope, N->getScope());
1616 EXPECT_EQ(File, N->getFile());
1617 EXPECT_EQ(Line, N->getLine());
1618 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001619 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001620
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001621 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001622 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1623 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1624 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1625 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001626
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001627 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001628 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001629}
1630
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001631typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001632
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001633TEST_F(DILexicalBlockFileTest, get) {
1634 DILocalScope *Scope = getSubprogram();
1635 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001636 unsigned Discriminator = 5;
1637
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001638 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001639
1640 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1641 EXPECT_EQ(Scope, N->getScope());
1642 EXPECT_EQ(File, N->getFile());
1643 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001644 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001645
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001646 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001647 Discriminator));
1648 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001649 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001650 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001651 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001652
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001653 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001654 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001655}
1656
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001657typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001658
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001659TEST_F(DINamespaceTest, get) {
1660 DIScope *Scope = getFile();
1661 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001662 StringRef Name = "namespace";
1663 unsigned Line = 5;
1664
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001665 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001666
1667 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1668 EXPECT_EQ(Scope, N->getScope());
1669 EXPECT_EQ(File, N->getFile());
1670 EXPECT_EQ(Name, N->getName());
1671 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001672 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001673
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001674 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1675 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1676 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1677 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001678
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001679 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001680 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001681}
1682
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001683typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001684
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001685TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001686 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001687 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001688
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001689 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001690
1691 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001692 EXPECT_EQ(Name, N->getName());
1693 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001694 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001695
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001696 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001697 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001698 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001699
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001700 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001701 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001702}
1703
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001704typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001705
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001706TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001707 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001708 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001709 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001710 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001711
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001712 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001713 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001714 EXPECT_EQ(Name, N->getName());
1715 EXPECT_EQ(Type, N->getType());
1716 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001717 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001718
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001719 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001720 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1721 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001722 EXPECT_NE(N,
1723 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1724 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001725 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001726 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001727 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001728
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001729 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001730 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001731}
1732
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001733typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001734
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001735TEST_F(DIGlobalVariableTest, get) {
1736 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001737 StringRef Name = "name";
1738 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001739 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001740 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001741 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001742 bool IsLocalToUnit = false;
1743 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001744 Constant *Variable = getConstant();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001745 DIDerivedType *StaticDataMemberDeclaration =
1746 cast<DIDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001747
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001748 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001749 Type, IsLocalToUnit, IsDefinition, Variable,
1750 StaticDataMemberDeclaration);
1751 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1752 EXPECT_EQ(Scope, N->getScope());
1753 EXPECT_EQ(Name, N->getName());
1754 EXPECT_EQ(LinkageName, N->getLinkageName());
1755 EXPECT_EQ(File, N->getFile());
1756 EXPECT_EQ(Line, N->getLine());
1757 EXPECT_EQ(Type, N->getType());
1758 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1759 EXPECT_EQ(IsDefinition, N->isDefinition());
1760 EXPECT_EQ(Variable, N->getVariable());
1761 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001762 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001763 Line, Type, IsLocalToUnit, IsDefinition,
1764 Variable, StaticDataMemberDeclaration));
1765
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001766 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001767 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001768 File, Line, Type, IsLocalToUnit, IsDefinition,
1769 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001770 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001771 Line, Type, IsLocalToUnit, IsDefinition,
1772 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001773 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001774 Type, IsLocalToUnit, IsDefinition,
1775 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001776 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001777 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001778 Line, Type, IsLocalToUnit, IsDefinition,
1779 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001780 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001781 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001782 Line + 1, Type, IsLocalToUnit, IsDefinition,
1783 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001784 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001785 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001786 getDerivedType(), IsLocalToUnit, IsDefinition,
1787 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001788 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001789 Line, Type, !IsLocalToUnit, IsDefinition,
1790 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001791 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001792 Line, Type, IsLocalToUnit, !IsDefinition,
1793 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001794 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001795 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001796 Type, IsLocalToUnit, IsDefinition,
1797 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001798 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001799 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001800 Type, IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001801 cast<DIDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001802
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001803 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001804 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001805}
1806
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001807typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001808
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001809TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001810 unsigned Tag = dwarf::DW_TAG_arg_variable;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001811 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001812 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001813 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001814 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001815 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001816 unsigned Arg = 6;
1817 unsigned Flags = 7;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001818
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001819 auto *N = DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001820 Arg, Flags);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001821 EXPECT_EQ(Tag, N->getTag());
1822 EXPECT_EQ(Scope, N->getScope());
1823 EXPECT_EQ(Name, N->getName());
1824 EXPECT_EQ(File, N->getFile());
1825 EXPECT_EQ(Line, N->getLine());
1826 EXPECT_EQ(Type, N->getType());
1827 EXPECT_EQ(Arg, N->getArg());
1828 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001829 EXPECT_EQ(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001830 Arg, Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001831
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001832 EXPECT_NE(N, DILocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001833 Name, File, Line, Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001834 EXPECT_NE(N, DILocalVariable::get(Context, Tag, getSubprogram(), Name, File,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001835 Line, Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001836 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, "other", File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001837 Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001838 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001839 Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001840 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001841 Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001842 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001843 getDerivedType(), Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001844 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001845 Arg + 1, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001846 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001847 Arg, ~Flags));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001848
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001849 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001850 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001851}
1852
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001853TEST_F(DILocalVariableTest, getArg256) {
1854 EXPECT_EQ(255u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001855 getSubprogram(), "", getFile(), 0,
1856 nullptr, 255, 0)
1857 ->getArg());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001858 EXPECT_EQ(256u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001859 getSubprogram(), "", getFile(), 0,
1860 nullptr, 256, 0)
1861 ->getArg());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001862 EXPECT_EQ(257u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001863 getSubprogram(), "", getFile(), 0,
1864 nullptr, 257, 0)
1865 ->getArg());
1866 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001867 EXPECT_EQ(Max, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001868 getSubprogram(), "", getFile(), 0,
1869 nullptr, Max, 0)
1870 ->getArg());
1871}
1872
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001873typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001874
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001875TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001876 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001877 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001878 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001879 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001880
1881 EXPECT_EQ(5u, N->getNumElements());
1882 EXPECT_EQ(2u, N->getElement(0));
1883 EXPECT_EQ(6u, N->getElement(1));
1884 EXPECT_EQ(9u, N->getElement(2));
1885 EXPECT_EQ(78u, N->getElement(3));
1886 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001887
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001888 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001889 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001890}
1891
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001892TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001893#define EXPECT_VALID(...) \
1894 do { \
1895 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001896 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001897 } while (false)
1898#define EXPECT_INVALID(...) \
1899 do { \
1900 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001901 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001902 } while (false)
1903
1904 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001905 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001906
1907 // Valid constructions.
1908 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1909 EXPECT_VALID(dwarf::DW_OP_deref);
1910 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1911 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1912 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1913 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1914 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1915
1916 // Invalid constructions.
1917 EXPECT_INVALID(~0u);
1918 EXPECT_INVALID(dwarf::DW_OP_plus);
1919 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1920 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1921 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1922 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1923
1924#undef EXPECT_VALID
1925#undef EXPECT_INVALID
1926}
1927
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001928typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001929
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001930TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001931 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001932 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001933 unsigned Line = 5;
1934 StringRef GetterName = "getter";
1935 StringRef SetterName = "setter";
1936 unsigned Attributes = 7;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001937 DIType *Type = cast<DIBasicType>(getBasicType("basic"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001938
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001939 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001940 SetterName, Attributes, Type);
1941
1942 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1943 EXPECT_EQ(Name, N->getName());
1944 EXPECT_EQ(File, N->getFile());
1945 EXPECT_EQ(Line, N->getLine());
1946 EXPECT_EQ(GetterName, N->getGetterName());
1947 EXPECT_EQ(SetterName, N->getSetterName());
1948 EXPECT_EQ(Attributes, N->getAttributes());
1949 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001950 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001951 SetterName, Attributes, Type));
1952
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001953 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001954 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001955 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001956 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001957 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001958 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001959 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001960 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001961 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001962 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001963 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001964 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001965 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001966 SetterName, Attributes,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001967 cast<DIBasicType>(getBasicType("other"))));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001968
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001969 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001970 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001971}
1972
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001973typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001974
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001975TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001976 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001977 DIScope *Scope = getSubprogram();
1978 DINodeRef Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001979 unsigned Line = 5;
1980 StringRef Name = "name";
1981
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001982 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001983
1984 EXPECT_EQ(Tag, N->getTag());
1985 EXPECT_EQ(Scope, N->getScope());
1986 EXPECT_EQ(Entity, N->getEntity());
1987 EXPECT_EQ(Line, N->getLine());
1988 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001989 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001990
1991 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001992 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001993 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001994 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001995 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001996 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001997 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001998 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001999 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002000 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002001 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002002
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002003 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002004 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002005}
2006
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002007typedef MetadataTest MetadataAsValueTest;
2008
2009TEST_F(MetadataAsValueTest, MDNode) {
2010 MDNode *N = MDNode::get(Context, None);
2011 auto *V = MetadataAsValue::get(Context, N);
2012 EXPECT_TRUE(V->getType()->isMetadataTy());
2013 EXPECT_EQ(N, V->getMetadata());
2014
2015 auto *V2 = MetadataAsValue::get(Context, N);
2016 EXPECT_EQ(V, V2);
2017}
2018
2019TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2020 MDNode *N = MDNode::get(Context, None);
2021 Metadata *Ops[] = {N};
2022 MDNode *N2 = MDNode::get(Context, Ops);
2023 auto *V = MetadataAsValue::get(Context, N2);
2024 EXPECT_TRUE(V->getType()->isMetadataTy());
2025 EXPECT_EQ(N2, V->getMetadata());
2026
2027 auto *V2 = MetadataAsValue::get(Context, N2);
2028 EXPECT_EQ(V, V2);
2029
2030 auto *V3 = MetadataAsValue::get(Context, N);
2031 EXPECT_TRUE(V3->getType()->isMetadataTy());
2032 EXPECT_NE(V, V3);
2033 EXPECT_EQ(N, V3->getMetadata());
2034}
2035
2036TEST_F(MetadataAsValueTest, MDNodeConstant) {
2037 auto *C = ConstantInt::getTrue(Context);
2038 auto *MD = ConstantAsMetadata::get(C);
2039 Metadata *Ops[] = {MD};
2040 auto *N = MDNode::get(Context, Ops);
2041
2042 auto *V = MetadataAsValue::get(Context, MD);
2043 EXPECT_TRUE(V->getType()->isMetadataTy());
2044 EXPECT_EQ(MD, V->getMetadata());
2045
2046 auto *V2 = MetadataAsValue::get(Context, N);
2047 EXPECT_EQ(MD, V2->getMetadata());
2048 EXPECT_EQ(V, V2);
2049}
2050
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002051typedef MetadataTest ValueAsMetadataTest;
2052
2053TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2054 Type *Ty = Type::getInt1PtrTy(Context);
2055 std::unique_ptr<GlobalVariable> GV0(
2056 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2057 auto *MD = ValueAsMetadata::get(GV0.get());
2058 EXPECT_TRUE(MD->getValue() == GV0.get());
2059 ASSERT_TRUE(GV0->use_empty());
2060
2061 std::unique_ptr<GlobalVariable> GV1(
2062 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2063 GV0->replaceAllUsesWith(GV1.get());
2064 EXPECT_TRUE(MD->getValue() == GV1.get());
2065}
2066
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002067TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2068 // Create a constant.
2069 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2070 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2071
2072 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002073 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002074
2075 // 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 +00002076 Metadata *Ops1[] = {CI, CI, Temp.get()};
2077 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002078
2079 auto *N1 = MDTuple::get(Context, Ops1);
2080 auto *N2 = MDTuple::get(Context, Ops2);
2081 ASSERT_NE(N1, N2);
2082
2083 // Tell metadata that the constant is getting deleted.
2084 //
2085 // After this, N1 will be invalid, so don't touch it.
2086 ValueAsMetadata::handleDeletion(CI->getValue());
2087 EXPECT_EQ(nullptr, N2->getOperand(0));
2088 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002089 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002090
2091 // Clean up Temp for teardown.
2092 Temp->replaceAllUsesWith(nullptr);
2093}
2094
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002095typedef MetadataTest TrackingMDRefTest;
2096
2097TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2098 Type *Ty = Type::getInt1PtrTy(Context);
2099 std::unique_ptr<GlobalVariable> GV0(
2100 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2101 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2102 EXPECT_TRUE(MD->getValue() == GV0.get());
2103 ASSERT_TRUE(GV0->use_empty());
2104
2105 std::unique_ptr<GlobalVariable> GV1(
2106 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2107 GV0->replaceAllUsesWith(GV1.get());
2108 EXPECT_TRUE(MD->getValue() == GV1.get());
2109
2110 // Reset it, so we don't inadvertently test deletion.
2111 MD.reset();
2112}
2113
2114TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2115 Type *Ty = Type::getInt1PtrTy(Context);
2116 std::unique_ptr<GlobalVariable> GV(
2117 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2118 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2119 EXPECT_TRUE(MD->getValue() == GV.get());
2120 ASSERT_TRUE(GV->use_empty());
2121
2122 GV.reset();
2123 EXPECT_TRUE(!MD);
2124}
2125
Devang Patel0924b332009-07-30 00:03:41 +00002126TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002127 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002128 ConstantAsMetadata *C =
2129 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2130 ConstantAsMetadata *C2 =
2131 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002132
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002133 Metadata *const V = C;
2134 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002135 MDNode *n = MDNode::get(Context, V);
2136 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002137
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002138 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002139 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002140 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2141 NMD->addOperand(n);
2142 NMD->addOperand(n2);
2143
Chris Lattnerbe354a62009-08-23 04:47:35 +00002144 std::string Str;
2145 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002146 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002147 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002148 oss.str().c_str());
2149}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002150
2151typedef MetadataTest FunctionAttachmentTest;
2152TEST_F(FunctionAttachmentTest, setMetadata) {
2153 Function *F = getFunction("foo");
2154 ASSERT_FALSE(F->hasMetadata());
2155 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2156 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2157 EXPECT_EQ(nullptr, F->getMetadata("other"));
2158
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002159 DISubprogram *SP1 = getSubprogram();
2160 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002161 ASSERT_NE(SP1, SP2);
2162
2163 F->setMetadata("dbg", SP1);
2164 EXPECT_TRUE(F->hasMetadata());
2165 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2166 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2167 EXPECT_EQ(nullptr, F->getMetadata("other"));
2168
2169 F->setMetadata(LLVMContext::MD_dbg, SP2);
2170 EXPECT_TRUE(F->hasMetadata());
2171 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2172 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2173 EXPECT_EQ(nullptr, F->getMetadata("other"));
2174
2175 F->setMetadata("dbg", nullptr);
2176 EXPECT_FALSE(F->hasMetadata());
2177 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2178 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2179 EXPECT_EQ(nullptr, F->getMetadata("other"));
2180
2181 MDTuple *T1 = getTuple();
2182 MDTuple *T2 = getTuple();
2183 ASSERT_NE(T1, T2);
2184
2185 F->setMetadata("other1", T1);
2186 F->setMetadata("other2", T2);
2187 EXPECT_TRUE(F->hasMetadata());
2188 EXPECT_EQ(T1, F->getMetadata("other1"));
2189 EXPECT_EQ(T2, F->getMetadata("other2"));
2190 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2191
2192 F->setMetadata("other1", T2);
2193 F->setMetadata("other2", T1);
2194 EXPECT_EQ(T2, F->getMetadata("other1"));
2195 EXPECT_EQ(T1, F->getMetadata("other2"));
2196
2197 F->setMetadata("other1", nullptr);
2198 F->setMetadata("other2", nullptr);
2199 EXPECT_FALSE(F->hasMetadata());
2200 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2201 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2202}
2203
2204TEST_F(FunctionAttachmentTest, getAll) {
2205 Function *F = getFunction("foo");
2206
2207 MDTuple *T1 = getTuple();
2208 MDTuple *T2 = getTuple();
2209 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002210 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002211
2212 F->setMetadata("other1", T2);
2213 F->setMetadata(LLVMContext::MD_dbg, SP);
2214 F->setMetadata("other2", T1);
2215 F->setMetadata(LLVMContext::MD_prof, P);
2216 F->setMetadata("other2", T2);
2217 F->setMetadata("other1", T1);
2218
2219 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2220 F->getAllMetadata(MDs);
2221 ASSERT_EQ(4u, MDs.size());
2222 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2223 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2224 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2225 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2226 EXPECT_EQ(SP, MDs[0].second);
2227 EXPECT_EQ(P, MDs[1].second);
2228 EXPECT_EQ(T1, MDs[2].second);
2229 EXPECT_EQ(T2, MDs[3].second);
2230}
2231
2232TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2233 Function *F = getFunction("foo");
2234
2235 MDTuple *T1 = getTuple();
2236 MDTuple *T2 = getTuple();
2237 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002238 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002239
2240 F->setMetadata("other1", T1);
2241 F->setMetadata(LLVMContext::MD_dbg, SP);
2242 F->setMetadata("other2", T2);
2243 F->setMetadata(LLVMContext::MD_prof, P);
2244
2245 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2246 F->dropUnknownMetadata(Known);
2247
2248 EXPECT_EQ(T2, F->getMetadata("other2"));
2249 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2250 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2251 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2252
2253 F->setMetadata("other2", nullptr);
2254 F->setMetadata(LLVMContext::MD_prof, nullptr);
2255 EXPECT_FALSE(F->hasMetadata());
2256}
2257
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002258TEST_F(FunctionAttachmentTest, Verifier) {
2259 Function *F = getFunction("foo");
2260 F->setMetadata("attach", getTuple());
2261
2262 // Confirm this has no body.
2263 ASSERT_TRUE(F->empty());
2264
2265 // Functions without a body cannot have metadata attachments (they also can't
2266 // be verified directly, so check that the module fails to verify).
2267 EXPECT_TRUE(verifyModule(*F->getParent()));
2268
2269 // Functions with a body can.
2270 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2271 EXPECT_FALSE(verifyModule(*F->getParent()));
2272 EXPECT_FALSE(verifyFunction(*F));
2273}
2274
Diego Novillo2567f3d2015-05-13 15:13:45 +00002275TEST_F(FunctionAttachmentTest, EntryCount) {
2276 Function *F = getFunction("foo");
2277 EXPECT_FALSE(F->getEntryCount().hasValue());
2278 F->setEntryCount(12304);
2279 EXPECT_TRUE(F->getEntryCount().hasValue());
2280 EXPECT_EQ(12304u, *F->getEntryCount());
2281}
2282
Nick Lewycky49f89192009-04-04 07:22:01 +00002283}