blob: 6741c79afc97913d43411df6e748526eda99765d [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
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000816typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000817
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000818TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000819 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000820 auto *Empty = MDNode::get(Context, None);
821 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000822 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000823 EXPECT_EQ(15u, N->getTag());
824 EXPECT_EQ(2u, N->getNumOperands());
825 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000826 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000827 EXPECT_EQ(1u, N->getNumDwarfOperands());
828 EXPECT_EQ(Empty, N->getDwarfOperand(0));
829 EXPECT_EQ(Empty, N->getOperand(1));
830 ASSERT_TRUE(N->isUniqued());
831
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000832 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000833
834 N->replaceOperandWith(1, nullptr);
835 EXPECT_EQ(15u, N->getTag());
836 EXPECT_EQ(Header, N->getHeader());
837 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
838 ASSERT_TRUE(N->isUniqued());
839
840 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000841 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000842
843 N->replaceDwarfOperandWith(0, Empty);
844 EXPECT_EQ(15u, N->getTag());
845 EXPECT_EQ(Header, N->getHeader());
846 EXPECT_EQ(Empty, N->getDwarfOperand(0));
847 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000848 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000849
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000850 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000851 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000852}
853
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000854TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000855 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000856 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000857 EXPECT_EQ(StringRef(), N->getHeader());
858 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000859}
860
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000861typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000862
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000863TEST_F(DISubrangeTest, get) {
864 auto *N = DISubrange::get(Context, 5, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000865 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
866 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000867 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000868 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
869 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000870
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000871 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000872 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000873}
874
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000875TEST_F(DISubrangeTest, getEmptyArray) {
876 auto *N = DISubrange::get(Context, -1, 0);
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000877 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
878 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000879 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000880 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000881}
882
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000883typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000884
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000885TEST_F(DIEnumeratorTest, get) {
886 auto *N = DIEnumerator::get(Context, 7, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000887 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
888 EXPECT_EQ(7, N->getValue());
889 EXPECT_EQ("name", N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000890 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000891
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000892 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
893 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000894
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000895 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000896 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000897}
898
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000899typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000900
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000901TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000902 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000903 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000904 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
905 EXPECT_EQ("special", N->getName());
906 EXPECT_EQ(33u, N->getSizeInBits());
907 EXPECT_EQ(26u, N->getAlignInBits());
908 EXPECT_EQ(7u, N->getEncoding());
909 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000910 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000911 26, 7));
912
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000913 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000914 "special", 33, 26, 7));
915 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000916 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
917 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000918 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000919 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000920 25, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000921 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000922 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000923
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000924 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000925 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000926}
927
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000928TEST_F(DIBasicTypeTest, getWithLargeValues) {
929 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000930 UINT64_MAX, UINT64_MAX - 1, 7);
931 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
932 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
933}
934
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000935TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000936 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000937 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000938 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
939 EXPECT_EQ("unspecified", N->getName());
940 EXPECT_EQ(0u, N->getSizeInBits());
941 EXPECT_EQ(0u, N->getAlignInBits());
942 EXPECT_EQ(0u, N->getEncoding());
943 EXPECT_EQ(0u, N->getLine());
944}
945
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000946typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000947
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000948TEST_F(DITypeTest, clone) {
949 // Check that DIType has a specialized clone that returns TempDIType.
950 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000951 dwarf::DW_ATE_signed);
952
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000953 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000954 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
955}
956
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000957TEST_F(DITypeTest, setFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000958 // void (void)
959 Metadata *TypesOps[] = {nullptr};
960 Metadata *Types = MDTuple::get(Context, TypesOps);
961
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000962 DIType *D = DISubroutineType::getDistinct(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000963 EXPECT_EQ(0u, D->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000964 D->setFlags(DINode::FlagRValueReference);
965 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000966 D->setFlags(0u);
967 EXPECT_EQ(0u, D->getFlags());
968
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000969 TempDIType T = DISubroutineType::getTemporary(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000970 EXPECT_EQ(0u, T->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000971 T->setFlags(DINode::FlagRValueReference);
972 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000973 T->setFlags(0u);
974 EXPECT_EQ(0u, T->getFlags());
975}
976
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000977typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000978
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000979TEST_F(DIDerivedTypeTest, get) {
980 DIFile *File = getFile();
981 DIScopeRef Scope = getSubprogramRef();
982 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000983 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000984
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000985 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000986 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
987 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
988 EXPECT_EQ("something", N->getName());
989 EXPECT_EQ(File, N->getFile());
990 EXPECT_EQ(1u, N->getLine());
991 EXPECT_EQ(Scope, N->getScope());
992 EXPECT_EQ(BaseType, N->getBaseType());
993 EXPECT_EQ(2u, N->getSizeInBits());
994 EXPECT_EQ(3u, N->getAlignInBits());
995 EXPECT_EQ(4u, N->getOffsetInBits());
996 EXPECT_EQ(5u, N->getFlags());
997 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000998 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000999 "something", File, 1, Scope, BaseType, 2, 3,
1000 4, 5, ExtraData));
1001
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001002 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001003 "something", File, 1, Scope, BaseType, 2, 3,
1004 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001005 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001006 File, 1, Scope, BaseType, 2, 3, 4, 5,
1007 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001008 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001009 "something", getFile(), 1, Scope, BaseType, 2,
1010 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001011 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001012 "something", File, 2, Scope, BaseType, 2, 3,
1013 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001014 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001015 "something", File, 1, getSubprogramRef(),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001016 BaseType, 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001017 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001018 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1019 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001020 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001021 "something", File, 1, Scope, BaseType, 3, 3,
1022 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001023 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001024 "something", File, 1, Scope, BaseType, 2, 2,
1025 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001026 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001027 "something", File, 1, Scope, BaseType, 2, 3,
1028 5, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001029 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001030 "something", File, 1, Scope, BaseType, 2, 3,
1031 4, 4, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001032 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001033 "something", File, 1, Scope, BaseType, 2, 3,
1034 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001035
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001036 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001037 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001038}
1039
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001040TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1041 DIFile *File = getFile();
1042 DIScopeRef Scope = getSubprogramRef();
1043 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001044 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001045
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001046 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001047 File, 1, Scope, BaseType, UINT64_MAX,
1048 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1049 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1050 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1051 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1052}
1053
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001054typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001055
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001056TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001057 unsigned Tag = dwarf::DW_TAG_structure_type;
1058 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001059 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001060 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001061 DIScopeRef Scope = getSubprogramRef();
1062 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001063 uint64_t SizeInBits = 2;
1064 uint64_t AlignInBits = 3;
1065 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001066 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001067 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001068 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001069 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001070 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001071 StringRef Identifier = "some id";
1072
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001073 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001074 BaseType, SizeInBits, AlignInBits,
1075 OffsetInBits, Flags, Elements, RuntimeLang,
1076 VTableHolder, TemplateParams, Identifier);
1077 EXPECT_EQ(Tag, N->getTag());
1078 EXPECT_EQ(Name, N->getName());
1079 EXPECT_EQ(File, N->getFile());
1080 EXPECT_EQ(Line, N->getLine());
1081 EXPECT_EQ(Scope, N->getScope());
1082 EXPECT_EQ(BaseType, N->getBaseType());
1083 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1084 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1085 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1086 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001087 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001088 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1089 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001090 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001091 EXPECT_EQ(Identifier, N->getIdentifier());
1092
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001093 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001094 BaseType, SizeInBits, AlignInBits,
1095 OffsetInBits, Flags, Elements, RuntimeLang,
1096 VTableHolder, TemplateParams, Identifier));
1097
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001098 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001099 BaseType, SizeInBits, AlignInBits,
1100 OffsetInBits, Flags, Elements, RuntimeLang,
1101 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001102 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001103 BaseType, SizeInBits, AlignInBits,
1104 OffsetInBits, Flags, Elements, RuntimeLang,
1105 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001106 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), 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, Name, File, Line + 1, 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(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001115 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001116 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1117 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001118 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001119 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1120 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1121 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001122 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001123 BaseType, SizeInBits + 1, AlignInBits,
1124 OffsetInBits, Flags, Elements, RuntimeLang,
1125 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001126 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001127 BaseType, SizeInBits, AlignInBits + 1,
1128 OffsetInBits, Flags, Elements, RuntimeLang,
1129 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001130 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001131 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1132 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1133 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001134 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001135 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1136 AlignInBits, OffsetInBits, Flags + 1, 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 Smith53855f02015-03-27 23:05:04 +00001139 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1140 AlignInBits, OffsetInBits, Flags, getTuple(), 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, Elements, RuntimeLang + 1,
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, Elements, RuntimeLang,
1149 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001150 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001151 BaseType, SizeInBits, AlignInBits,
1152 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001153 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001154 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001155 BaseType, SizeInBits, AlignInBits,
1156 OffsetInBits, Flags, Elements, RuntimeLang,
1157 VTableHolder, TemplateParams, "other"));
1158
1159 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001160 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1161 BaseType, SizeInBits, AlignInBits,
1162 OffsetInBits, Flags, Elements, RuntimeLang,
1163 VTableHolder, TemplateParams, "")
1164 ->getRawIdentifier());
1165 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1166 BaseType, SizeInBits, AlignInBits,
1167 OffsetInBits, Flags, Elements, RuntimeLang,
1168 VTableHolder, TemplateParams)
1169 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001170
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001171 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001172 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001173}
1174
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001175TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001176 unsigned Tag = dwarf::DW_TAG_structure_type;
1177 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001178 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001179 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001180 DIScopeRef Scope = getSubprogramRef();
1181 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001182 uint64_t SizeInBits = UINT64_MAX;
1183 uint64_t AlignInBits = UINT64_MAX - 1;
1184 uint64_t OffsetInBits = UINT64_MAX - 2;
1185 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001186 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001187 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001188 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001189 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001190 StringRef Identifier = "some id";
1191
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001192 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001193 BaseType, SizeInBits, AlignInBits,
1194 OffsetInBits, Flags, Elements, RuntimeLang,
1195 VTableHolder, TemplateParams, Identifier);
1196 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1197 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1198 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1199}
1200
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001201TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001202 unsigned Tag = dwarf::DW_TAG_structure_type;
1203 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001204 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001205 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001206 DIScopeRef Scope = getSubprogramRef();
1207 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001208 uint64_t SizeInBits = 2;
1209 uint64_t AlignInBits = 3;
1210 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001211 unsigned Flags = 5;
1212 unsigned RuntimeLang = 6;
1213 StringRef Identifier = "some id";
1214
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001215 auto *N = DICompositeType::get(
1216 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1217 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001218
1219 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001220 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001221 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001222 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001223 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001224 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001225
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001226 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001227 EXPECT_EQ(nullptr, N->getVTableHolder());
1228 N->replaceVTableHolder(VTableHolder);
1229 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1230 N->replaceVTableHolder(nullptr);
1231 EXPECT_EQ(nullptr, N->getVTableHolder());
1232
1233 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001234 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001235 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001236 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001237 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001238 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001239}
1240
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001241typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001242
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001243TEST_F(DISubroutineTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001244 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001245 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001246
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001247 auto *N = DISubroutineType::get(Context, Flags, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001248 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1249 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001250 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001251 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001252
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001253 EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, TypeArray));
1254 EXPECT_NE(N, DISubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001255
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001256 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001257 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001258
1259 // Test always-empty operands.
1260 EXPECT_EQ(nullptr, N->getScope());
1261 EXPECT_EQ(nullptr, N->getFile());
1262 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001263}
1264
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001265typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001266
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001267TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001268 StringRef Filename = "file";
1269 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001270 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001271
1272 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1273 EXPECT_EQ(Filename, N->getFilename());
1274 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001275 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001276
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001277 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1278 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001279
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001280 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001281 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001282}
1283
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001284TEST_F(DIFileTest, ScopeGetFile) {
1285 // Ensure that DIScope::getFile() returns itself.
1286 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001287 EXPECT_EQ(N, N->getFile());
1288}
1289
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001290typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001291
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001292TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001293 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001294 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001295 StringRef Producer = "some producer";
1296 bool IsOptimized = false;
1297 StringRef Flags = "flag after flag";
1298 unsigned RuntimeVersion = 2;
1299 StringRef SplitDebugFilename = "another/file";
1300 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001301 MDTuple *EnumTypes = getTuple();
1302 MDTuple *RetainedTypes = getTuple();
1303 MDTuple *Subprograms = getTuple();
1304 MDTuple *GlobalVariables = getTuple();
1305 MDTuple *ImportedEntities = getTuple();
Adrian Prantl1f599f92015-05-21 20:37:30 +00001306 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001307 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001308 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1309 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001310 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, DWOId);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001311
1312 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1313 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1314 EXPECT_EQ(File, N->getFile());
1315 EXPECT_EQ(Producer, N->getProducer());
1316 EXPECT_EQ(IsOptimized, N->isOptimized());
1317 EXPECT_EQ(Flags, N->getFlags());
1318 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1319 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1320 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001321 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1322 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1323 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1324 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1325 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001326 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001327
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001328 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001329 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1330 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1331 EXPECT_EQ(File, Temp->getFile());
1332 EXPECT_EQ(Producer, Temp->getProducer());
1333 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1334 EXPECT_EQ(Flags, Temp->getFlags());
1335 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1336 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1337 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1338 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1339 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
1340 EXPECT_EQ(Subprograms, Temp->getSubprograms().get());
1341 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1342 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
1343 EXPECT_EQ(DWOId, Temp->getDWOId());
1344
1345 auto *TempAddress = Temp.get();
1346 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1347 EXPECT_TRUE(Clone->isDistinct());
1348 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001349}
1350
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001351TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001352 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001353 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001354 StringRef Producer = "some producer";
1355 bool IsOptimized = false;
1356 StringRef Flags = "flag after flag";
1357 unsigned RuntimeVersion = 2;
1358 StringRef SplitDebugFilename = "another/file";
1359 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001360 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1361 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1362 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001363 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001364 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001365 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1366 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001367 RetainedTypes, nullptr, nullptr, ImportedEntities, DWOId);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001368
1369 auto *Subprograms = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001370 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001371 N->replaceSubprograms(Subprograms);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001372 EXPECT_EQ(Subprograms, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001373 N->replaceSubprograms(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001374 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001375
1376 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001377 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001378 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001379 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001380 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001381 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001382}
1383
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001384typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001385
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001386TEST_F(DISubprogramTest, get) {
1387 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001388 StringRef Name = "name";
1389 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001390 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001391 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001392 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001393 bool IsLocalToUnit = false;
1394 bool IsDefinition = true;
1395 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001396 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001397 unsigned Virtuality = 4;
1398 unsigned VirtualIndex = 5;
1399 unsigned Flags = 6;
1400 bool IsOptimized = false;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001401 llvm::Function *Function = getFunction("foo");
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001402 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001403 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001404 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001405
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001406 auto *N = DISubprogram::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001407 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1408 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1409 IsOptimized, Function, TemplateParams, Declaration, Variables);
1410
1411 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1412 EXPECT_EQ(Scope, N->getScope());
1413 EXPECT_EQ(Name, N->getName());
1414 EXPECT_EQ(LinkageName, N->getLinkageName());
1415 EXPECT_EQ(File, N->getFile());
1416 EXPECT_EQ(Line, N->getLine());
1417 EXPECT_EQ(Type, N->getType());
1418 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1419 EXPECT_EQ(IsDefinition, N->isDefinition());
1420 EXPECT_EQ(ScopeLine, N->getScopeLine());
1421 EXPECT_EQ(ContainingType, N->getContainingType());
1422 EXPECT_EQ(Virtuality, N->getVirtuality());
1423 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1424 EXPECT_EQ(Flags, N->getFlags());
1425 EXPECT_EQ(IsOptimized, N->isOptimized());
1426 EXPECT_EQ(Function, N->getFunction());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001427 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001428 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001429 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001430 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001431 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1432 ContainingType, Virtuality, VirtualIndex,
1433 Flags, IsOptimized, Function, TemplateParams,
1434 Declaration, Variables));
1435
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001436 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001437 File, Line, Type, IsLocalToUnit, IsDefinition,
1438 ScopeLine, ContainingType, Virtuality,
1439 VirtualIndex, Flags, IsOptimized, Function,
1440 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001441 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001442 Line, Type, IsLocalToUnit, IsDefinition,
1443 ScopeLine, ContainingType, Virtuality,
1444 VirtualIndex, Flags, IsOptimized, Function,
1445 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001446 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001447 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1448 ContainingType, Virtuality, VirtualIndex,
1449 Flags, IsOptimized, Function, TemplateParams,
1450 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001451 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001452 Line, Type, IsLocalToUnit, IsDefinition,
1453 ScopeLine, ContainingType, Virtuality,
1454 VirtualIndex, Flags, IsOptimized, Function,
1455 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001456 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001457 Line + 1, Type, IsLocalToUnit, IsDefinition,
1458 ScopeLine, ContainingType, Virtuality,
1459 VirtualIndex, Flags, IsOptimized, Function,
1460 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001461 EXPECT_NE(N, DISubprogram::get(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001462 Context, Scope, Name, LinkageName, File, Line,
1463 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine,
1464 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1465 Function, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001466 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001467 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1468 ContainingType, Virtuality, VirtualIndex,
1469 Flags, IsOptimized, Function, TemplateParams,
1470 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001471 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001472 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1473 ContainingType, Virtuality, VirtualIndex,
1474 Flags, IsOptimized, Function, TemplateParams,
1475 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001476 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001477 Type, IsLocalToUnit, IsDefinition,
1478 ScopeLine + 1, ContainingType, Virtuality,
1479 VirtualIndex, Flags, IsOptimized, Function,
1480 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001481 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001482 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001483 getCompositeType(), Virtuality, VirtualIndex,
1484 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001485 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001486 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001487 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1488 ContainingType, Virtuality + 1, VirtualIndex,
1489 Flags, IsOptimized, Function, TemplateParams,
1490 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001491 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001492 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1493 ContainingType, Virtuality, VirtualIndex + 1,
1494 Flags, IsOptimized, Function, TemplateParams,
1495 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001496 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001497 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1498 ContainingType, Virtuality, VirtualIndex,
1499 ~Flags, IsOptimized, Function, TemplateParams,
1500 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001501 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001502 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1503 ContainingType, Virtuality, VirtualIndex,
1504 Flags, !IsOptimized, Function, TemplateParams,
1505 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001506 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001507 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1508 ContainingType, Virtuality, VirtualIndex,
1509 Flags, IsOptimized, getFunction("bar"),
1510 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001511 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001512 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1513 ContainingType, Virtuality, VirtualIndex,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001514 Flags, IsOptimized, Function, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001515 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001516 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001517 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1518 ContainingType, Virtuality, VirtualIndex,
1519 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001520 getSubprogram(), Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001521 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001522 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1523 ContainingType, Virtuality, VirtualIndex,
1524 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001525 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001526
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001527 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001528 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001529}
1530
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001531TEST_F(DISubprogramTest, replaceFunction) {
1532 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001533 StringRef Name = "name";
1534 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001535 DIFile *File = getFile();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001536 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001537 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001538 bool IsLocalToUnit = false;
1539 bool IsDefinition = true;
1540 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001541 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001542 unsigned Virtuality = 4;
1543 unsigned VirtualIndex = 5;
1544 unsigned Flags = 6;
1545 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001546 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001547 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001548 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001549
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001550 auto *N = DISubprogram::get(
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001551 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1552 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1553 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1554
1555 EXPECT_EQ(nullptr, N->getFunction());
1556
1557 std::unique_ptr<Function> F(
1558 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1559 GlobalValue::ExternalLinkage));
1560 N->replaceFunction(F.get());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001561 EXPECT_EQ(F.get(), N->getFunction());
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001562
1563 N->replaceFunction(nullptr);
1564 EXPECT_EQ(nullptr, N->getFunction());
1565}
1566
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001567typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001568
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001569TEST_F(DILexicalBlockTest, get) {
1570 DILocalScope *Scope = getSubprogram();
1571 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001572 unsigned Line = 5;
1573 unsigned Column = 8;
1574
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001575 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001576
1577 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1578 EXPECT_EQ(Scope, N->getScope());
1579 EXPECT_EQ(File, N->getFile());
1580 EXPECT_EQ(Line, N->getLine());
1581 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001582 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001583
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001584 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001585 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1586 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1587 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1588 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001589
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001590 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001591 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001592}
1593
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001594typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001595
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001596TEST_F(DILexicalBlockFileTest, get) {
1597 DILocalScope *Scope = getSubprogram();
1598 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001599 unsigned Discriminator = 5;
1600
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001601 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001602
1603 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1604 EXPECT_EQ(Scope, N->getScope());
1605 EXPECT_EQ(File, N->getFile());
1606 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001607 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001608
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001609 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001610 Discriminator));
1611 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001612 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001613 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001614 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001615
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001616 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001617 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001618}
1619
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001620typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001621
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001622TEST_F(DINamespaceTest, get) {
1623 DIScope *Scope = getFile();
1624 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001625 StringRef Name = "namespace";
1626 unsigned Line = 5;
1627
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001628 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001629
1630 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1631 EXPECT_EQ(Scope, N->getScope());
1632 EXPECT_EQ(File, N->getFile());
1633 EXPECT_EQ(Name, N->getName());
1634 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001635 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001636
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001637 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1638 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1639 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1640 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001641
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001642 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001643 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001644}
1645
Adrian Prantlab1243f2015-06-29 23:03:47 +00001646typedef MetadataTest DIModuleTest;
1647
1648TEST_F(DIModuleTest, get) {
1649 DIScope *Scope = getFile();
1650 StringRef Name = "module";
1651 StringRef ConfigMacro = "-DNDEBUG";
1652 StringRef Includes = "-I.";
1653 StringRef Sysroot = "/";
1654
1655 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1656
1657 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1658 EXPECT_EQ(Scope, N->getScope());
1659 EXPECT_EQ(Name, N->getName());
1660 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1661 EXPECT_EQ(Includes, N->getIncludePath());
1662 EXPECT_EQ(Sysroot, N->getISysRoot());
1663 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1664 ConfigMacro, Includes, Sysroot));
1665 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1666 ConfigMacro, Includes, Sysroot));
1667 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1668 ConfigMacro, Includes, Sysroot));
1669 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1670 "other", Includes, Sysroot));
1671 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1672 ConfigMacro, "other", Sysroot));
1673 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1674 ConfigMacro, Includes, "other"));
1675
1676 TempDIModule Temp = N->clone();
1677 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1678}
1679
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001680typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001681
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001682TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001683 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001684 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001685
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001686 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001687
1688 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001689 EXPECT_EQ(Name, N->getName());
1690 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001691 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001692
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001693 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001694 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001695 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001696
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001697 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001698 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001699}
1700
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001701typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001702
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001703TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001704 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001705 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001706 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001707 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001708
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001709 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001710 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001711 EXPECT_EQ(Name, N->getName());
1712 EXPECT_EQ(Type, N->getType());
1713 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001714 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001715
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001716 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001717 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1718 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001719 EXPECT_NE(N,
1720 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1721 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001722 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001723 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001724 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001725
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001726 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001727 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001728}
1729
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001730typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001731
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001732TEST_F(DIGlobalVariableTest, get) {
1733 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001734 StringRef Name = "name";
1735 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001736 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001737 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001738 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001739 bool IsLocalToUnit = false;
1740 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001741 Constant *Variable = getConstant();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001742 DIDerivedType *StaticDataMemberDeclaration =
1743 cast<DIDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001744
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001745 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001746 Type, IsLocalToUnit, IsDefinition, Variable,
1747 StaticDataMemberDeclaration);
1748 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1749 EXPECT_EQ(Scope, N->getScope());
1750 EXPECT_EQ(Name, N->getName());
1751 EXPECT_EQ(LinkageName, N->getLinkageName());
1752 EXPECT_EQ(File, N->getFile());
1753 EXPECT_EQ(Line, N->getLine());
1754 EXPECT_EQ(Type, N->getType());
1755 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1756 EXPECT_EQ(IsDefinition, N->isDefinition());
1757 EXPECT_EQ(Variable, N->getVariable());
1758 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001759 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001760 Line, Type, IsLocalToUnit, IsDefinition,
1761 Variable, StaticDataMemberDeclaration));
1762
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001763 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001764 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001765 File, Line, Type, IsLocalToUnit, IsDefinition,
1766 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001767 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001768 Line, Type, IsLocalToUnit, IsDefinition,
1769 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001770 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001771 Type, IsLocalToUnit, IsDefinition,
1772 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001773 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001774 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001775 Line, Type, IsLocalToUnit, IsDefinition,
1776 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001777 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001778 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001779 Line + 1, Type, IsLocalToUnit, IsDefinition,
1780 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001781 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001782 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001783 getDerivedType(), IsLocalToUnit, IsDefinition,
1784 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001785 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001786 Line, Type, !IsLocalToUnit, IsDefinition,
1787 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001788 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001789 Line, Type, IsLocalToUnit, !IsDefinition,
1790 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001791 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001792 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001793 Type, IsLocalToUnit, IsDefinition,
1794 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001795 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001796 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001797 Type, IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798 cast<DIDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001799
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001800 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001801 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001802}
1803
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001804typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001805
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001806TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001807 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001808 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001809 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001810 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001811 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001812 unsigned Arg = 6;
1813 unsigned Flags = 7;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001814
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001815 auto *N =
1816 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags);
1817 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001818 EXPECT_EQ(Scope, N->getScope());
1819 EXPECT_EQ(Name, N->getName());
1820 EXPECT_EQ(File, N->getFile());
1821 EXPECT_EQ(Line, N->getLine());
1822 EXPECT_EQ(Type, N->getType());
1823 EXPECT_EQ(Arg, N->getArg());
1824 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001825 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1826 Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001827
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001828 EXPECT_FALSE(
1829 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags)
1830 ->isParameter());
1831 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001832 Type, Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001833 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
1834 Arg, Flags));
1835 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
1836 Arg, Flags));
1837 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
1838 Arg, Flags));
1839 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001840 getDerivedType(), Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001841 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001842 Arg + 1, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001843 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1844 ~Flags));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001845
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001846 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001847 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001848}
1849
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001850TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001851 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1852 0, nullptr, 255, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001853 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001854 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1855 0, nullptr, 256, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001856 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001857 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1858 0, nullptr, 257, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001859 ->getArg());
1860 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001861 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1862 0, nullptr, Max, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001863 ->getArg());
1864}
1865
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001866typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001867
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001868TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001869 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001870 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001871 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001872 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001873
1874 EXPECT_EQ(5u, N->getNumElements());
1875 EXPECT_EQ(2u, N->getElement(0));
1876 EXPECT_EQ(6u, N->getElement(1));
1877 EXPECT_EQ(9u, N->getElement(2));
1878 EXPECT_EQ(78u, N->getElement(3));
1879 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001880
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001881 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001882 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001883}
1884
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001885TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001886#define EXPECT_VALID(...) \
1887 do { \
1888 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001889 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001890 } while (false)
1891#define EXPECT_INVALID(...) \
1892 do { \
1893 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001894 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001895 } while (false)
1896
1897 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001898 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001899
1900 // Valid constructions.
1901 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1902 EXPECT_VALID(dwarf::DW_OP_deref);
1903 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1904 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1905 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1906 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1907 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1908
1909 // Invalid constructions.
1910 EXPECT_INVALID(~0u);
1911 EXPECT_INVALID(dwarf::DW_OP_plus);
1912 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1913 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1914 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1915 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1916
1917#undef EXPECT_VALID
1918#undef EXPECT_INVALID
1919}
1920
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001921typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001922
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001923TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001924 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001925 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001926 unsigned Line = 5;
1927 StringRef GetterName = "getter";
1928 StringRef SetterName = "setter";
1929 unsigned Attributes = 7;
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001930 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001931
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001932 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001933 SetterName, Attributes, Type);
1934
1935 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1936 EXPECT_EQ(Name, N->getName());
1937 EXPECT_EQ(File, N->getFile());
1938 EXPECT_EQ(Line, N->getLine());
1939 EXPECT_EQ(GetterName, N->getGetterName());
1940 EXPECT_EQ(SetterName, N->getSetterName());
1941 EXPECT_EQ(Attributes, N->getAttributes());
1942 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001943 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001944 SetterName, Attributes, Type));
1945
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001946 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001947 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001948 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001949 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001950 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001951 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001952 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001953 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001954 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001955 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001956 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001957 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001958 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001959 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001960 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001961
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001962 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001963 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001964}
1965
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001966typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001967
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001968TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001969 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001970 DIScope *Scope = getSubprogram();
1971 DINodeRef Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001972 unsigned Line = 5;
1973 StringRef Name = "name";
1974
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001975 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001976
1977 EXPECT_EQ(Tag, N->getTag());
1978 EXPECT_EQ(Scope, N->getScope());
1979 EXPECT_EQ(Entity, N->getEntity());
1980 EXPECT_EQ(Line, N->getLine());
1981 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001982 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001983
1984 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001985 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001986 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001987 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001988 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001989 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001990 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001991 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001992 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001993 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001994 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001995
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001996 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001997 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001998}
1999
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002000typedef MetadataTest MetadataAsValueTest;
2001
2002TEST_F(MetadataAsValueTest, MDNode) {
2003 MDNode *N = MDNode::get(Context, None);
2004 auto *V = MetadataAsValue::get(Context, N);
2005 EXPECT_TRUE(V->getType()->isMetadataTy());
2006 EXPECT_EQ(N, V->getMetadata());
2007
2008 auto *V2 = MetadataAsValue::get(Context, N);
2009 EXPECT_EQ(V, V2);
2010}
2011
2012TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2013 MDNode *N = MDNode::get(Context, None);
2014 Metadata *Ops[] = {N};
2015 MDNode *N2 = MDNode::get(Context, Ops);
2016 auto *V = MetadataAsValue::get(Context, N2);
2017 EXPECT_TRUE(V->getType()->isMetadataTy());
2018 EXPECT_EQ(N2, V->getMetadata());
2019
2020 auto *V2 = MetadataAsValue::get(Context, N2);
2021 EXPECT_EQ(V, V2);
2022
2023 auto *V3 = MetadataAsValue::get(Context, N);
2024 EXPECT_TRUE(V3->getType()->isMetadataTy());
2025 EXPECT_NE(V, V3);
2026 EXPECT_EQ(N, V3->getMetadata());
2027}
2028
2029TEST_F(MetadataAsValueTest, MDNodeConstant) {
2030 auto *C = ConstantInt::getTrue(Context);
2031 auto *MD = ConstantAsMetadata::get(C);
2032 Metadata *Ops[] = {MD};
2033 auto *N = MDNode::get(Context, Ops);
2034
2035 auto *V = MetadataAsValue::get(Context, MD);
2036 EXPECT_TRUE(V->getType()->isMetadataTy());
2037 EXPECT_EQ(MD, V->getMetadata());
2038
2039 auto *V2 = MetadataAsValue::get(Context, N);
2040 EXPECT_EQ(MD, V2->getMetadata());
2041 EXPECT_EQ(V, V2);
2042}
2043
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002044typedef MetadataTest ValueAsMetadataTest;
2045
2046TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2047 Type *Ty = Type::getInt1PtrTy(Context);
2048 std::unique_ptr<GlobalVariable> GV0(
2049 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2050 auto *MD = ValueAsMetadata::get(GV0.get());
2051 EXPECT_TRUE(MD->getValue() == GV0.get());
2052 ASSERT_TRUE(GV0->use_empty());
2053
2054 std::unique_ptr<GlobalVariable> GV1(
2055 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2056 GV0->replaceAllUsesWith(GV1.get());
2057 EXPECT_TRUE(MD->getValue() == GV1.get());
2058}
2059
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002060TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2061 // Create a constant.
2062 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2063 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2064
2065 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002066 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002067
2068 // 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 +00002069 Metadata *Ops1[] = {CI, CI, Temp.get()};
2070 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002071
2072 auto *N1 = MDTuple::get(Context, Ops1);
2073 auto *N2 = MDTuple::get(Context, Ops2);
2074 ASSERT_NE(N1, N2);
2075
2076 // Tell metadata that the constant is getting deleted.
2077 //
2078 // After this, N1 will be invalid, so don't touch it.
2079 ValueAsMetadata::handleDeletion(CI->getValue());
2080 EXPECT_EQ(nullptr, N2->getOperand(0));
2081 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002082 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002083
2084 // Clean up Temp for teardown.
2085 Temp->replaceAllUsesWith(nullptr);
2086}
2087
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002088typedef MetadataTest TrackingMDRefTest;
2089
2090TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2091 Type *Ty = Type::getInt1PtrTy(Context);
2092 std::unique_ptr<GlobalVariable> GV0(
2093 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2094 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2095 EXPECT_TRUE(MD->getValue() == GV0.get());
2096 ASSERT_TRUE(GV0->use_empty());
2097
2098 std::unique_ptr<GlobalVariable> GV1(
2099 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2100 GV0->replaceAllUsesWith(GV1.get());
2101 EXPECT_TRUE(MD->getValue() == GV1.get());
2102
2103 // Reset it, so we don't inadvertently test deletion.
2104 MD.reset();
2105}
2106
2107TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2108 Type *Ty = Type::getInt1PtrTy(Context);
2109 std::unique_ptr<GlobalVariable> GV(
2110 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2111 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2112 EXPECT_TRUE(MD->getValue() == GV.get());
2113 ASSERT_TRUE(GV->use_empty());
2114
2115 GV.reset();
2116 EXPECT_TRUE(!MD);
2117}
2118
Devang Patel0924b332009-07-30 00:03:41 +00002119TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002120 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002121 ConstantAsMetadata *C =
2122 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2123 ConstantAsMetadata *C2 =
2124 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002125
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002126 Metadata *const V = C;
2127 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002128 MDNode *n = MDNode::get(Context, V);
2129 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002130
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002131 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002132 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002133 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2134 NMD->addOperand(n);
2135 NMD->addOperand(n2);
2136
Chris Lattnerbe354a62009-08-23 04:47:35 +00002137 std::string Str;
2138 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002139 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002140 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002141 oss.str().c_str());
2142}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002143
2144typedef MetadataTest FunctionAttachmentTest;
2145TEST_F(FunctionAttachmentTest, setMetadata) {
2146 Function *F = getFunction("foo");
2147 ASSERT_FALSE(F->hasMetadata());
2148 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2149 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2150 EXPECT_EQ(nullptr, F->getMetadata("other"));
2151
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002152 DISubprogram *SP1 = getSubprogram();
2153 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002154 ASSERT_NE(SP1, SP2);
2155
2156 F->setMetadata("dbg", SP1);
2157 EXPECT_TRUE(F->hasMetadata());
2158 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2159 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2160 EXPECT_EQ(nullptr, F->getMetadata("other"));
2161
2162 F->setMetadata(LLVMContext::MD_dbg, SP2);
2163 EXPECT_TRUE(F->hasMetadata());
2164 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2165 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2166 EXPECT_EQ(nullptr, F->getMetadata("other"));
2167
2168 F->setMetadata("dbg", nullptr);
2169 EXPECT_FALSE(F->hasMetadata());
2170 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2171 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2172 EXPECT_EQ(nullptr, F->getMetadata("other"));
2173
2174 MDTuple *T1 = getTuple();
2175 MDTuple *T2 = getTuple();
2176 ASSERT_NE(T1, T2);
2177
2178 F->setMetadata("other1", T1);
2179 F->setMetadata("other2", T2);
2180 EXPECT_TRUE(F->hasMetadata());
2181 EXPECT_EQ(T1, F->getMetadata("other1"));
2182 EXPECT_EQ(T2, F->getMetadata("other2"));
2183 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2184
2185 F->setMetadata("other1", T2);
2186 F->setMetadata("other2", T1);
2187 EXPECT_EQ(T2, F->getMetadata("other1"));
2188 EXPECT_EQ(T1, F->getMetadata("other2"));
2189
2190 F->setMetadata("other1", nullptr);
2191 F->setMetadata("other2", nullptr);
2192 EXPECT_FALSE(F->hasMetadata());
2193 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2194 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2195}
2196
2197TEST_F(FunctionAttachmentTest, getAll) {
2198 Function *F = getFunction("foo");
2199
2200 MDTuple *T1 = getTuple();
2201 MDTuple *T2 = getTuple();
2202 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002203 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002204
2205 F->setMetadata("other1", T2);
2206 F->setMetadata(LLVMContext::MD_dbg, SP);
2207 F->setMetadata("other2", T1);
2208 F->setMetadata(LLVMContext::MD_prof, P);
2209 F->setMetadata("other2", T2);
2210 F->setMetadata("other1", T1);
2211
2212 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2213 F->getAllMetadata(MDs);
2214 ASSERT_EQ(4u, MDs.size());
2215 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2216 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2217 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2218 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2219 EXPECT_EQ(SP, MDs[0].second);
2220 EXPECT_EQ(P, MDs[1].second);
2221 EXPECT_EQ(T1, MDs[2].second);
2222 EXPECT_EQ(T2, MDs[3].second);
2223}
2224
2225TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2226 Function *F = getFunction("foo");
2227
2228 MDTuple *T1 = getTuple();
2229 MDTuple *T2 = getTuple();
2230 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002231 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002232
2233 F->setMetadata("other1", T1);
2234 F->setMetadata(LLVMContext::MD_dbg, SP);
2235 F->setMetadata("other2", T2);
2236 F->setMetadata(LLVMContext::MD_prof, P);
2237
2238 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2239 F->dropUnknownMetadata(Known);
2240
2241 EXPECT_EQ(T2, F->getMetadata("other2"));
2242 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2243 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2244 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2245
2246 F->setMetadata("other2", nullptr);
2247 F->setMetadata(LLVMContext::MD_prof, nullptr);
2248 EXPECT_FALSE(F->hasMetadata());
2249}
2250
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002251TEST_F(FunctionAttachmentTest, Verifier) {
2252 Function *F = getFunction("foo");
2253 F->setMetadata("attach", getTuple());
2254
2255 // Confirm this has no body.
2256 ASSERT_TRUE(F->empty());
2257
2258 // Functions without a body cannot have metadata attachments (they also can't
2259 // be verified directly, so check that the module fails to verify).
2260 EXPECT_TRUE(verifyModule(*F->getParent()));
2261
2262 // Functions with a body can.
2263 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2264 EXPECT_FALSE(verifyModule(*F->getParent()));
2265 EXPECT_FALSE(verifyFunction(*F));
2266}
2267
Diego Novillo2567f3d2015-05-13 15:13:45 +00002268TEST_F(FunctionAttachmentTest, EntryCount) {
2269 Function *F = getFunction("foo");
2270 EXPECT_FALSE(F->getEntryCount().hasValue());
2271 F->setEntryCount(12304);
2272 EXPECT_TRUE(F->getEntryCount().hasValue());
2273 EXPECT_EQ(12304u, *F->getEntryCount());
2274}
2275
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002276TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2277 Function *F = getFunction("foo");
2278 DISubprogram *SP = getSubprogram();
2279 F->setSubprogram(SP);
2280
2281 // Note that the static_cast confirms that F->getSubprogram() actually
2282 // returns an DISubprogram.
2283 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2284 EXPECT_EQ(SP, F->getMetadata("dbg"));
2285 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2286}
2287
Nick Lewycky49f89192009-04-04 07:22:01 +00002288}