blob: 8f346f53a2d22b72790d74726e071c6473a68484 [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,
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000087 nullptr, false, false, 0, nullptr, 0, 0, 0,
88 0);
89 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000090 DIScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
91 DIFile *getFile() {
92 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000093 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000094 DITypeRef getBasicType(StringRef Name) {
95 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +000096 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000097 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000098 DITypeRef getDerivedType() {
99 return DIDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000100 nullptr, 0, nullptr,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000101 getBasicType("basictype"), 1, 2, 0, 0)
102 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000103 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000104 Constant *getConstant() {
105 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
106 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000107 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000108 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000109 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000110 DITypeRef getCompositeType() {
111 return DICompositeType::getDistinct(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000112 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
113 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
114 ->getRef();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000115 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000116 Function *getFunction(StringRef Name) {
117 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000118 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
119 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000120};
121typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000122
Nick Lewycky49f89192009-04-04 07:22:01 +0000123// Test that construction of MDString with different value produces different
124// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000125TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000126 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000127 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000128 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000129 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000130 EXPECT_NE(s1, s2);
131}
132
133// Test that creation of MDStrings with the same string contents produces the
134// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000135TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000136 char x[4] = { 'a', 'b', 'c', 'X' };
137 char y[4] = { 'a', 'b', 'c', 'Y' };
138
Owen Anderson23587322009-07-31 21:38:10 +0000139 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
140 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000141 EXPECT_EQ(s1, s2);
142}
143
144// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000145TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000146 char *str = new char[13];
147 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000148 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000149 strncpy(str, "aaaaaaaaaaaaa", 13);
150 delete[] str;
151
Chris Lattnerbe354a62009-08-23 04:47:35 +0000152 std::string Str;
153 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000154 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000155 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000156}
157
158// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000159TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000160 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000161 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000162 std::string Str;
163 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000164 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000165 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000166}
167
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000168typedef MetadataTest MDNodeTest;
169
Nick Lewycky49f89192009-04-04 07:22:01 +0000170// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000171TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000172 char x[3] = { 'a', 'b', 'c' };
173 char y[3] = { '1', '2', '3' };
174
Owen Anderson23587322009-07-31 21:38:10 +0000175 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
176 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000177 ConstantAsMetadata *CI = ConstantAsMetadata::get(
178 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000179
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000180 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000181 V.push_back(s1);
182 V.push_back(CI);
183 V.push_back(s2);
184
Jay Foad5514afe2011-04-21 19:59:31 +0000185 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000186 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000187 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000188 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000189 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000190 MDNode *n4 = MDNode::getIfExists(Context, V);
191 MDNode *n5 = MDNode::getIfExists(Context, c1);
192 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000193 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000194 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000195 EXPECT_EQ(n4, n1);
196 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000197 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000198
Chris Lattner9b493022009-12-31 01:22:29 +0000199 EXPECT_EQ(3u, n1->getNumOperands());
200 EXPECT_EQ(s1, n1->getOperand(0));
201 EXPECT_EQ(CI, n1->getOperand(1));
202 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000203
Chris Lattner9b493022009-12-31 01:22:29 +0000204 EXPECT_EQ(1u, n2->getNumOperands());
205 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000206}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000207
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000208TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000209 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
210 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000211
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000212 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000213 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000214 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000215
216 EXPECT_EQ(n, wvh);
217
218 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000219}
Devang Patel0924b332009-07-30 00:03:41 +0000220
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000221TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000222 // !0 = !{!0}
223 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000224 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000225 auto Temp = MDNode::getTemporary(Context, None);
226 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000227 MDNode *Self = MDNode::get(Context, Args);
228 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000229 ASSERT_EQ(Self, Self->getOperand(0));
230
231 // Self-references should be distinct, so MDNode::get() should grab a
232 // uniqued node that references Self, not Self.
233 Args[0] = Self;
234 MDNode *Ref1 = MDNode::get(Context, Args);
235 MDNode *Ref2 = MDNode::get(Context, Args);
236 EXPECT_NE(Self, Ref1);
237 EXPECT_EQ(Ref1, Ref2);
238 }
239
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000240 // !0 = !{!0, !{}}
241 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000242 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000243 auto Temp = MDNode::getTemporary(Context, None);
244 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000245 MDNode *Self = MDNode::get(Context, Args);
246 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000247 ASSERT_EQ(Self, Self->getOperand(0));
248
249 // Self-references should be distinct, so MDNode::get() should grab a
250 // uniqued node that references Self, not Self itself.
251 Args[0] = Self;
252 MDNode *Ref1 = MDNode::get(Context, Args);
253 MDNode *Ref2 = MDNode::get(Context, Args);
254 EXPECT_NE(Self, Ref1);
255 EXPECT_EQ(Ref1, Ref2);
256 }
257}
258
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000259TEST_F(MDNodeTest, Print) {
260 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
261 MDString *S = MDString::get(Context, "foo");
262 MDNode *N0 = getNode();
263 MDNode *N1 = getNode(N0);
264 MDNode *N2 = getNode(N0, N1);
265
266 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
267 MDNode *N = MDNode::get(Context, Args);
268
269 std::string Expected;
270 {
271 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000272 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000273 C->printAsOperand(OS);
274 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000275 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000276 OS << ", null";
277 MDNode *Nodes[] = {N0, N1, N2};
278 for (auto *Node : Nodes)
279 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000280 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000281 }
282
283 std::string Actual;
284 {
285 raw_string_ostream OS(Actual);
286 N->print(OS);
287 }
288
289 EXPECT_EQ(Expected, Actual);
290}
291
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000292#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
293 do { \
294 std::string Actual_; \
295 raw_string_ostream OS(Actual_); \
296 PRINT; \
297 OS.flush(); \
298 std::string Expected_(EXPECTED); \
299 EXPECT_EQ(Expected_, Actual_); \
300 } while (false)
301
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000302TEST_F(MDNodeTest, PrintTemporary) {
303 MDNode *Arg = getNode();
304 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
305 MDNode *N = getNode(Temp.get());
306 Module M("test", Context);
307 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
308 NMD->addOperand(N);
309
310 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
311 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
312 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
313
314 // Cleanup.
315 Temp->replaceAllUsesWith(Arg);
316}
317
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000318TEST_F(MDNodeTest, PrintFromModule) {
319 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
320 MDString *S = MDString::get(Context, "foo");
321 MDNode *N0 = getNode();
322 MDNode *N1 = getNode(N0);
323 MDNode *N2 = getNode(N0, N1);
324
325 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
326 MDNode *N = MDNode::get(Context, Args);
327 Module M("test", Context);
328 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
329 NMD->addOperand(N);
330
331 std::string Expected;
332 {
333 raw_string_ostream OS(Expected);
334 OS << "!0 = !{";
335 C->printAsOperand(OS);
336 OS << ", ";
337 S->printAsOperand(OS);
338 OS << ", null, !1, !2, !3}";
339 }
340
341 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
342}
343
344TEST_F(MDNodeTest, PrintFromFunction) {
345 Module M("test", Context);
346 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
347 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
348 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
349 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
350 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
351 auto *R0 = ReturnInst::Create(Context, BB0);
352 auto *R1 = ReturnInst::Create(Context, BB1);
353 auto *N0 = MDNode::getDistinct(Context, None);
354 auto *N1 = MDNode::getDistinct(Context, None);
355 R0->setMetadata("md", N0);
356 R1->setMetadata("md", N1);
357
358 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
359 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000360
361 ModuleSlotTracker MST(&M);
362 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
363 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000364}
365
366TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
367 Module M("test", Context);
368
369 auto *Intrinsic =
370 Function::Create(FunctionType::get(Type::getVoidTy(Context),
371 Type::getMetadataTy(Context), false),
372 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
373
374 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
375 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
376 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
377 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
378 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
379 auto *N0 = MDNode::getDistinct(Context, None);
380 auto *N1 = MDNode::getDistinct(Context, None);
381 auto *MAV0 = MetadataAsValue::get(Context, N0);
382 auto *MAV1 = MetadataAsValue::get(Context, N1);
383 CallInst::Create(Intrinsic, MAV0, "", BB0);
384 CallInst::Create(Intrinsic, MAV1, "", BB1);
385
386 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
387 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
388 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
389 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
390 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
391 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000392
393 ModuleSlotTracker MST(&M);
394 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
395 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
396 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
397 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
398 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
399 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000400}
401#undef EXPECT_PRINTER_EQ
402
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000403TEST_F(MDNodeTest, NullOperand) {
404 // metadata !{}
405 MDNode *Empty = MDNode::get(Context, None);
406
407 // metadata !{metadata !{}}
408 Metadata *Ops[] = {Empty};
409 MDNode *N = MDNode::get(Context, Ops);
410 ASSERT_EQ(Empty, N->getOperand(0));
411
412 // metadata !{metadata !{}} => metadata !{null}
413 N->replaceOperandWith(0, nullptr);
414 ASSERT_EQ(nullptr, N->getOperand(0));
415
416 // metadata !{null}
417 Ops[0] = nullptr;
418 MDNode *NullOp = MDNode::get(Context, Ops);
419 ASSERT_EQ(nullptr, NullOp->getOperand(0));
420 EXPECT_EQ(N, NullOp);
421}
422
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000423TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
424 // !{}
425 MDNode *Empty = MDNode::get(Context, None);
426 ASSERT_TRUE(Empty->isResolved());
427 EXPECT_FALSE(Empty->isDistinct());
428
429 // !{!{}}
430 Metadata *Wrapped1Ops[] = {Empty};
431 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
432 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
433 ASSERT_TRUE(Wrapped1->isResolved());
434 EXPECT_FALSE(Wrapped1->isDistinct());
435
436 // !{!{!{}}}
437 Metadata *Wrapped2Ops[] = {Wrapped1};
438 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
439 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
440 ASSERT_TRUE(Wrapped2->isResolved());
441 EXPECT_FALSE(Wrapped2->isDistinct());
442
443 // !{!{!{}}} => !{!{}}
444 Wrapped2->replaceOperandWith(0, Empty);
445 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
446 EXPECT_TRUE(Wrapped2->isDistinct());
447 EXPECT_FALSE(Wrapped1->isDistinct());
448}
449
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000450TEST_F(MDNodeTest, getDistinct) {
451 // !{}
452 MDNode *Empty = MDNode::get(Context, None);
453 ASSERT_TRUE(Empty->isResolved());
454 ASSERT_FALSE(Empty->isDistinct());
455 ASSERT_EQ(Empty, MDNode::get(Context, None));
456
457 // distinct !{}
458 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
459 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
460 EXPECT_TRUE(Distinct1->isResolved());
461 EXPECT_TRUE(Distinct2->isDistinct());
462 EXPECT_NE(Empty, Distinct1);
463 EXPECT_NE(Empty, Distinct2);
464 EXPECT_NE(Distinct1, Distinct2);
465
466 // !{}
467 ASSERT_EQ(Empty, MDNode::get(Context, None));
468}
469
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000470TEST_F(MDNodeTest, isUniqued) {
471 MDNode *U = MDTuple::get(Context, None);
472 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000473 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000474 EXPECT_TRUE(U->isUniqued());
475 EXPECT_FALSE(D->isUniqued());
476 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000477}
478
479TEST_F(MDNodeTest, isDistinct) {
480 MDNode *U = MDTuple::get(Context, None);
481 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000482 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000483 EXPECT_FALSE(U->isDistinct());
484 EXPECT_TRUE(D->isDistinct());
485 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000486}
487
488TEST_F(MDNodeTest, isTemporary) {
489 MDNode *U = MDTuple::get(Context, None);
490 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000491 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000492 EXPECT_FALSE(U->isTemporary());
493 EXPECT_FALSE(D->isTemporary());
494 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000495}
496
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000497TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
498 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000499 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000500 ASSERT_FALSE(Temp->isResolved());
501
502 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000503 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000504 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
505 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000506 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000507
508 // temporary !{} => !{}
509 MDNode *Empty = MDNode::get(Context, None);
510 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000511 EXPECT_EQ(Empty, Distinct->getOperand(0));
512}
513
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000514TEST_F(MDNodeTest, handleChangedOperandRecursion) {
515 // !0 = !{}
516 MDNode *N0 = MDNode::get(Context, None);
517
518 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000519 auto Temp3 = MDTuple::getTemporary(Context, None);
520 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000521 MDNode *N1 = MDNode::get(Context, Ops1);
522
523 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000524 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000525 MDNode *N2 = MDNode::get(Context, Ops2);
526
527 // !3 = !{!2}
528 Metadata *Ops3[] = {N2};
529 MDNode *N3 = MDNode::get(Context, Ops3);
530 Temp3->replaceAllUsesWith(N3);
531
532 // !4 = !{!1}
533 Metadata *Ops4[] = {N1};
534 MDNode *N4 = MDNode::get(Context, Ops4);
535
536 // Confirm that the cycle prevented RAUW from getting dropped.
537 EXPECT_TRUE(N0->isResolved());
538 EXPECT_FALSE(N1->isResolved());
539 EXPECT_FALSE(N2->isResolved());
540 EXPECT_FALSE(N3->isResolved());
541 EXPECT_FALSE(N4->isResolved());
542
543 // Create a couple of distinct nodes to observe what's going on.
544 //
545 // !5 = distinct !{!2}
546 // !6 = distinct !{!3}
547 Metadata *Ops5[] = {N2};
548 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
549 Metadata *Ops6[] = {N3};
550 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
551
552 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
553 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
554 // references !3, this can cause a re-entry of handleChangedOperand() when !3
555 // is not ready for it.
556 //
557 // !2->replaceOperandWith(1, nullptr)
558 // !2: !{!3, !0} => !{!3, null}
559 // !2->replaceAllUsesWith(!1)
560 // !3: !{!2] => !{!1}
561 // !3->replaceAllUsesWith(!4)
562 N2->replaceOperandWith(1, nullptr);
563
564 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
565 // under us. Just check that the other nodes are sane.
566 //
567 // !1 = !{!4, null}
568 // !4 = !{!1}
569 // !5 = distinct !{!1}
570 // !6 = distinct !{!4}
571 EXPECT_EQ(N4, N1->getOperand(0));
572 EXPECT_EQ(N1, N4->getOperand(0));
573 EXPECT_EQ(N1, N5->getOperand(0));
574 EXPECT_EQ(N4, N6->getOperand(0));
575}
576
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000577TEST_F(MDNodeTest, replaceResolvedOperand) {
578 // Check code for replacing one resolved operand with another. If doing this
579 // directly (via replaceOperandWith()) becomes illegal, change the operand to
580 // a global value that gets RAUW'ed.
581 //
582 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000583 auto Temp = MDTuple::getTemporary(Context, None);
584 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000585
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000586 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000587 MDNode *N = MDTuple::get(Context, Ops);
588 EXPECT_EQ(nullptr, N->getOperand(0));
589 ASSERT_FALSE(N->isResolved());
590
591 // Check code for replacing resolved nodes.
592 N->replaceOperandWith(0, Empty);
593 EXPECT_EQ(Empty, N->getOperand(0));
594
595 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000596 N->replaceOperandWith(0, Temp.get());
597 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000598
599 // Remove the references to Temp; required for teardown.
600 Temp->replaceAllUsesWith(nullptr);
601}
602
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000603TEST_F(MDNodeTest, replaceWithUniqued) {
604 auto *Empty = MDTuple::get(Context, None);
605 MDTuple *FirstUniqued;
606 {
607 Metadata *Ops[] = {Empty};
608 auto Temp = MDTuple::getTemporary(Context, Ops);
609 EXPECT_TRUE(Temp->isTemporary());
610
611 // Don't expect a collision.
612 auto *Current = Temp.get();
613 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
614 EXPECT_TRUE(FirstUniqued->isUniqued());
615 EXPECT_TRUE(FirstUniqued->isResolved());
616 EXPECT_EQ(Current, FirstUniqued);
617 }
618 {
619 Metadata *Ops[] = {Empty};
620 auto Temp = MDTuple::getTemporary(Context, Ops);
621 EXPECT_TRUE(Temp->isTemporary());
622
623 // Should collide with Uniqued above this time.
624 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
625 EXPECT_TRUE(Uniqued->isUniqued());
626 EXPECT_TRUE(Uniqued->isResolved());
627 EXPECT_EQ(FirstUniqued, Uniqued);
628 }
629 {
630 auto Unresolved = MDTuple::getTemporary(Context, None);
631 Metadata *Ops[] = {Unresolved.get()};
632 auto Temp = MDTuple::getTemporary(Context, Ops);
633 EXPECT_TRUE(Temp->isTemporary());
634
635 // Shouldn't be resolved.
636 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
637 EXPECT_TRUE(Uniqued->isUniqued());
638 EXPECT_FALSE(Uniqued->isResolved());
639
640 // Should be a different node.
641 EXPECT_NE(FirstUniqued, Uniqued);
642
643 // Should resolve when we update its node (note: be careful to avoid a
644 // collision with any other nodes above).
645 Uniqued->replaceOperandWith(0, nullptr);
646 EXPECT_TRUE(Uniqued->isResolved());
647 }
648}
649
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000650TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000651 // temp !{}
652 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
653 EXPECT_FALSE(Op->isResolved());
654
655 // temp !{temp !{}}
656 Metadata *Ops[] = {Op};
657 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
658 EXPECT_FALSE(N->isResolved());
659
660 // temp !{temp !{}} => !{temp !{}}
661 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
662 EXPECT_FALSE(N->isResolved());
663
664 // !{temp !{}} => !{!{}}
665 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
666 EXPECT_TRUE(Op->isResolved());
667 EXPECT_TRUE(N->isResolved());
668}
669
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000670TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000671 // i1* @GV
672 Type *Ty = Type::getInt1PtrTy(Context);
673 std::unique_ptr<GlobalVariable> GV(
674 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
675 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
676
677 // temp !{i1* @GV}
678 Metadata *Ops[] = {Op};
679 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
680
681 // temp !{i1* @GV} => !{i1* @GV}
682 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
683 ASSERT_TRUE(N->isUniqued());
684
685 // !{i1* @GV} => !{null}
686 GV.reset();
687 ASSERT_TRUE(N->isUniqued());
688 Metadata *NullOps[] = {nullptr};
689 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
690}
691
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000692TEST_F(MDNodeTest, replaceWithDistinct) {
693 {
694 auto *Empty = MDTuple::get(Context, None);
695 Metadata *Ops[] = {Empty};
696 auto Temp = MDTuple::getTemporary(Context, Ops);
697 EXPECT_TRUE(Temp->isTemporary());
698
699 // Don't expect a collision.
700 auto *Current = Temp.get();
701 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
702 EXPECT_TRUE(Distinct->isDistinct());
703 EXPECT_TRUE(Distinct->isResolved());
704 EXPECT_EQ(Current, Distinct);
705 }
706 {
707 auto Unresolved = MDTuple::getTemporary(Context, None);
708 Metadata *Ops[] = {Unresolved.get()};
709 auto Temp = MDTuple::getTemporary(Context, Ops);
710 EXPECT_TRUE(Temp->isTemporary());
711
712 // Don't expect a collision.
713 auto *Current = Temp.get();
714 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
715 EXPECT_TRUE(Distinct->isDistinct());
716 EXPECT_TRUE(Distinct->isResolved());
717 EXPECT_EQ(Current, Distinct);
718
719 // Cleanup; required for teardown.
720 Unresolved->replaceAllUsesWith(nullptr);
721 }
722}
723
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000724TEST_F(MDNodeTest, replaceWithPermanent) {
725 Metadata *Ops[] = {nullptr};
726 auto Temp = MDTuple::getTemporary(Context, Ops);
727 auto *T = Temp.get();
728
729 // U is a normal, uniqued node that references T.
730 auto *U = MDTuple::get(Context, T);
731 EXPECT_TRUE(U->isUniqued());
732
733 // Make Temp self-referencing.
734 Temp->replaceOperandWith(0, T);
735
736 // Try to uniquify Temp. This should, despite the name in the API, give a
737 // 'distinct' node, since self-references aren't allowed to be uniqued.
738 //
739 // Since it's distinct, N should have the same address as when it was a
740 // temporary (i.e., be equal to T not U).
741 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
742 EXPECT_EQ(N, T);
743 EXPECT_TRUE(N->isDistinct());
744
745 // U should be the canonical unique node with N as the argument.
746 EXPECT_EQ(U, MDTuple::get(Context, N));
747 EXPECT_TRUE(U->isUniqued());
748
749 // This temporary should collide with U when replaced, but it should still be
750 // uniqued.
751 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
752 EXPECT_TRUE(U->isUniqued());
753
754 // This temporary should become a new uniqued node.
755 auto Temp2 = MDTuple::getTemporary(Context, U);
756 auto *V = Temp2.get();
757 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
758 EXPECT_TRUE(V->isUniqued());
759 EXPECT_EQ(U, V->getOperand(0));
760}
761
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000762TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
763 TrackingMDRef Ref;
764 EXPECT_EQ(nullptr, Ref.get());
765 {
766 auto Temp = MDTuple::getTemporary(Context, None);
767 Ref.reset(Temp.get());
768 EXPECT_EQ(Temp.get(), Ref.get());
769 }
770 EXPECT_EQ(nullptr, Ref.get());
771}
772
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000773typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000774
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000775TEST_F(DILocationTest, Overflow) {
776 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000777 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000778 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000779 EXPECT_EQ(2u, L->getLine());
780 EXPECT_EQ(7u, L->getColumn());
781 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000782 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000783 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000784 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000785 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000786 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000787 }
788 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000789 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000790 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000791 EXPECT_EQ(0u, L->getColumn());
792 }
793 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000794 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000795 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000796 EXPECT_EQ(0u, L->getColumn());
797 }
798}
799
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000800TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000801 MDNode *N = getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000802 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000803 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000804 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000805 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000806 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000807}
808
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000809TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000810 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000811 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000812 EXPECT_TRUE(L->isTemporary());
813 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000814}
815
Teresa Johnsond98152b62015-12-07 15:05:44 +0000816TEST_F(DILocationTest, cloneTemporary) {
817 MDNode *N = MDNode::get(Context, None);
818 auto L = DILocation::getTemporary(Context, 2, 7, N);
819 EXPECT_TRUE(L->isTemporary());
820 auto L2 = L->clone();
821 EXPECT_TRUE(L2->isTemporary());
822}
823
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000824typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000825
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000826TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000827 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000828 auto *Empty = MDNode::get(Context, None);
829 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000830 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000831 EXPECT_EQ(15u, N->getTag());
832 EXPECT_EQ(2u, N->getNumOperands());
833 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000834 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000835 EXPECT_EQ(1u, N->getNumDwarfOperands());
836 EXPECT_EQ(Empty, N->getDwarfOperand(0));
837 EXPECT_EQ(Empty, N->getOperand(1));
838 ASSERT_TRUE(N->isUniqued());
839
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000840 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000841
842 N->replaceOperandWith(1, nullptr);
843 EXPECT_EQ(15u, N->getTag());
844 EXPECT_EQ(Header, N->getHeader());
845 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
846 ASSERT_TRUE(N->isUniqued());
847
848 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000849 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000850
851 N->replaceDwarfOperandWith(0, Empty);
852 EXPECT_EQ(15u, N->getTag());
853 EXPECT_EQ(Header, N->getHeader());
854 EXPECT_EQ(Empty, N->getDwarfOperand(0));
855 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000856 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000857
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000858 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000859 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000860}
861
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000862TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000863 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000864 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000865 EXPECT_EQ(StringRef(), N->getHeader());
866 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000867}
868
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000869typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000870
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000871TEST_F(DISubrangeTest, get) {
872 auto *N = DISubrange::get(Context, 5, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000873 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
874 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000875 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000876 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
877 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000878
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000879 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000880 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000881}
882
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000883TEST_F(DISubrangeTest, getEmptyArray) {
884 auto *N = DISubrange::get(Context, -1, 0);
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000885 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
886 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000887 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000888 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000889}
890
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000891typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000892
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000893TEST_F(DIEnumeratorTest, get) {
894 auto *N = DIEnumerator::get(Context, 7, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000895 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
896 EXPECT_EQ(7, N->getValue());
897 EXPECT_EQ("name", N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000898 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000899
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000900 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
901 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000902
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000903 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000904 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000905}
906
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000907typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000908
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000909TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000910 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000911 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000912 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
913 EXPECT_EQ("special", N->getName());
914 EXPECT_EQ(33u, N->getSizeInBits());
915 EXPECT_EQ(26u, N->getAlignInBits());
916 EXPECT_EQ(7u, N->getEncoding());
917 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000918 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000919 26, 7));
920
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000921 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000922 "special", 33, 26, 7));
923 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000924 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
925 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000926 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000927 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000928 25, 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 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000931
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000932 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000933 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000934}
935
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000936TEST_F(DIBasicTypeTest, getWithLargeValues) {
937 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000938 UINT64_MAX, UINT64_MAX - 1, 7);
939 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
940 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
941}
942
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000943TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000944 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000945 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000946 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
947 EXPECT_EQ("unspecified", N->getName());
948 EXPECT_EQ(0u, N->getSizeInBits());
949 EXPECT_EQ(0u, N->getAlignInBits());
950 EXPECT_EQ(0u, N->getEncoding());
951 EXPECT_EQ(0u, N->getLine());
952}
953
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000954typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000955
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000956TEST_F(DITypeTest, clone) {
957 // Check that DIType has a specialized clone that returns TempDIType.
958 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000959 dwarf::DW_ATE_signed);
960
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000961 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000962 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
963}
964
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000965TEST_F(DITypeTest, setFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000966 // void (void)
967 Metadata *TypesOps[] = {nullptr};
968 Metadata *Types = MDTuple::get(Context, TypesOps);
969
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000970 DIType *D = DISubroutineType::getDistinct(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000971 EXPECT_EQ(0u, D->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000972 D->setFlags(DINode::FlagRValueReference);
973 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000974 D->setFlags(0u);
975 EXPECT_EQ(0u, D->getFlags());
976
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000977 TempDIType T = DISubroutineType::getTemporary(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000978 EXPECT_EQ(0u, T->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000979 T->setFlags(DINode::FlagRValueReference);
980 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000981 T->setFlags(0u);
982 EXPECT_EQ(0u, T->getFlags());
983}
984
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000985typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000986
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000987TEST_F(DIDerivedTypeTest, get) {
988 DIFile *File = getFile();
989 DIScopeRef Scope = getSubprogramRef();
990 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000991 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000992
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000993 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000994 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
995 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
996 EXPECT_EQ("something", N->getName());
997 EXPECT_EQ(File, N->getFile());
998 EXPECT_EQ(1u, N->getLine());
999 EXPECT_EQ(Scope, N->getScope());
1000 EXPECT_EQ(BaseType, N->getBaseType());
1001 EXPECT_EQ(2u, N->getSizeInBits());
1002 EXPECT_EQ(3u, N->getAlignInBits());
1003 EXPECT_EQ(4u, N->getOffsetInBits());
1004 EXPECT_EQ(5u, N->getFlags());
1005 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001006 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001007 "something", File, 1, Scope, BaseType, 2, 3,
1008 4, 5, ExtraData));
1009
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001010 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001011 "something", File, 1, Scope, BaseType, 2, 3,
1012 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001013 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001014 File, 1, Scope, BaseType, 2, 3, 4, 5,
1015 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001016 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001017 "something", getFile(), 1, Scope, BaseType, 2,
1018 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001019 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001020 "something", File, 2, Scope, BaseType, 2, 3,
1021 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001022 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001023 "something", File, 1, getSubprogramRef(),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001024 BaseType, 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001025 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001026 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1027 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001028 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001029 "something", File, 1, Scope, BaseType, 3, 3,
1030 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001031 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001032 "something", File, 1, Scope, BaseType, 2, 2,
1033 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001034 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001035 "something", File, 1, Scope, BaseType, 2, 3,
1036 5, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001037 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001038 "something", File, 1, Scope, BaseType, 2, 3,
1039 4, 4, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001040 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001041 "something", File, 1, Scope, BaseType, 2, 3,
1042 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001043
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001044 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001045 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001046}
1047
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001048TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1049 DIFile *File = getFile();
1050 DIScopeRef Scope = getSubprogramRef();
1051 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001052 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001053
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001054 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001055 File, 1, Scope, BaseType, UINT64_MAX,
1056 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1057 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1058 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1059 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1060}
1061
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001062typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001063
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001064TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001065 unsigned Tag = dwarf::DW_TAG_structure_type;
1066 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001067 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001068 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001069 DIScopeRef Scope = getSubprogramRef();
1070 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001071 uint64_t SizeInBits = 2;
1072 uint64_t AlignInBits = 3;
1073 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001074 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001075 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001076 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001077 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001078 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001079 StringRef Identifier = "some id";
1080
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001081 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001082 BaseType, SizeInBits, AlignInBits,
1083 OffsetInBits, Flags, Elements, RuntimeLang,
1084 VTableHolder, TemplateParams, Identifier);
1085 EXPECT_EQ(Tag, N->getTag());
1086 EXPECT_EQ(Name, N->getName());
1087 EXPECT_EQ(File, N->getFile());
1088 EXPECT_EQ(Line, N->getLine());
1089 EXPECT_EQ(Scope, N->getScope());
1090 EXPECT_EQ(BaseType, N->getBaseType());
1091 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1092 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1093 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1094 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001095 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001096 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1097 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001098 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001099 EXPECT_EQ(Identifier, N->getIdentifier());
1100
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001101 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001102 BaseType, SizeInBits, AlignInBits,
1103 OffsetInBits, Flags, Elements, RuntimeLang,
1104 VTableHolder, TemplateParams, Identifier));
1105
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001106 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001107 BaseType, SizeInBits, AlignInBits,
1108 OffsetInBits, Flags, Elements, RuntimeLang,
1109 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001110 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001111 BaseType, SizeInBits, AlignInBits,
1112 OffsetInBits, Flags, Elements, RuntimeLang,
1113 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001114 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001115 BaseType, SizeInBits, AlignInBits,
1116 OffsetInBits, Flags, Elements, RuntimeLang,
1117 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001118 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001119 BaseType, SizeInBits, AlignInBits,
1120 OffsetInBits, Flags, Elements, RuntimeLang,
1121 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001122 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001123 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001124 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1125 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001126 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001127 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1128 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1129 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001130 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001131 BaseType, SizeInBits + 1, AlignInBits,
1132 OffsetInBits, Flags, Elements, RuntimeLang,
1133 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001134 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001135 BaseType, SizeInBits, AlignInBits + 1,
1136 OffsetInBits, Flags, Elements, RuntimeLang,
1137 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001138 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001139 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1140 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1141 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001142 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001143 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1144 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1145 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001146 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001147 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1148 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1149 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001150 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001151 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1152 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1153 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001154 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001155 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1156 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1157 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001158 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001159 BaseType, SizeInBits, AlignInBits,
1160 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001161 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001162 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001163 BaseType, SizeInBits, AlignInBits,
1164 OffsetInBits, Flags, Elements, RuntimeLang,
1165 VTableHolder, TemplateParams, "other"));
1166
1167 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001168 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1169 BaseType, SizeInBits, AlignInBits,
1170 OffsetInBits, Flags, Elements, RuntimeLang,
1171 VTableHolder, TemplateParams, "")
1172 ->getRawIdentifier());
1173 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1174 BaseType, SizeInBits, AlignInBits,
1175 OffsetInBits, Flags, Elements, RuntimeLang,
1176 VTableHolder, TemplateParams)
1177 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001178
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001179 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001180 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001181}
1182
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001183TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001184 unsigned Tag = dwarf::DW_TAG_structure_type;
1185 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001186 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001187 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001188 DIScopeRef Scope = getSubprogramRef();
1189 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001190 uint64_t SizeInBits = UINT64_MAX;
1191 uint64_t AlignInBits = UINT64_MAX - 1;
1192 uint64_t OffsetInBits = UINT64_MAX - 2;
1193 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001194 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001195 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001196 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001197 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001198 StringRef Identifier = "some id";
1199
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001200 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001201 BaseType, SizeInBits, AlignInBits,
1202 OffsetInBits, Flags, Elements, RuntimeLang,
1203 VTableHolder, TemplateParams, Identifier);
1204 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1205 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1206 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1207}
1208
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001209TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001210 unsigned Tag = dwarf::DW_TAG_structure_type;
1211 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001212 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001213 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001214 DIScopeRef Scope = getSubprogramRef();
1215 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001216 uint64_t SizeInBits = 2;
1217 uint64_t AlignInBits = 3;
1218 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001219 unsigned Flags = 5;
1220 unsigned RuntimeLang = 6;
1221 StringRef Identifier = "some id";
1222
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001223 auto *N = DICompositeType::get(
1224 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1225 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001226
1227 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001228 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001229 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001230 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001231 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001232 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001233
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001234 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001235 EXPECT_EQ(nullptr, N->getVTableHolder());
1236 N->replaceVTableHolder(VTableHolder);
1237 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1238 N->replaceVTableHolder(nullptr);
1239 EXPECT_EQ(nullptr, N->getVTableHolder());
1240
1241 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001242 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001243 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001244 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001245 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001246 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001247}
1248
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001249typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001250
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001251TEST_F(DISubroutineTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001252 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001253 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001254
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001255 auto *N = DISubroutineType::get(Context, Flags, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001256 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1257 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001258 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001259 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001260
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001261 EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, TypeArray));
1262 EXPECT_NE(N, DISubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001263
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001264 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001265 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001266
1267 // Test always-empty operands.
1268 EXPECT_EQ(nullptr, N->getScope());
1269 EXPECT_EQ(nullptr, N->getFile());
1270 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001271}
1272
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001273typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001275TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001276 StringRef Filename = "file";
1277 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001278 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001279
1280 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1281 EXPECT_EQ(Filename, N->getFilename());
1282 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001283 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001284
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001285 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1286 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001287
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001288 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001289 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001290}
1291
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001292TEST_F(DIFileTest, ScopeGetFile) {
1293 // Ensure that DIScope::getFile() returns itself.
1294 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001295 EXPECT_EQ(N, N->getFile());
1296}
1297
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001298typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001299
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001300TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001301 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001302 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001303 StringRef Producer = "some producer";
1304 bool IsOptimized = false;
1305 StringRef Flags = "flag after flag";
1306 unsigned RuntimeVersion = 2;
1307 StringRef SplitDebugFilename = "another/file";
1308 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001309 MDTuple *EnumTypes = getTuple();
1310 MDTuple *RetainedTypes = getTuple();
1311 MDTuple *Subprograms = getTuple();
1312 MDTuple *GlobalVariables = getTuple();
1313 MDTuple *ImportedEntities = getTuple();
Adrian Prantle3b49e02015-09-22 23:42:47 +00001314 uint64_t DWOId = 0x10000000c0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001315 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001316 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1317 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001318 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, DWOId);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001319
1320 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1321 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1322 EXPECT_EQ(File, N->getFile());
1323 EXPECT_EQ(Producer, N->getProducer());
1324 EXPECT_EQ(IsOptimized, N->isOptimized());
1325 EXPECT_EQ(Flags, N->getFlags());
1326 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1327 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1328 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001329 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1330 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1331 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1332 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1333 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001334 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001335
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001336 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001337 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1338 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1339 EXPECT_EQ(File, Temp->getFile());
1340 EXPECT_EQ(Producer, Temp->getProducer());
1341 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1342 EXPECT_EQ(Flags, Temp->getFlags());
1343 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1344 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1345 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1346 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1347 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
1348 EXPECT_EQ(Subprograms, Temp->getSubprograms().get());
1349 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1350 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
1351 EXPECT_EQ(DWOId, Temp->getDWOId());
1352
1353 auto *TempAddress = Temp.get();
1354 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1355 EXPECT_TRUE(Clone->isDistinct());
1356 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001357}
1358
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001359TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001360 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001361 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001362 StringRef Producer = "some producer";
1363 bool IsOptimized = false;
1364 StringRef Flags = "flag after flag";
1365 unsigned RuntimeVersion = 2;
1366 StringRef SplitDebugFilename = "another/file";
1367 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001368 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1369 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1370 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001371 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001372 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001373 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1374 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001375 RetainedTypes, nullptr, nullptr, ImportedEntities, DWOId);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001376
1377 auto *Subprograms = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001378 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001379 N->replaceSubprograms(Subprograms);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001380 EXPECT_EQ(Subprograms, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001381 N->replaceSubprograms(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001382 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001383
1384 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001385 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001386 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001387 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001388 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001389 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001390}
1391
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001392typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001393
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001394TEST_F(DISubprogramTest, get) {
1395 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001396 StringRef Name = "name";
1397 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001398 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001399 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001400 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001401 bool IsLocalToUnit = false;
1402 bool IsDefinition = true;
1403 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001404 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001405 unsigned Virtuality = 4;
1406 unsigned VirtualIndex = 5;
1407 unsigned Flags = 6;
1408 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001409 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001410 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001411 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001412
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001413 auto *N = DISubprogram::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001414 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1415 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001416 IsOptimized, TemplateParams, Declaration, Variables);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001417
1418 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1419 EXPECT_EQ(Scope, N->getScope());
1420 EXPECT_EQ(Name, N->getName());
1421 EXPECT_EQ(LinkageName, N->getLinkageName());
1422 EXPECT_EQ(File, N->getFile());
1423 EXPECT_EQ(Line, N->getLine());
1424 EXPECT_EQ(Type, N->getType());
1425 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1426 EXPECT_EQ(IsDefinition, N->isDefinition());
1427 EXPECT_EQ(ScopeLine, N->getScopeLine());
1428 EXPECT_EQ(ContainingType, N->getContainingType());
1429 EXPECT_EQ(Virtuality, N->getVirtuality());
1430 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1431 EXPECT_EQ(Flags, N->getFlags());
1432 EXPECT_EQ(IsOptimized, N->isOptimized());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001433 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001434 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001435 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001436 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001437 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1438 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001439 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001440 Declaration, Variables));
1441
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001442 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001443 File, Line, Type, IsLocalToUnit, IsDefinition,
1444 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001445 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001446 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001447 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001448 Line, Type, IsLocalToUnit, IsDefinition,
1449 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001450 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001451 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001452 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001453 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1454 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001455 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001456 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001457 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001458 Line, Type, IsLocalToUnit, IsDefinition,
1459 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001460 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001461 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001462 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001463 Line + 1, Type, IsLocalToUnit, IsDefinition,
1464 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001465 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001466 TemplateParams, Declaration, Variables));
Peter Collingbourned4bff302015-11-05 22:03:56 +00001467 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1468 getSubroutineType(), IsLocalToUnit,
1469 IsDefinition, ScopeLine, ContainingType,
1470 Virtuality, VirtualIndex, Flags, IsOptimized,
1471 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001472 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001473 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1474 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001475 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001476 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001477 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001478 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1479 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001480 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001481 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001482 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001483 Type, IsLocalToUnit, IsDefinition,
1484 ScopeLine + 1, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001485 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001486 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001487 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001488 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001489 getCompositeType(), Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001490 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001491 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001492 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001493 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1494 ContainingType, Virtuality + 1, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001495 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001496 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001497 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001498 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1499 ContainingType, Virtuality, VirtualIndex + 1,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001500 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001501 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001502 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001503 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1504 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001505 ~Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001506 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001507 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001508 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1509 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001510 Flags, !IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001511 Declaration, Variables));
Peter Collingbourned4bff302015-11-05 22:03:56 +00001512 EXPECT_NE(N,
1513 DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1514 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1515 ContainingType, Virtuality, VirtualIndex, Flags,
1516 IsOptimized, getTuple(), Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001517 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001518 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1519 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001520 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001521 getSubprogram(), Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001522 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001523 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1524 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001525 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001526 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001527
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001528 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001529 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001530}
1531
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001532typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001533
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001534TEST_F(DILexicalBlockTest, get) {
1535 DILocalScope *Scope = getSubprogram();
1536 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001537 unsigned Line = 5;
1538 unsigned Column = 8;
1539
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001540 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001541
1542 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1543 EXPECT_EQ(Scope, N->getScope());
1544 EXPECT_EQ(File, N->getFile());
1545 EXPECT_EQ(Line, N->getLine());
1546 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001547 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001548
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001549 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001550 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1551 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1552 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1553 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001554
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001555 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001556 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001557}
1558
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +00001559TEST_F(DILexicalBlockTest, Overflow) {
1560 DISubprogram *SP = getSubprogram();
1561 DIFile *F = getFile();
1562 {
1563 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1564 EXPECT_EQ(2u, LB->getLine());
1565 EXPECT_EQ(7u, LB->getColumn());
1566 }
1567 unsigned U16 = 1u << 16;
1568 {
1569 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1570 EXPECT_EQ(UINT32_MAX, LB->getLine());
1571 EXPECT_EQ(U16 - 1, LB->getColumn());
1572 }
1573 {
1574 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1575 EXPECT_EQ(UINT32_MAX, LB->getLine());
1576 EXPECT_EQ(0u, LB->getColumn());
1577 }
1578 {
1579 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1580 EXPECT_EQ(UINT32_MAX, LB->getLine());
1581 EXPECT_EQ(0u, LB->getColumn());
1582 }
1583}
1584
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001585typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001586
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001587TEST_F(DILexicalBlockFileTest, get) {
1588 DILocalScope *Scope = getSubprogram();
1589 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001590 unsigned Discriminator = 5;
1591
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001592 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001593
1594 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1595 EXPECT_EQ(Scope, N->getScope());
1596 EXPECT_EQ(File, N->getFile());
1597 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001598 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001599
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001600 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001601 Discriminator));
1602 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001603 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001604 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001605 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001606
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001607 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001608 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001609}
1610
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001611typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001612
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001613TEST_F(DINamespaceTest, get) {
1614 DIScope *Scope = getFile();
1615 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001616 StringRef Name = "namespace";
1617 unsigned Line = 5;
1618
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001619 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001620
1621 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1622 EXPECT_EQ(Scope, N->getScope());
1623 EXPECT_EQ(File, N->getFile());
1624 EXPECT_EQ(Name, N->getName());
1625 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001626 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001627
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001628 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1629 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1630 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1631 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001632
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001633 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001634 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001635}
1636
Adrian Prantlab1243f2015-06-29 23:03:47 +00001637typedef MetadataTest DIModuleTest;
1638
1639TEST_F(DIModuleTest, get) {
1640 DIScope *Scope = getFile();
1641 StringRef Name = "module";
1642 StringRef ConfigMacro = "-DNDEBUG";
1643 StringRef Includes = "-I.";
1644 StringRef Sysroot = "/";
1645
1646 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1647
1648 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1649 EXPECT_EQ(Scope, N->getScope());
1650 EXPECT_EQ(Name, N->getName());
1651 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1652 EXPECT_EQ(Includes, N->getIncludePath());
1653 EXPECT_EQ(Sysroot, N->getISysRoot());
1654 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1655 ConfigMacro, Includes, Sysroot));
1656 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1657 ConfigMacro, Includes, Sysroot));
1658 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1659 ConfigMacro, Includes, Sysroot));
1660 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1661 "other", Includes, Sysroot));
1662 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1663 ConfigMacro, "other", Sysroot));
1664 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1665 ConfigMacro, Includes, "other"));
1666
1667 TempDIModule Temp = N->clone();
1668 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1669}
1670
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001671typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001672
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001673TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001674 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001675 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001676
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001677 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001678
1679 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001680 EXPECT_EQ(Name, N->getName());
1681 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001682 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001683
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001684 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001685 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001686 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001687
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001688 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001689 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001690}
1691
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001692typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001693
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001694TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001695 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001696 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001697 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001698 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001699
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001700 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001701 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001702 EXPECT_EQ(Name, N->getName());
1703 EXPECT_EQ(Type, N->getType());
1704 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001705 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001706
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001707 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001708 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1709 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001710 EXPECT_NE(N,
1711 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1712 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001713 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001714 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001715 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001716
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001717 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001718 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001719}
1720
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001721typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001722
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001723TEST_F(DIGlobalVariableTest, get) {
1724 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001725 StringRef Name = "name";
1726 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001727 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001728 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001729 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001730 bool IsLocalToUnit = false;
1731 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001732 Constant *Variable = getConstant();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001733 DIDerivedType *StaticDataMemberDeclaration =
1734 cast<DIDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001735
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001736 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001737 Type, IsLocalToUnit, IsDefinition, Variable,
1738 StaticDataMemberDeclaration);
1739 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1740 EXPECT_EQ(Scope, N->getScope());
1741 EXPECT_EQ(Name, N->getName());
1742 EXPECT_EQ(LinkageName, N->getLinkageName());
1743 EXPECT_EQ(File, N->getFile());
1744 EXPECT_EQ(Line, N->getLine());
1745 EXPECT_EQ(Type, N->getType());
1746 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1747 EXPECT_EQ(IsDefinition, N->isDefinition());
1748 EXPECT_EQ(Variable, N->getVariable());
1749 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001750 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001751 Line, Type, IsLocalToUnit, IsDefinition,
1752 Variable, StaticDataMemberDeclaration));
1753
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001754 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001755 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001756 File, Line, Type, IsLocalToUnit, IsDefinition,
1757 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001758 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001759 Line, Type, IsLocalToUnit, IsDefinition,
1760 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001761 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001762 Type, IsLocalToUnit, IsDefinition,
1763 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001764 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001765 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001766 Line, Type, IsLocalToUnit, IsDefinition,
1767 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001768 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001769 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001770 Line + 1, Type, IsLocalToUnit, IsDefinition,
1771 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001772 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001773 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001774 getDerivedType(), IsLocalToUnit, IsDefinition,
1775 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001776 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001777 Line, Type, !IsLocalToUnit, IsDefinition,
1778 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001779 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001780 Line, Type, IsLocalToUnit, !IsDefinition,
1781 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001782 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001783 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001784 Type, IsLocalToUnit, IsDefinition,
1785 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001786 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001787 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001788 Type, IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001789 cast<DIDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001790
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001791 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001792 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001793}
1794
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001795typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001796
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001797TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001799 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001800 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001801 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001802 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001803 unsigned Arg = 6;
1804 unsigned Flags = 7;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001805
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001806 auto *N =
1807 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags);
1808 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001809 EXPECT_EQ(Scope, N->getScope());
1810 EXPECT_EQ(Name, N->getName());
1811 EXPECT_EQ(File, N->getFile());
1812 EXPECT_EQ(Line, N->getLine());
1813 EXPECT_EQ(Type, N->getType());
1814 EXPECT_EQ(Arg, N->getArg());
1815 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001816 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1817 Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001818
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001819 EXPECT_FALSE(
1820 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags)
1821 ->isParameter());
1822 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001823 Type, Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001824 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
1825 Arg, Flags));
1826 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
1827 Arg, Flags));
1828 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
1829 Arg, Flags));
1830 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001831 getDerivedType(), Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001832 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001833 Arg + 1, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001834 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1835 ~Flags));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001836
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001837 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001838 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001839}
1840
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001841TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001842 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1843 0, nullptr, 255, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001844 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001845 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1846 0, nullptr, 256, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001847 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001848 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1849 0, nullptr, 257, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001850 ->getArg());
1851 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001852 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1853 0, nullptr, Max, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001854 ->getArg());
1855}
1856
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001857typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001858
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001859TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001860 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001861 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001862 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001863 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001864
1865 EXPECT_EQ(5u, N->getNumElements());
1866 EXPECT_EQ(2u, N->getElement(0));
1867 EXPECT_EQ(6u, N->getElement(1));
1868 EXPECT_EQ(9u, N->getElement(2));
1869 EXPECT_EQ(78u, N->getElement(3));
1870 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001871
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001872 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001873 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001874}
1875
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001876TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001877#define EXPECT_VALID(...) \
1878 do { \
1879 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001880 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001881 } while (false)
1882#define EXPECT_INVALID(...) \
1883 do { \
1884 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001885 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001886 } while (false)
1887
1888 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001889 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001890
1891 // Valid constructions.
1892 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1893 EXPECT_VALID(dwarf::DW_OP_deref);
1894 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1895 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1896 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1897 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1898 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1899
1900 // Invalid constructions.
1901 EXPECT_INVALID(~0u);
1902 EXPECT_INVALID(dwarf::DW_OP_plus);
1903 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1904 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1905 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1906 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1907
1908#undef EXPECT_VALID
1909#undef EXPECT_INVALID
1910}
1911
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001912typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001913
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001914TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001915 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001916 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001917 unsigned Line = 5;
1918 StringRef GetterName = "getter";
1919 StringRef SetterName = "setter";
1920 unsigned Attributes = 7;
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001921 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001922
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001923 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001924 SetterName, Attributes, Type);
1925
1926 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1927 EXPECT_EQ(Name, N->getName());
1928 EXPECT_EQ(File, N->getFile());
1929 EXPECT_EQ(Line, N->getLine());
1930 EXPECT_EQ(GetterName, N->getGetterName());
1931 EXPECT_EQ(SetterName, N->getSetterName());
1932 EXPECT_EQ(Attributes, N->getAttributes());
1933 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001934 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001935 SetterName, Attributes, Type));
1936
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001937 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001938 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001939 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001940 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001941 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001942 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001943 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001944 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001945 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001946 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001947 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001948 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001949 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001950 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001951 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001952
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001953 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001954 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001955}
1956
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001957typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001958
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001959TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001960 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001961 DIScope *Scope = getSubprogram();
1962 DINodeRef Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001963 unsigned Line = 5;
1964 StringRef Name = "name";
1965
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001966 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001967
1968 EXPECT_EQ(Tag, N->getTag());
1969 EXPECT_EQ(Scope, N->getScope());
1970 EXPECT_EQ(Entity, N->getEntity());
1971 EXPECT_EQ(Line, N->getLine());
1972 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001973 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001974
1975 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001976 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001977 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001978 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001979 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001980 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001981 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001982 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001983 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001984 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001985 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001986
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001987 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001988 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001989}
1990
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001991typedef MetadataTest MetadataAsValueTest;
1992
1993TEST_F(MetadataAsValueTest, MDNode) {
1994 MDNode *N = MDNode::get(Context, None);
1995 auto *V = MetadataAsValue::get(Context, N);
1996 EXPECT_TRUE(V->getType()->isMetadataTy());
1997 EXPECT_EQ(N, V->getMetadata());
1998
1999 auto *V2 = MetadataAsValue::get(Context, N);
2000 EXPECT_EQ(V, V2);
2001}
2002
2003TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2004 MDNode *N = MDNode::get(Context, None);
2005 Metadata *Ops[] = {N};
2006 MDNode *N2 = MDNode::get(Context, Ops);
2007 auto *V = MetadataAsValue::get(Context, N2);
2008 EXPECT_TRUE(V->getType()->isMetadataTy());
2009 EXPECT_EQ(N2, V->getMetadata());
2010
2011 auto *V2 = MetadataAsValue::get(Context, N2);
2012 EXPECT_EQ(V, V2);
2013
2014 auto *V3 = MetadataAsValue::get(Context, N);
2015 EXPECT_TRUE(V3->getType()->isMetadataTy());
2016 EXPECT_NE(V, V3);
2017 EXPECT_EQ(N, V3->getMetadata());
2018}
2019
2020TEST_F(MetadataAsValueTest, MDNodeConstant) {
2021 auto *C = ConstantInt::getTrue(Context);
2022 auto *MD = ConstantAsMetadata::get(C);
2023 Metadata *Ops[] = {MD};
2024 auto *N = MDNode::get(Context, Ops);
2025
2026 auto *V = MetadataAsValue::get(Context, MD);
2027 EXPECT_TRUE(V->getType()->isMetadataTy());
2028 EXPECT_EQ(MD, V->getMetadata());
2029
2030 auto *V2 = MetadataAsValue::get(Context, N);
2031 EXPECT_EQ(MD, V2->getMetadata());
2032 EXPECT_EQ(V, V2);
2033}
2034
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002035typedef MetadataTest ValueAsMetadataTest;
2036
2037TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2038 Type *Ty = Type::getInt1PtrTy(Context);
2039 std::unique_ptr<GlobalVariable> GV0(
2040 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2041 auto *MD = ValueAsMetadata::get(GV0.get());
2042 EXPECT_TRUE(MD->getValue() == GV0.get());
2043 ASSERT_TRUE(GV0->use_empty());
2044
2045 std::unique_ptr<GlobalVariable> GV1(
2046 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2047 GV0->replaceAllUsesWith(GV1.get());
2048 EXPECT_TRUE(MD->getValue() == GV1.get());
2049}
2050
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002051TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2052 // Create a constant.
2053 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2054 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2055
2056 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002057 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002058
2059 // 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 +00002060 Metadata *Ops1[] = {CI, CI, Temp.get()};
2061 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002062
2063 auto *N1 = MDTuple::get(Context, Ops1);
2064 auto *N2 = MDTuple::get(Context, Ops2);
2065 ASSERT_NE(N1, N2);
2066
2067 // Tell metadata that the constant is getting deleted.
2068 //
2069 // After this, N1 will be invalid, so don't touch it.
2070 ValueAsMetadata::handleDeletion(CI->getValue());
2071 EXPECT_EQ(nullptr, N2->getOperand(0));
2072 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002073 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002074
2075 // Clean up Temp for teardown.
2076 Temp->replaceAllUsesWith(nullptr);
2077}
2078
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002079typedef MetadataTest TrackingMDRefTest;
2080
2081TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2082 Type *Ty = Type::getInt1PtrTy(Context);
2083 std::unique_ptr<GlobalVariable> GV0(
2084 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2085 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2086 EXPECT_TRUE(MD->getValue() == GV0.get());
2087 ASSERT_TRUE(GV0->use_empty());
2088
2089 std::unique_ptr<GlobalVariable> GV1(
2090 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2091 GV0->replaceAllUsesWith(GV1.get());
2092 EXPECT_TRUE(MD->getValue() == GV1.get());
2093
2094 // Reset it, so we don't inadvertently test deletion.
2095 MD.reset();
2096}
2097
2098TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2099 Type *Ty = Type::getInt1PtrTy(Context);
2100 std::unique_ptr<GlobalVariable> GV(
2101 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2102 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2103 EXPECT_TRUE(MD->getValue() == GV.get());
2104 ASSERT_TRUE(GV->use_empty());
2105
2106 GV.reset();
2107 EXPECT_TRUE(!MD);
2108}
2109
Devang Patel0924b332009-07-30 00:03:41 +00002110TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002111 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002112 ConstantAsMetadata *C =
2113 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2114 ConstantAsMetadata *C2 =
2115 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002116
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002117 Metadata *const V = C;
2118 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002119 MDNode *n = MDNode::get(Context, V);
2120 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002121
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002122 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002123 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002124 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2125 NMD->addOperand(n);
2126 NMD->addOperand(n2);
2127
Chris Lattnerbe354a62009-08-23 04:47:35 +00002128 std::string Str;
2129 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002130 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002131 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002132 oss.str().c_str());
2133}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002134
2135typedef MetadataTest FunctionAttachmentTest;
2136TEST_F(FunctionAttachmentTest, setMetadata) {
2137 Function *F = getFunction("foo");
2138 ASSERT_FALSE(F->hasMetadata());
2139 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2140 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2141 EXPECT_EQ(nullptr, F->getMetadata("other"));
2142
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002143 DISubprogram *SP1 = getSubprogram();
2144 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002145 ASSERT_NE(SP1, SP2);
2146
2147 F->setMetadata("dbg", SP1);
2148 EXPECT_TRUE(F->hasMetadata());
2149 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2150 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2151 EXPECT_EQ(nullptr, F->getMetadata("other"));
2152
2153 F->setMetadata(LLVMContext::MD_dbg, SP2);
2154 EXPECT_TRUE(F->hasMetadata());
2155 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2156 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2157 EXPECT_EQ(nullptr, F->getMetadata("other"));
2158
2159 F->setMetadata("dbg", nullptr);
2160 EXPECT_FALSE(F->hasMetadata());
2161 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2162 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2163 EXPECT_EQ(nullptr, F->getMetadata("other"));
2164
2165 MDTuple *T1 = getTuple();
2166 MDTuple *T2 = getTuple();
2167 ASSERT_NE(T1, T2);
2168
2169 F->setMetadata("other1", T1);
2170 F->setMetadata("other2", T2);
2171 EXPECT_TRUE(F->hasMetadata());
2172 EXPECT_EQ(T1, F->getMetadata("other1"));
2173 EXPECT_EQ(T2, F->getMetadata("other2"));
2174 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2175
2176 F->setMetadata("other1", T2);
2177 F->setMetadata("other2", T1);
2178 EXPECT_EQ(T2, F->getMetadata("other1"));
2179 EXPECT_EQ(T1, F->getMetadata("other2"));
2180
2181 F->setMetadata("other1", nullptr);
2182 F->setMetadata("other2", nullptr);
2183 EXPECT_FALSE(F->hasMetadata());
2184 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2185 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2186}
2187
2188TEST_F(FunctionAttachmentTest, getAll) {
2189 Function *F = getFunction("foo");
2190
2191 MDTuple *T1 = getTuple();
2192 MDTuple *T2 = getTuple();
2193 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002194 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002195
2196 F->setMetadata("other1", T2);
2197 F->setMetadata(LLVMContext::MD_dbg, SP);
2198 F->setMetadata("other2", T1);
2199 F->setMetadata(LLVMContext::MD_prof, P);
2200 F->setMetadata("other2", T2);
2201 F->setMetadata("other1", T1);
2202
2203 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2204 F->getAllMetadata(MDs);
2205 ASSERT_EQ(4u, MDs.size());
2206 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2207 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2208 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2209 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2210 EXPECT_EQ(SP, MDs[0].second);
2211 EXPECT_EQ(P, MDs[1].second);
2212 EXPECT_EQ(T1, MDs[2].second);
2213 EXPECT_EQ(T2, MDs[3].second);
2214}
2215
2216TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2217 Function *F = getFunction("foo");
2218
2219 MDTuple *T1 = getTuple();
2220 MDTuple *T2 = getTuple();
2221 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002222 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002223
2224 F->setMetadata("other1", T1);
2225 F->setMetadata(LLVMContext::MD_dbg, SP);
2226 F->setMetadata("other2", T2);
2227 F->setMetadata(LLVMContext::MD_prof, P);
2228
2229 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2230 F->dropUnknownMetadata(Known);
2231
2232 EXPECT_EQ(T2, F->getMetadata("other2"));
2233 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2234 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2235 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2236
2237 F->setMetadata("other2", nullptr);
2238 F->setMetadata(LLVMContext::MD_prof, nullptr);
2239 EXPECT_FALSE(F->hasMetadata());
2240}
2241
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002242TEST_F(FunctionAttachmentTest, Verifier) {
2243 Function *F = getFunction("foo");
2244 F->setMetadata("attach", getTuple());
2245
2246 // Confirm this has no body.
2247 ASSERT_TRUE(F->empty());
2248
2249 // Functions without a body cannot have metadata attachments (they also can't
2250 // be verified directly, so check that the module fails to verify).
2251 EXPECT_TRUE(verifyModule(*F->getParent()));
2252
2253 // Functions with a body can.
2254 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2255 EXPECT_FALSE(verifyModule(*F->getParent()));
2256 EXPECT_FALSE(verifyFunction(*F));
2257}
2258
Diego Novillo2567f3d2015-05-13 15:13:45 +00002259TEST_F(FunctionAttachmentTest, EntryCount) {
2260 Function *F = getFunction("foo");
2261 EXPECT_FALSE(F->getEntryCount().hasValue());
2262 F->setEntryCount(12304);
2263 EXPECT_TRUE(F->getEntryCount().hasValue());
2264 EXPECT_EQ(12304u, *F->getEntryCount());
2265}
2266
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002267TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2268 Function *F = getFunction("foo");
2269 DISubprogram *SP = getSubprogram();
2270 F->setSubprogram(SP);
2271
2272 // Note that the static_cast confirms that F->getSubprogram() actually
2273 // returns an DISubprogram.
2274 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2275 EXPECT_EQ(SP, F->getMetadata("dbg"));
2276 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2277}
2278
Nick Lewycky49f89192009-04-04 07:22:01 +00002279}