blob: d14743dad70ed3d233a793acbb80f4edda91dcb8 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000012#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000013#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000014#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +000019#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000023#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024using namespace llvm;
25
26namespace {
27
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000028TEST(ContextAndReplaceableUsesTest, FromContext) {
29 LLVMContext Context;
30 ContextAndReplaceableUses CRU(Context);
31 EXPECT_EQ(&Context, &CRU.getContext());
32 EXPECT_FALSE(CRU.hasReplaceableUses());
33 EXPECT_FALSE(CRU.getReplaceableUses());
34}
35
36TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
37 LLVMContext Context;
38 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
39 EXPECT_EQ(&Context, &CRU.getContext());
40 EXPECT_TRUE(CRU.hasReplaceableUses());
41 EXPECT_TRUE(CRU.getReplaceableUses());
42}
43
44TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
45 LLVMContext Context;
46 ContextAndReplaceableUses CRU(Context);
47 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
48 EXPECT_EQ(&Context, &CRU.getContext());
49 EXPECT_TRUE(CRU.hasReplaceableUses());
50 EXPECT_TRUE(CRU.getReplaceableUses());
51}
52
53TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
54 LLVMContext Context;
55 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
56 auto *Ptr = ReplaceableUses.get();
57 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
58 ReplaceableUses = CRU.takeReplaceableUses();
59 EXPECT_EQ(&Context, &CRU.getContext());
60 EXPECT_FALSE(CRU.hasReplaceableUses());
61 EXPECT_FALSE(CRU.getReplaceableUses());
62 EXPECT_EQ(Ptr, ReplaceableUses.get());
63}
64
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000065class MetadataTest : public testing::Test {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000066public:
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000067 MetadataTest() : M("test", Context), Counter(0) {}
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000068
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000069protected:
70 LLVMContext Context;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000071 Module M;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000072 int Counter;
73
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000074 MDNode *getNode() { return MDNode::get(Context, None); }
75 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
76 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
77 Metadata *MDs[] = {MD1, MD2};
78 return MDNode::get(Context, MDs);
79 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000080
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000081 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000082 DISubroutineType *getSubroutineType() {
83 return DISubroutineType::getDistinct(Context, 0, getNode(nullptr));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000084 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000085 DISubprogram *getSubprogram() {
86 return DISubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
Adrian Prantl75819ae2016-04-15 15:57:41 +000087 nullptr, false, false, 0, nullptr,
88 0, 0, 0, false, nullptr);
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000089 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000090 DIFile *getFile() {
91 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000092 }
Adrian Prantl75819ae2016-04-15 15:57:41 +000093 DICompileUnit *getUnit() {
94 return DICompileUnit::getDistinct(Context, 1, getFile(), "clang", false,
95 "-g", 2, "", DICompileUnit::FullDebug,
96 getTuple(), getTuple(), getTuple(),
97 getTuple(), getTuple(), 0);
98 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000099 DIType *getBasicType(StringRef Name) {
100 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000101 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000102 DIType *getDerivedType() {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000103 return DIDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000104 nullptr, 0, nullptr,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000105 getBasicType("basictype"), 1, 2, 0, 0);
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000106 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000107 Constant *getConstant() {
108 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
109 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000110 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000111 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000112 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000113 DIType *getCompositeType() {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000114 return DICompositeType::getDistinct(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000115 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
116 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000117 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000118 Function *getFunction(StringRef Name) {
119 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000120 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
121 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000122};
123typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000124
Nick Lewycky49f89192009-04-04 07:22:01 +0000125// Test that construction of MDString with different value produces different
126// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000127TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000128 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000129 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000130 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000131 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000132 EXPECT_NE(s1, s2);
133}
134
135// Test that creation of MDStrings with the same string contents produces the
136// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000137TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000138 char x[4] = { 'a', 'b', 'c', 'X' };
139 char y[4] = { 'a', 'b', 'c', 'Y' };
140
Owen Anderson23587322009-07-31 21:38:10 +0000141 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
142 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000143 EXPECT_EQ(s1, s2);
144}
145
146// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000147TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000148 char *str = new char[13];
149 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000150 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000151 strncpy(str, "aaaaaaaaaaaaa", 13);
152 delete[] str;
153
Chris Lattnerbe354a62009-08-23 04:47:35 +0000154 std::string Str;
155 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000156 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000157 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000158}
159
160// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000161TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000162 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000163 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000164 std::string Str;
165 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000166 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000167 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000168}
169
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000170typedef MetadataTest MDNodeTest;
171
Nick Lewycky49f89192009-04-04 07:22:01 +0000172// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000173TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000174 char x[3] = { 'a', 'b', 'c' };
175 char y[3] = { '1', '2', '3' };
176
Owen Anderson23587322009-07-31 21:38:10 +0000177 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
178 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Mehdi Amini03b42e42016-04-14 21:59:01 +0000179 ConstantAsMetadata *CI =
180 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000181
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000182 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000183 V.push_back(s1);
184 V.push_back(CI);
185 V.push_back(s2);
186
Jay Foad5514afe2011-04-21 19:59:31 +0000187 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000188 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000189 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000190 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000191 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000192 MDNode *n4 = MDNode::getIfExists(Context, V);
193 MDNode *n5 = MDNode::getIfExists(Context, c1);
194 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000195 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000196 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000197 EXPECT_EQ(n4, n1);
198 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000199 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000200
Chris Lattner9b493022009-12-31 01:22:29 +0000201 EXPECT_EQ(3u, n1->getNumOperands());
202 EXPECT_EQ(s1, n1->getOperand(0));
203 EXPECT_EQ(CI, n1->getOperand(1));
204 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000205
Chris Lattner9b493022009-12-31 01:22:29 +0000206 EXPECT_EQ(1u, n2->getNumOperands());
207 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000208}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000209
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000210TEST_F(MDNodeTest, Delete) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000211 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
212 Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000213
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000214 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000215 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000216 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000217
218 EXPECT_EQ(n, wvh);
219
220 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000221}
Devang Patel0924b332009-07-30 00:03:41 +0000222
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000223TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000224 // !0 = !{!0}
225 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000226 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000227 auto Temp = MDNode::getTemporary(Context, None);
228 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000229 MDNode *Self = MDNode::get(Context, Args);
230 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000231 ASSERT_EQ(Self, Self->getOperand(0));
232
233 // Self-references should be distinct, so MDNode::get() should grab a
234 // uniqued node that references Self, not Self.
235 Args[0] = Self;
236 MDNode *Ref1 = MDNode::get(Context, Args);
237 MDNode *Ref2 = MDNode::get(Context, Args);
238 EXPECT_NE(Self, Ref1);
239 EXPECT_EQ(Ref1, Ref2);
240 }
241
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000242 // !0 = !{!0, !{}}
243 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000244 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000245 auto Temp = MDNode::getTemporary(Context, None);
246 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000247 MDNode *Self = MDNode::get(Context, Args);
248 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000249 ASSERT_EQ(Self, Self->getOperand(0));
250
251 // Self-references should be distinct, so MDNode::get() should grab a
252 // uniqued node that references Self, not Self itself.
253 Args[0] = Self;
254 MDNode *Ref1 = MDNode::get(Context, Args);
255 MDNode *Ref2 = MDNode::get(Context, Args);
256 EXPECT_NE(Self, Ref1);
257 EXPECT_EQ(Ref1, Ref2);
258 }
259}
260
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000261TEST_F(MDNodeTest, Print) {
262 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
263 MDString *S = MDString::get(Context, "foo");
264 MDNode *N0 = getNode();
265 MDNode *N1 = getNode(N0);
266 MDNode *N2 = getNode(N0, N1);
267
268 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
269 MDNode *N = MDNode::get(Context, Args);
270
271 std::string Expected;
272 {
273 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000274 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000275 C->printAsOperand(OS);
276 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000277 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000278 OS << ", null";
279 MDNode *Nodes[] = {N0, N1, N2};
280 for (auto *Node : Nodes)
281 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000282 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000283 }
284
285 std::string Actual;
286 {
287 raw_string_ostream OS(Actual);
288 N->print(OS);
289 }
290
291 EXPECT_EQ(Expected, Actual);
292}
293
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000294#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
295 do { \
296 std::string Actual_; \
297 raw_string_ostream OS(Actual_); \
298 PRINT; \
299 OS.flush(); \
300 std::string Expected_(EXPECTED); \
301 EXPECT_EQ(Expected_, Actual_); \
302 } while (false)
303
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000304TEST_F(MDNodeTest, PrintTemporary) {
305 MDNode *Arg = getNode();
306 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
307 MDNode *N = getNode(Temp.get());
308 Module M("test", Context);
309 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
310 NMD->addOperand(N);
311
312 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
313 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
314 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
315
316 // Cleanup.
317 Temp->replaceAllUsesWith(Arg);
318}
319
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000320TEST_F(MDNodeTest, PrintFromModule) {
321 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
322 MDString *S = MDString::get(Context, "foo");
323 MDNode *N0 = getNode();
324 MDNode *N1 = getNode(N0);
325 MDNode *N2 = getNode(N0, N1);
326
327 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
328 MDNode *N = MDNode::get(Context, Args);
329 Module M("test", Context);
330 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
331 NMD->addOperand(N);
332
333 std::string Expected;
334 {
335 raw_string_ostream OS(Expected);
336 OS << "!0 = !{";
337 C->printAsOperand(OS);
338 OS << ", ";
339 S->printAsOperand(OS);
340 OS << ", null, !1, !2, !3}";
341 }
342
343 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
344}
345
346TEST_F(MDNodeTest, PrintFromFunction) {
347 Module M("test", Context);
348 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
349 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
350 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
351 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
352 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
353 auto *R0 = ReturnInst::Create(Context, BB0);
354 auto *R1 = ReturnInst::Create(Context, BB1);
355 auto *N0 = MDNode::getDistinct(Context, None);
356 auto *N1 = MDNode::getDistinct(Context, None);
357 R0->setMetadata("md", N0);
358 R1->setMetadata("md", N1);
359
360 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
361 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000362
363 ModuleSlotTracker MST(&M);
364 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
365 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000366}
367
368TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
369 Module M("test", Context);
370
371 auto *Intrinsic =
372 Function::Create(FunctionType::get(Type::getVoidTy(Context),
373 Type::getMetadataTy(Context), false),
374 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
375
376 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
377 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
378 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
379 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
380 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
381 auto *N0 = MDNode::getDistinct(Context, None);
382 auto *N1 = MDNode::getDistinct(Context, None);
383 auto *MAV0 = MetadataAsValue::get(Context, N0);
384 auto *MAV1 = MetadataAsValue::get(Context, N1);
385 CallInst::Create(Intrinsic, MAV0, "", BB0);
386 CallInst::Create(Intrinsic, MAV1, "", BB1);
387
388 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
389 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
390 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
391 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
392 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
393 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000394
395 ModuleSlotTracker MST(&M);
396 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
397 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
398 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
399 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
400 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
401 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000402}
403#undef EXPECT_PRINTER_EQ
404
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000405TEST_F(MDNodeTest, NullOperand) {
406 // metadata !{}
407 MDNode *Empty = MDNode::get(Context, None);
408
409 // metadata !{metadata !{}}
410 Metadata *Ops[] = {Empty};
411 MDNode *N = MDNode::get(Context, Ops);
412 ASSERT_EQ(Empty, N->getOperand(0));
413
414 // metadata !{metadata !{}} => metadata !{null}
415 N->replaceOperandWith(0, nullptr);
416 ASSERT_EQ(nullptr, N->getOperand(0));
417
418 // metadata !{null}
419 Ops[0] = nullptr;
420 MDNode *NullOp = MDNode::get(Context, Ops);
421 ASSERT_EQ(nullptr, NullOp->getOperand(0));
422 EXPECT_EQ(N, NullOp);
423}
424
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000425TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
426 // !{}
427 MDNode *Empty = MDNode::get(Context, None);
428 ASSERT_TRUE(Empty->isResolved());
429 EXPECT_FALSE(Empty->isDistinct());
430
431 // !{!{}}
432 Metadata *Wrapped1Ops[] = {Empty};
433 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
434 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
435 ASSERT_TRUE(Wrapped1->isResolved());
436 EXPECT_FALSE(Wrapped1->isDistinct());
437
438 // !{!{!{}}}
439 Metadata *Wrapped2Ops[] = {Wrapped1};
440 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
441 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
442 ASSERT_TRUE(Wrapped2->isResolved());
443 EXPECT_FALSE(Wrapped2->isDistinct());
444
445 // !{!{!{}}} => !{!{}}
446 Wrapped2->replaceOperandWith(0, Empty);
447 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
448 EXPECT_TRUE(Wrapped2->isDistinct());
449 EXPECT_FALSE(Wrapped1->isDistinct());
450}
451
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000452TEST_F(MDNodeTest, getDistinct) {
453 // !{}
454 MDNode *Empty = MDNode::get(Context, None);
455 ASSERT_TRUE(Empty->isResolved());
456 ASSERT_FALSE(Empty->isDistinct());
457 ASSERT_EQ(Empty, MDNode::get(Context, None));
458
459 // distinct !{}
460 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
461 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
462 EXPECT_TRUE(Distinct1->isResolved());
463 EXPECT_TRUE(Distinct2->isDistinct());
464 EXPECT_NE(Empty, Distinct1);
465 EXPECT_NE(Empty, Distinct2);
466 EXPECT_NE(Distinct1, Distinct2);
467
468 // !{}
469 ASSERT_EQ(Empty, MDNode::get(Context, None));
470}
471
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000472TEST_F(MDNodeTest, isUniqued) {
473 MDNode *U = MDTuple::get(Context, None);
474 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000475 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000476 EXPECT_TRUE(U->isUniqued());
477 EXPECT_FALSE(D->isUniqued());
478 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000479}
480
481TEST_F(MDNodeTest, isDistinct) {
482 MDNode *U = MDTuple::get(Context, None);
483 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000484 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000485 EXPECT_FALSE(U->isDistinct());
486 EXPECT_TRUE(D->isDistinct());
487 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000488}
489
490TEST_F(MDNodeTest, isTemporary) {
491 MDNode *U = MDTuple::get(Context, None);
492 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000493 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000494 EXPECT_FALSE(U->isTemporary());
495 EXPECT_FALSE(D->isTemporary());
496 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000497}
498
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000499TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
500 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000501 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000502 ASSERT_FALSE(Temp->isResolved());
503
504 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000505 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000506 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
507 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000508 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000509
510 // temporary !{} => !{}
511 MDNode *Empty = MDNode::get(Context, None);
512 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000513 EXPECT_EQ(Empty, Distinct->getOperand(0));
514}
515
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000516TEST_F(MDNodeTest, handleChangedOperandRecursion) {
517 // !0 = !{}
518 MDNode *N0 = MDNode::get(Context, None);
519
520 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000521 auto Temp3 = MDTuple::getTemporary(Context, None);
522 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000523 MDNode *N1 = MDNode::get(Context, Ops1);
524
525 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000526 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000527 MDNode *N2 = MDNode::get(Context, Ops2);
528
529 // !3 = !{!2}
530 Metadata *Ops3[] = {N2};
531 MDNode *N3 = MDNode::get(Context, Ops3);
532 Temp3->replaceAllUsesWith(N3);
533
534 // !4 = !{!1}
535 Metadata *Ops4[] = {N1};
536 MDNode *N4 = MDNode::get(Context, Ops4);
537
538 // Confirm that the cycle prevented RAUW from getting dropped.
539 EXPECT_TRUE(N0->isResolved());
540 EXPECT_FALSE(N1->isResolved());
541 EXPECT_FALSE(N2->isResolved());
542 EXPECT_FALSE(N3->isResolved());
543 EXPECT_FALSE(N4->isResolved());
544
545 // Create a couple of distinct nodes to observe what's going on.
546 //
547 // !5 = distinct !{!2}
548 // !6 = distinct !{!3}
549 Metadata *Ops5[] = {N2};
550 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
551 Metadata *Ops6[] = {N3};
552 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
553
554 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
555 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
556 // references !3, this can cause a re-entry of handleChangedOperand() when !3
557 // is not ready for it.
558 //
559 // !2->replaceOperandWith(1, nullptr)
560 // !2: !{!3, !0} => !{!3, null}
561 // !2->replaceAllUsesWith(!1)
562 // !3: !{!2] => !{!1}
563 // !3->replaceAllUsesWith(!4)
564 N2->replaceOperandWith(1, nullptr);
565
566 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
567 // under us. Just check that the other nodes are sane.
568 //
569 // !1 = !{!4, null}
570 // !4 = !{!1}
571 // !5 = distinct !{!1}
572 // !6 = distinct !{!4}
573 EXPECT_EQ(N4, N1->getOperand(0));
574 EXPECT_EQ(N1, N4->getOperand(0));
575 EXPECT_EQ(N1, N5->getOperand(0));
576 EXPECT_EQ(N4, N6->getOperand(0));
577}
578
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000579TEST_F(MDNodeTest, replaceResolvedOperand) {
580 // Check code for replacing one resolved operand with another. If doing this
581 // directly (via replaceOperandWith()) becomes illegal, change the operand to
582 // a global value that gets RAUW'ed.
583 //
584 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000585 auto Temp = MDTuple::getTemporary(Context, None);
586 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000587
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000588 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000589 MDNode *N = MDTuple::get(Context, Ops);
590 EXPECT_EQ(nullptr, N->getOperand(0));
591 ASSERT_FALSE(N->isResolved());
592
593 // Check code for replacing resolved nodes.
594 N->replaceOperandWith(0, Empty);
595 EXPECT_EQ(Empty, N->getOperand(0));
596
597 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000598 N->replaceOperandWith(0, Temp.get());
599 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000600
601 // Remove the references to Temp; required for teardown.
602 Temp->replaceAllUsesWith(nullptr);
603}
604
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000605TEST_F(MDNodeTest, replaceWithUniqued) {
606 auto *Empty = MDTuple::get(Context, None);
607 MDTuple *FirstUniqued;
608 {
609 Metadata *Ops[] = {Empty};
610 auto Temp = MDTuple::getTemporary(Context, Ops);
611 EXPECT_TRUE(Temp->isTemporary());
612
613 // Don't expect a collision.
614 auto *Current = Temp.get();
615 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
616 EXPECT_TRUE(FirstUniqued->isUniqued());
617 EXPECT_TRUE(FirstUniqued->isResolved());
618 EXPECT_EQ(Current, FirstUniqued);
619 }
620 {
621 Metadata *Ops[] = {Empty};
622 auto Temp = MDTuple::getTemporary(Context, Ops);
623 EXPECT_TRUE(Temp->isTemporary());
624
625 // Should collide with Uniqued above this time.
626 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
627 EXPECT_TRUE(Uniqued->isUniqued());
628 EXPECT_TRUE(Uniqued->isResolved());
629 EXPECT_EQ(FirstUniqued, Uniqued);
630 }
631 {
632 auto Unresolved = MDTuple::getTemporary(Context, None);
633 Metadata *Ops[] = {Unresolved.get()};
634 auto Temp = MDTuple::getTemporary(Context, Ops);
635 EXPECT_TRUE(Temp->isTemporary());
636
637 // Shouldn't be resolved.
638 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
639 EXPECT_TRUE(Uniqued->isUniqued());
640 EXPECT_FALSE(Uniqued->isResolved());
641
642 // Should be a different node.
643 EXPECT_NE(FirstUniqued, Uniqued);
644
645 // Should resolve when we update its node (note: be careful to avoid a
646 // collision with any other nodes above).
647 Uniqued->replaceOperandWith(0, nullptr);
648 EXPECT_TRUE(Uniqued->isResolved());
649 }
650}
651
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000652TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000653 // temp !{}
654 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
655 EXPECT_FALSE(Op->isResolved());
656
657 // temp !{temp !{}}
658 Metadata *Ops[] = {Op};
659 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
660 EXPECT_FALSE(N->isResolved());
661
662 // temp !{temp !{}} => !{temp !{}}
663 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
664 EXPECT_FALSE(N->isResolved());
665
666 // !{temp !{}} => !{!{}}
667 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
668 EXPECT_TRUE(Op->isResolved());
669 EXPECT_TRUE(N->isResolved());
670}
671
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000672TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000673 // i1* @GV
674 Type *Ty = Type::getInt1PtrTy(Context);
675 std::unique_ptr<GlobalVariable> GV(
676 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
677 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
678
679 // temp !{i1* @GV}
680 Metadata *Ops[] = {Op};
681 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
682
683 // temp !{i1* @GV} => !{i1* @GV}
684 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
685 ASSERT_TRUE(N->isUniqued());
686
687 // !{i1* @GV} => !{null}
688 GV.reset();
689 ASSERT_TRUE(N->isUniqued());
690 Metadata *NullOps[] = {nullptr};
691 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
692}
693
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000694TEST_F(MDNodeTest, replaceWithDistinct) {
695 {
696 auto *Empty = MDTuple::get(Context, None);
697 Metadata *Ops[] = {Empty};
698 auto Temp = MDTuple::getTemporary(Context, Ops);
699 EXPECT_TRUE(Temp->isTemporary());
700
701 // Don't expect a collision.
702 auto *Current = Temp.get();
703 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
704 EXPECT_TRUE(Distinct->isDistinct());
705 EXPECT_TRUE(Distinct->isResolved());
706 EXPECT_EQ(Current, Distinct);
707 }
708 {
709 auto Unresolved = MDTuple::getTemporary(Context, None);
710 Metadata *Ops[] = {Unresolved.get()};
711 auto Temp = MDTuple::getTemporary(Context, Ops);
712 EXPECT_TRUE(Temp->isTemporary());
713
714 // Don't expect a collision.
715 auto *Current = Temp.get();
716 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
717 EXPECT_TRUE(Distinct->isDistinct());
718 EXPECT_TRUE(Distinct->isResolved());
719 EXPECT_EQ(Current, Distinct);
720
721 // Cleanup; required for teardown.
722 Unresolved->replaceAllUsesWith(nullptr);
723 }
724}
725
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000726TEST_F(MDNodeTest, replaceWithPermanent) {
727 Metadata *Ops[] = {nullptr};
728 auto Temp = MDTuple::getTemporary(Context, Ops);
729 auto *T = Temp.get();
730
731 // U is a normal, uniqued node that references T.
732 auto *U = MDTuple::get(Context, T);
733 EXPECT_TRUE(U->isUniqued());
734
735 // Make Temp self-referencing.
736 Temp->replaceOperandWith(0, T);
737
738 // Try to uniquify Temp. This should, despite the name in the API, give a
739 // 'distinct' node, since self-references aren't allowed to be uniqued.
740 //
741 // Since it's distinct, N should have the same address as when it was a
742 // temporary (i.e., be equal to T not U).
743 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
744 EXPECT_EQ(N, T);
745 EXPECT_TRUE(N->isDistinct());
746
747 // U should be the canonical unique node with N as the argument.
748 EXPECT_EQ(U, MDTuple::get(Context, N));
749 EXPECT_TRUE(U->isUniqued());
750
751 // This temporary should collide with U when replaced, but it should still be
752 // uniqued.
753 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
754 EXPECT_TRUE(U->isUniqued());
755
756 // This temporary should become a new uniqued node.
757 auto Temp2 = MDTuple::getTemporary(Context, U);
758 auto *V = Temp2.get();
759 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
760 EXPECT_TRUE(V->isUniqued());
761 EXPECT_EQ(U, V->getOperand(0));
762}
763
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000764TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
765 TrackingMDRef Ref;
766 EXPECT_EQ(nullptr, Ref.get());
767 {
768 auto Temp = MDTuple::getTemporary(Context, None);
769 Ref.reset(Temp.get());
770 EXPECT_EQ(Temp.get(), Ref.get());
771 }
772 EXPECT_EQ(nullptr, Ref.get());
773}
774
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000775typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000776
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000777TEST_F(DILocationTest, Overflow) {
778 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000779 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000780 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000781 EXPECT_EQ(2u, L->getLine());
782 EXPECT_EQ(7u, L->getColumn());
783 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000784 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000785 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000786 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000787 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000788 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000789 }
790 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000791 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000792 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000793 EXPECT_EQ(0u, L->getColumn());
794 }
795 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000796 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000797 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000798 EXPECT_EQ(0u, L->getColumn());
799 }
800}
801
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000802TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000803 MDNode *N = getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000804 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000805 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000806 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000807 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000808 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000809}
810
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000811TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000812 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000813 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000814 EXPECT_TRUE(L->isTemporary());
815 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000816}
817
Teresa Johnsond98152b62015-12-07 15:05:44 +0000818TEST_F(DILocationTest, cloneTemporary) {
819 MDNode *N = MDNode::get(Context, None);
820 auto L = DILocation::getTemporary(Context, 2, 7, N);
821 EXPECT_TRUE(L->isTemporary());
822 auto L2 = L->clone();
823 EXPECT_TRUE(L2->isTemporary());
824}
825
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000826typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000827
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000828TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000829 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000830 auto *Empty = MDNode::get(Context, None);
831 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000832 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000833 EXPECT_EQ(15u, N->getTag());
834 EXPECT_EQ(2u, N->getNumOperands());
835 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000836 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000837 EXPECT_EQ(1u, N->getNumDwarfOperands());
838 EXPECT_EQ(Empty, N->getDwarfOperand(0));
839 EXPECT_EQ(Empty, N->getOperand(1));
840 ASSERT_TRUE(N->isUniqued());
841
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000842 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000843
844 N->replaceOperandWith(1, nullptr);
845 EXPECT_EQ(15u, N->getTag());
846 EXPECT_EQ(Header, N->getHeader());
847 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
848 ASSERT_TRUE(N->isUniqued());
849
850 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000851 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000852
853 N->replaceDwarfOperandWith(0, Empty);
854 EXPECT_EQ(15u, N->getTag());
855 EXPECT_EQ(Header, N->getHeader());
856 EXPECT_EQ(Empty, N->getDwarfOperand(0));
857 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000858 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000859
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000860 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000861 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000862}
863
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000864TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000865 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000866 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000867 EXPECT_EQ(StringRef(), N->getHeader());
868 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000869}
870
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000871typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000872
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000873TEST_F(DISubrangeTest, get) {
874 auto *N = DISubrange::get(Context, 5, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000875 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
876 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000877 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000878 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
879 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000880
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000881 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000882 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000883}
884
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000885TEST_F(DISubrangeTest, getEmptyArray) {
886 auto *N = DISubrange::get(Context, -1, 0);
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000887 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
888 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000889 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000890 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000891}
892
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000893typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000894
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000895TEST_F(DIEnumeratorTest, get) {
896 auto *N = DIEnumerator::get(Context, 7, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000897 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
898 EXPECT_EQ(7, N->getValue());
899 EXPECT_EQ("name", N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000900 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000901
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000902 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
903 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000904
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000905 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000906 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000907}
908
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000909typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000910
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000911TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000912 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000913 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000914 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
915 EXPECT_EQ("special", N->getName());
916 EXPECT_EQ(33u, N->getSizeInBits());
917 EXPECT_EQ(26u, N->getAlignInBits());
918 EXPECT_EQ(7u, N->getEncoding());
919 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000920 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000921 26, 7));
922
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000923 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000924 "special", 33, 26, 7));
925 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000926 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
927 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000928 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000929 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000930 25, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000931 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000932 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000933
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000934 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000935 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000936}
937
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000938TEST_F(DIBasicTypeTest, getWithLargeValues) {
939 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000940 UINT64_MAX, UINT64_MAX - 1, 7);
941 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
942 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
943}
944
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000945TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000946 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000947 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000948 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
949 EXPECT_EQ("unspecified", N->getName());
950 EXPECT_EQ(0u, N->getSizeInBits());
951 EXPECT_EQ(0u, N->getAlignInBits());
952 EXPECT_EQ(0u, N->getEncoding());
953 EXPECT_EQ(0u, N->getLine());
954}
955
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000956typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000957
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000958TEST_F(DITypeTest, clone) {
959 // Check that DIType has a specialized clone that returns TempDIType.
960 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000961 dwarf::DW_ATE_signed);
962
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000963 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000964 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
965}
966
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000967TEST_F(DITypeTest, setFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000968 // void (void)
969 Metadata *TypesOps[] = {nullptr};
970 Metadata *Types = MDTuple::get(Context, TypesOps);
971
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000972 DIType *D = DISubroutineType::getDistinct(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000973 EXPECT_EQ(0u, D->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000974 D->setFlags(DINode::FlagRValueReference);
975 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000976 D->setFlags(0u);
977 EXPECT_EQ(0u, D->getFlags());
978
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000979 TempDIType T = DISubroutineType::getTemporary(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000980 EXPECT_EQ(0u, T->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000981 T->setFlags(DINode::FlagRValueReference);
982 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000983 T->setFlags(0u);
984 EXPECT_EQ(0u, T->getFlags());
985}
986
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000987typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000988
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000989TEST_F(DIDerivedTypeTest, get) {
990 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000991 DIScope *Scope = getSubprogram();
992 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000993 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000994
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000995 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000996 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
997 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
998 EXPECT_EQ("something", N->getName());
999 EXPECT_EQ(File, N->getFile());
1000 EXPECT_EQ(1u, N->getLine());
1001 EXPECT_EQ(Scope, N->getScope());
1002 EXPECT_EQ(BaseType, N->getBaseType());
1003 EXPECT_EQ(2u, N->getSizeInBits());
1004 EXPECT_EQ(3u, N->getAlignInBits());
1005 EXPECT_EQ(4u, N->getOffsetInBits());
1006 EXPECT_EQ(5u, N->getFlags());
1007 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001008 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001009 "something", File, 1, Scope, BaseType, 2, 3,
1010 4, 5, ExtraData));
1011
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001012 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001013 "something", File, 1, Scope, BaseType, 2, 3,
1014 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001015 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001016 File, 1, Scope, BaseType, 2, 3, 4, 5,
1017 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001018 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001019 "something", getFile(), 1, Scope, BaseType, 2,
1020 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001021 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001022 "something", File, 2, Scope, BaseType, 2, 3,
1023 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001024 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001025 "something", File, 1, getSubprogram(),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001026 BaseType, 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001027 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001028 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1029 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001030 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001031 "something", File, 1, Scope, BaseType, 3, 3,
1032 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001033 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001034 "something", File, 1, Scope, BaseType, 2, 2,
1035 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001036 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001037 "something", File, 1, Scope, BaseType, 2, 3,
1038 5, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001039 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001040 "something", File, 1, Scope, BaseType, 2, 3,
1041 4, 4, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001042 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001043 "something", File, 1, Scope, BaseType, 2, 3,
1044 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001045
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001046 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001047 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001048}
1049
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001050TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1051 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001052 DIScope *Scope = getSubprogram();
1053 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001054 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001055
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001056 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001057 File, 1, Scope, BaseType, UINT64_MAX,
1058 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1059 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1060 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1061 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1062}
1063
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001064typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001065
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001066TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001067 unsigned Tag = dwarf::DW_TAG_structure_type;
1068 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001069 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001070 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001071 DIScope *Scope = getSubprogram();
1072 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001073 uint64_t SizeInBits = 2;
1074 uint64_t AlignInBits = 3;
1075 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001076 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001077 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001078 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001079 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001080 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001081 StringRef Identifier = "some id";
1082
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001083 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001084 BaseType, SizeInBits, AlignInBits,
1085 OffsetInBits, Flags, Elements, RuntimeLang,
1086 VTableHolder, TemplateParams, Identifier);
1087 EXPECT_EQ(Tag, N->getTag());
1088 EXPECT_EQ(Name, N->getName());
1089 EXPECT_EQ(File, N->getFile());
1090 EXPECT_EQ(Line, N->getLine());
1091 EXPECT_EQ(Scope, N->getScope());
1092 EXPECT_EQ(BaseType, N->getBaseType());
1093 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1094 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1095 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1096 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001097 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001098 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1099 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001100 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001101 EXPECT_EQ(Identifier, N->getIdentifier());
1102
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001103 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001104 BaseType, SizeInBits, AlignInBits,
1105 OffsetInBits, Flags, Elements, RuntimeLang,
1106 VTableHolder, TemplateParams, Identifier));
1107
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001108 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001109 BaseType, SizeInBits, AlignInBits,
1110 OffsetInBits, Flags, Elements, RuntimeLang,
1111 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001112 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001113 BaseType, SizeInBits, AlignInBits,
1114 OffsetInBits, Flags, Elements, RuntimeLang,
1115 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001116 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001117 BaseType, SizeInBits, AlignInBits,
1118 OffsetInBits, Flags, Elements, RuntimeLang,
1119 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001120 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001121 BaseType, SizeInBits, AlignInBits,
1122 OffsetInBits, Flags, Elements, RuntimeLang,
1123 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001124 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001125 Context, Tag, Name, File, Line, getSubprogram(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001126 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1127 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001128 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001129 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1130 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1131 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001132 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001133 BaseType, SizeInBits + 1, AlignInBits,
1134 OffsetInBits, Flags, Elements, RuntimeLang,
1135 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001136 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001137 BaseType, SizeInBits, AlignInBits + 1,
1138 OffsetInBits, Flags, Elements, RuntimeLang,
1139 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001140 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001141 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1142 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1143 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001144 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001145 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1146 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1147 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001148 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001149 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1150 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1151 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001152 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001153 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1154 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1155 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001156 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001157 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1158 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1159 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001160 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001161 BaseType, SizeInBits, AlignInBits,
1162 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001163 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001164 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001165 BaseType, SizeInBits, AlignInBits,
1166 OffsetInBits, Flags, Elements, RuntimeLang,
1167 VTableHolder, TemplateParams, "other"));
1168
1169 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001170 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1171 BaseType, SizeInBits, AlignInBits,
1172 OffsetInBits, Flags, Elements, RuntimeLang,
1173 VTableHolder, TemplateParams, "")
1174 ->getRawIdentifier());
1175 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1176 BaseType, SizeInBits, AlignInBits,
1177 OffsetInBits, Flags, Elements, RuntimeLang,
1178 VTableHolder, TemplateParams)
1179 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001180
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001181 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001182 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001183}
1184
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001185TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001186 unsigned Tag = dwarf::DW_TAG_structure_type;
1187 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001188 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001189 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001190 DIScope *Scope = getSubprogram();
1191 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001192 uint64_t SizeInBits = UINT64_MAX;
1193 uint64_t AlignInBits = UINT64_MAX - 1;
1194 uint64_t OffsetInBits = UINT64_MAX - 2;
1195 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001196 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001197 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001198 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001199 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001200 StringRef Identifier = "some id";
1201
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001202 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001203 BaseType, SizeInBits, AlignInBits,
1204 OffsetInBits, Flags, Elements, RuntimeLang,
1205 VTableHolder, TemplateParams, Identifier);
1206 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1207 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1208 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1209}
1210
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001211TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001212 unsigned Tag = dwarf::DW_TAG_structure_type;
1213 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001214 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001215 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001216 DIScope *Scope = getSubprogram();
1217 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001218 uint64_t SizeInBits = 2;
1219 uint64_t AlignInBits = 3;
1220 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001221 unsigned Flags = 5;
1222 unsigned RuntimeLang = 6;
1223 StringRef Identifier = "some id";
1224
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001225 auto *N = DICompositeType::get(
1226 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1227 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001228
1229 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001230 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001231 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001232 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001233 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001234 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001235
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001236 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001237 EXPECT_EQ(nullptr, N->getVTableHolder());
1238 N->replaceVTableHolder(VTableHolder);
1239 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1240 N->replaceVTableHolder(nullptr);
1241 EXPECT_EQ(nullptr, N->getVTableHolder());
1242
1243 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001244 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001245 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001246 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001247 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001248 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001249}
1250
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001251typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001252
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001253TEST_F(DISubroutineTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001254 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001255 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001256
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001257 auto *N = DISubroutineType::get(Context, Flags, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001258 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1259 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001260 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001261 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001262
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001263 EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, TypeArray));
1264 EXPECT_NE(N, DISubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001265
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001266 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001267 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001268
1269 // Test always-empty operands.
1270 EXPECT_EQ(nullptr, N->getScope());
1271 EXPECT_EQ(nullptr, N->getFile());
1272 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001273}
1274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001275typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001276
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001277TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001278 StringRef Filename = "file";
1279 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001280 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001281
1282 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1283 EXPECT_EQ(Filename, N->getFilename());
1284 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001285 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001286
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001287 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1288 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001289
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001290 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001291 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001292}
1293
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001294TEST_F(DIFileTest, ScopeGetFile) {
1295 // Ensure that DIScope::getFile() returns itself.
1296 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001297 EXPECT_EQ(N, N->getFile());
1298}
1299
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001300typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001301
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001302TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001303 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001304 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001305 StringRef Producer = "some producer";
1306 bool IsOptimized = false;
1307 StringRef Flags = "flag after flag";
1308 unsigned RuntimeVersion = 2;
1309 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001310 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001311 MDTuple *EnumTypes = getTuple();
1312 MDTuple *RetainedTypes = getTuple();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001313 MDTuple *GlobalVariables = getTuple();
1314 MDTuple *ImportedEntities = getTuple();
Adrian Prantle3b49e02015-09-22 23:42:47 +00001315 uint64_t DWOId = 0x10000000c0ffee;
Amjad Abouda9bcf162015-12-10 12:56:35 +00001316 MDTuple *Macros = getTuple();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001317 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001318 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1319 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001320 RetainedTypes, GlobalVariables, ImportedEntities, Macros,
Amjad Abouda9bcf162015-12-10 12:56:35 +00001321 DWOId);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001322
1323 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1324 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1325 EXPECT_EQ(File, N->getFile());
1326 EXPECT_EQ(Producer, N->getProducer());
1327 EXPECT_EQ(IsOptimized, N->isOptimized());
1328 EXPECT_EQ(Flags, N->getFlags());
1329 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1330 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1331 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001332 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1333 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001334 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1335 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001336 EXPECT_EQ(Macros, N->getMacros().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001337 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001338
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001339 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001340 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1341 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1342 EXPECT_EQ(File, Temp->getFile());
1343 EXPECT_EQ(Producer, Temp->getProducer());
1344 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1345 EXPECT_EQ(Flags, Temp->getFlags());
1346 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1347 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1348 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1349 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1350 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001351 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1352 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001353 EXPECT_EQ(Macros, Temp->getMacros().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001354 EXPECT_EQ(DWOId, Temp->getDWOId());
1355
1356 auto *TempAddress = Temp.get();
1357 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1358 EXPECT_TRUE(Clone->isDistinct());
1359 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001360}
1361
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001362TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001363 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001364 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001365 StringRef Producer = "some producer";
1366 bool IsOptimized = false;
1367 StringRef Flags = "flag after flag";
1368 unsigned RuntimeVersion = 2;
1369 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001370 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001371 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1372 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1373 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001374 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001375 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001376 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1377 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001378 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001379
1380 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001381 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001382 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001383 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001384 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001385 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001386
1387 auto *Macros = MDTuple::getDistinct(Context, None);
1388 EXPECT_EQ(nullptr, N->getMacros().get());
1389 N->replaceMacros(Macros);
1390 EXPECT_EQ(Macros, N->getMacros().get());
1391 N->replaceMacros(nullptr);
1392 EXPECT_EQ(nullptr, N->getMacros().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001393}
1394
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001395typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001396
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001397TEST_F(DISubprogramTest, get) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001398 DIScope *Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001399 StringRef Name = "name";
1400 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001401 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001402 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001403 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001404 bool IsLocalToUnit = false;
1405 bool IsDefinition = true;
1406 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001407 DIType *ContainingType = getCompositeType();
Davide Italiano236e7442016-04-13 20:17:42 +00001408 unsigned Virtuality = 2;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001409 unsigned VirtualIndex = 5;
1410 unsigned Flags = 6;
Davide Italiano236e7442016-04-13 20:17:42 +00001411 unsigned NotFlags = (~Flags) & ((1 << 27) - 1);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001412 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001413 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001414 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001415 MDTuple *Variables = getTuple();
Adrian Prantl75819ae2016-04-15 15:57:41 +00001416 DICompileUnit *Unit = getUnit();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001417
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001418 auto *N = DISubprogram::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001419 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1420 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001421 IsOptimized, Unit, TemplateParams, Declaration, Variables);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001422
1423 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1424 EXPECT_EQ(Scope, N->getScope());
1425 EXPECT_EQ(Name, N->getName());
1426 EXPECT_EQ(LinkageName, N->getLinkageName());
1427 EXPECT_EQ(File, N->getFile());
1428 EXPECT_EQ(Line, N->getLine());
1429 EXPECT_EQ(Type, N->getType());
1430 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1431 EXPECT_EQ(IsDefinition, N->isDefinition());
1432 EXPECT_EQ(ScopeLine, N->getScopeLine());
1433 EXPECT_EQ(ContainingType, N->getContainingType());
1434 EXPECT_EQ(Virtuality, N->getVirtuality());
1435 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1436 EXPECT_EQ(Flags, N->getFlags());
1437 EXPECT_EQ(IsOptimized, N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001438 EXPECT_EQ(Unit, N->getUnit());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001439 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001440 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001441 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001442 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001443 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1444 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001445 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001446 Declaration, Variables));
1447
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001448 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001449 File, Line, Type, IsLocalToUnit, IsDefinition,
1450 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001451 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001452 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001453 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001454 Line, Type, IsLocalToUnit, IsDefinition,
1455 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001456 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001457 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001458 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001459 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1460 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001461 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001462 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001463 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001464 Line, Type, IsLocalToUnit, IsDefinition,
1465 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001466 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001467 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001468 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001469 Line + 1, Type, IsLocalToUnit, IsDefinition,
1470 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001471 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001472 TemplateParams, Declaration, Variables));
Peter Collingbourned4bff302015-11-05 22:03:56 +00001473 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1474 getSubroutineType(), IsLocalToUnit,
1475 IsDefinition, ScopeLine, ContainingType,
1476 Virtuality, VirtualIndex, Flags, IsOptimized,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001477 Unit, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001478 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001479 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1480 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001481 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001482 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001483 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001484 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1485 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001486 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001487 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001488 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001489 Type, IsLocalToUnit, IsDefinition,
1490 ScopeLine + 1, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001491 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001492 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001493 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001494 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001495 getCompositeType(), Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001496 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001497 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001498 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001499 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1500 ContainingType, Virtuality + 1, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001501 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001502 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 + 1,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001506 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001507 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,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001511 NotFlags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001512 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, ScopeLine,
1515 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001516 Flags, !IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001517 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 Smith7ad0bd52015-04-11 20:27:40 +00001519 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1520 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001521 Flags, IsOptimized, nullptr, TemplateParams,
1522 Declaration, Variables));
1523 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1524 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1525 ContainingType, Virtuality, VirtualIndex,
1526 Flags, IsOptimized, Unit, getTuple(),
1527 Declaration, Variables));
1528 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1529 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1530 ContainingType, Virtuality, VirtualIndex,
1531 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001532 getSubprogram(), 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,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001536 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001537 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001538
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001539 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001540 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001541}
1542
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001543typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001544
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001545TEST_F(DILexicalBlockTest, get) {
1546 DILocalScope *Scope = getSubprogram();
1547 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001548 unsigned Line = 5;
1549 unsigned Column = 8;
1550
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001551 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001552
1553 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1554 EXPECT_EQ(Scope, N->getScope());
1555 EXPECT_EQ(File, N->getFile());
1556 EXPECT_EQ(Line, N->getLine());
1557 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001558 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001559
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001560 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001561 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1562 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1563 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1564 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001565
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001566 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001567 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001568}
1569
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +00001570TEST_F(DILexicalBlockTest, Overflow) {
1571 DISubprogram *SP = getSubprogram();
1572 DIFile *F = getFile();
1573 {
1574 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1575 EXPECT_EQ(2u, LB->getLine());
1576 EXPECT_EQ(7u, LB->getColumn());
1577 }
1578 unsigned U16 = 1u << 16;
1579 {
1580 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1581 EXPECT_EQ(UINT32_MAX, LB->getLine());
1582 EXPECT_EQ(U16 - 1, LB->getColumn());
1583 }
1584 {
1585 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1586 EXPECT_EQ(UINT32_MAX, LB->getLine());
1587 EXPECT_EQ(0u, LB->getColumn());
1588 }
1589 {
1590 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1591 EXPECT_EQ(UINT32_MAX, LB->getLine());
1592 EXPECT_EQ(0u, LB->getColumn());
1593 }
1594}
1595
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001596typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001597
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001598TEST_F(DILexicalBlockFileTest, get) {
1599 DILocalScope *Scope = getSubprogram();
1600 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001601 unsigned Discriminator = 5;
1602
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001603 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001604
1605 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1606 EXPECT_EQ(Scope, N->getScope());
1607 EXPECT_EQ(File, N->getFile());
1608 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001609 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001610
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001611 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001612 Discriminator));
1613 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001614 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001615 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001616 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001617
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001618 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001619 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001620}
1621
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001622typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001623
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001624TEST_F(DINamespaceTest, get) {
1625 DIScope *Scope = getFile();
1626 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001627 StringRef Name = "namespace";
1628 unsigned Line = 5;
1629
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001630 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001631
1632 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1633 EXPECT_EQ(Scope, N->getScope());
1634 EXPECT_EQ(File, N->getFile());
1635 EXPECT_EQ(Name, N->getName());
1636 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001637 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001638
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001639 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1640 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1641 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1642 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001643
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001644 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001645 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001646}
1647
Adrian Prantlab1243f2015-06-29 23:03:47 +00001648typedef MetadataTest DIModuleTest;
1649
1650TEST_F(DIModuleTest, get) {
1651 DIScope *Scope = getFile();
1652 StringRef Name = "module";
1653 StringRef ConfigMacro = "-DNDEBUG";
1654 StringRef Includes = "-I.";
1655 StringRef Sysroot = "/";
1656
1657 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1658
1659 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1660 EXPECT_EQ(Scope, N->getScope());
1661 EXPECT_EQ(Name, N->getName());
1662 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1663 EXPECT_EQ(Includes, N->getIncludePath());
1664 EXPECT_EQ(Sysroot, N->getISysRoot());
1665 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1666 ConfigMacro, Includes, Sysroot));
1667 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1668 ConfigMacro, Includes, Sysroot));
1669 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1670 ConfigMacro, Includes, Sysroot));
1671 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1672 "other", Includes, Sysroot));
1673 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1674 ConfigMacro, "other", Sysroot));
1675 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1676 ConfigMacro, Includes, "other"));
1677
1678 TempDIModule Temp = N->clone();
1679 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1680}
1681
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001682typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001683
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001684TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001685 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001686 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001687
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001688 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001689
1690 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001691 EXPECT_EQ(Name, N->getName());
1692 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001693 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001694
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001695 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001696 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001697 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001698
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001699 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001700 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001701}
1702
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001703typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001704
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001705TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001706 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001707 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001708 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001709 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001710
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001711 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001712 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001713 EXPECT_EQ(Name, N->getName());
1714 EXPECT_EQ(Type, N->getType());
1715 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001716 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001717
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001718 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001719 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1720 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001721 EXPECT_NE(N,
1722 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1723 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001724 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001725 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001726 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001727
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001728 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001729 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001730}
1731
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001732typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001733
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001734TEST_F(DIGlobalVariableTest, get) {
1735 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001736 StringRef Name = "name";
1737 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001738 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001739 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001740 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001741 bool IsLocalToUnit = false;
1742 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001743 Constant *Variable = getConstant();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001744 DIDerivedType *StaticDataMemberDeclaration =
1745 cast<DIDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001746
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001747 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001748 Type, IsLocalToUnit, IsDefinition, Variable,
1749 StaticDataMemberDeclaration);
1750 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1751 EXPECT_EQ(Scope, N->getScope());
1752 EXPECT_EQ(Name, N->getName());
1753 EXPECT_EQ(LinkageName, N->getLinkageName());
1754 EXPECT_EQ(File, N->getFile());
1755 EXPECT_EQ(Line, N->getLine());
1756 EXPECT_EQ(Type, N->getType());
1757 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1758 EXPECT_EQ(IsDefinition, N->isDefinition());
1759 EXPECT_EQ(Variable, N->getVariable());
1760 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001761 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001762 Line, Type, IsLocalToUnit, IsDefinition,
1763 Variable, StaticDataMemberDeclaration));
1764
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001765 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001766 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001767 File, Line, Type, IsLocalToUnit, IsDefinition,
1768 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001769 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001770 Line, Type, IsLocalToUnit, IsDefinition,
1771 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001772 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001773 Type, IsLocalToUnit, IsDefinition,
1774 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001775 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001776 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001777 Line, Type, IsLocalToUnit, IsDefinition,
1778 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001779 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001780 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001781 Line + 1, Type, IsLocalToUnit, IsDefinition,
1782 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001783 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001784 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001785 getDerivedType(), IsLocalToUnit, IsDefinition,
1786 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001787 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001788 Line, Type, !IsLocalToUnit, IsDefinition,
1789 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001790 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001791 Line, Type, IsLocalToUnit, !IsDefinition,
1792 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001793 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001794 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001795 Type, IsLocalToUnit, IsDefinition,
1796 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001797 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001799 Type, IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001800 cast<DIDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001801
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001802 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001803 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001804}
1805
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001806typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001807
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001808TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001809 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001810 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001811 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001812 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001813 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001814 unsigned Arg = 6;
1815 unsigned Flags = 7;
Davide Italiano0d2ef012016-04-16 03:23:48 +00001816 unsigned NotFlags = (~Flags) & ((1 << 16) - 1);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001817
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001818 auto *N =
1819 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags);
1820 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001821 EXPECT_EQ(Scope, N->getScope());
1822 EXPECT_EQ(Name, N->getName());
1823 EXPECT_EQ(File, N->getFile());
1824 EXPECT_EQ(Line, N->getLine());
1825 EXPECT_EQ(Type, N->getType());
1826 EXPECT_EQ(Arg, N->getArg());
1827 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001828 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1829 Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001830
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001831 EXPECT_FALSE(
1832 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags)
1833 ->isParameter());
1834 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001835 Type, Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001836 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
1837 Arg, Flags));
1838 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
1839 Arg, Flags));
1840 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
1841 Arg, Flags));
1842 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001843 getDerivedType(), Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001844 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001845 Arg + 1, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001846 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
Davide Italiano0d2ef012016-04-16 03:23:48 +00001847 NotFlags));
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) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001854 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1855 0, nullptr, 255, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001856 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001857 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1858 0, nullptr, 256, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001859 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001860 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1861 0, nullptr, 257, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001862 ->getArg());
1863 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001864 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1865 0, nullptr, Max, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001866 ->getArg());
1867}
1868
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001869typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001870
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001871TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001872 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001873 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001874 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001875 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001876
1877 EXPECT_EQ(5u, N->getNumElements());
1878 EXPECT_EQ(2u, N->getElement(0));
1879 EXPECT_EQ(6u, N->getElement(1));
1880 EXPECT_EQ(9u, N->getElement(2));
1881 EXPECT_EQ(78u, N->getElement(3));
1882 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001883
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001884 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001885 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001886}
1887
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001888TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001889#define EXPECT_VALID(...) \
1890 do { \
1891 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001892 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001893 } while (false)
1894#define EXPECT_INVALID(...) \
1895 do { \
1896 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001897 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001898 } while (false)
1899
1900 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001901 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001902
1903 // Valid constructions.
1904 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1905 EXPECT_VALID(dwarf::DW_OP_deref);
1906 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1907 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1908 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1909 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1910 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1911
1912 // Invalid constructions.
1913 EXPECT_INVALID(~0u);
1914 EXPECT_INVALID(dwarf::DW_OP_plus);
1915 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1916 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1917 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1918 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1919
1920#undef EXPECT_VALID
1921#undef EXPECT_INVALID
1922}
1923
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001924typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001925
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001926TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001927 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001928 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001929 unsigned Line = 5;
1930 StringRef GetterName = "getter";
1931 StringRef SetterName = "setter";
1932 unsigned Attributes = 7;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001933 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001934
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001935 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001936 SetterName, Attributes, Type);
1937
1938 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1939 EXPECT_EQ(Name, N->getName());
1940 EXPECT_EQ(File, N->getFile());
1941 EXPECT_EQ(Line, N->getLine());
1942 EXPECT_EQ(GetterName, N->getGetterName());
1943 EXPECT_EQ(SetterName, N->getSetterName());
1944 EXPECT_EQ(Attributes, N->getAttributes());
1945 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001946 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001947 SetterName, Attributes, Type));
1948
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001949 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001950 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001951 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001952 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001953 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, 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, File, Line, "other",
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, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001958 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001959 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001960 SetterName, Attributes + 1, 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 Smith3ec5fa62015-04-06 19:03:45 +00001962 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001963 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001964
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001965 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001966 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001967}
1968
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001969typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001970
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001971TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001972 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001973 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001974 DINode *Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001975 unsigned Line = 5;
1976 StringRef Name = "name";
1977
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001978 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001979
1980 EXPECT_EQ(Tag, N->getTag());
1981 EXPECT_EQ(Scope, N->getScope());
1982 EXPECT_EQ(Entity, N->getEntity());
1983 EXPECT_EQ(Line, N->getLine());
1984 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001985 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001986
1987 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001988 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001989 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001990 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001991 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001992 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001993 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001994 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001995 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001996 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001997 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001998
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001999 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002000 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002001}
2002
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002003typedef MetadataTest MetadataAsValueTest;
2004
2005TEST_F(MetadataAsValueTest, MDNode) {
2006 MDNode *N = MDNode::get(Context, None);
2007 auto *V = MetadataAsValue::get(Context, N);
2008 EXPECT_TRUE(V->getType()->isMetadataTy());
2009 EXPECT_EQ(N, V->getMetadata());
2010
2011 auto *V2 = MetadataAsValue::get(Context, N);
2012 EXPECT_EQ(V, V2);
2013}
2014
2015TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2016 MDNode *N = MDNode::get(Context, None);
2017 Metadata *Ops[] = {N};
2018 MDNode *N2 = MDNode::get(Context, Ops);
2019 auto *V = MetadataAsValue::get(Context, N2);
2020 EXPECT_TRUE(V->getType()->isMetadataTy());
2021 EXPECT_EQ(N2, V->getMetadata());
2022
2023 auto *V2 = MetadataAsValue::get(Context, N2);
2024 EXPECT_EQ(V, V2);
2025
2026 auto *V3 = MetadataAsValue::get(Context, N);
2027 EXPECT_TRUE(V3->getType()->isMetadataTy());
2028 EXPECT_NE(V, V3);
2029 EXPECT_EQ(N, V3->getMetadata());
2030}
2031
2032TEST_F(MetadataAsValueTest, MDNodeConstant) {
2033 auto *C = ConstantInt::getTrue(Context);
2034 auto *MD = ConstantAsMetadata::get(C);
2035 Metadata *Ops[] = {MD};
2036 auto *N = MDNode::get(Context, Ops);
2037
2038 auto *V = MetadataAsValue::get(Context, MD);
2039 EXPECT_TRUE(V->getType()->isMetadataTy());
2040 EXPECT_EQ(MD, V->getMetadata());
2041
2042 auto *V2 = MetadataAsValue::get(Context, N);
2043 EXPECT_EQ(MD, V2->getMetadata());
2044 EXPECT_EQ(V, V2);
2045}
2046
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002047typedef MetadataTest ValueAsMetadataTest;
2048
2049TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2050 Type *Ty = Type::getInt1PtrTy(Context);
2051 std::unique_ptr<GlobalVariable> GV0(
2052 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2053 auto *MD = ValueAsMetadata::get(GV0.get());
2054 EXPECT_TRUE(MD->getValue() == GV0.get());
2055 ASSERT_TRUE(GV0->use_empty());
2056
2057 std::unique_ptr<GlobalVariable> GV1(
2058 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2059 GV0->replaceAllUsesWith(GV1.get());
2060 EXPECT_TRUE(MD->getValue() == GV1.get());
2061}
2062
Adrian Prantlcbec1602016-02-08 17:02:34 +00002063TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2064 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002065 ConstantAsMetadata *CI =
2066 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Adrian Prantlcbec1602016-02-08 17:02:34 +00002067
Adrian Prantlcbec1602016-02-08 17:02:34 +00002068 auto Temp1 = MDTuple::getTemporary(Context, None);
Adrian Prantl817c47b2016-02-08 19:13:15 +00002069 auto Temp2 = MDTuple::getTemporary(Context, {CI});
2070 auto *N = MDTuple::get(Context, {Temp1.get()});
Adrian Prantlcbec1602016-02-08 17:02:34 +00002071
2072 // Test replacing a temporary node with another temporary node.
2073 Temp1->replaceAllUsesWith(Temp2.get());
2074 EXPECT_EQ(N->getOperand(0), Temp2.get());
2075
2076 // Clean up Temp2 for teardown.
2077 Temp2->replaceAllUsesWith(nullptr);
2078}
2079
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002080TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2081 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002082 ConstantAsMetadata *CI =
2083 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002084
2085 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002086 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002087
2088 // 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 +00002089 Metadata *Ops1[] = {CI, CI, Temp.get()};
2090 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002091
2092 auto *N1 = MDTuple::get(Context, Ops1);
2093 auto *N2 = MDTuple::get(Context, Ops2);
2094 ASSERT_NE(N1, N2);
2095
2096 // Tell metadata that the constant is getting deleted.
2097 //
2098 // After this, N1 will be invalid, so don't touch it.
2099 ValueAsMetadata::handleDeletion(CI->getValue());
2100 EXPECT_EQ(nullptr, N2->getOperand(0));
2101 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002102 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002103
2104 // Clean up Temp for teardown.
2105 Temp->replaceAllUsesWith(nullptr);
2106}
2107
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002108typedef MetadataTest TrackingMDRefTest;
2109
2110TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2111 Type *Ty = Type::getInt1PtrTy(Context);
2112 std::unique_ptr<GlobalVariable> GV0(
2113 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2114 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2115 EXPECT_TRUE(MD->getValue() == GV0.get());
2116 ASSERT_TRUE(GV0->use_empty());
2117
2118 std::unique_ptr<GlobalVariable> GV1(
2119 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2120 GV0->replaceAllUsesWith(GV1.get());
2121 EXPECT_TRUE(MD->getValue() == GV1.get());
2122
2123 // Reset it, so we don't inadvertently test deletion.
2124 MD.reset();
2125}
2126
2127TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2128 Type *Ty = Type::getInt1PtrTy(Context);
2129 std::unique_ptr<GlobalVariable> GV(
2130 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2131 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2132 EXPECT_TRUE(MD->getValue() == GV.get());
2133 ASSERT_TRUE(GV->use_empty());
2134
2135 GV.reset();
2136 EXPECT_TRUE(!MD);
2137}
2138
Devang Patel0924b332009-07-30 00:03:41 +00002139TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002140 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002141 ConstantAsMetadata *C =
2142 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2143 ConstantAsMetadata *C2 =
2144 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002145
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002146 Metadata *const V = C;
2147 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002148 MDNode *n = MDNode::get(Context, V);
2149 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002150
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002151 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002152 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002153 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2154 NMD->addOperand(n);
2155 NMD->addOperand(n2);
2156
Chris Lattnerbe354a62009-08-23 04:47:35 +00002157 std::string Str;
2158 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002159 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002160 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002161 oss.str().c_str());
2162}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002163
2164typedef MetadataTest FunctionAttachmentTest;
2165TEST_F(FunctionAttachmentTest, setMetadata) {
2166 Function *F = getFunction("foo");
2167 ASSERT_FALSE(F->hasMetadata());
2168 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2169 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2170 EXPECT_EQ(nullptr, F->getMetadata("other"));
2171
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002172 DISubprogram *SP1 = getSubprogram();
2173 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002174 ASSERT_NE(SP1, SP2);
2175
2176 F->setMetadata("dbg", SP1);
2177 EXPECT_TRUE(F->hasMetadata());
2178 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2179 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2180 EXPECT_EQ(nullptr, F->getMetadata("other"));
2181
2182 F->setMetadata(LLVMContext::MD_dbg, SP2);
2183 EXPECT_TRUE(F->hasMetadata());
2184 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2185 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2186 EXPECT_EQ(nullptr, F->getMetadata("other"));
2187
2188 F->setMetadata("dbg", nullptr);
2189 EXPECT_FALSE(F->hasMetadata());
2190 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2191 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2192 EXPECT_EQ(nullptr, F->getMetadata("other"));
2193
2194 MDTuple *T1 = getTuple();
2195 MDTuple *T2 = getTuple();
2196 ASSERT_NE(T1, T2);
2197
2198 F->setMetadata("other1", T1);
2199 F->setMetadata("other2", T2);
2200 EXPECT_TRUE(F->hasMetadata());
2201 EXPECT_EQ(T1, F->getMetadata("other1"));
2202 EXPECT_EQ(T2, F->getMetadata("other2"));
2203 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2204
2205 F->setMetadata("other1", T2);
2206 F->setMetadata("other2", T1);
2207 EXPECT_EQ(T2, F->getMetadata("other1"));
2208 EXPECT_EQ(T1, F->getMetadata("other2"));
2209
2210 F->setMetadata("other1", nullptr);
2211 F->setMetadata("other2", nullptr);
2212 EXPECT_FALSE(F->hasMetadata());
2213 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2214 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2215}
2216
2217TEST_F(FunctionAttachmentTest, getAll) {
2218 Function *F = getFunction("foo");
2219
2220 MDTuple *T1 = getTuple();
2221 MDTuple *T2 = getTuple();
2222 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002223 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002224
2225 F->setMetadata("other1", T2);
2226 F->setMetadata(LLVMContext::MD_dbg, SP);
2227 F->setMetadata("other2", T1);
2228 F->setMetadata(LLVMContext::MD_prof, P);
2229 F->setMetadata("other2", T2);
2230 F->setMetadata("other1", T1);
2231
2232 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2233 F->getAllMetadata(MDs);
2234 ASSERT_EQ(4u, MDs.size());
2235 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2236 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2237 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2238 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2239 EXPECT_EQ(SP, MDs[0].second);
2240 EXPECT_EQ(P, MDs[1].second);
2241 EXPECT_EQ(T1, MDs[2].second);
2242 EXPECT_EQ(T2, MDs[3].second);
2243}
2244
2245TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2246 Function *F = getFunction("foo");
2247
2248 MDTuple *T1 = getTuple();
2249 MDTuple *T2 = getTuple();
2250 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002251 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002252
2253 F->setMetadata("other1", T1);
2254 F->setMetadata(LLVMContext::MD_dbg, SP);
2255 F->setMetadata("other2", T2);
2256 F->setMetadata(LLVMContext::MD_prof, P);
2257
2258 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2259 F->dropUnknownMetadata(Known);
2260
2261 EXPECT_EQ(T2, F->getMetadata("other2"));
2262 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2263 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2264 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2265
2266 F->setMetadata("other2", nullptr);
2267 F->setMetadata(LLVMContext::MD_prof, nullptr);
2268 EXPECT_FALSE(F->hasMetadata());
2269}
2270
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002271TEST_F(FunctionAttachmentTest, Verifier) {
2272 Function *F = getFunction("foo");
2273 F->setMetadata("attach", getTuple());
2274
2275 // Confirm this has no body.
2276 ASSERT_TRUE(F->empty());
2277
2278 // Functions without a body cannot have metadata attachments (they also can't
2279 // be verified directly, so check that the module fails to verify).
2280 EXPECT_TRUE(verifyModule(*F->getParent()));
2281
2282 // Functions with a body can.
2283 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2284 EXPECT_FALSE(verifyModule(*F->getParent()));
2285 EXPECT_FALSE(verifyFunction(*F));
2286}
2287
Diego Novillo2567f3d2015-05-13 15:13:45 +00002288TEST_F(FunctionAttachmentTest, EntryCount) {
2289 Function *F = getFunction("foo");
2290 EXPECT_FALSE(F->getEntryCount().hasValue());
2291 F->setEntryCount(12304);
2292 EXPECT_TRUE(F->getEntryCount().hasValue());
2293 EXPECT_EQ(12304u, *F->getEntryCount());
2294}
2295
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002296TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2297 Function *F = getFunction("foo");
2298 DISubprogram *SP = getSubprogram();
2299 F->setSubprogram(SP);
2300
2301 // Note that the static_cast confirms that F->getSubprogram() actually
2302 // returns an DISubprogram.
2303 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2304 EXPECT_EQ(SP, F->getMetadata("dbg"));
2305 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2306}
2307
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002308typedef MetadataTest DistinctMDOperandPlaceholderTest;
2309TEST_F(DistinctMDOperandPlaceholderTest, getID) {
2310 EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
2311}
2312
2313TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
2314 // Set up some placeholders.
2315 DistinctMDOperandPlaceholder PH0(7);
2316 DistinctMDOperandPlaceholder PH1(3);
2317 DistinctMDOperandPlaceholder PH2(0);
2318 Metadata *Ops[] = {&PH0, &PH1, &PH2};
2319 auto *D = MDTuple::getDistinct(Context, Ops);
2320 ASSERT_EQ(&PH0, D->getOperand(0));
2321 ASSERT_EQ(&PH1, D->getOperand(1));
2322 ASSERT_EQ(&PH2, D->getOperand(2));
2323
2324 // Replace them.
2325 auto *N0 = MDTuple::get(Context, None);
2326 auto *N1 = MDTuple::get(Context, N0);
2327 PH0.replaceUseWith(N0);
2328 PH1.replaceUseWith(N1);
2329 PH2.replaceUseWith(nullptr);
2330 EXPECT_EQ(N0, D->getOperand(0));
2331 EXPECT_EQ(N1, D->getOperand(1));
2332 EXPECT_EQ(nullptr, D->getOperand(2));
2333}
2334
2335TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
2336 // There is no user, but we can still call replace.
2337 DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None));
2338}
2339
Duncan P. N. Exon Smith004eb552016-04-23 04:34:11 +00002340#ifndef NDEBUG
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002341#ifdef GTEST_HAS_DEATH_TEST
2342TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
2343 // This shouldn't crash.
2344 DistinctMDOperandPlaceholder PH(7);
2345 EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
2346 "Unexpected callback to owner");
2347}
2348
2349TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
2350 // This shouldn't crash.
2351 DistinctMDOperandPlaceholder PH(7);
2352 EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
2353}
2354
2355TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
2356 // This shouldn't crash.
2357 DistinctMDOperandPlaceholder PH(7);
2358 MDTuple::getDistinct(Context, &PH);
2359 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2360 "Placeholders can only be used once");
2361}
2362
2363TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
2364 // TrackingMDRef doesn't install an owner callback, so it can't be detected
2365 // as an invalid use. However, using a placeholder in a TrackingMDRef *and*
2366 // a distinct node isn't possible and we should assert.
2367 //
2368 // (There's no positive test for using TrackingMDRef because it's not a
2369 // useful thing to do.)
2370 {
2371 DistinctMDOperandPlaceholder PH(7);
2372 MDTuple::getDistinct(Context, &PH);
2373 EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
2374 }
2375 {
2376 DistinctMDOperandPlaceholder PH(7);
2377 TrackingMDRef Ref(&PH);
2378 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2379 "Placeholders can only be used once");
2380 }
2381}
2382#endif
Duncan P. N. Exon Smith004eb552016-04-23 04:34:11 +00002383#endif
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002384
Duncan P. N. Exon Smith2923a432016-04-23 04:02:39 +00002385} // end namespace