blob: c1da06814caa11b6c763e2dca98ea5f7520daaf3 [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());
1263 EXPECT_EQ(nullptr, N->getBaseType());
1264 EXPECT_EQ(nullptr, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001265 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001266 EXPECT_EQ("", N->getIdentifier());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001267}
1268
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001269typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001270
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001271TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001272 StringRef Filename = "file";
1273 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001274 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001275
1276 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1277 EXPECT_EQ(Filename, N->getFilename());
1278 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001279 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001280
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001281 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1282 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001283
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001284 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001285 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001286}
1287
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001288TEST_F(DIFileTest, ScopeGetFile) {
1289 // Ensure that DIScope::getFile() returns itself.
1290 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001291 EXPECT_EQ(N, N->getFile());
1292}
1293
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001294typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001295
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001296TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001297 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001298 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001299 StringRef Producer = "some producer";
1300 bool IsOptimized = false;
1301 StringRef Flags = "flag after flag";
1302 unsigned RuntimeVersion = 2;
1303 StringRef SplitDebugFilename = "another/file";
1304 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001305 MDTuple *EnumTypes = getTuple();
1306 MDTuple *RetainedTypes = getTuple();
1307 MDTuple *Subprograms = getTuple();
1308 MDTuple *GlobalVariables = getTuple();
1309 MDTuple *ImportedEntities = getTuple();
Adrian Prantl1f599f92015-05-21 20:37:30 +00001310 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001311 auto *N = DICompileUnit::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001312 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1313 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001314 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, DWOId);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001315
1316 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1317 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1318 EXPECT_EQ(File, N->getFile());
1319 EXPECT_EQ(Producer, N->getProducer());
1320 EXPECT_EQ(IsOptimized, N->isOptimized());
1321 EXPECT_EQ(Flags, N->getFlags());
1322 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1323 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1324 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001325 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1326 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1327 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1328 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1329 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001330 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001331 EXPECT_EQ(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001332 IsOptimized, Flags, RuntimeVersion,
1333 SplitDebugFilename, EmissionKind, EnumTypes,
1334 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001335 ImportedEntities, DWOId));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001336
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001337 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage + 1, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001338 IsOptimized, Flags, RuntimeVersion,
1339 SplitDebugFilename, EmissionKind, EnumTypes,
1340 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001341 ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001342 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, getFile(), Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001343 IsOptimized, Flags, RuntimeVersion,
1344 SplitDebugFilename, EmissionKind, EnumTypes,
1345 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001346 ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001347 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001348 IsOptimized, Flags, RuntimeVersion,
1349 SplitDebugFilename, EmissionKind, EnumTypes,
1350 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001351 ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001352 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001353 !IsOptimized, Flags, RuntimeVersion,
1354 SplitDebugFilename, EmissionKind, EnumTypes,
1355 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001356 ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001357 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001358 IsOptimized, "other", RuntimeVersion,
1359 SplitDebugFilename, EmissionKind, EnumTypes,
1360 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001361 ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001362 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001363 IsOptimized, Flags, RuntimeVersion + 1,
1364 SplitDebugFilename, EmissionKind, EnumTypes,
1365 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001366 ImportedEntities, DWOId));
1367 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1368 IsOptimized, Flags, RuntimeVersion, "other",
1369 EmissionKind, EnumTypes, RetainedTypes,
1370 Subprograms, GlobalVariables,
1371 ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001372 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001373 IsOptimized, Flags, RuntimeVersion,
1374 SplitDebugFilename, EmissionKind + 1,
1375 EnumTypes, RetainedTypes, Subprograms,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001376 GlobalVariables, ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001377 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001378 IsOptimized, Flags, RuntimeVersion,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001379 SplitDebugFilename, EmissionKind, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001380 RetainedTypes, Subprograms, GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001381 ImportedEntities, DWOId));
1382 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1383 IsOptimized, Flags, RuntimeVersion,
1384 SplitDebugFilename, EmissionKind, EnumTypes,
1385 getTuple(), Subprograms, GlobalVariables,
1386 ImportedEntities, DWOId));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001387 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001388 IsOptimized, Flags, RuntimeVersion,
1389 SplitDebugFilename, EmissionKind, EnumTypes,
1390 RetainedTypes, getTuple(), GlobalVariables,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001391 ImportedEntities, DWOId));
1392 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1393 IsOptimized, Flags, RuntimeVersion,
1394 SplitDebugFilename, EmissionKind, EnumTypes,
1395 RetainedTypes, Subprograms, getTuple(),
1396 ImportedEntities, DWOId));
1397 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1398 IsOptimized, Flags, RuntimeVersion,
1399 SplitDebugFilename, EmissionKind, EnumTypes,
1400 RetainedTypes, Subprograms, GlobalVariables,
1401 getTuple(), DWOId));
1402 EXPECT_NE(N, DICompileUnit::get(Context, SourceLanguage, File, Producer,
1403 IsOptimized, Flags, RuntimeVersion,
1404 SplitDebugFilename, EmissionKind, EnumTypes,
1405 RetainedTypes, Subprograms, GlobalVariables,
1406 ImportedEntities, DWOId + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001407
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001408 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001409 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001410}
1411
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001412TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001413 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001414 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001415 StringRef Producer = "some producer";
1416 bool IsOptimized = false;
1417 StringRef Flags = "flag after flag";
1418 unsigned RuntimeVersion = 2;
1419 StringRef SplitDebugFilename = "another/file";
1420 unsigned EmissionKind = 3;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001421 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1422 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1423 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001424 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001425 auto *N = DICompileUnit::get(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001426 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1427 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl1f599f92015-05-21 20:37:30 +00001428 RetainedTypes, nullptr, nullptr, ImportedEntities, DWOId);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001429
1430 auto *Subprograms = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001431 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001432 N->replaceSubprograms(Subprograms);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001433 EXPECT_EQ(Subprograms, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001434 N->replaceSubprograms(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001435 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001436
1437 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001438 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001439 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001440 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001441 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001442 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001443}
1444
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001445typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001446
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001447TEST_F(DISubprogramTest, get) {
1448 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001449 StringRef Name = "name";
1450 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001451 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001452 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001453 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001454 bool IsLocalToUnit = false;
1455 bool IsDefinition = true;
1456 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001457 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001458 unsigned Virtuality = 4;
1459 unsigned VirtualIndex = 5;
1460 unsigned Flags = 6;
1461 bool IsOptimized = false;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001462 llvm::Function *Function = getFunction("foo");
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001463 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001464 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001465 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001466
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001467 auto *N = DISubprogram::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001468 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1469 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1470 IsOptimized, Function, TemplateParams, Declaration, Variables);
1471
1472 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1473 EXPECT_EQ(Scope, N->getScope());
1474 EXPECT_EQ(Name, N->getName());
1475 EXPECT_EQ(LinkageName, N->getLinkageName());
1476 EXPECT_EQ(File, N->getFile());
1477 EXPECT_EQ(Line, N->getLine());
1478 EXPECT_EQ(Type, N->getType());
1479 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1480 EXPECT_EQ(IsDefinition, N->isDefinition());
1481 EXPECT_EQ(ScopeLine, N->getScopeLine());
1482 EXPECT_EQ(ContainingType, N->getContainingType());
1483 EXPECT_EQ(Virtuality, N->getVirtuality());
1484 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1485 EXPECT_EQ(Flags, N->getFlags());
1486 EXPECT_EQ(IsOptimized, N->isOptimized());
1487 EXPECT_EQ(Function, N->getFunction());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001488 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001489 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001490 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001491 EXPECT_EQ(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,
1494 Flags, IsOptimized, Function, TemplateParams,
1495 Declaration, Variables));
1496
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001497 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001498 File, Line, Type, IsLocalToUnit, IsDefinition,
1499 ScopeLine, ContainingType, Virtuality,
1500 VirtualIndex, Flags, IsOptimized, Function,
1501 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001502 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001503 Line, Type, IsLocalToUnit, IsDefinition,
1504 ScopeLine, ContainingType, Virtuality,
1505 VirtualIndex, Flags, IsOptimized, Function,
1506 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001507 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001508 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1509 ContainingType, Virtuality, VirtualIndex,
1510 Flags, IsOptimized, Function, TemplateParams,
1511 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001512 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001513 Line, Type, IsLocalToUnit, IsDefinition,
1514 ScopeLine, ContainingType, Virtuality,
1515 VirtualIndex, Flags, IsOptimized, Function,
1516 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001517 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001518 Line + 1, Type, IsLocalToUnit, IsDefinition,
1519 ScopeLine, ContainingType, Virtuality,
1520 VirtualIndex, Flags, IsOptimized, Function,
1521 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001522 EXPECT_NE(N, DISubprogram::get(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001523 Context, Scope, Name, LinkageName, File, Line,
1524 getSubroutineType(), IsLocalToUnit, IsDefinition, ScopeLine,
1525 ContainingType, Virtuality, VirtualIndex, Flags, IsOptimized,
1526 Function, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001527 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001528 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1529 ContainingType, Virtuality, VirtualIndex,
1530 Flags, IsOptimized, Function, TemplateParams,
1531 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001532 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001533 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1534 ContainingType, Virtuality, VirtualIndex,
1535 Flags, IsOptimized, Function, TemplateParams,
1536 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001537 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001538 Type, IsLocalToUnit, IsDefinition,
1539 ScopeLine + 1, ContainingType, Virtuality,
1540 VirtualIndex, Flags, IsOptimized, Function,
1541 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001542 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001543 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001544 getCompositeType(), Virtuality, VirtualIndex,
1545 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001546 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001547 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001548 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1549 ContainingType, Virtuality + 1, VirtualIndex,
1550 Flags, IsOptimized, Function, TemplateParams,
1551 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001552 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001553 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1554 ContainingType, Virtuality, VirtualIndex + 1,
1555 Flags, IsOptimized, Function, TemplateParams,
1556 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001557 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001558 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1559 ContainingType, Virtuality, VirtualIndex,
1560 ~Flags, IsOptimized, Function, TemplateParams,
1561 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001562 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001563 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1564 ContainingType, Virtuality, VirtualIndex,
1565 Flags, !IsOptimized, Function, TemplateParams,
1566 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001567 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001568 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1569 ContainingType, Virtuality, VirtualIndex,
1570 Flags, IsOptimized, getFunction("bar"),
1571 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001572 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001573 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1574 ContainingType, Virtuality, VirtualIndex,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001575 Flags, IsOptimized, Function, getTuple(),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001576 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001577 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001578 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1579 ContainingType, Virtuality, VirtualIndex,
1580 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001581 getSubprogram(), Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001582 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001583 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1584 ContainingType, Virtuality, VirtualIndex,
1585 Flags, IsOptimized, Function, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001586 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001587
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001588 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001589 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001590}
1591
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001592TEST_F(DISubprogramTest, replaceFunction) {
1593 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001594 StringRef Name = "name";
1595 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001596 DIFile *File = getFile();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001597 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001598 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001599 bool IsLocalToUnit = false;
1600 bool IsDefinition = true;
1601 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001602 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001603 unsigned Virtuality = 4;
1604 unsigned VirtualIndex = 5;
1605 unsigned Flags = 6;
1606 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001607 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001608 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001609 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001610
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001611 auto *N = DISubprogram::get(
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001612 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1613 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1614 IsOptimized, nullptr, TemplateParams, Declaration, Variables);
1615
1616 EXPECT_EQ(nullptr, N->getFunction());
1617
1618 std::unique_ptr<Function> F(
1619 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
1620 GlobalValue::ExternalLinkage));
1621 N->replaceFunction(F.get());
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001622 EXPECT_EQ(F.get(), N->getFunction());
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +00001623
1624 N->replaceFunction(nullptr);
1625 EXPECT_EQ(nullptr, N->getFunction());
1626}
1627
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001628typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001629
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001630TEST_F(DILexicalBlockTest, get) {
1631 DILocalScope *Scope = getSubprogram();
1632 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001633 unsigned Line = 5;
1634 unsigned Column = 8;
1635
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001636 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001637
1638 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1639 EXPECT_EQ(Scope, N->getScope());
1640 EXPECT_EQ(File, N->getFile());
1641 EXPECT_EQ(Line, N->getLine());
1642 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001643 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001644
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001645 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001646 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1647 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1648 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1649 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001650
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001651 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001652 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001653}
1654
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001655typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001656
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001657TEST_F(DILexicalBlockFileTest, get) {
1658 DILocalScope *Scope = getSubprogram();
1659 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001660 unsigned Discriminator = 5;
1661
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001662 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001663
1664 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1665 EXPECT_EQ(Scope, N->getScope());
1666 EXPECT_EQ(File, N->getFile());
1667 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001668 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001669
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001670 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001671 Discriminator));
1672 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001673 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001674 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001675 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001676
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001677 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001678 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001679}
1680
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001681typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001682
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001683TEST_F(DINamespaceTest, get) {
1684 DIScope *Scope = getFile();
1685 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001686 StringRef Name = "namespace";
1687 unsigned Line = 5;
1688
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001689 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001690
1691 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1692 EXPECT_EQ(Scope, N->getScope());
1693 EXPECT_EQ(File, N->getFile());
1694 EXPECT_EQ(Name, N->getName());
1695 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001696 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001697
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001698 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1699 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1700 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1701 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001702
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001703 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001704 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001705}
1706
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001707typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001708
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001709TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001710 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001711 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001712
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001713 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001714
1715 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001716 EXPECT_EQ(Name, N->getName());
1717 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001718 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001719
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001720 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001721 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001722 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001723
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001724 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001725 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001726}
1727
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001728typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001729
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001730TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001731 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001732 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001733 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001734 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001735
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001736 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001737 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001738 EXPECT_EQ(Name, N->getName());
1739 EXPECT_EQ(Type, N->getType());
1740 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001741 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001742
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001743 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001744 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1745 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001746 EXPECT_NE(N,
1747 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1748 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001749 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001750 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001751 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001752
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001753 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001754 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001755}
1756
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001757typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001758
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001759TEST_F(DIGlobalVariableTest, get) {
1760 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001761 StringRef Name = "name";
1762 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001763 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001764 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001765 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001766 bool IsLocalToUnit = false;
1767 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001768 Constant *Variable = getConstant();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001769 DIDerivedType *StaticDataMemberDeclaration =
1770 cast<DIDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001771
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001772 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001773 Type, IsLocalToUnit, IsDefinition, Variable,
1774 StaticDataMemberDeclaration);
1775 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1776 EXPECT_EQ(Scope, N->getScope());
1777 EXPECT_EQ(Name, N->getName());
1778 EXPECT_EQ(LinkageName, N->getLinkageName());
1779 EXPECT_EQ(File, N->getFile());
1780 EXPECT_EQ(Line, N->getLine());
1781 EXPECT_EQ(Type, N->getType());
1782 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1783 EXPECT_EQ(IsDefinition, N->isDefinition());
1784 EXPECT_EQ(Variable, N->getVariable());
1785 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001786 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001787 Line, Type, IsLocalToUnit, IsDefinition,
1788 Variable, StaticDataMemberDeclaration));
1789
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001790 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001791 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001792 File, Line, Type, IsLocalToUnit, IsDefinition,
1793 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001794 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001795 Line, Type, IsLocalToUnit, IsDefinition,
1796 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001797 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001798 Type, IsLocalToUnit, IsDefinition,
1799 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001800 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001801 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001802 Line, Type, IsLocalToUnit, IsDefinition,
1803 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001804 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001805 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001806 Line + 1, Type, IsLocalToUnit, IsDefinition,
1807 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001808 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001809 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001810 getDerivedType(), IsLocalToUnit, IsDefinition,
1811 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001812 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001813 Line, Type, !IsLocalToUnit, IsDefinition,
1814 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001815 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001816 Line, Type, IsLocalToUnit, !IsDefinition,
1817 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001818 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001819 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001820 Type, IsLocalToUnit, IsDefinition,
1821 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001822 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001823 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001824 Type, IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001825 cast<DIDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001826
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001827 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001828 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001829}
1830
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001831typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001832
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001833TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001834 unsigned Tag = dwarf::DW_TAG_arg_variable;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001835 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001836 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001837 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001838 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001839 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001840 unsigned Arg = 6;
1841 unsigned Flags = 7;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001842
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001843 auto *N = DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001844 Arg, Flags);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001845 EXPECT_EQ(Tag, N->getTag());
1846 EXPECT_EQ(Scope, N->getScope());
1847 EXPECT_EQ(Name, N->getName());
1848 EXPECT_EQ(File, N->getFile());
1849 EXPECT_EQ(Line, N->getLine());
1850 EXPECT_EQ(Type, N->getType());
1851 EXPECT_EQ(Arg, N->getArg());
1852 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001853 EXPECT_EQ(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001854 Arg, Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001855
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001856 EXPECT_NE(N, DILocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001857 Name, File, Line, Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001858 EXPECT_NE(N, DILocalVariable::get(Context, Tag, getSubprogram(), Name, File,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001859 Line, Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001860 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, "other", File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001861 Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001862 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, getFile(), Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001863 Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001864 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001865 Type, Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001866 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001867 getDerivedType(), Arg, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001868 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001869 Arg + 1, Flags));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001870 EXPECT_NE(N, DILocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001871 Arg, ~Flags));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001872
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001873 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001874 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001875}
1876
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001877TEST_F(DILocalVariableTest, getArg256) {
1878 EXPECT_EQ(255u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001879 getSubprogram(), "", getFile(), 0,
1880 nullptr, 255, 0)
1881 ->getArg());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001882 EXPECT_EQ(256u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001883 getSubprogram(), "", getFile(), 0,
1884 nullptr, 256, 0)
1885 ->getArg());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001886 EXPECT_EQ(257u, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001887 getSubprogram(), "", getFile(), 0,
1888 nullptr, 257, 0)
1889 ->getArg());
1890 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001891 EXPECT_EQ(Max, DILocalVariable::get(Context, dwarf::DW_TAG_arg_variable,
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001892 getSubprogram(), "", getFile(), 0,
1893 nullptr, Max, 0)
1894 ->getArg());
1895}
1896
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001897typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001898
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001899TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001900 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001901 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001902 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001903 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001904
1905 EXPECT_EQ(5u, N->getNumElements());
1906 EXPECT_EQ(2u, N->getElement(0));
1907 EXPECT_EQ(6u, N->getElement(1));
1908 EXPECT_EQ(9u, N->getElement(2));
1909 EXPECT_EQ(78u, N->getElement(3));
1910 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001911
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001912 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001913 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001914}
1915
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001916TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001917#define EXPECT_VALID(...) \
1918 do { \
1919 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001920 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001921 } while (false)
1922#define EXPECT_INVALID(...) \
1923 do { \
1924 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001925 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001926 } while (false)
1927
1928 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001929 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001930
1931 // Valid constructions.
1932 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1933 EXPECT_VALID(dwarf::DW_OP_deref);
1934 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1935 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1936 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1937 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1938 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1939
1940 // Invalid constructions.
1941 EXPECT_INVALID(~0u);
1942 EXPECT_INVALID(dwarf::DW_OP_plus);
1943 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1944 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1945 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1946 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1947
1948#undef EXPECT_VALID
1949#undef EXPECT_INVALID
1950}
1951
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001952typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001953
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001954TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001955 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001956 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001957 unsigned Line = 5;
1958 StringRef GetterName = "getter";
1959 StringRef SetterName = "setter";
1960 unsigned Attributes = 7;
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001961 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001962
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001963 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001964 SetterName, Attributes, Type);
1965
1966 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1967 EXPECT_EQ(Name, N->getName());
1968 EXPECT_EQ(File, N->getFile());
1969 EXPECT_EQ(Line, N->getLine());
1970 EXPECT_EQ(GetterName, N->getGetterName());
1971 EXPECT_EQ(SetterName, N->getSetterName());
1972 EXPECT_EQ(Attributes, N->getAttributes());
1973 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001974 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001975 SetterName, Attributes, Type));
1976
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001977 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001978 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001979 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001980 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001981 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001982 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001983 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001984 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001985 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001986 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001987 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001988 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001989 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001990 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001991 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001992
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001993 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001994 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001995}
1996
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001997typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001998
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001999TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002000 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002001 DIScope *Scope = getSubprogram();
2002 DINodeRef Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002003 unsigned Line = 5;
2004 StringRef Name = "name";
2005
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002006 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002007
2008 EXPECT_EQ(Tag, N->getTag());
2009 EXPECT_EQ(Scope, N->getScope());
2010 EXPECT_EQ(Entity, N->getEntity());
2011 EXPECT_EQ(Line, N->getLine());
2012 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002013 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002014
2015 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002016 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002017 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002018 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002019 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002020 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002021 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002022 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002023 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002024 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002025 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002026
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002027 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002028 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002029}
2030
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002031typedef MetadataTest MetadataAsValueTest;
2032
2033TEST_F(MetadataAsValueTest, MDNode) {
2034 MDNode *N = MDNode::get(Context, None);
2035 auto *V = MetadataAsValue::get(Context, N);
2036 EXPECT_TRUE(V->getType()->isMetadataTy());
2037 EXPECT_EQ(N, V->getMetadata());
2038
2039 auto *V2 = MetadataAsValue::get(Context, N);
2040 EXPECT_EQ(V, V2);
2041}
2042
2043TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2044 MDNode *N = MDNode::get(Context, None);
2045 Metadata *Ops[] = {N};
2046 MDNode *N2 = MDNode::get(Context, Ops);
2047 auto *V = MetadataAsValue::get(Context, N2);
2048 EXPECT_TRUE(V->getType()->isMetadataTy());
2049 EXPECT_EQ(N2, V->getMetadata());
2050
2051 auto *V2 = MetadataAsValue::get(Context, N2);
2052 EXPECT_EQ(V, V2);
2053
2054 auto *V3 = MetadataAsValue::get(Context, N);
2055 EXPECT_TRUE(V3->getType()->isMetadataTy());
2056 EXPECT_NE(V, V3);
2057 EXPECT_EQ(N, V3->getMetadata());
2058}
2059
2060TEST_F(MetadataAsValueTest, MDNodeConstant) {
2061 auto *C = ConstantInt::getTrue(Context);
2062 auto *MD = ConstantAsMetadata::get(C);
2063 Metadata *Ops[] = {MD};
2064 auto *N = MDNode::get(Context, Ops);
2065
2066 auto *V = MetadataAsValue::get(Context, MD);
2067 EXPECT_TRUE(V->getType()->isMetadataTy());
2068 EXPECT_EQ(MD, V->getMetadata());
2069
2070 auto *V2 = MetadataAsValue::get(Context, N);
2071 EXPECT_EQ(MD, V2->getMetadata());
2072 EXPECT_EQ(V, V2);
2073}
2074
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002075typedef MetadataTest ValueAsMetadataTest;
2076
2077TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2078 Type *Ty = Type::getInt1PtrTy(Context);
2079 std::unique_ptr<GlobalVariable> GV0(
2080 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2081 auto *MD = ValueAsMetadata::get(GV0.get());
2082 EXPECT_TRUE(MD->getValue() == GV0.get());
2083 ASSERT_TRUE(GV0->use_empty());
2084
2085 std::unique_ptr<GlobalVariable> GV1(
2086 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2087 GV0->replaceAllUsesWith(GV1.get());
2088 EXPECT_TRUE(MD->getValue() == GV1.get());
2089}
2090
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002091TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2092 // Create a constant.
2093 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2094 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2095
2096 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002097 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002098
2099 // 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 +00002100 Metadata *Ops1[] = {CI, CI, Temp.get()};
2101 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002102
2103 auto *N1 = MDTuple::get(Context, Ops1);
2104 auto *N2 = MDTuple::get(Context, Ops2);
2105 ASSERT_NE(N1, N2);
2106
2107 // Tell metadata that the constant is getting deleted.
2108 //
2109 // After this, N1 will be invalid, so don't touch it.
2110 ValueAsMetadata::handleDeletion(CI->getValue());
2111 EXPECT_EQ(nullptr, N2->getOperand(0));
2112 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002113 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002114
2115 // Clean up Temp for teardown.
2116 Temp->replaceAllUsesWith(nullptr);
2117}
2118
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002119typedef MetadataTest TrackingMDRefTest;
2120
2121TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2122 Type *Ty = Type::getInt1PtrTy(Context);
2123 std::unique_ptr<GlobalVariable> GV0(
2124 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2125 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2126 EXPECT_TRUE(MD->getValue() == GV0.get());
2127 ASSERT_TRUE(GV0->use_empty());
2128
2129 std::unique_ptr<GlobalVariable> GV1(
2130 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2131 GV0->replaceAllUsesWith(GV1.get());
2132 EXPECT_TRUE(MD->getValue() == GV1.get());
2133
2134 // Reset it, so we don't inadvertently test deletion.
2135 MD.reset();
2136}
2137
2138TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2139 Type *Ty = Type::getInt1PtrTy(Context);
2140 std::unique_ptr<GlobalVariable> GV(
2141 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2142 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2143 EXPECT_TRUE(MD->getValue() == GV.get());
2144 ASSERT_TRUE(GV->use_empty());
2145
2146 GV.reset();
2147 EXPECT_TRUE(!MD);
2148}
2149
Devang Patel0924b332009-07-30 00:03:41 +00002150TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002151 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002152 ConstantAsMetadata *C =
2153 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2154 ConstantAsMetadata *C2 =
2155 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002156
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002157 Metadata *const V = C;
2158 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002159 MDNode *n = MDNode::get(Context, V);
2160 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002161
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002162 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002163 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002164 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2165 NMD->addOperand(n);
2166 NMD->addOperand(n2);
2167
Chris Lattnerbe354a62009-08-23 04:47:35 +00002168 std::string Str;
2169 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002170 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002171 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002172 oss.str().c_str());
2173}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002174
2175typedef MetadataTest FunctionAttachmentTest;
2176TEST_F(FunctionAttachmentTest, setMetadata) {
2177 Function *F = getFunction("foo");
2178 ASSERT_FALSE(F->hasMetadata());
2179 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2180 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2181 EXPECT_EQ(nullptr, F->getMetadata("other"));
2182
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002183 DISubprogram *SP1 = getSubprogram();
2184 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002185 ASSERT_NE(SP1, SP2);
2186
2187 F->setMetadata("dbg", SP1);
2188 EXPECT_TRUE(F->hasMetadata());
2189 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2190 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2191 EXPECT_EQ(nullptr, F->getMetadata("other"));
2192
2193 F->setMetadata(LLVMContext::MD_dbg, SP2);
2194 EXPECT_TRUE(F->hasMetadata());
2195 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2196 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2197 EXPECT_EQ(nullptr, F->getMetadata("other"));
2198
2199 F->setMetadata("dbg", nullptr);
2200 EXPECT_FALSE(F->hasMetadata());
2201 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2202 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2203 EXPECT_EQ(nullptr, F->getMetadata("other"));
2204
2205 MDTuple *T1 = getTuple();
2206 MDTuple *T2 = getTuple();
2207 ASSERT_NE(T1, T2);
2208
2209 F->setMetadata("other1", T1);
2210 F->setMetadata("other2", T2);
2211 EXPECT_TRUE(F->hasMetadata());
2212 EXPECT_EQ(T1, F->getMetadata("other1"));
2213 EXPECT_EQ(T2, F->getMetadata("other2"));
2214 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2215
2216 F->setMetadata("other1", T2);
2217 F->setMetadata("other2", T1);
2218 EXPECT_EQ(T2, F->getMetadata("other1"));
2219 EXPECT_EQ(T1, F->getMetadata("other2"));
2220
2221 F->setMetadata("other1", nullptr);
2222 F->setMetadata("other2", nullptr);
2223 EXPECT_FALSE(F->hasMetadata());
2224 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2225 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2226}
2227
2228TEST_F(FunctionAttachmentTest, getAll) {
2229 Function *F = getFunction("foo");
2230
2231 MDTuple *T1 = getTuple();
2232 MDTuple *T2 = getTuple();
2233 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002234 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002235
2236 F->setMetadata("other1", T2);
2237 F->setMetadata(LLVMContext::MD_dbg, SP);
2238 F->setMetadata("other2", T1);
2239 F->setMetadata(LLVMContext::MD_prof, P);
2240 F->setMetadata("other2", T2);
2241 F->setMetadata("other1", T1);
2242
2243 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2244 F->getAllMetadata(MDs);
2245 ASSERT_EQ(4u, MDs.size());
2246 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2247 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2248 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2249 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2250 EXPECT_EQ(SP, MDs[0].second);
2251 EXPECT_EQ(P, MDs[1].second);
2252 EXPECT_EQ(T1, MDs[2].second);
2253 EXPECT_EQ(T2, MDs[3].second);
2254}
2255
2256TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2257 Function *F = getFunction("foo");
2258
2259 MDTuple *T1 = getTuple();
2260 MDTuple *T2 = getTuple();
2261 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002262 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002263
2264 F->setMetadata("other1", T1);
2265 F->setMetadata(LLVMContext::MD_dbg, SP);
2266 F->setMetadata("other2", T2);
2267 F->setMetadata(LLVMContext::MD_prof, P);
2268
2269 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2270 F->dropUnknownMetadata(Known);
2271
2272 EXPECT_EQ(T2, F->getMetadata("other2"));
2273 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2274 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2275 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2276
2277 F->setMetadata("other2", nullptr);
2278 F->setMetadata(LLVMContext::MD_prof, nullptr);
2279 EXPECT_FALSE(F->hasMetadata());
2280}
2281
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002282TEST_F(FunctionAttachmentTest, Verifier) {
2283 Function *F = getFunction("foo");
2284 F->setMetadata("attach", getTuple());
2285
2286 // Confirm this has no body.
2287 ASSERT_TRUE(F->empty());
2288
2289 // Functions without a body cannot have metadata attachments (they also can't
2290 // be verified directly, so check that the module fails to verify).
2291 EXPECT_TRUE(verifyModule(*F->getParent()));
2292
2293 // Functions with a body can.
2294 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2295 EXPECT_FALSE(verifyModule(*F->getParent()));
2296 EXPECT_FALSE(verifyFunction(*F));
2297}
2298
Diego Novillo2567f3d2015-05-13 15:13:45 +00002299TEST_F(FunctionAttachmentTest, EntryCount) {
2300 Function *F = getFunction("foo");
2301 EXPECT_FALSE(F->getEntryCount().hasValue());
2302 F->setEntryCount(12304);
2303 EXPECT_TRUE(F->getEntryCount().hasValue());
2304 EXPECT_EQ(12304u, *F->getEntryCount());
2305}
2306
Nick Lewycky49f89192009-04-04 07:22:01 +00002307}