blob: 5c53ec74ab8b28d98840e55753841752d07309b2 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000012#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000013#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000014#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +000019#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000023#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024using namespace llvm;
25
26namespace {
27
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000028TEST(ContextAndReplaceableUsesTest, FromContext) {
29 LLVMContext Context;
30 ContextAndReplaceableUses CRU(Context);
31 EXPECT_EQ(&Context, &CRU.getContext());
32 EXPECT_FALSE(CRU.hasReplaceableUses());
33 EXPECT_FALSE(CRU.getReplaceableUses());
34}
35
36TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
37 LLVMContext Context;
38 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
39 EXPECT_EQ(&Context, &CRU.getContext());
40 EXPECT_TRUE(CRU.hasReplaceableUses());
41 EXPECT_TRUE(CRU.getReplaceableUses());
42}
43
44TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
45 LLVMContext Context;
46 ContextAndReplaceableUses CRU(Context);
47 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
48 EXPECT_EQ(&Context, &CRU.getContext());
49 EXPECT_TRUE(CRU.hasReplaceableUses());
50 EXPECT_TRUE(CRU.getReplaceableUses());
51}
52
53TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
54 LLVMContext Context;
55 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
56 auto *Ptr = ReplaceableUses.get();
57 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
58 ReplaceableUses = CRU.takeReplaceableUses();
59 EXPECT_EQ(&Context, &CRU.getContext());
60 EXPECT_FALSE(CRU.hasReplaceableUses());
61 EXPECT_FALSE(CRU.getReplaceableUses());
62 EXPECT_EQ(Ptr, ReplaceableUses.get());
63}
64
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000065class MetadataTest : public testing::Test {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000066public:
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000067 MetadataTest() : M("test", Context), Counter(0) {}
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000068
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000069protected:
70 LLVMContext Context;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000071 Module M;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000072 int Counter;
73
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000074 MDNode *getNode() { return MDNode::get(Context, None); }
75 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
76 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
77 Metadata *MDs[] = {MD1, MD2};
78 return MDNode::get(Context, MDs);
79 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000080
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000081 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000082 DISubroutineType *getSubroutineType() {
83 return DISubroutineType::getDistinct(Context, 0, getNode(nullptr));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000084 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000085 DISubprogram *getSubprogram() {
86 return DISubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000087 nullptr, false, false, 0, nullptr, 0, 0, 0,
88 0);
89 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000090 DIScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
91 DIFile *getFile() {
92 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000093 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000094 DITypeRef getBasicType(StringRef Name) {
95 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +000096 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000097 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000098 DITypeRef getDerivedType() {
99 return DIDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000100 nullptr, 0, nullptr,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000101 getBasicType("basictype"), 1, 2, 0, 0)
102 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000103 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000104 Constant *getConstant() {
105 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
106 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000107 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000108 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000109 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000110 DITypeRef getCompositeType() {
111 return DICompositeType::getDistinct(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000112 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
113 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
114 ->getRef();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000115 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000116 Function *getFunction(StringRef Name) {
117 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000118 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
119 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000120};
121typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000122
Nick Lewycky49f89192009-04-04 07:22:01 +0000123// Test that construction of MDString with different value produces different
124// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000125TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000126 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000127 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000128 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000129 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000130 EXPECT_NE(s1, s2);
131}
132
133// Test that creation of MDStrings with the same string contents produces the
134// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000135TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000136 char x[4] = { 'a', 'b', 'c', 'X' };
137 char y[4] = { 'a', 'b', 'c', 'Y' };
138
Owen Anderson23587322009-07-31 21:38:10 +0000139 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
140 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000141 EXPECT_EQ(s1, s2);
142}
143
144// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000145TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000146 char *str = new char[13];
147 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000148 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000149 strncpy(str, "aaaaaaaaaaaaa", 13);
150 delete[] str;
151
Chris Lattnerbe354a62009-08-23 04:47:35 +0000152 std::string Str;
153 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000154 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000155 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000156}
157
158// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000159TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000160 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000161 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000162 std::string Str;
163 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000164 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000165 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000166}
167
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000168typedef MetadataTest MDNodeTest;
169
Nick Lewycky49f89192009-04-04 07:22:01 +0000170// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000171TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000172 char x[3] = { 'a', 'b', 'c' };
173 char y[3] = { '1', '2', '3' };
174
Owen Anderson23587322009-07-31 21:38:10 +0000175 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
176 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000177 ConstantAsMetadata *CI = ConstantAsMetadata::get(
178 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000179
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000180 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000181 V.push_back(s1);
182 V.push_back(CI);
183 V.push_back(s2);
184
Jay Foad5514afe2011-04-21 19:59:31 +0000185 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000186 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000187 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000188 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000189 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000190 MDNode *n4 = MDNode::getIfExists(Context, V);
191 MDNode *n5 = MDNode::getIfExists(Context, c1);
192 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000193 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000194 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000195 EXPECT_EQ(n4, n1);
196 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000197 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000198
Chris Lattner9b493022009-12-31 01:22:29 +0000199 EXPECT_EQ(3u, n1->getNumOperands());
200 EXPECT_EQ(s1, n1->getOperand(0));
201 EXPECT_EQ(CI, n1->getOperand(1));
202 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000203
Chris Lattner9b493022009-12-31 01:22:29 +0000204 EXPECT_EQ(1u, n2->getNumOperands());
205 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000206}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000207
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000208TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000209 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
210 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000211
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000212 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000213 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000214 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000215
216 EXPECT_EQ(n, wvh);
217
218 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000219}
Devang Patel0924b332009-07-30 00:03:41 +0000220
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000221TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000222 // !0 = !{!0}
223 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000224 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000225 auto Temp = MDNode::getTemporary(Context, None);
226 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000227 MDNode *Self = MDNode::get(Context, Args);
228 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000229 ASSERT_EQ(Self, Self->getOperand(0));
230
231 // Self-references should be distinct, so MDNode::get() should grab a
232 // uniqued node that references Self, not Self.
233 Args[0] = Self;
234 MDNode *Ref1 = MDNode::get(Context, Args);
235 MDNode *Ref2 = MDNode::get(Context, Args);
236 EXPECT_NE(Self, Ref1);
237 EXPECT_EQ(Ref1, Ref2);
238 }
239
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000240 // !0 = !{!0, !{}}
241 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000242 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000243 auto Temp = MDNode::getTemporary(Context, None);
244 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000245 MDNode *Self = MDNode::get(Context, Args);
246 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000247 ASSERT_EQ(Self, Self->getOperand(0));
248
249 // Self-references should be distinct, so MDNode::get() should grab a
250 // uniqued node that references Self, not Self itself.
251 Args[0] = Self;
252 MDNode *Ref1 = MDNode::get(Context, Args);
253 MDNode *Ref2 = MDNode::get(Context, Args);
254 EXPECT_NE(Self, Ref1);
255 EXPECT_EQ(Ref1, Ref2);
256 }
257}
258
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000259TEST_F(MDNodeTest, Print) {
260 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
261 MDString *S = MDString::get(Context, "foo");
262 MDNode *N0 = getNode();
263 MDNode *N1 = getNode(N0);
264 MDNode *N2 = getNode(N0, N1);
265
266 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
267 MDNode *N = MDNode::get(Context, Args);
268
269 std::string Expected;
270 {
271 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000272 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000273 C->printAsOperand(OS);
274 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000275 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000276 OS << ", null";
277 MDNode *Nodes[] = {N0, N1, N2};
278 for (auto *Node : Nodes)
279 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000280 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000281 }
282
283 std::string Actual;
284 {
285 raw_string_ostream OS(Actual);
286 N->print(OS);
287 }
288
289 EXPECT_EQ(Expected, Actual);
290}
291
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000292#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
293 do { \
294 std::string Actual_; \
295 raw_string_ostream OS(Actual_); \
296 PRINT; \
297 OS.flush(); \
298 std::string Expected_(EXPECTED); \
299 EXPECT_EQ(Expected_, Actual_); \
300 } while (false)
301
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000302TEST_F(MDNodeTest, PrintTemporary) {
303 MDNode *Arg = getNode();
304 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
305 MDNode *N = getNode(Temp.get());
306 Module M("test", Context);
307 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
308 NMD->addOperand(N);
309
310 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
311 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
312 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
313
314 // Cleanup.
315 Temp->replaceAllUsesWith(Arg);
316}
317
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000318TEST_F(MDNodeTest, PrintFromModule) {
319 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
320 MDString *S = MDString::get(Context, "foo");
321 MDNode *N0 = getNode();
322 MDNode *N1 = getNode(N0);
323 MDNode *N2 = getNode(N0, N1);
324
325 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
326 MDNode *N = MDNode::get(Context, Args);
327 Module M("test", Context);
328 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
329 NMD->addOperand(N);
330
331 std::string Expected;
332 {
333 raw_string_ostream OS(Expected);
334 OS << "!0 = !{";
335 C->printAsOperand(OS);
336 OS << ", ";
337 S->printAsOperand(OS);
338 OS << ", null, !1, !2, !3}";
339 }
340
341 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
342}
343
344TEST_F(MDNodeTest, PrintFromFunction) {
345 Module M("test", Context);
346 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
347 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
348 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
349 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
350 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
351 auto *R0 = ReturnInst::Create(Context, BB0);
352 auto *R1 = ReturnInst::Create(Context, BB1);
353 auto *N0 = MDNode::getDistinct(Context, None);
354 auto *N1 = MDNode::getDistinct(Context, None);
355 R0->setMetadata("md", N0);
356 R1->setMetadata("md", N1);
357
358 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
359 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000360
361 ModuleSlotTracker MST(&M);
362 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
363 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000364}
365
366TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
367 Module M("test", Context);
368
369 auto *Intrinsic =
370 Function::Create(FunctionType::get(Type::getVoidTy(Context),
371 Type::getMetadataTy(Context), false),
372 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
373
374 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
375 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
376 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
377 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
378 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
379 auto *N0 = MDNode::getDistinct(Context, None);
380 auto *N1 = MDNode::getDistinct(Context, None);
381 auto *MAV0 = MetadataAsValue::get(Context, N0);
382 auto *MAV1 = MetadataAsValue::get(Context, N1);
383 CallInst::Create(Intrinsic, MAV0, "", BB0);
384 CallInst::Create(Intrinsic, MAV1, "", BB1);
385
386 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
387 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
388 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
389 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
390 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
391 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000392
393 ModuleSlotTracker MST(&M);
394 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
395 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
396 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
397 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
398 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
399 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000400}
401#undef EXPECT_PRINTER_EQ
402
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000403TEST_F(MDNodeTest, NullOperand) {
404 // metadata !{}
405 MDNode *Empty = MDNode::get(Context, None);
406
407 // metadata !{metadata !{}}
408 Metadata *Ops[] = {Empty};
409 MDNode *N = MDNode::get(Context, Ops);
410 ASSERT_EQ(Empty, N->getOperand(0));
411
412 // metadata !{metadata !{}} => metadata !{null}
413 N->replaceOperandWith(0, nullptr);
414 ASSERT_EQ(nullptr, N->getOperand(0));
415
416 // metadata !{null}
417 Ops[0] = nullptr;
418 MDNode *NullOp = MDNode::get(Context, Ops);
419 ASSERT_EQ(nullptr, NullOp->getOperand(0));
420 EXPECT_EQ(N, NullOp);
421}
422
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000423TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
424 // !{}
425 MDNode *Empty = MDNode::get(Context, None);
426 ASSERT_TRUE(Empty->isResolved());
427 EXPECT_FALSE(Empty->isDistinct());
428
429 // !{!{}}
430 Metadata *Wrapped1Ops[] = {Empty};
431 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
432 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
433 ASSERT_TRUE(Wrapped1->isResolved());
434 EXPECT_FALSE(Wrapped1->isDistinct());
435
436 // !{!{!{}}}
437 Metadata *Wrapped2Ops[] = {Wrapped1};
438 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
439 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
440 ASSERT_TRUE(Wrapped2->isResolved());
441 EXPECT_FALSE(Wrapped2->isDistinct());
442
443 // !{!{!{}}} => !{!{}}
444 Wrapped2->replaceOperandWith(0, Empty);
445 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
446 EXPECT_TRUE(Wrapped2->isDistinct());
447 EXPECT_FALSE(Wrapped1->isDistinct());
448}
449
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000450TEST_F(MDNodeTest, getDistinct) {
451 // !{}
452 MDNode *Empty = MDNode::get(Context, None);
453 ASSERT_TRUE(Empty->isResolved());
454 ASSERT_FALSE(Empty->isDistinct());
455 ASSERT_EQ(Empty, MDNode::get(Context, None));
456
457 // distinct !{}
458 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
459 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
460 EXPECT_TRUE(Distinct1->isResolved());
461 EXPECT_TRUE(Distinct2->isDistinct());
462 EXPECT_NE(Empty, Distinct1);
463 EXPECT_NE(Empty, Distinct2);
464 EXPECT_NE(Distinct1, Distinct2);
465
466 // !{}
467 ASSERT_EQ(Empty, MDNode::get(Context, None));
468}
469
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000470TEST_F(MDNodeTest, isUniqued) {
471 MDNode *U = MDTuple::get(Context, None);
472 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000473 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000474 EXPECT_TRUE(U->isUniqued());
475 EXPECT_FALSE(D->isUniqued());
476 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000477}
478
479TEST_F(MDNodeTest, isDistinct) {
480 MDNode *U = MDTuple::get(Context, None);
481 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000482 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000483 EXPECT_FALSE(U->isDistinct());
484 EXPECT_TRUE(D->isDistinct());
485 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000486}
487
488TEST_F(MDNodeTest, isTemporary) {
489 MDNode *U = MDTuple::get(Context, None);
490 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000491 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000492 EXPECT_FALSE(U->isTemporary());
493 EXPECT_FALSE(D->isTemporary());
494 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000495}
496
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000497TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
498 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000499 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000500 ASSERT_FALSE(Temp->isResolved());
501
502 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000503 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000504 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
505 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000506 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000507
508 // temporary !{} => !{}
509 MDNode *Empty = MDNode::get(Context, None);
510 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000511 EXPECT_EQ(Empty, Distinct->getOperand(0));
512}
513
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000514TEST_F(MDNodeTest, handleChangedOperandRecursion) {
515 // !0 = !{}
516 MDNode *N0 = MDNode::get(Context, None);
517
518 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000519 auto Temp3 = MDTuple::getTemporary(Context, None);
520 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000521 MDNode *N1 = MDNode::get(Context, Ops1);
522
523 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000524 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000525 MDNode *N2 = MDNode::get(Context, Ops2);
526
527 // !3 = !{!2}
528 Metadata *Ops3[] = {N2};
529 MDNode *N3 = MDNode::get(Context, Ops3);
530 Temp3->replaceAllUsesWith(N3);
531
532 // !4 = !{!1}
533 Metadata *Ops4[] = {N1};
534 MDNode *N4 = MDNode::get(Context, Ops4);
535
536 // Confirm that the cycle prevented RAUW from getting dropped.
537 EXPECT_TRUE(N0->isResolved());
538 EXPECT_FALSE(N1->isResolved());
539 EXPECT_FALSE(N2->isResolved());
540 EXPECT_FALSE(N3->isResolved());
541 EXPECT_FALSE(N4->isResolved());
542
543 // Create a couple of distinct nodes to observe what's going on.
544 //
545 // !5 = distinct !{!2}
546 // !6 = distinct !{!3}
547 Metadata *Ops5[] = {N2};
548 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
549 Metadata *Ops6[] = {N3};
550 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
551
552 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
553 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
554 // references !3, this can cause a re-entry of handleChangedOperand() when !3
555 // is not ready for it.
556 //
557 // !2->replaceOperandWith(1, nullptr)
558 // !2: !{!3, !0} => !{!3, null}
559 // !2->replaceAllUsesWith(!1)
560 // !3: !{!2] => !{!1}
561 // !3->replaceAllUsesWith(!4)
562 N2->replaceOperandWith(1, nullptr);
563
564 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
565 // under us. Just check that the other nodes are sane.
566 //
567 // !1 = !{!4, null}
568 // !4 = !{!1}
569 // !5 = distinct !{!1}
570 // !6 = distinct !{!4}
571 EXPECT_EQ(N4, N1->getOperand(0));
572 EXPECT_EQ(N1, N4->getOperand(0));
573 EXPECT_EQ(N1, N5->getOperand(0));
574 EXPECT_EQ(N4, N6->getOperand(0));
575}
576
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000577TEST_F(MDNodeTest, replaceResolvedOperand) {
578 // Check code for replacing one resolved operand with another. If doing this
579 // directly (via replaceOperandWith()) becomes illegal, change the operand to
580 // a global value that gets RAUW'ed.
581 //
582 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000583 auto Temp = MDTuple::getTemporary(Context, None);
584 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000585
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000586 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000587 MDNode *N = MDTuple::get(Context, Ops);
588 EXPECT_EQ(nullptr, N->getOperand(0));
589 ASSERT_FALSE(N->isResolved());
590
591 // Check code for replacing resolved nodes.
592 N->replaceOperandWith(0, Empty);
593 EXPECT_EQ(Empty, N->getOperand(0));
594
595 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000596 N->replaceOperandWith(0, Temp.get());
597 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000598
599 // Remove the references to Temp; required for teardown.
600 Temp->replaceAllUsesWith(nullptr);
601}
602
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000603TEST_F(MDNodeTest, replaceWithUniqued) {
604 auto *Empty = MDTuple::get(Context, None);
605 MDTuple *FirstUniqued;
606 {
607 Metadata *Ops[] = {Empty};
608 auto Temp = MDTuple::getTemporary(Context, Ops);
609 EXPECT_TRUE(Temp->isTemporary());
610
611 // Don't expect a collision.
612 auto *Current = Temp.get();
613 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
614 EXPECT_TRUE(FirstUniqued->isUniqued());
615 EXPECT_TRUE(FirstUniqued->isResolved());
616 EXPECT_EQ(Current, FirstUniqued);
617 }
618 {
619 Metadata *Ops[] = {Empty};
620 auto Temp = MDTuple::getTemporary(Context, Ops);
621 EXPECT_TRUE(Temp->isTemporary());
622
623 // Should collide with Uniqued above this time.
624 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
625 EXPECT_TRUE(Uniqued->isUniqued());
626 EXPECT_TRUE(Uniqued->isResolved());
627 EXPECT_EQ(FirstUniqued, Uniqued);
628 }
629 {
630 auto Unresolved = MDTuple::getTemporary(Context, None);
631 Metadata *Ops[] = {Unresolved.get()};
632 auto Temp = MDTuple::getTemporary(Context, Ops);
633 EXPECT_TRUE(Temp->isTemporary());
634
635 // Shouldn't be resolved.
636 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
637 EXPECT_TRUE(Uniqued->isUniqued());
638 EXPECT_FALSE(Uniqued->isResolved());
639
640 // Should be a different node.
641 EXPECT_NE(FirstUniqued, Uniqued);
642
643 // Should resolve when we update its node (note: be careful to avoid a
644 // collision with any other nodes above).
645 Uniqued->replaceOperandWith(0, nullptr);
646 EXPECT_TRUE(Uniqued->isResolved());
647 }
648}
649
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000650TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000651 // temp !{}
652 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
653 EXPECT_FALSE(Op->isResolved());
654
655 // temp !{temp !{}}
656 Metadata *Ops[] = {Op};
657 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
658 EXPECT_FALSE(N->isResolved());
659
660 // temp !{temp !{}} => !{temp !{}}
661 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
662 EXPECT_FALSE(N->isResolved());
663
664 // !{temp !{}} => !{!{}}
665 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
666 EXPECT_TRUE(Op->isResolved());
667 EXPECT_TRUE(N->isResolved());
668}
669
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000670TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000671 // i1* @GV
672 Type *Ty = Type::getInt1PtrTy(Context);
673 std::unique_ptr<GlobalVariable> GV(
674 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
675 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
676
677 // temp !{i1* @GV}
678 Metadata *Ops[] = {Op};
679 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
680
681 // temp !{i1* @GV} => !{i1* @GV}
682 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
683 ASSERT_TRUE(N->isUniqued());
684
685 // !{i1* @GV} => !{null}
686 GV.reset();
687 ASSERT_TRUE(N->isUniqued());
688 Metadata *NullOps[] = {nullptr};
689 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
690}
691
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000692TEST_F(MDNodeTest, replaceWithDistinct) {
693 {
694 auto *Empty = MDTuple::get(Context, None);
695 Metadata *Ops[] = {Empty};
696 auto Temp = MDTuple::getTemporary(Context, Ops);
697 EXPECT_TRUE(Temp->isTemporary());
698
699 // Don't expect a collision.
700 auto *Current = Temp.get();
701 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
702 EXPECT_TRUE(Distinct->isDistinct());
703 EXPECT_TRUE(Distinct->isResolved());
704 EXPECT_EQ(Current, Distinct);
705 }
706 {
707 auto Unresolved = MDTuple::getTemporary(Context, None);
708 Metadata *Ops[] = {Unresolved.get()};
709 auto Temp = MDTuple::getTemporary(Context, Ops);
710 EXPECT_TRUE(Temp->isTemporary());
711
712 // Don't expect a collision.
713 auto *Current = Temp.get();
714 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
715 EXPECT_TRUE(Distinct->isDistinct());
716 EXPECT_TRUE(Distinct->isResolved());
717 EXPECT_EQ(Current, Distinct);
718
719 // Cleanup; required for teardown.
720 Unresolved->replaceAllUsesWith(nullptr);
721 }
722}
723
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000724TEST_F(MDNodeTest, replaceWithPermanent) {
725 Metadata *Ops[] = {nullptr};
726 auto Temp = MDTuple::getTemporary(Context, Ops);
727 auto *T = Temp.get();
728
729 // U is a normal, uniqued node that references T.
730 auto *U = MDTuple::get(Context, T);
731 EXPECT_TRUE(U->isUniqued());
732
733 // Make Temp self-referencing.
734 Temp->replaceOperandWith(0, T);
735
736 // Try to uniquify Temp. This should, despite the name in the API, give a
737 // 'distinct' node, since self-references aren't allowed to be uniqued.
738 //
739 // Since it's distinct, N should have the same address as when it was a
740 // temporary (i.e., be equal to T not U).
741 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
742 EXPECT_EQ(N, T);
743 EXPECT_TRUE(N->isDistinct());
744
745 // U should be the canonical unique node with N as the argument.
746 EXPECT_EQ(U, MDTuple::get(Context, N));
747 EXPECT_TRUE(U->isUniqued());
748
749 // This temporary should collide with U when replaced, but it should still be
750 // uniqued.
751 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
752 EXPECT_TRUE(U->isUniqued());
753
754 // This temporary should become a new uniqued node.
755 auto Temp2 = MDTuple::getTemporary(Context, U);
756 auto *V = Temp2.get();
757 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
758 EXPECT_TRUE(V->isUniqued());
759 EXPECT_EQ(U, V->getOperand(0));
760}
761
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000762TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
763 TrackingMDRef Ref;
764 EXPECT_EQ(nullptr, Ref.get());
765 {
766 auto Temp = MDTuple::getTemporary(Context, None);
767 Ref.reset(Temp.get());
768 EXPECT_EQ(Temp.get(), Ref.get());
769 }
770 EXPECT_EQ(nullptr, Ref.get());
771}
772
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000773typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000774
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000775TEST_F(DILocationTest, Overflow) {
776 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000777 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000778 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000779 EXPECT_EQ(2u, L->getLine());
780 EXPECT_EQ(7u, L->getColumn());
781 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000782 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000783 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000784 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000785 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000786 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000787 }
788 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000789 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000790 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000791 EXPECT_EQ(0u, L->getColumn());
792 }
793 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000794 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000795 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000796 EXPECT_EQ(0u, L->getColumn());
797 }
798}
799
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000800TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000801 MDNode *N = getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000802 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000803 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000804 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000805 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000806 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000807}
808
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000809TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000810 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000811 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000812 EXPECT_TRUE(L->isTemporary());
813 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000814}
815
Teresa Johnsond98152b62015-12-07 15:05:44 +0000816TEST_F(DILocationTest, cloneTemporary) {
817 MDNode *N = MDNode::get(Context, None);
818 auto L = DILocation::getTemporary(Context, 2, 7, N);
819 EXPECT_TRUE(L->isTemporary());
820 auto L2 = L->clone();
821 EXPECT_TRUE(L2->isTemporary());
822}
823
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000824typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000825
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000826TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000827 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000828 auto *Empty = MDNode::get(Context, None);
829 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000830 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000831 EXPECT_EQ(15u, N->getTag());
832 EXPECT_EQ(2u, N->getNumOperands());
833 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000834 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000835 EXPECT_EQ(1u, N->getNumDwarfOperands());
836 EXPECT_EQ(Empty, N->getDwarfOperand(0));
837 EXPECT_EQ(Empty, N->getOperand(1));
838 ASSERT_TRUE(N->isUniqued());
839
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000840 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000841
842 N->replaceOperandWith(1, nullptr);
843 EXPECT_EQ(15u, N->getTag());
844 EXPECT_EQ(Header, N->getHeader());
845 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
846 ASSERT_TRUE(N->isUniqued());
847
848 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000849 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000850
851 N->replaceDwarfOperandWith(0, Empty);
852 EXPECT_EQ(15u, N->getTag());
853 EXPECT_EQ(Header, N->getHeader());
854 EXPECT_EQ(Empty, N->getDwarfOperand(0));
855 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000856 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000857
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000858 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000859 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000860}
861
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000862TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000863 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000864 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000865 EXPECT_EQ(StringRef(), N->getHeader());
866 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000867}
868
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000869typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000870
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000871TEST_F(DISubrangeTest, get) {
872 auto *N = DISubrange::get(Context, 5, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000873 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
874 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000875 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000876 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
877 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000878
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000879 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000880 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000881}
882
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000883TEST_F(DISubrangeTest, getEmptyArray) {
884 auto *N = DISubrange::get(Context, -1, 0);
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000885 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
886 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000887 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000888 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000889}
890
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000891typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000892
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000893TEST_F(DIEnumeratorTest, get) {
894 auto *N = DIEnumerator::get(Context, 7, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000895 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
896 EXPECT_EQ(7, N->getValue());
897 EXPECT_EQ("name", N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000898 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000899
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000900 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
901 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000902
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000903 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000904 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000905}
906
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000907typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000908
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000909TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000910 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000911 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000912 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
913 EXPECT_EQ("special", N->getName());
914 EXPECT_EQ(33u, N->getSizeInBits());
915 EXPECT_EQ(26u, N->getAlignInBits());
916 EXPECT_EQ(7u, N->getEncoding());
917 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000918 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000919 26, 7));
920
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000921 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000922 "special", 33, 26, 7));
923 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000924 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
925 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000926 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000927 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000928 25, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000929 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000930 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000931
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000932 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000933 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000934}
935
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000936TEST_F(DIBasicTypeTest, getWithLargeValues) {
937 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000938 UINT64_MAX, UINT64_MAX - 1, 7);
939 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
940 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
941}
942
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000943TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000944 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000945 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000946 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
947 EXPECT_EQ("unspecified", N->getName());
948 EXPECT_EQ(0u, N->getSizeInBits());
949 EXPECT_EQ(0u, N->getAlignInBits());
950 EXPECT_EQ(0u, N->getEncoding());
951 EXPECT_EQ(0u, N->getLine());
952}
953
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000954typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000955
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000956TEST_F(DITypeTest, clone) {
957 // Check that DIType has a specialized clone that returns TempDIType.
958 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000959 dwarf::DW_ATE_signed);
960
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000961 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000962 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
963}
964
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000965TEST_F(DITypeTest, setFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000966 // void (void)
967 Metadata *TypesOps[] = {nullptr};
968 Metadata *Types = MDTuple::get(Context, TypesOps);
969
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000970 DIType *D = DISubroutineType::getDistinct(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000971 EXPECT_EQ(0u, D->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000972 D->setFlags(DINode::FlagRValueReference);
973 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000974 D->setFlags(0u);
975 EXPECT_EQ(0u, D->getFlags());
976
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000977 TempDIType T = DISubroutineType::getTemporary(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000978 EXPECT_EQ(0u, T->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000979 T->setFlags(DINode::FlagRValueReference);
980 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000981 T->setFlags(0u);
982 EXPECT_EQ(0u, T->getFlags());
983}
984
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000985typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000986
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000987TEST_F(DIDerivedTypeTest, get) {
988 DIFile *File = getFile();
989 DIScopeRef Scope = getSubprogramRef();
990 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000991 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000992
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000993 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000994 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
995 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
996 EXPECT_EQ("something", N->getName());
997 EXPECT_EQ(File, N->getFile());
998 EXPECT_EQ(1u, N->getLine());
999 EXPECT_EQ(Scope, N->getScope());
1000 EXPECT_EQ(BaseType, N->getBaseType());
1001 EXPECT_EQ(2u, N->getSizeInBits());
1002 EXPECT_EQ(3u, N->getAlignInBits());
1003 EXPECT_EQ(4u, N->getOffsetInBits());
1004 EXPECT_EQ(5u, N->getFlags());
1005 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001006 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001007 "something", File, 1, Scope, BaseType, 2, 3,
1008 4, 5, ExtraData));
1009
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001010 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001011 "something", File, 1, Scope, BaseType, 2, 3,
1012 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001013 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001014 File, 1, Scope, BaseType, 2, 3, 4, 5,
1015 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001016 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001017 "something", getFile(), 1, Scope, BaseType, 2,
1018 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001019 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001020 "something", File, 2, Scope, BaseType, 2, 3,
1021 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001022 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001023 "something", File, 1, getSubprogramRef(),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001024 BaseType, 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001025 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001026 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1027 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001028 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001029 "something", File, 1, Scope, BaseType, 3, 3,
1030 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001031 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001032 "something", File, 1, Scope, BaseType, 2, 2,
1033 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001034 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001035 "something", File, 1, Scope, BaseType, 2, 3,
1036 5, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001037 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001038 "something", File, 1, Scope, BaseType, 2, 3,
1039 4, 4, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001040 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001041 "something", File, 1, Scope, BaseType, 2, 3,
1042 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001043
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001044 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001045 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001046}
1047
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001048TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1049 DIFile *File = getFile();
1050 DIScopeRef Scope = getSubprogramRef();
1051 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001052 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001053
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001054 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001055 File, 1, Scope, BaseType, UINT64_MAX,
1056 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1057 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1058 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1059 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1060}
1061
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001062typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001063
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001064TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001065 unsigned Tag = dwarf::DW_TAG_structure_type;
1066 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001067 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001068 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001069 DIScopeRef Scope = getSubprogramRef();
1070 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001071 uint64_t SizeInBits = 2;
1072 uint64_t AlignInBits = 3;
1073 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001074 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001075 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001076 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001077 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001078 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001079 StringRef Identifier = "some id";
1080
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001081 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001082 BaseType, SizeInBits, AlignInBits,
1083 OffsetInBits, Flags, Elements, RuntimeLang,
1084 VTableHolder, TemplateParams, Identifier);
1085 EXPECT_EQ(Tag, N->getTag());
1086 EXPECT_EQ(Name, N->getName());
1087 EXPECT_EQ(File, N->getFile());
1088 EXPECT_EQ(Line, N->getLine());
1089 EXPECT_EQ(Scope, N->getScope());
1090 EXPECT_EQ(BaseType, N->getBaseType());
1091 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1092 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1093 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1094 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001095 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001096 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1097 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001098 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001099 EXPECT_EQ(Identifier, N->getIdentifier());
1100
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001101 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001102 BaseType, SizeInBits, AlignInBits,
1103 OffsetInBits, Flags, Elements, RuntimeLang,
1104 VTableHolder, TemplateParams, Identifier));
1105
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001106 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001107 BaseType, SizeInBits, AlignInBits,
1108 OffsetInBits, Flags, Elements, RuntimeLang,
1109 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001110 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001111 BaseType, SizeInBits, AlignInBits,
1112 OffsetInBits, Flags, Elements, RuntimeLang,
1113 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001114 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001115 BaseType, SizeInBits, AlignInBits,
1116 OffsetInBits, Flags, Elements, RuntimeLang,
1117 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001118 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001119 BaseType, SizeInBits, AlignInBits,
1120 OffsetInBits, Flags, Elements, RuntimeLang,
1121 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001122 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001123 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001124 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1125 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001126 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001127 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1128 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1129 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001130 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001131 BaseType, SizeInBits + 1, AlignInBits,
1132 OffsetInBits, Flags, Elements, RuntimeLang,
1133 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001134 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001135 BaseType, SizeInBits, AlignInBits + 1,
1136 OffsetInBits, Flags, Elements, RuntimeLang,
1137 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001138 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001139 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1140 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1141 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001142 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001143 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1144 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1145 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001146 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001147 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1148 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1149 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001150 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001151 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1152 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1153 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001154 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001155 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1156 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1157 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001158 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001159 BaseType, SizeInBits, AlignInBits,
1160 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001161 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001162 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001163 BaseType, SizeInBits, AlignInBits,
1164 OffsetInBits, Flags, Elements, RuntimeLang,
1165 VTableHolder, TemplateParams, "other"));
1166
1167 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001168 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1169 BaseType, SizeInBits, AlignInBits,
1170 OffsetInBits, Flags, Elements, RuntimeLang,
1171 VTableHolder, TemplateParams, "")
1172 ->getRawIdentifier());
1173 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1174 BaseType, SizeInBits, AlignInBits,
1175 OffsetInBits, Flags, Elements, RuntimeLang,
1176 VTableHolder, TemplateParams)
1177 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001178
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001179 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001180 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001181}
1182
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001183TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001184 unsigned Tag = dwarf::DW_TAG_structure_type;
1185 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001186 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001187 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001188 DIScopeRef Scope = getSubprogramRef();
1189 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001190 uint64_t SizeInBits = UINT64_MAX;
1191 uint64_t AlignInBits = UINT64_MAX - 1;
1192 uint64_t OffsetInBits = UINT64_MAX - 2;
1193 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001194 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001195 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001196 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001197 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001198 StringRef Identifier = "some id";
1199
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001200 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001201 BaseType, SizeInBits, AlignInBits,
1202 OffsetInBits, Flags, Elements, RuntimeLang,
1203 VTableHolder, TemplateParams, Identifier);
1204 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1205 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1206 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1207}
1208
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001209TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001210 unsigned Tag = dwarf::DW_TAG_structure_type;
1211 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001212 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001213 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001214 DIScopeRef Scope = getSubprogramRef();
1215 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001216 uint64_t SizeInBits = 2;
1217 uint64_t AlignInBits = 3;
1218 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001219 unsigned Flags = 5;
1220 unsigned RuntimeLang = 6;
1221 StringRef Identifier = "some id";
1222
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001223 auto *N = DICompositeType::get(
1224 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1225 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001226
1227 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001228 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001229 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001230 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001231 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001232 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001233
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001234 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001235 EXPECT_EQ(nullptr, N->getVTableHolder());
1236 N->replaceVTableHolder(VTableHolder);
1237 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1238 N->replaceVTableHolder(nullptr);
1239 EXPECT_EQ(nullptr, N->getVTableHolder());
1240
1241 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001242 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001243 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001244 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001245 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001246 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001247}
1248
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001249typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001250
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001251TEST_F(DISubroutineTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001252 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001253 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001254
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001255 auto *N = DISubroutineType::get(Context, Flags, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001256 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1257 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001258 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001259 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001260
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001261 EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, TypeArray));
1262 EXPECT_NE(N, DISubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001263
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001264 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001265 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001266
1267 // Test always-empty operands.
1268 EXPECT_EQ(nullptr, N->getScope());
1269 EXPECT_EQ(nullptr, N->getFile());
1270 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001271}
1272
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001273typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001275TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001276 StringRef Filename = "file";
1277 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001278 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001279
1280 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1281 EXPECT_EQ(Filename, N->getFilename());
1282 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001283 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001284
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001285 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1286 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001287
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001288 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001289 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001290}
1291
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001292TEST_F(DIFileTest, ScopeGetFile) {
1293 // Ensure that DIScope::getFile() returns itself.
1294 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001295 EXPECT_EQ(N, N->getFile());
1296}
1297
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001298typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001299
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001300TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001301 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001302 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001303 StringRef Producer = "some producer";
1304 bool IsOptimized = false;
1305 StringRef Flags = "flag after flag";
1306 unsigned RuntimeVersion = 2;
1307 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001308 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001309 MDTuple *EnumTypes = getTuple();
1310 MDTuple *RetainedTypes = getTuple();
1311 MDTuple *Subprograms = getTuple();
1312 MDTuple *GlobalVariables = getTuple();
1313 MDTuple *ImportedEntities = getTuple();
Adrian Prantle3b49e02015-09-22 23:42:47 +00001314 uint64_t DWOId = 0x10000000c0ffee;
Amjad Abouda9bcf162015-12-10 12:56:35 +00001315 MDTuple *Macros = getTuple();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001316 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001317 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1318 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Amjad Abouda9bcf162015-12-10 12:56:35 +00001319 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities, Macros,
1320 DWOId);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001321
1322 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1323 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1324 EXPECT_EQ(File, N->getFile());
1325 EXPECT_EQ(Producer, N->getProducer());
1326 EXPECT_EQ(IsOptimized, N->isOptimized());
1327 EXPECT_EQ(Flags, N->getFlags());
1328 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1329 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1330 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001331 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1332 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1333 EXPECT_EQ(Subprograms, N->getSubprograms().get());
1334 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1335 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001336 EXPECT_EQ(Macros, N->getMacros().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001337 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001338
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001339 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001340 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1341 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1342 EXPECT_EQ(File, Temp->getFile());
1343 EXPECT_EQ(Producer, Temp->getProducer());
1344 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1345 EXPECT_EQ(Flags, Temp->getFlags());
1346 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1347 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1348 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1349 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1350 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
1351 EXPECT_EQ(Subprograms, Temp->getSubprograms().get());
1352 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1353 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001354 EXPECT_EQ(Macros, Temp->getMacros().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001355 EXPECT_EQ(DWOId, Temp->getDWOId());
1356
1357 auto *TempAddress = Temp.get();
1358 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1359 EXPECT_TRUE(Clone->isDistinct());
1360 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001361}
1362
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001363TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001364 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001365 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001366 StringRef Producer = "some producer";
1367 bool IsOptimized = false;
1368 StringRef Flags = "flag after flag";
1369 unsigned RuntimeVersion = 2;
1370 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001371 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001372 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1373 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1374 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001375 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001376 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001377 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1378 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Amjad Abouda9bcf162015-12-10 12:56:35 +00001379 RetainedTypes, nullptr, nullptr, ImportedEntities, nullptr, DWOId);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001380
1381 auto *Subprograms = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001382 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001383 N->replaceSubprograms(Subprograms);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001384 EXPECT_EQ(Subprograms, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001385 N->replaceSubprograms(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001386 EXPECT_EQ(nullptr, N->getSubprograms().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001387
1388 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001389 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001390 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001391 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001392 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001393 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001394
1395 auto *Macros = MDTuple::getDistinct(Context, None);
1396 EXPECT_EQ(nullptr, N->getMacros().get());
1397 N->replaceMacros(Macros);
1398 EXPECT_EQ(Macros, N->getMacros().get());
1399 N->replaceMacros(nullptr);
1400 EXPECT_EQ(nullptr, N->getMacros().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001401}
1402
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001403typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001404
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001405TEST_F(DISubprogramTest, get) {
1406 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001407 StringRef Name = "name";
1408 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001409 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001410 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001411 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001412 bool IsLocalToUnit = false;
1413 bool IsDefinition = true;
1414 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001415 DITypeRef ContainingType = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001416 unsigned Virtuality = 4;
1417 unsigned VirtualIndex = 5;
1418 unsigned Flags = 6;
1419 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001420 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001421 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001422 MDTuple *Variables = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001423
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001424 auto *N = DISubprogram::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001425 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1426 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001427 IsOptimized, TemplateParams, Declaration, Variables);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001428
1429 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1430 EXPECT_EQ(Scope, N->getScope());
1431 EXPECT_EQ(Name, N->getName());
1432 EXPECT_EQ(LinkageName, N->getLinkageName());
1433 EXPECT_EQ(File, N->getFile());
1434 EXPECT_EQ(Line, N->getLine());
1435 EXPECT_EQ(Type, N->getType());
1436 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1437 EXPECT_EQ(IsDefinition, N->isDefinition());
1438 EXPECT_EQ(ScopeLine, N->getScopeLine());
1439 EXPECT_EQ(ContainingType, N->getContainingType());
1440 EXPECT_EQ(Virtuality, N->getVirtuality());
1441 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1442 EXPECT_EQ(Flags, N->getFlags());
1443 EXPECT_EQ(IsOptimized, N->isOptimized());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001444 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001445 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001446 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001447 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001448 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1449 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001450 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001451 Declaration, Variables));
1452
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001453 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001454 File, Line, Type, IsLocalToUnit, IsDefinition,
1455 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001456 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001457 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001458 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001459 Line, Type, IsLocalToUnit, IsDefinition,
1460 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001461 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001462 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001463 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001464 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1465 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001466 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001467 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001468 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001469 Line, Type, IsLocalToUnit, IsDefinition,
1470 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001471 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001472 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001473 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001474 Line + 1, Type, IsLocalToUnit, IsDefinition,
1475 ScopeLine, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001476 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001477 TemplateParams, Declaration, Variables));
Peter Collingbourned4bff302015-11-05 22:03:56 +00001478 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1479 getSubroutineType(), IsLocalToUnit,
1480 IsDefinition, ScopeLine, ContainingType,
1481 Virtuality, VirtualIndex, Flags, IsOptimized,
1482 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001483 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001484 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1485 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001486 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001487 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001488 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001489 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1490 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001491 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001492 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001493 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001494 Type, IsLocalToUnit, IsDefinition,
1495 ScopeLine + 1, ContainingType, Virtuality,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001496 VirtualIndex, Flags, IsOptimized,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001497 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001498 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001499 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001500 getCompositeType(), Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001501 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001502 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001503 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001504 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1505 ContainingType, Virtuality + 1, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001506 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001507 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001508 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001509 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1510 ContainingType, Virtuality, VirtualIndex + 1,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001511 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001512 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001513 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001514 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1515 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001516 ~Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001517 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001518 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001519 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1520 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001521 Flags, !IsOptimized, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001522 Declaration, Variables));
Peter Collingbourned4bff302015-11-05 22:03:56 +00001523 EXPECT_NE(N,
1524 DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1525 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1526 ContainingType, Virtuality, VirtualIndex, Flags,
1527 IsOptimized, getTuple(), Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001528 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001529 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1530 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001531 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001532 getSubprogram(), Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001533 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001534 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1535 ContainingType, Virtuality, VirtualIndex,
Peter Collingbourned4bff302015-11-05 22:03:56 +00001536 Flags, IsOptimized, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001537 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001538
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001539 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001540 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001541}
1542
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001543typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001544
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001545TEST_F(DILexicalBlockTest, get) {
1546 DILocalScope *Scope = getSubprogram();
1547 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001548 unsigned Line = 5;
1549 unsigned Column = 8;
1550
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001551 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001552
1553 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1554 EXPECT_EQ(Scope, N->getScope());
1555 EXPECT_EQ(File, N->getFile());
1556 EXPECT_EQ(Line, N->getLine());
1557 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001558 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001559
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001560 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001561 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1562 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1563 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1564 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001565
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001566 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001567 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001568}
1569
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +00001570TEST_F(DILexicalBlockTest, Overflow) {
1571 DISubprogram *SP = getSubprogram();
1572 DIFile *F = getFile();
1573 {
1574 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1575 EXPECT_EQ(2u, LB->getLine());
1576 EXPECT_EQ(7u, LB->getColumn());
1577 }
1578 unsigned U16 = 1u << 16;
1579 {
1580 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1581 EXPECT_EQ(UINT32_MAX, LB->getLine());
1582 EXPECT_EQ(U16 - 1, LB->getColumn());
1583 }
1584 {
1585 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1586 EXPECT_EQ(UINT32_MAX, LB->getLine());
1587 EXPECT_EQ(0u, LB->getColumn());
1588 }
1589 {
1590 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1591 EXPECT_EQ(UINT32_MAX, LB->getLine());
1592 EXPECT_EQ(0u, LB->getColumn());
1593 }
1594}
1595
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001596typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001597
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001598TEST_F(DILexicalBlockFileTest, get) {
1599 DILocalScope *Scope = getSubprogram();
1600 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001601 unsigned Discriminator = 5;
1602
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001603 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001604
1605 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1606 EXPECT_EQ(Scope, N->getScope());
1607 EXPECT_EQ(File, N->getFile());
1608 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001609 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001610
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001611 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001612 Discriminator));
1613 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001614 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001615 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001616 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001617
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001618 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001619 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001620}
1621
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001622typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001623
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001624TEST_F(DINamespaceTest, get) {
1625 DIScope *Scope = getFile();
1626 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001627 StringRef Name = "namespace";
1628 unsigned Line = 5;
1629
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001630 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001631
1632 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1633 EXPECT_EQ(Scope, N->getScope());
1634 EXPECT_EQ(File, N->getFile());
1635 EXPECT_EQ(Name, N->getName());
1636 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001637 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001638
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001639 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1640 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1641 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1642 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001643
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001644 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001645 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001646}
1647
Adrian Prantlab1243f2015-06-29 23:03:47 +00001648typedef MetadataTest DIModuleTest;
1649
1650TEST_F(DIModuleTest, get) {
1651 DIScope *Scope = getFile();
1652 StringRef Name = "module";
1653 StringRef ConfigMacro = "-DNDEBUG";
1654 StringRef Includes = "-I.";
1655 StringRef Sysroot = "/";
1656
1657 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1658
1659 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1660 EXPECT_EQ(Scope, N->getScope());
1661 EXPECT_EQ(Name, N->getName());
1662 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1663 EXPECT_EQ(Includes, N->getIncludePath());
1664 EXPECT_EQ(Sysroot, N->getISysRoot());
1665 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1666 ConfigMacro, Includes, Sysroot));
1667 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1668 ConfigMacro, Includes, Sysroot));
1669 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1670 ConfigMacro, Includes, Sysroot));
1671 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1672 "other", Includes, Sysroot));
1673 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1674 ConfigMacro, "other", Sysroot));
1675 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1676 ConfigMacro, Includes, "other"));
1677
1678 TempDIModule Temp = N->clone();
1679 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1680}
1681
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001682typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001683
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001684TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001685 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001686 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001687
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001688 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001689
1690 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001691 EXPECT_EQ(Name, N->getName());
1692 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001693 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001694
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001695 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001696 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001697 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001698
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001699 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001700 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001701}
1702
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001703typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001704
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001705TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001706 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001707 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001708 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001709 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001710
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001711 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001712 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001713 EXPECT_EQ(Name, N->getName());
1714 EXPECT_EQ(Type, N->getType());
1715 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001716 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001717
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001718 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001719 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1720 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001721 EXPECT_NE(N,
1722 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1723 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001724 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001725 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001726 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001727
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001728 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001729 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001730}
1731
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001732typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001733
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001734TEST_F(DIGlobalVariableTest, get) {
1735 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001736 StringRef Name = "name";
1737 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001738 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001739 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001740 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001741 bool IsLocalToUnit = false;
1742 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001743 Constant *Variable = getConstant();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001744 DIDerivedType *StaticDataMemberDeclaration =
1745 cast<DIDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001746
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001747 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001748 Type, IsLocalToUnit, IsDefinition, Variable,
1749 StaticDataMemberDeclaration);
1750 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1751 EXPECT_EQ(Scope, N->getScope());
1752 EXPECT_EQ(Name, N->getName());
1753 EXPECT_EQ(LinkageName, N->getLinkageName());
1754 EXPECT_EQ(File, N->getFile());
1755 EXPECT_EQ(Line, N->getLine());
1756 EXPECT_EQ(Type, N->getType());
1757 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1758 EXPECT_EQ(IsDefinition, N->isDefinition());
1759 EXPECT_EQ(Variable, N->getVariable());
1760 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001761 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001762 Line, Type, IsLocalToUnit, IsDefinition,
1763 Variable, StaticDataMemberDeclaration));
1764
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001765 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001766 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001767 File, Line, Type, IsLocalToUnit, IsDefinition,
1768 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001769 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001770 Line, Type, IsLocalToUnit, IsDefinition,
1771 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001772 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001773 Type, IsLocalToUnit, IsDefinition,
1774 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001775 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001776 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001777 Line, Type, IsLocalToUnit, IsDefinition,
1778 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001779 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001780 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001781 Line + 1, Type, IsLocalToUnit, IsDefinition,
1782 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001783 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001784 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001785 getDerivedType(), IsLocalToUnit, IsDefinition,
1786 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001787 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001788 Line, Type, !IsLocalToUnit, IsDefinition,
1789 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001790 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001791 Line, Type, IsLocalToUnit, !IsDefinition,
1792 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001793 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001794 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001795 Type, IsLocalToUnit, IsDefinition,
1796 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001797 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001799 Type, IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001800 cast<DIDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001801
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001802 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001803 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001804}
1805
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001806typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001807
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001808TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001809 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001810 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001811 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001812 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001813 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001814 unsigned Arg = 6;
1815 unsigned Flags = 7;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001816
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001817 auto *N =
1818 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags);
1819 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001820 EXPECT_EQ(Scope, N->getScope());
1821 EXPECT_EQ(Name, N->getName());
1822 EXPECT_EQ(File, N->getFile());
1823 EXPECT_EQ(Line, N->getLine());
1824 EXPECT_EQ(Type, N->getType());
1825 EXPECT_EQ(Arg, N->getArg());
1826 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001827 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1828 Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001829
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001830 EXPECT_FALSE(
1831 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags)
1832 ->isParameter());
1833 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001834 Type, Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001835 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
1836 Arg, Flags));
1837 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
1838 Arg, Flags));
1839 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
1840 Arg, Flags));
1841 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001842 getDerivedType(), Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001843 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001844 Arg + 1, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001845 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1846 ~Flags));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001847
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001848 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001849 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001850}
1851
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001852TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001853 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1854 0, nullptr, 255, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001855 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001856 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1857 0, nullptr, 256, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001858 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001859 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1860 0, nullptr, 257, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001861 ->getArg());
1862 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001863 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1864 0, nullptr, Max, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001865 ->getArg());
1866}
1867
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001868typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001869
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001870TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001871 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001872 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001873 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001874 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001875
1876 EXPECT_EQ(5u, N->getNumElements());
1877 EXPECT_EQ(2u, N->getElement(0));
1878 EXPECT_EQ(6u, N->getElement(1));
1879 EXPECT_EQ(9u, N->getElement(2));
1880 EXPECT_EQ(78u, N->getElement(3));
1881 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001882
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001883 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001884 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001885}
1886
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001887TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001888#define EXPECT_VALID(...) \
1889 do { \
1890 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001891 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001892 } while (false)
1893#define EXPECT_INVALID(...) \
1894 do { \
1895 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001896 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001897 } while (false)
1898
1899 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001900 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001901
1902 // Valid constructions.
1903 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1904 EXPECT_VALID(dwarf::DW_OP_deref);
1905 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1906 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1907 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1908 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1909 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1910
1911 // Invalid constructions.
1912 EXPECT_INVALID(~0u);
1913 EXPECT_INVALID(dwarf::DW_OP_plus);
1914 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1915 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1916 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1917 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1918
1919#undef EXPECT_VALID
1920#undef EXPECT_INVALID
1921}
1922
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001923typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001924
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001925TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001926 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001927 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001928 unsigned Line = 5;
1929 StringRef GetterName = "getter";
1930 StringRef SetterName = "setter";
1931 unsigned Attributes = 7;
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001932 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001933
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001934 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001935 SetterName, Attributes, Type);
1936
1937 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1938 EXPECT_EQ(Name, N->getName());
1939 EXPECT_EQ(File, N->getFile());
1940 EXPECT_EQ(Line, N->getLine());
1941 EXPECT_EQ(GetterName, N->getGetterName());
1942 EXPECT_EQ(SetterName, N->getSetterName());
1943 EXPECT_EQ(Attributes, N->getAttributes());
1944 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001945 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001946 SetterName, Attributes, Type));
1947
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001948 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001949 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001950 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001951 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001952 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001953 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001954 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001955 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001956 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001957 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001958 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001959 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001960 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001961 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001962 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001963
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001964 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001965 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001966}
1967
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001968typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001969
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001970TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001971 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001972 DIScope *Scope = getSubprogram();
1973 DINodeRef Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001974 unsigned Line = 5;
1975 StringRef Name = "name";
1976
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001977 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001978
1979 EXPECT_EQ(Tag, N->getTag());
1980 EXPECT_EQ(Scope, N->getScope());
1981 EXPECT_EQ(Entity, N->getEntity());
1982 EXPECT_EQ(Line, N->getLine());
1983 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001984 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001985
1986 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001987 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001988 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001989 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001990 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001991 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001992 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001993 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001994 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001995 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001996 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001997
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001998 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001999 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002000}
2001
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002002typedef MetadataTest MetadataAsValueTest;
2003
2004TEST_F(MetadataAsValueTest, MDNode) {
2005 MDNode *N = MDNode::get(Context, None);
2006 auto *V = MetadataAsValue::get(Context, N);
2007 EXPECT_TRUE(V->getType()->isMetadataTy());
2008 EXPECT_EQ(N, V->getMetadata());
2009
2010 auto *V2 = MetadataAsValue::get(Context, N);
2011 EXPECT_EQ(V, V2);
2012}
2013
2014TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2015 MDNode *N = MDNode::get(Context, None);
2016 Metadata *Ops[] = {N};
2017 MDNode *N2 = MDNode::get(Context, Ops);
2018 auto *V = MetadataAsValue::get(Context, N2);
2019 EXPECT_TRUE(V->getType()->isMetadataTy());
2020 EXPECT_EQ(N2, V->getMetadata());
2021
2022 auto *V2 = MetadataAsValue::get(Context, N2);
2023 EXPECT_EQ(V, V2);
2024
2025 auto *V3 = MetadataAsValue::get(Context, N);
2026 EXPECT_TRUE(V3->getType()->isMetadataTy());
2027 EXPECT_NE(V, V3);
2028 EXPECT_EQ(N, V3->getMetadata());
2029}
2030
2031TEST_F(MetadataAsValueTest, MDNodeConstant) {
2032 auto *C = ConstantInt::getTrue(Context);
2033 auto *MD = ConstantAsMetadata::get(C);
2034 Metadata *Ops[] = {MD};
2035 auto *N = MDNode::get(Context, Ops);
2036
2037 auto *V = MetadataAsValue::get(Context, MD);
2038 EXPECT_TRUE(V->getType()->isMetadataTy());
2039 EXPECT_EQ(MD, V->getMetadata());
2040
2041 auto *V2 = MetadataAsValue::get(Context, N);
2042 EXPECT_EQ(MD, V2->getMetadata());
2043 EXPECT_EQ(V, V2);
2044}
2045
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002046typedef MetadataTest ValueAsMetadataTest;
2047
2048TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2049 Type *Ty = Type::getInt1PtrTy(Context);
2050 std::unique_ptr<GlobalVariable> GV0(
2051 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2052 auto *MD = ValueAsMetadata::get(GV0.get());
2053 EXPECT_TRUE(MD->getValue() == GV0.get());
2054 ASSERT_TRUE(GV0->use_empty());
2055
2056 std::unique_ptr<GlobalVariable> GV1(
2057 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2058 GV0->replaceAllUsesWith(GV1.get());
2059 EXPECT_TRUE(MD->getValue() == GV1.get());
2060}
2061
Adrian Prantlcbec1602016-02-08 17:02:34 +00002062TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2063 // Create a constant.
2064 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2065 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2066
Adrian Prantlcbec1602016-02-08 17:02:34 +00002067 auto Temp1 = MDTuple::getTemporary(Context, None);
Adrian Prantl817c47b2016-02-08 19:13:15 +00002068 auto Temp2 = MDTuple::getTemporary(Context, {CI});
2069 auto *N = MDTuple::get(Context, {Temp1.get()});
Adrian Prantlcbec1602016-02-08 17:02:34 +00002070
2071 // Test replacing a temporary node with another temporary node.
2072 Temp1->replaceAllUsesWith(Temp2.get());
2073 EXPECT_EQ(N->getOperand(0), Temp2.get());
2074
2075 // Clean up Temp2 for teardown.
2076 Temp2->replaceAllUsesWith(nullptr);
2077}
2078
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002079TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2080 // Create a constant.
2081 ConstantAsMetadata *CI = ConstantAsMetadata::get(
2082 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
2083
2084 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002085 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002086
2087 // 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 +00002088 Metadata *Ops1[] = {CI, CI, Temp.get()};
2089 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002090
2091 auto *N1 = MDTuple::get(Context, Ops1);
2092 auto *N2 = MDTuple::get(Context, Ops2);
2093 ASSERT_NE(N1, N2);
2094
2095 // Tell metadata that the constant is getting deleted.
2096 //
2097 // After this, N1 will be invalid, so don't touch it.
2098 ValueAsMetadata::handleDeletion(CI->getValue());
2099 EXPECT_EQ(nullptr, N2->getOperand(0));
2100 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002101 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002102
2103 // Clean up Temp for teardown.
2104 Temp->replaceAllUsesWith(nullptr);
2105}
2106
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002107typedef MetadataTest TrackingMDRefTest;
2108
2109TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2110 Type *Ty = Type::getInt1PtrTy(Context);
2111 std::unique_ptr<GlobalVariable> GV0(
2112 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2113 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2114 EXPECT_TRUE(MD->getValue() == GV0.get());
2115 ASSERT_TRUE(GV0->use_empty());
2116
2117 std::unique_ptr<GlobalVariable> GV1(
2118 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2119 GV0->replaceAllUsesWith(GV1.get());
2120 EXPECT_TRUE(MD->getValue() == GV1.get());
2121
2122 // Reset it, so we don't inadvertently test deletion.
2123 MD.reset();
2124}
2125
2126TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2127 Type *Ty = Type::getInt1PtrTy(Context);
2128 std::unique_ptr<GlobalVariable> GV(
2129 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2130 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2131 EXPECT_TRUE(MD->getValue() == GV.get());
2132 ASSERT_TRUE(GV->use_empty());
2133
2134 GV.reset();
2135 EXPECT_TRUE(!MD);
2136}
2137
Devang Patel0924b332009-07-30 00:03:41 +00002138TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002139 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002140 ConstantAsMetadata *C =
2141 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2142 ConstantAsMetadata *C2 =
2143 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002144
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002145 Metadata *const V = C;
2146 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002147 MDNode *n = MDNode::get(Context, V);
2148 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002149
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002150 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002151 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002152 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2153 NMD->addOperand(n);
2154 NMD->addOperand(n2);
2155
Chris Lattnerbe354a62009-08-23 04:47:35 +00002156 std::string Str;
2157 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002158 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002159 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002160 oss.str().c_str());
2161}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002162
2163typedef MetadataTest FunctionAttachmentTest;
2164TEST_F(FunctionAttachmentTest, setMetadata) {
2165 Function *F = getFunction("foo");
2166 ASSERT_FALSE(F->hasMetadata());
2167 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2168 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2169 EXPECT_EQ(nullptr, F->getMetadata("other"));
2170
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002171 DISubprogram *SP1 = getSubprogram();
2172 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002173 ASSERT_NE(SP1, SP2);
2174
2175 F->setMetadata("dbg", SP1);
2176 EXPECT_TRUE(F->hasMetadata());
2177 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2178 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2179 EXPECT_EQ(nullptr, F->getMetadata("other"));
2180
2181 F->setMetadata(LLVMContext::MD_dbg, SP2);
2182 EXPECT_TRUE(F->hasMetadata());
2183 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2184 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2185 EXPECT_EQ(nullptr, F->getMetadata("other"));
2186
2187 F->setMetadata("dbg", nullptr);
2188 EXPECT_FALSE(F->hasMetadata());
2189 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2190 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2191 EXPECT_EQ(nullptr, F->getMetadata("other"));
2192
2193 MDTuple *T1 = getTuple();
2194 MDTuple *T2 = getTuple();
2195 ASSERT_NE(T1, T2);
2196
2197 F->setMetadata("other1", T1);
2198 F->setMetadata("other2", T2);
2199 EXPECT_TRUE(F->hasMetadata());
2200 EXPECT_EQ(T1, F->getMetadata("other1"));
2201 EXPECT_EQ(T2, F->getMetadata("other2"));
2202 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2203
2204 F->setMetadata("other1", T2);
2205 F->setMetadata("other2", T1);
2206 EXPECT_EQ(T2, F->getMetadata("other1"));
2207 EXPECT_EQ(T1, F->getMetadata("other2"));
2208
2209 F->setMetadata("other1", nullptr);
2210 F->setMetadata("other2", nullptr);
2211 EXPECT_FALSE(F->hasMetadata());
2212 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2213 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2214}
2215
2216TEST_F(FunctionAttachmentTest, getAll) {
2217 Function *F = getFunction("foo");
2218
2219 MDTuple *T1 = getTuple();
2220 MDTuple *T2 = getTuple();
2221 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002222 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002223
2224 F->setMetadata("other1", T2);
2225 F->setMetadata(LLVMContext::MD_dbg, SP);
2226 F->setMetadata("other2", T1);
2227 F->setMetadata(LLVMContext::MD_prof, P);
2228 F->setMetadata("other2", T2);
2229 F->setMetadata("other1", T1);
2230
2231 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2232 F->getAllMetadata(MDs);
2233 ASSERT_EQ(4u, MDs.size());
2234 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2235 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2236 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2237 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2238 EXPECT_EQ(SP, MDs[0].second);
2239 EXPECT_EQ(P, MDs[1].second);
2240 EXPECT_EQ(T1, MDs[2].second);
2241 EXPECT_EQ(T2, MDs[3].second);
2242}
2243
2244TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2245 Function *F = getFunction("foo");
2246
2247 MDTuple *T1 = getTuple();
2248 MDTuple *T2 = getTuple();
2249 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002250 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002251
2252 F->setMetadata("other1", T1);
2253 F->setMetadata(LLVMContext::MD_dbg, SP);
2254 F->setMetadata("other2", T2);
2255 F->setMetadata(LLVMContext::MD_prof, P);
2256
2257 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2258 F->dropUnknownMetadata(Known);
2259
2260 EXPECT_EQ(T2, F->getMetadata("other2"));
2261 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2262 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2263 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2264
2265 F->setMetadata("other2", nullptr);
2266 F->setMetadata(LLVMContext::MD_prof, nullptr);
2267 EXPECT_FALSE(F->hasMetadata());
2268}
2269
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002270TEST_F(FunctionAttachmentTest, Verifier) {
2271 Function *F = getFunction("foo");
2272 F->setMetadata("attach", getTuple());
2273
2274 // Confirm this has no body.
2275 ASSERT_TRUE(F->empty());
2276
2277 // Functions without a body cannot have metadata attachments (they also can't
2278 // be verified directly, so check that the module fails to verify).
2279 EXPECT_TRUE(verifyModule(*F->getParent()));
2280
2281 // Functions with a body can.
2282 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2283 EXPECT_FALSE(verifyModule(*F->getParent()));
2284 EXPECT_FALSE(verifyFunction(*F));
2285}
2286
Diego Novillo2567f3d2015-05-13 15:13:45 +00002287TEST_F(FunctionAttachmentTest, EntryCount) {
2288 Function *F = getFunction("foo");
2289 EXPECT_FALSE(F->getEntryCount().hasValue());
2290 F->setEntryCount(12304);
2291 EXPECT_TRUE(F->getEntryCount().hasValue());
2292 EXPECT_EQ(12304u, *F->getEntryCount());
2293}
2294
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002295TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2296 Function *F = getFunction("foo");
2297 DISubprogram *SP = getSubprogram();
2298 F->setSubprogram(SP);
2299
2300 // Note that the static_cast confirms that F->getSubprogram() actually
2301 // returns an DISubprogram.
2302 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2303 EXPECT_EQ(SP, F->getMetadata("dbg"));
2304 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2305}
2306
Nick Lewycky49f89192009-04-04 07:22:01 +00002307}