blob: 0f710974939928b2e50aa4e268c6fdf318b1b3f3 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Nick Lewycky49f89192009-04-04 07:22:01 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth9a67b072017-06-06 11:06:56 +00009#include "llvm/IR/Metadata.h"
stozer269a9af2019-12-03 12:24:41 +000010#include "llvm/ADT/DenseMap.h"
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000013#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000014#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000015#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Instructions.h"
17#include "llvm/IR/LLVMContext.h"
18#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;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000038 ContextAndReplaceableUses CRU(std::make_unique<ReplaceableMetadataImpl>(Context));
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000039 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);
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000047 CRU.makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(Context));
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000048 EXPECT_EQ(&Context, &CRU.getContext());
49 EXPECT_TRUE(CRU.hasReplaceableUses());
50 EXPECT_TRUE(CRU.getReplaceableUses());
51}
52
53TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
54 LLVMContext Context;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000055 auto ReplaceableUses = std::make_unique<ReplaceableMetadataImpl>(Context);
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000056 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() {
Leny Kholodov40c62352016-09-06 17:03:02 +000083 return DISubroutineType::getDistinct(Context, DINode::FlagZero, 0,
84 getNode(nullptr));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000085 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000086 DISubprogram *getSubprogram() {
Paul Robinsoncda54212018-11-19 18:29:28 +000087 return DISubprogram::getDistinct(
88 Context, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0,
89 DINode::FlagZero, DISubprogram::SPFlagZero, nullptr);
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000090 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000091 DIFile *getFile() {
92 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000093 }
Adrian Prantl75819ae2016-04-15 15:57:41 +000094 DICompileUnit *getUnit() {
Peter Collingbourneb52e2362017-09-12 21:50:41 +000095 return DICompileUnit::getDistinct(
96 Context, 1, getFile(), "clang", false, "-g", 2, "",
97 DICompileUnit::FullDebug, getTuple(), getTuple(), getTuple(),
David Blaikie66cf14d2018-08-16 21:29:55 +000098 getTuple(), getTuple(), 0, true, false,
Adrian Prantle4e7e442020-03-04 14:12:54 -080099 DICompileUnit::DebugNameTableKind::Default, false, "/", "");
Adrian Prantl75819ae2016-04-15 15:57:41 +0000100 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000101 DIType *getBasicType(StringRef Name) {
102 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000103 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000104 DIType *getDerivedType() {
Leny Kholodov40c62352016-09-06 17:03:02 +0000105 return DIDerivedType::getDistinct(
106 Context, dwarf::DW_TAG_pointer_type, "", nullptr, 0, nullptr,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000107 getBasicType("basictype"), 1, 2, 0, None, DINode::FlagZero);
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000108 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000109 Constant *getConstant() {
110 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
111 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000112 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000113 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000114 }
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000115 DIType *getCompositeType() {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000116 return DICompositeType::getDistinct(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000117 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
Leny Kholodov5fcc4182016-09-06 10:46:28 +0000118 32, 32, 0, DINode::FlagZero, nullptr, 0, nullptr, nullptr, "");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000119 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000120 Function *getFunction(StringRef Name) {
James Y Knight13680222019-02-01 02:28:03 +0000121 return Function::Create(
122 FunctionType::get(Type::getVoidTy(Context), None, false),
123 Function::ExternalLinkage, Name, M);
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000124 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000125};
126typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000127
Nick Lewycky49f89192009-04-04 07:22:01 +0000128// Test that construction of MDString with different value produces different
129// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000130TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000131 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000132 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000133 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000134 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000135 EXPECT_NE(s1, s2);
136}
137
138// Test that creation of MDStrings with the same string contents produces the
139// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000140TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000141 char x[4] = { 'a', 'b', 'c', 'X' };
142 char y[4] = { 'a', 'b', 'c', 'Y' };
143
Owen Anderson23587322009-07-31 21:38:10 +0000144 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
145 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000146 EXPECT_EQ(s1, s2);
147}
148
149// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000150TEST_F(MDStringTest, PrintingSimple) {
Florian Hahn4238cd52018-11-25 19:38:02 +0000151 char str[14] = "testing 1 2 3";
152 MDString *s = MDString::get(Context, StringRef(&str[0], 13));
153 strncpy(str, "aaaaaaaaaaaaa", 14);
Nick Lewycky49f89192009-04-04 07:22:01 +0000154
Chris Lattnerbe354a62009-08-23 04:47:35 +0000155 std::string Str;
156 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000157 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000158 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000159}
160
161// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000162TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000163 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000164 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000165 std::string Str;
166 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000167 s->print(oss);
Reid Kleckner67d440b2019-10-10 18:31:57 +0000168 EXPECT_STREQ("!\"\\00\\0A\\22\\\\\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000169}
170
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000171typedef MetadataTest MDNodeTest;
172
Nick Lewycky49f89192009-04-04 07:22:01 +0000173// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000174TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000175 char x[3] = { 'a', 'b', 'c' };
176 char y[3] = { '1', '2', '3' };
177
Owen Anderson23587322009-07-31 21:38:10 +0000178 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
179 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Mehdi Amini03b42e42016-04-14 21:59:01 +0000180 ConstantAsMetadata *CI =
181 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000182
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000183 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000184 V.push_back(s1);
185 V.push_back(CI);
186 V.push_back(s2);
187
Jay Foad5514afe2011-04-21 19:59:31 +0000188 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000189 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000190 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000191 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000192 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000193 MDNode *n4 = MDNode::getIfExists(Context, V);
194 MDNode *n5 = MDNode::getIfExists(Context, c1);
195 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000196 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000197 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000198 EXPECT_EQ(n4, n1);
199 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000200 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000201
Chris Lattner9b493022009-12-31 01:22:29 +0000202 EXPECT_EQ(3u, n1->getNumOperands());
203 EXPECT_EQ(s1, n1->getOperand(0));
204 EXPECT_EQ(CI, n1->getOperand(1));
205 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000206
Chris Lattner9b493022009-12-31 01:22:29 +0000207 EXPECT_EQ(1u, n2->getNumOperands());
208 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000209}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000210
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000211TEST_F(MDNodeTest, Delete) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000212 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
213 Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000214
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000215 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000216 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000217 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000218
219 EXPECT_EQ(n, wvh);
220
Reid Kleckner96ab8722017-05-18 17:24:10 +0000221 I->deleteValue();
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000222}
Devang Patel0924b332009-07-30 00:03:41 +0000223
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000224TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000225 // !0 = !{!0}
226 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000227 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000228 auto Temp = MDNode::getTemporary(Context, None);
229 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000230 MDNode *Self = MDNode::get(Context, Args);
231 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000232 ASSERT_EQ(Self, Self->getOperand(0));
233
234 // Self-references should be distinct, so MDNode::get() should grab a
235 // uniqued node that references Self, not Self.
236 Args[0] = Self;
237 MDNode *Ref1 = MDNode::get(Context, Args);
238 MDNode *Ref2 = MDNode::get(Context, Args);
239 EXPECT_NE(Self, Ref1);
240 EXPECT_EQ(Ref1, Ref2);
241 }
242
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000243 // !0 = !{!0, !{}}
244 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000245 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000246 auto Temp = MDNode::getTemporary(Context, None);
247 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000248 MDNode *Self = MDNode::get(Context, Args);
249 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000250 ASSERT_EQ(Self, Self->getOperand(0));
251
252 // Self-references should be distinct, so MDNode::get() should grab a
253 // uniqued node that references Self, not Self itself.
254 Args[0] = Self;
255 MDNode *Ref1 = MDNode::get(Context, Args);
256 MDNode *Ref2 = MDNode::get(Context, Args);
257 EXPECT_NE(Self, Ref1);
258 EXPECT_EQ(Ref1, Ref2);
259 }
260}
261
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000262TEST_F(MDNodeTest, Print) {
263 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
264 MDString *S = MDString::get(Context, "foo");
265 MDNode *N0 = getNode();
266 MDNode *N1 = getNode(N0);
267 MDNode *N2 = getNode(N0, N1);
268
269 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
270 MDNode *N = MDNode::get(Context, Args);
271
272 std::string Expected;
273 {
274 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000275 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000276 C->printAsOperand(OS);
277 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000278 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000279 OS << ", null";
280 MDNode *Nodes[] = {N0, N1, N2};
281 for (auto *Node : Nodes)
282 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000283 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000284 }
285
286 std::string Actual;
287 {
288 raw_string_ostream OS(Actual);
289 N->print(OS);
290 }
291
292 EXPECT_EQ(Expected, Actual);
293}
294
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000295#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
296 do { \
297 std::string Actual_; \
298 raw_string_ostream OS(Actual_); \
299 PRINT; \
300 OS.flush(); \
301 std::string Expected_(EXPECTED); \
302 EXPECT_EQ(Expected_, Actual_); \
303 } while (false)
304
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000305TEST_F(MDNodeTest, PrintTemporary) {
306 MDNode *Arg = getNode();
307 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
308 MDNode *N = getNode(Temp.get());
309 Module M("test", Context);
310 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
311 NMD->addOperand(N);
312
313 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
314 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
315 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
316
317 // Cleanup.
318 Temp->replaceAllUsesWith(Arg);
319}
320
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000321TEST_F(MDNodeTest, PrintFromModule) {
322 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
323 MDString *S = MDString::get(Context, "foo");
324 MDNode *N0 = getNode();
325 MDNode *N1 = getNode(N0);
326 MDNode *N2 = getNode(N0, N1);
327
328 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
329 MDNode *N = MDNode::get(Context, Args);
330 Module M("test", Context);
331 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
332 NMD->addOperand(N);
333
334 std::string Expected;
335 {
336 raw_string_ostream OS(Expected);
337 OS << "!0 = !{";
338 C->printAsOperand(OS);
339 OS << ", ";
340 S->printAsOperand(OS);
341 OS << ", null, !1, !2, !3}";
342 }
343
344 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
345}
346
347TEST_F(MDNodeTest, PrintFromFunction) {
348 Module M("test", Context);
349 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
350 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
351 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
352 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
353 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
354 auto *R0 = ReturnInst::Create(Context, BB0);
355 auto *R1 = ReturnInst::Create(Context, BB1);
356 auto *N0 = MDNode::getDistinct(Context, None);
357 auto *N1 = MDNode::getDistinct(Context, None);
358 R0->setMetadata("md", N0);
359 R1->setMetadata("md", N1);
360
361 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
362 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000363
364 ModuleSlotTracker MST(&M);
365 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
366 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000367}
368
369TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
370 Module M("test", Context);
371
372 auto *Intrinsic =
373 Function::Create(FunctionType::get(Type::getVoidTy(Context),
374 Type::getMetadataTy(Context), false),
375 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
376
377 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
378 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
379 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
380 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
381 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
382 auto *N0 = MDNode::getDistinct(Context, None);
383 auto *N1 = MDNode::getDistinct(Context, None);
384 auto *MAV0 = MetadataAsValue::get(Context, N0);
385 auto *MAV1 = MetadataAsValue::get(Context, N1);
386 CallInst::Create(Intrinsic, MAV0, "", BB0);
387 CallInst::Create(Intrinsic, MAV1, "", BB1);
388
389 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
390 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
391 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
392 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
393 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
394 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000395
396 ModuleSlotTracker MST(&M);
397 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
398 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
399 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
400 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
401 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
402 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000403}
David Stenberg5595b1e2018-11-02 11:46:24 +0000404
405TEST_F(MDNodeTest, PrintWithDroppedCallOperand) {
406 Module M("test", Context);
407
408 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
409 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
410 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
411 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
412
413 CallInst *CI0 = CallInst::Create(F1, "", BB0);
414 CI0->dropAllReferences();
415
416 auto *R0 = ReturnInst::Create(Context, BB0);
417 auto *N0 = MDNode::getDistinct(Context, None);
418 R0->setMetadata("md", N0);
419
420 // Printing the metadata node would previously result in a failed assertion
421 // due to the call instruction's dropped function operand.
422 ModuleSlotTracker MST(&M);
423 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
424}
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000425#undef EXPECT_PRINTER_EQ
426
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000427TEST_F(MDNodeTest, NullOperand) {
428 // metadata !{}
429 MDNode *Empty = MDNode::get(Context, None);
430
431 // metadata !{metadata !{}}
432 Metadata *Ops[] = {Empty};
433 MDNode *N = MDNode::get(Context, Ops);
434 ASSERT_EQ(Empty, N->getOperand(0));
435
436 // metadata !{metadata !{}} => metadata !{null}
437 N->replaceOperandWith(0, nullptr);
438 ASSERT_EQ(nullptr, N->getOperand(0));
439
440 // metadata !{null}
441 Ops[0] = nullptr;
442 MDNode *NullOp = MDNode::get(Context, Ops);
443 ASSERT_EQ(nullptr, NullOp->getOperand(0));
444 EXPECT_EQ(N, NullOp);
445}
446
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000447TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
448 // !{}
449 MDNode *Empty = MDNode::get(Context, None);
450 ASSERT_TRUE(Empty->isResolved());
451 EXPECT_FALSE(Empty->isDistinct());
452
453 // !{!{}}
454 Metadata *Wrapped1Ops[] = {Empty};
455 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
456 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
457 ASSERT_TRUE(Wrapped1->isResolved());
458 EXPECT_FALSE(Wrapped1->isDistinct());
459
460 // !{!{!{}}}
461 Metadata *Wrapped2Ops[] = {Wrapped1};
462 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
463 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
464 ASSERT_TRUE(Wrapped2->isResolved());
465 EXPECT_FALSE(Wrapped2->isDistinct());
466
467 // !{!{!{}}} => !{!{}}
468 Wrapped2->replaceOperandWith(0, Empty);
469 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
470 EXPECT_TRUE(Wrapped2->isDistinct());
471 EXPECT_FALSE(Wrapped1->isDistinct());
472}
473
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000474TEST_F(MDNodeTest, UniquedOnDeletedOperand) {
475 // temp !{}
476 TempMDTuple T = MDTuple::getTemporary(Context, None);
477
478 // !{temp !{}}
479 Metadata *Ops[] = {T.get()};
480 MDTuple *N = MDTuple::get(Context, Ops);
481
482 // !{temp !{}} => !{null}
483 T.reset();
484 ASSERT_TRUE(N->isUniqued());
485 Metadata *NullOps[] = {nullptr};
486 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
487}
488
489TEST_F(MDNodeTest, DistinctOnDeletedValueOperand) {
490 // i1* @GV
491 Type *Ty = Type::getInt1PtrTy(Context);
492 std::unique_ptr<GlobalVariable> GV(
493 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
494 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
495
496 // !{i1* @GV}
497 Metadata *Ops[] = {Op};
498 MDTuple *N = MDTuple::get(Context, Ops);
499
500 // !{i1* @GV} => !{null}
501 GV.reset();
502 ASSERT_TRUE(N->isDistinct());
503 ASSERT_EQ(nullptr, N->getOperand(0));
504 Metadata *NullOps[] = {nullptr};
505 ASSERT_NE(N, MDTuple::get(Context, NullOps));
506}
507
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000508TEST_F(MDNodeTest, getDistinct) {
509 // !{}
510 MDNode *Empty = MDNode::get(Context, None);
511 ASSERT_TRUE(Empty->isResolved());
512 ASSERT_FALSE(Empty->isDistinct());
513 ASSERT_EQ(Empty, MDNode::get(Context, None));
514
515 // distinct !{}
516 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
517 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
518 EXPECT_TRUE(Distinct1->isResolved());
519 EXPECT_TRUE(Distinct2->isDistinct());
520 EXPECT_NE(Empty, Distinct1);
521 EXPECT_NE(Empty, Distinct2);
522 EXPECT_NE(Distinct1, Distinct2);
523
524 // !{}
525 ASSERT_EQ(Empty, MDNode::get(Context, None));
526}
527
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000528TEST_F(MDNodeTest, isUniqued) {
529 MDNode *U = MDTuple::get(Context, None);
530 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000531 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000532 EXPECT_TRUE(U->isUniqued());
533 EXPECT_FALSE(D->isUniqued());
534 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000535}
536
537TEST_F(MDNodeTest, isDistinct) {
538 MDNode *U = MDTuple::get(Context, None);
539 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000540 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000541 EXPECT_FALSE(U->isDistinct());
542 EXPECT_TRUE(D->isDistinct());
543 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000544}
545
546TEST_F(MDNodeTest, isTemporary) {
547 MDNode *U = MDTuple::get(Context, None);
548 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000549 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000550 EXPECT_FALSE(U->isTemporary());
551 EXPECT_FALSE(D->isTemporary());
552 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000553}
554
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000555TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
556 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000557 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000558 ASSERT_FALSE(Temp->isResolved());
559
560 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000561 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000562 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
563 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000564 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000565
566 // temporary !{} => !{}
567 MDNode *Empty = MDNode::get(Context, None);
568 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000569 EXPECT_EQ(Empty, Distinct->getOperand(0));
570}
571
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000572TEST_F(MDNodeTest, handleChangedOperandRecursion) {
573 // !0 = !{}
574 MDNode *N0 = MDNode::get(Context, None);
575
576 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000577 auto Temp3 = MDTuple::getTemporary(Context, None);
578 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000579 MDNode *N1 = MDNode::get(Context, Ops1);
580
581 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000582 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000583 MDNode *N2 = MDNode::get(Context, Ops2);
584
585 // !3 = !{!2}
586 Metadata *Ops3[] = {N2};
587 MDNode *N3 = MDNode::get(Context, Ops3);
588 Temp3->replaceAllUsesWith(N3);
589
590 // !4 = !{!1}
591 Metadata *Ops4[] = {N1};
592 MDNode *N4 = MDNode::get(Context, Ops4);
593
594 // Confirm that the cycle prevented RAUW from getting dropped.
595 EXPECT_TRUE(N0->isResolved());
596 EXPECT_FALSE(N1->isResolved());
597 EXPECT_FALSE(N2->isResolved());
598 EXPECT_FALSE(N3->isResolved());
599 EXPECT_FALSE(N4->isResolved());
600
601 // Create a couple of distinct nodes to observe what's going on.
602 //
603 // !5 = distinct !{!2}
604 // !6 = distinct !{!3}
605 Metadata *Ops5[] = {N2};
606 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
607 Metadata *Ops6[] = {N3};
608 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
609
610 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
611 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
612 // references !3, this can cause a re-entry of handleChangedOperand() when !3
613 // is not ready for it.
614 //
615 // !2->replaceOperandWith(1, nullptr)
616 // !2: !{!3, !0} => !{!3, null}
617 // !2->replaceAllUsesWith(!1)
618 // !3: !{!2] => !{!1}
619 // !3->replaceAllUsesWith(!4)
620 N2->replaceOperandWith(1, nullptr);
621
622 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
623 // under us. Just check that the other nodes are sane.
624 //
625 // !1 = !{!4, null}
626 // !4 = !{!1}
627 // !5 = distinct !{!1}
628 // !6 = distinct !{!4}
629 EXPECT_EQ(N4, N1->getOperand(0));
630 EXPECT_EQ(N1, N4->getOperand(0));
631 EXPECT_EQ(N1, N5->getOperand(0));
632 EXPECT_EQ(N4, N6->getOperand(0));
633}
634
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000635TEST_F(MDNodeTest, replaceResolvedOperand) {
636 // Check code for replacing one resolved operand with another. If doing this
637 // directly (via replaceOperandWith()) becomes illegal, change the operand to
638 // a global value that gets RAUW'ed.
639 //
640 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000641 auto Temp = MDTuple::getTemporary(Context, None);
642 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000643
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000644 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000645 MDNode *N = MDTuple::get(Context, Ops);
646 EXPECT_EQ(nullptr, N->getOperand(0));
647 ASSERT_FALSE(N->isResolved());
648
649 // Check code for replacing resolved nodes.
650 N->replaceOperandWith(0, Empty);
651 EXPECT_EQ(Empty, N->getOperand(0));
652
653 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000654 N->replaceOperandWith(0, Temp.get());
655 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000656
657 // Remove the references to Temp; required for teardown.
658 Temp->replaceAllUsesWith(nullptr);
659}
660
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000661TEST_F(MDNodeTest, replaceWithUniqued) {
662 auto *Empty = MDTuple::get(Context, None);
663 MDTuple *FirstUniqued;
664 {
665 Metadata *Ops[] = {Empty};
666 auto Temp = MDTuple::getTemporary(Context, Ops);
667 EXPECT_TRUE(Temp->isTemporary());
668
669 // Don't expect a collision.
670 auto *Current = Temp.get();
671 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
672 EXPECT_TRUE(FirstUniqued->isUniqued());
673 EXPECT_TRUE(FirstUniqued->isResolved());
674 EXPECT_EQ(Current, FirstUniqued);
675 }
676 {
677 Metadata *Ops[] = {Empty};
678 auto Temp = MDTuple::getTemporary(Context, Ops);
679 EXPECT_TRUE(Temp->isTemporary());
680
681 // Should collide with Uniqued above this time.
682 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
683 EXPECT_TRUE(Uniqued->isUniqued());
684 EXPECT_TRUE(Uniqued->isResolved());
685 EXPECT_EQ(FirstUniqued, Uniqued);
686 }
687 {
688 auto Unresolved = MDTuple::getTemporary(Context, None);
689 Metadata *Ops[] = {Unresolved.get()};
690 auto Temp = MDTuple::getTemporary(Context, Ops);
691 EXPECT_TRUE(Temp->isTemporary());
692
693 // Shouldn't be resolved.
694 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
695 EXPECT_TRUE(Uniqued->isUniqued());
696 EXPECT_FALSE(Uniqued->isResolved());
697
698 // Should be a different node.
699 EXPECT_NE(FirstUniqued, Uniqued);
700
701 // Should resolve when we update its node (note: be careful to avoid a
702 // collision with any other nodes above).
703 Uniqued->replaceOperandWith(0, nullptr);
704 EXPECT_TRUE(Uniqued->isResolved());
705 }
706}
707
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000708TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000709 // temp !{}
710 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
711 EXPECT_FALSE(Op->isResolved());
712
713 // temp !{temp !{}}
714 Metadata *Ops[] = {Op};
715 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
716 EXPECT_FALSE(N->isResolved());
717
718 // temp !{temp !{}} => !{temp !{}}
719 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
720 EXPECT_FALSE(N->isResolved());
721
722 // !{temp !{}} => !{!{}}
723 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
724 EXPECT_TRUE(Op->isResolved());
725 EXPECT_TRUE(N->isResolved());
726}
727
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000728TEST_F(MDNodeTest, replaceWithUniquedDeletedOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000729 // i1* @GV
730 Type *Ty = Type::getInt1PtrTy(Context);
731 std::unique_ptr<GlobalVariable> GV(
732 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
733 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
734
735 // temp !{i1* @GV}
736 Metadata *Ops[] = {Op};
737 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
738
739 // temp !{i1* @GV} => !{i1* @GV}
740 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
741 ASSERT_TRUE(N->isUniqued());
742
743 // !{i1* @GV} => !{null}
744 GV.reset();
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000745 ASSERT_TRUE(N->isDistinct());
746 ASSERT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000747 Metadata *NullOps[] = {nullptr};
Duncan P. N. Exon Smith9cbc69d2016-08-03 18:19:43 +0000748 ASSERT_NE(N, MDTuple::get(Context, NullOps));
749}
750
751TEST_F(MDNodeTest, replaceWithUniquedChangedOperand) {
752 // i1* @GV
753 Type *Ty = Type::getInt1PtrTy(Context);
754 std::unique_ptr<GlobalVariable> GV(
755 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
756 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
757
758 // temp !{i1* @GV}
759 Metadata *Ops[] = {Op};
760 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
761
762 // temp !{i1* @GV} => !{i1* @GV}
763 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
764 ASSERT_TRUE(N->isUniqued());
765
766 // !{i1* @GV} => !{i1* @GV2}
767 std::unique_ptr<GlobalVariable> GV2(
768 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
769 GV->replaceAllUsesWith(GV2.get());
770 ASSERT_TRUE(N->isUniqued());
771 Metadata *NullOps[] = {ConstantAsMetadata::get(GV2.get())};
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000772 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
773}
774
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000775TEST_F(MDNodeTest, replaceWithDistinct) {
776 {
777 auto *Empty = MDTuple::get(Context, None);
778 Metadata *Ops[] = {Empty};
779 auto Temp = MDTuple::getTemporary(Context, Ops);
780 EXPECT_TRUE(Temp->isTemporary());
781
782 // Don't expect a collision.
783 auto *Current = Temp.get();
784 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
785 EXPECT_TRUE(Distinct->isDistinct());
786 EXPECT_TRUE(Distinct->isResolved());
787 EXPECT_EQ(Current, Distinct);
788 }
789 {
790 auto Unresolved = MDTuple::getTemporary(Context, None);
791 Metadata *Ops[] = {Unresolved.get()};
792 auto Temp = MDTuple::getTemporary(Context, Ops);
793 EXPECT_TRUE(Temp->isTemporary());
794
795 // Don't expect a collision.
796 auto *Current = Temp.get();
797 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
798 EXPECT_TRUE(Distinct->isDistinct());
799 EXPECT_TRUE(Distinct->isResolved());
800 EXPECT_EQ(Current, Distinct);
801
802 // Cleanup; required for teardown.
803 Unresolved->replaceAllUsesWith(nullptr);
804 }
805}
806
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000807TEST_F(MDNodeTest, replaceWithPermanent) {
808 Metadata *Ops[] = {nullptr};
809 auto Temp = MDTuple::getTemporary(Context, Ops);
810 auto *T = Temp.get();
811
812 // U is a normal, uniqued node that references T.
813 auto *U = MDTuple::get(Context, T);
814 EXPECT_TRUE(U->isUniqued());
815
816 // Make Temp self-referencing.
817 Temp->replaceOperandWith(0, T);
818
819 // Try to uniquify Temp. This should, despite the name in the API, give a
820 // 'distinct' node, since self-references aren't allowed to be uniqued.
821 //
822 // Since it's distinct, N should have the same address as when it was a
823 // temporary (i.e., be equal to T not U).
824 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
825 EXPECT_EQ(N, T);
826 EXPECT_TRUE(N->isDistinct());
827
828 // U should be the canonical unique node with N as the argument.
829 EXPECT_EQ(U, MDTuple::get(Context, N));
830 EXPECT_TRUE(U->isUniqued());
831
832 // This temporary should collide with U when replaced, but it should still be
833 // uniqued.
834 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
835 EXPECT_TRUE(U->isUniqued());
836
837 // This temporary should become a new uniqued node.
838 auto Temp2 = MDTuple::getTemporary(Context, U);
839 auto *V = Temp2.get();
840 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
841 EXPECT_TRUE(V->isUniqued());
842 EXPECT_EQ(U, V->getOperand(0));
843}
844
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000845TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
846 TrackingMDRef Ref;
847 EXPECT_EQ(nullptr, Ref.get());
848 {
849 auto Temp = MDTuple::getTemporary(Context, None);
850 Ref.reset(Temp.get());
851 EXPECT_EQ(Temp.get(), Ref.get());
852 }
853 EXPECT_EQ(nullptr, Ref.get());
854}
855
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000856typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000857
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000858TEST_F(DILocationTest, Overflow) {
859 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000860 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000861 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000862 EXPECT_EQ(2u, L->getLine());
863 EXPECT_EQ(7u, L->getColumn());
864 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000865 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000866 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000867 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000868 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000869 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000870 }
871 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000872 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000873 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000874 EXPECT_EQ(0u, L->getColumn());
875 }
876 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000877 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000878 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000879 EXPECT_EQ(0u, L->getColumn());
880 }
881}
882
Adrian Prantl4ddd0592018-08-24 23:30:57 +0000883TEST_F(DILocationTest, Merge) {
884 DISubprogram *N = getSubprogram();
885 DIScope *S = DILexicalBlock::get(Context, N, getFile(), 3, 4);
886
887 {
888 // Identical.
889 auto *A = DILocation::get(Context, 2, 7, N);
890 auto *B = DILocation::get(Context, 2, 7, N);
891 auto *M = DILocation::getMergedLocation(A, B);
892 EXPECT_EQ(2u, M->getLine());
893 EXPECT_EQ(7u, M->getColumn());
894 EXPECT_EQ(N, M->getScope());
895 }
896
897 {
898 // Identical, different scopes.
899 auto *A = DILocation::get(Context, 2, 7, N);
900 auto *B = DILocation::get(Context, 2, 7, S);
901 auto *M = DILocation::getMergedLocation(A, B);
902 EXPECT_EQ(0u, M->getLine()); // FIXME: Should this be 2?
903 EXPECT_EQ(0u, M->getColumn()); // FIXME: Should this be 7?
904 EXPECT_EQ(N, M->getScope());
905 }
906
907 {
908 // Different lines, same scopes.
909 auto *A = DILocation::get(Context, 1, 6, N);
910 auto *B = DILocation::get(Context, 2, 7, N);
911 auto *M = DILocation::getMergedLocation(A, B);
912 EXPECT_EQ(0u, M->getLine());
913 EXPECT_EQ(0u, M->getColumn());
914 EXPECT_EQ(N, M->getScope());
915 }
916
917 {
918 // Twisty locations, all different, same function.
919 auto *A = DILocation::get(Context, 1, 6, N);
920 auto *B = DILocation::get(Context, 2, 7, S);
921 auto *M = DILocation::getMergedLocation(A, B);
922 EXPECT_EQ(0u, M->getLine());
923 EXPECT_EQ(0u, M->getColumn());
924 EXPECT_EQ(N, M->getScope());
925 }
926
927 {
928 // Different function, same inlined-at.
929 auto *F = getFile();
930 auto *SP1 = DISubprogram::getDistinct(Context, F, "a", "a", F, 0, nullptr,
Paul Robinsoncda54212018-11-19 18:29:28 +0000931 0, nullptr, 0, 0, DINode::FlagZero,
932 DISubprogram::SPFlagZero, nullptr);
Adrian Prantl4ddd0592018-08-24 23:30:57 +0000933 auto *SP2 = DISubprogram::getDistinct(Context, F, "b", "b", F, 0, nullptr,
Paul Robinsoncda54212018-11-19 18:29:28 +0000934 0, nullptr, 0, 0, DINode::FlagZero,
935 DISubprogram::SPFlagZero, nullptr);
Adrian Prantl4ddd0592018-08-24 23:30:57 +0000936
937 auto *I = DILocation::get(Context, 2, 7, N);
938 auto *A = DILocation::get(Context, 1, 6, SP1, I);
939 auto *B = DILocation::get(Context, 2, 7, SP2, I);
940 auto *M = DILocation::getMergedLocation(A, B);
941 EXPECT_EQ(0u, M->getLine());
942 EXPECT_EQ(0u, M->getColumn());
943 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
944 EXPECT_EQ(I, M->getInlinedAt());
945 }
946
947 {
948 // Completely different.
949 auto *I = DILocation::get(Context, 2, 7, N);
950 auto *A = DILocation::get(Context, 1, 6, S, I);
951 auto *B = DILocation::get(Context, 2, 7, getSubprogram());
952 auto *M = DILocation::getMergedLocation(A, B);
953 EXPECT_EQ(0u, M->getLine());
954 EXPECT_EQ(0u, M->getColumn());
955 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
956 EXPECT_EQ(S, M->getScope());
957 EXPECT_EQ(nullptr, M->getInlinedAt());
958 }
959}
960
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000961TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000962 MDNode *N = getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000963 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000964 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000965 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000966 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000967 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000968}
969
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000970TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000971 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000972 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000973 EXPECT_TRUE(L->isTemporary());
974 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000975}
976
Teresa Johnsond98152b62015-12-07 15:05:44 +0000977TEST_F(DILocationTest, cloneTemporary) {
978 MDNode *N = MDNode::get(Context, None);
979 auto L = DILocation::getTemporary(Context, 2, 7, N);
980 EXPECT_TRUE(L->isTemporary());
981 auto L2 = L->clone();
982 EXPECT_TRUE(L2->isTemporary());
983}
984
Mircea Trofinb53eeb62018-12-21 22:48:50 +0000985TEST_F(DILocationTest, discriminatorEncoding) {
986 EXPECT_EQ(0U, DILocation::encodeDiscriminator(0, 0, 0).getValue());
987
988 // Encode base discriminator as a component: lsb is 0, then the value.
989 // The other components are all absent, so we leave all the other bits 0.
990 EXPECT_EQ(2U, DILocation::encodeDiscriminator(1, 0, 0).getValue());
991
992 // Base discriminator component is empty, so lsb is 1. Next component is not
993 // empty, so its lsb is 0, then its value (1). Next component is empty.
994 // So the bit pattern is 101.
995 EXPECT_EQ(5U, DILocation::encodeDiscriminator(0, 1, 0).getValue());
996
997 // First 2 components are empty, so the bit pattern is 11. Then the
998 // next component - ending up with 1011.
999 EXPECT_EQ(0xbU, DILocation::encodeDiscriminator(0, 0, 1).getValue());
1000
1001 // The bit pattern for the first 2 components is 11. The next bit is 0,
1002 // because the last component is not empty. We have 29 bits usable for
1003 // encoding, but we cap it at 12 bits uniformously for all components. We
1004 // encode the last component over 14 bits.
1005 EXPECT_EQ(0xfffbU, DILocation::encodeDiscriminator(0, 0, 0xfff).getValue());
1006
1007 EXPECT_EQ(0x102U, DILocation::encodeDiscriminator(1, 1, 0).getValue());
1008
1009 EXPECT_EQ(0x13eU, DILocation::encodeDiscriminator(0x1f, 1, 0).getValue());
1010
1011 EXPECT_EQ(0x87feU, DILocation::encodeDiscriminator(0x1ff, 1, 0).getValue());
1012
1013 EXPECT_EQ(0x1f3eU, DILocation::encodeDiscriminator(0x1f, 0x1f, 0).getValue());
1014
1015 EXPECT_EQ(0x3ff3eU,
1016 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0).getValue());
1017
1018 EXPECT_EQ(0x1ff87feU,
1019 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 0).getValue());
1020
1021 EXPECT_EQ(0xfff9f3eU,
1022 DILocation::encodeDiscriminator(0x1f, 0x1f, 0xfff).getValue());
1023
1024 EXPECT_EQ(0xffc3ff3eU,
1025 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0x1ff).getValue());
1026
1027 EXPECT_EQ(0xffcf87feU,
1028 DILocation::encodeDiscriminator(0x1ff, 0x1f, 0x1ff).getValue());
1029
1030 EXPECT_EQ(0xe1ff87feU,
1031 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 7).getValue());
1032}
1033
1034TEST_F(DILocationTest, discriminatorEncodingNegativeTests) {
1035 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
1036 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1000, 0, 0));
1037 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0x1000, 0));
1038 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
1039 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1ff, 0x1ff, 8));
1040 EXPECT_EQ(None,
1041 DILocation::encodeDiscriminator(std::numeric_limits<uint32_t>::max(),
1042 std::numeric_limits<uint32_t>::max(),
1043 0));
1044}
1045
1046TEST_F(DILocationTest, discriminatorSpecialCases) {
1047 // We don't test getCopyIdentifier here because the only way
1048 // to set it is by constructing an encoded discriminator using
1049 // encodeDiscriminator, which is already tested.
1050 auto L1 = DILocation::get(Context, 1, 2, getSubprogram());
1051 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1052 EXPECT_EQ(1U, L1->getDuplicationFactor());
1053
Mircea Trofinec026302019-01-24 00:10:25 +00001054 EXPECT_EQ(L1, L1->cloneWithBaseDiscriminator(0).getValue());
1055 EXPECT_EQ(L1, L1->cloneByMultiplyingDuplicationFactor(0).getValue());
1056 EXPECT_EQ(L1, L1->cloneByMultiplyingDuplicationFactor(1).getValue());
1057
1058 auto L2 = L1->cloneWithBaseDiscriminator(1).getValue();
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001059 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1060 EXPECT_EQ(1U, L1->getDuplicationFactor());
1061
1062 EXPECT_EQ(1U, L2->getBaseDiscriminator());
1063 EXPECT_EQ(1U, L2->getDuplicationFactor());
1064
Mircea Trofinec026302019-01-24 00:10:25 +00001065 auto L3 = L2->cloneByMultiplyingDuplicationFactor(2).getValue();
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001066 EXPECT_EQ(1U, L3->getBaseDiscriminator());
1067 EXPECT_EQ(2U, L3->getDuplicationFactor());
1068
Mircea Trofinec026302019-01-24 00:10:25 +00001069 EXPECT_EQ(L2, L2->cloneByMultiplyingDuplicationFactor(1).getValue());
1070
1071 auto L4 = L3->cloneByMultiplyingDuplicationFactor(4).getValue();
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001072 EXPECT_EQ(1U, L4->getBaseDiscriminator());
1073 EXPECT_EQ(8U, L4->getDuplicationFactor());
1074
Mircea Trofinec026302019-01-24 00:10:25 +00001075 auto L5 = L4->cloneWithBaseDiscriminator(2).getValue();
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001076 EXPECT_EQ(2U, L5->getBaseDiscriminator());
Mircea Trofinec026302019-01-24 00:10:25 +00001077 EXPECT_EQ(8U, L5->getDuplicationFactor());
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001078
1079 // Check extreme cases
Mircea Trofinec026302019-01-24 00:10:25 +00001080 auto L6 = L1->cloneWithBaseDiscriminator(0xfff).getValue();
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001081 EXPECT_EQ(0xfffU, L6->getBaseDiscriminator());
Mircea Trofinec026302019-01-24 00:10:25 +00001082 EXPECT_EQ(0xfffU, L6->cloneByMultiplyingDuplicationFactor(0xfff)
1083 .getValue()
1084 ->getDuplicationFactor());
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001085
1086 // Check we return None for unencodable cases.
Mircea Trofinec026302019-01-24 00:10:25 +00001087 EXPECT_EQ(None, L4->cloneWithBaseDiscriminator(0x1000));
1088 EXPECT_EQ(None, L4->cloneByMultiplyingDuplicationFactor(0x1000));
Mircea Trofinb53eeb62018-12-21 22:48:50 +00001089}
1090
1091
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001092typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001093
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001094TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +00001095 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001096 auto *Empty = MDNode::get(Context, None);
1097 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001098 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001099 EXPECT_EQ(15u, N->getTag());
1100 EXPECT_EQ(2u, N->getNumOperands());
1101 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +00001102 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001103 EXPECT_EQ(1u, N->getNumDwarfOperands());
1104 EXPECT_EQ(Empty, N->getDwarfOperand(0));
1105 EXPECT_EQ(Empty, N->getOperand(1));
1106 ASSERT_TRUE(N->isUniqued());
1107
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001108 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001109
1110 N->replaceOperandWith(1, nullptr);
1111 EXPECT_EQ(15u, N->getTag());
1112 EXPECT_EQ(Header, N->getHeader());
1113 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
1114 ASSERT_TRUE(N->isUniqued());
1115
1116 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001117 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001118
1119 N->replaceDwarfOperandWith(0, Empty);
1120 EXPECT_EQ(15u, N->getTag());
1121 EXPECT_EQ(Header, N->getHeader());
1122 EXPECT_EQ(Empty, N->getDwarfOperand(0));
1123 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001124 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001125
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001126 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001127 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001128}
1129
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001130TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +00001131 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001132 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +00001133 EXPECT_EQ(StringRef(), N->getHeader());
1134 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +00001135}
1136
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001137typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001138
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001139TEST_F(DISubrangeTest, get) {
1140 auto *N = DISubrange::get(Context, 5, 7);
Sander de Smalenfdf40912018-01-24 09:56:07 +00001141 auto Count = N->getCount();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001142 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
Sander de Smalenfdf40912018-01-24 09:56:07 +00001143 ASSERT_TRUE(Count);
1144 ASSERT_TRUE(Count.is<ConstantInt*>());
1145 EXPECT_EQ(5, Count.get<ConstantInt*>()->getSExtValue());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001146 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001147 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
1148 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001149
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001150 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001151 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001152}
1153
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001154TEST_F(DISubrangeTest, getEmptyArray) {
1155 auto *N = DISubrange::get(Context, -1, 0);
Sander de Smalenfdf40912018-01-24 09:56:07 +00001156 auto Count = N->getCount();
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +00001157 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
Sander de Smalenfdf40912018-01-24 09:56:07 +00001158 ASSERT_TRUE(Count);
1159 ASSERT_TRUE(Count.is<ConstantInt*>());
1160 EXPECT_EQ(-1, Count.get<ConstantInt*>()->getSExtValue());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001161 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001162 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +00001163}
1164
Sander de Smalenfdf40912018-01-24 09:56:07 +00001165TEST_F(DISubrangeTest, getVariableCount) {
1166 DILocalScope *Scope = getSubprogram();
1167 DIFile *File = getFile();
1168 DIType *Type = getDerivedType();
1169 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
1170 auto *VlaExpr = DILocalVariable::get(Context, Scope, "vla_expr", File, 8,
1171 Type, 2, Flags, 8);
1172
1173 auto *N = DISubrange::get(Context, VlaExpr, 0);
1174 auto Count = N->getCount();
1175 ASSERT_TRUE(Count);
1176 ASSERT_TRUE(Count.is<DIVariable*>());
1177 EXPECT_EQ(VlaExpr, Count.get<DIVariable*>());
1178 ASSERT_TRUE(isa<DIVariable>(N->getRawCountNode()));
1179 EXPECT_EQ(0, N->getLowerBound());
1180 EXPECT_EQ("vla_expr", Count.get<DIVariable*>()->getName());
1181 EXPECT_EQ(N, DISubrange::get(Context, VlaExpr, 0));
1182}
1183
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001184typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001185
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001186TEST_F(DIEnumeratorTest, get) {
Momchil Velikov08dc66e2018-02-12 16:10:09 +00001187 auto *N = DIEnumerator::get(Context, 7, false, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001188 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
1189 EXPECT_EQ(7, N->getValue());
Momchil Velikovaf6312a2018-02-14 13:11:56 +00001190 EXPECT_FALSE(N->isUnsigned());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001191 EXPECT_EQ("name", N->getName());
Momchil Velikov08dc66e2018-02-12 16:10:09 +00001192 EXPECT_EQ(N, DIEnumerator::get(Context, 7, false, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001193
Momchil Velikov08dc66e2018-02-12 16:10:09 +00001194 EXPECT_NE(N, DIEnumerator::get(Context, 7, true, "name"));
1195 EXPECT_NE(N, DIEnumerator::get(Context, 8, false, "name"));
1196 EXPECT_NE(N, DIEnumerator::get(Context, 7, false, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001197
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001198 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001199 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001200}
1201
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001202typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001203
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001204TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001205 auto *N =
Adrian Prantl55f42622018-08-14 19:35:34 +00001206 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7,
1207 DINode::FlagZero);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001208 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
1209 EXPECT_EQ("special", N->getName());
1210 EXPECT_EQ(33u, N->getSizeInBits());
1211 EXPECT_EQ(26u, N->getAlignInBits());
1212 EXPECT_EQ(7u, N->getEncoding());
1213 EXPECT_EQ(0u, N->getLine());
Adrian Prantl55f42622018-08-14 19:35:34 +00001214 EXPECT_EQ(DINode::FlagZero, N->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001215 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Adrian Prantl55f42622018-08-14 19:35:34 +00001216 26, 7, DINode::FlagZero));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001217
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001218 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Adrian Prantl55f42622018-08-14 19:35:34 +00001219 "special", 33, 26, 7, DINode::FlagZero));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001220 EXPECT_NE(N,
Adrian Prantl55f42622018-08-14 19:35:34 +00001221 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7,
1222 DINode::FlagZero));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001223 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Adrian Prantl55f42622018-08-14 19:35:34 +00001224 26, 7, DINode::FlagZero));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001225 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Adrian Prantl55f42622018-08-14 19:35:34 +00001226 25, 7, DINode::FlagZero));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001227 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Adrian Prantl55f42622018-08-14 19:35:34 +00001228 26, 6, DINode::FlagZero));
1229 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
1230 26, 7, DINode::FlagBigEndian));
1231 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
1232 26, 7, DINode::FlagLittleEndian));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001233
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001234 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001235 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001236}
1237
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001238TEST_F(DIBasicTypeTest, getWithLargeValues) {
1239 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Adrian Prantl55f42622018-08-14 19:35:34 +00001240 UINT64_MAX, UINT32_MAX - 1, 7, DINode::FlagZero);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001241 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk197aa312016-10-18 14:31:22 +00001242 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001243}
1244
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001245TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001246 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001247 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001248 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
1249 EXPECT_EQ("unspecified", N->getName());
1250 EXPECT_EQ(0u, N->getSizeInBits());
1251 EXPECT_EQ(0u, N->getAlignInBits());
1252 EXPECT_EQ(0u, N->getEncoding());
1253 EXPECT_EQ(0u, N->getLine());
Adrian Prantl55f42622018-08-14 19:35:34 +00001254 EXPECT_EQ(DINode::FlagZero, N->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001255}
1256
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001257typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001258
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001259TEST_F(DITypeTest, clone) {
1260 // Check that DIType has a specialized clone that returns TempDIType.
1261 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Adrian Prantl55f42622018-08-14 19:35:34 +00001262 dwarf::DW_ATE_signed, DINode::FlagZero);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001263
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001264 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001265 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1266}
1267
Roman Tereshincf88ffa2018-06-01 23:15:09 +00001268TEST_F(DITypeTest, cloneWithFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001269 // void (void)
1270 Metadata *TypesOps[] = {nullptr};
1271 Metadata *Types = MDTuple::get(Context, TypesOps);
1272
Leny Kholodov40c62352016-09-06 17:03:02 +00001273 DIType *D =
1274 DISubroutineType::getDistinct(Context, DINode::FlagZero, 0, Types);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001275 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Roman Tereshincf88ffa2018-06-01 23:15:09 +00001276 TempDIType D2 = D->cloneWithFlags(DINode::FlagRValueReference);
1277 EXPECT_EQ(DINode::FlagRValueReference, D2->getFlags());
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001278 EXPECT_EQ(DINode::FlagZero, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001279
Leny Kholodov40c62352016-09-06 17:03:02 +00001280 TempDIType T =
1281 DISubroutineType::getTemporary(Context, DINode::FlagZero, 0, Types);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001282 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Roman Tereshincf88ffa2018-06-01 23:15:09 +00001283 TempDIType T2 = T->cloneWithFlags(DINode::FlagRValueReference);
1284 EXPECT_EQ(DINode::FlagRValueReference, T2->getFlags());
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001285 EXPECT_EQ(DINode::FlagZero, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +00001286}
1287
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001288typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001289
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001290TEST_F(DIDerivedTypeTest, get) {
1291 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001292 DIScope *Scope = getSubprogram();
1293 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001294 MDTuple *ExtraData = getTuple();
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001295 unsigned DWARFAddressSpace = 8;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001296 DINode::DIFlags Flags5 = static_cast<DINode::DIFlags>(5);
1297 DINode::DIFlags Flags4 = static_cast<DINode::DIFlags>(4);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001298
Leny Kholodov40c62352016-09-06 17:03:02 +00001299 auto *N =
1300 DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", File,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001301 1, Scope, BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
1302 ExtraData);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001303 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
1304 EXPECT_EQ("something", N->getName());
1305 EXPECT_EQ(File, N->getFile());
1306 EXPECT_EQ(1u, N->getLine());
1307 EXPECT_EQ(Scope, N->getScope());
1308 EXPECT_EQ(BaseType, N->getBaseType());
1309 EXPECT_EQ(2u, N->getSizeInBits());
1310 EXPECT_EQ(3u, N->getAlignInBits());
1311 EXPECT_EQ(4u, N->getOffsetInBits());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001312 EXPECT_EQ(DWARFAddressSpace, N->getDWARFAddressSpace().getValue());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001313 EXPECT_EQ(5u, N->getFlags());
1314 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001315 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001316 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001317 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001318
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001319 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001320 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001321 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001322 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001323 File, 1, Scope, BaseType, 2, 3,
1324 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001325 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001326 "something", getFile(), 1, Scope, BaseType, 2,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001327 3, 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001328 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001329 "something", File, 2, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001330 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001331 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001332 "something", File, 1, getSubprogram(),
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001333 BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
1334 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001335 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001336 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001337 Scope, getBasicType("basic2"), 2, 3, 4, DWARFAddressSpace,
1338 Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001339 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001340 "something", File, 1, Scope, BaseType, 3, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001341 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001342 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001343 "something", File, 1, Scope, BaseType, 2, 2,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001344 4, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001345 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001346 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001347 5, DWARFAddressSpace, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001348 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001349 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001350 4, DWARFAddressSpace + 1, Flags5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001351 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001352 "something", File, 1, Scope, BaseType, 2, 3,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001353 4, DWARFAddressSpace, Flags4, ExtraData));
1354 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1355 "something", File, 1, Scope, BaseType, 2, 3,
1356 4, DWARFAddressSpace, Flags5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001357
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001358 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001359 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001360}
1361
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001362TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1363 DIFile *File = getFile();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001364 DIScope *Scope = getSubprogram();
1365 DIType *BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001366 MDTuple *ExtraData = getTuple();
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001367 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001368
Leny Kholodov40c62352016-09-06 17:03:02 +00001369 auto *N = DIDerivedType::get(
1370 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, Scope,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001371 BaseType, UINT64_MAX, UINT32_MAX - 1, UINT64_MAX - 2, UINT32_MAX - 3,
1372 Flags, ExtraData);
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001373 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
Victor Leschuk197aa312016-10-18 14:31:22 +00001374 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001375 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001376 EXPECT_EQ(UINT32_MAX - 3, N->getDWARFAddressSpace().getValue());
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001377}
1378
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001379typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001380
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001381TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001382 unsigned Tag = dwarf::DW_TAG_structure_type;
1383 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001384 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001385 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001386 DIScope *Scope = getSubprogram();
1387 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001388 uint64_t SizeInBits = 2;
Victor Leschuk197aa312016-10-18 14:31:22 +00001389 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001390 uint64_t OffsetInBits = 4;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001391 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001392 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001393 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001394 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001395 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001396 StringRef Identifier = "some id";
1397
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001398 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001399 BaseType, SizeInBits, AlignInBits,
1400 OffsetInBits, Flags, Elements, RuntimeLang,
1401 VTableHolder, TemplateParams, Identifier);
1402 EXPECT_EQ(Tag, N->getTag());
1403 EXPECT_EQ(Name, N->getName());
1404 EXPECT_EQ(File, N->getFile());
1405 EXPECT_EQ(Line, N->getLine());
1406 EXPECT_EQ(Scope, N->getScope());
1407 EXPECT_EQ(BaseType, N->getBaseType());
1408 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1409 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1410 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1411 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001412 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001413 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1414 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001415 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001416 EXPECT_EQ(Identifier, N->getIdentifier());
1417
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001418 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001419 BaseType, SizeInBits, AlignInBits,
1420 OffsetInBits, Flags, Elements, RuntimeLang,
1421 VTableHolder, TemplateParams, Identifier));
1422
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001423 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001424 BaseType, SizeInBits, AlignInBits,
1425 OffsetInBits, Flags, Elements, RuntimeLang,
1426 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001427 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001428 BaseType, SizeInBits, AlignInBits,
1429 OffsetInBits, Flags, Elements, RuntimeLang,
1430 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001431 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001432 BaseType, SizeInBits, AlignInBits,
1433 OffsetInBits, Flags, Elements, RuntimeLang,
1434 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001435 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001436 BaseType, SizeInBits, AlignInBits,
1437 OffsetInBits, Flags, Elements, RuntimeLang,
1438 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001439 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001440 Context, Tag, Name, File, Line, getSubprogram(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001441 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1442 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001443 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001444 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1445 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1446 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001447 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001448 BaseType, SizeInBits + 1, AlignInBits,
1449 OffsetInBits, Flags, Elements, RuntimeLang,
1450 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001451 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001452 BaseType, SizeInBits, AlignInBits + 1,
1453 OffsetInBits, Flags, Elements, RuntimeLang,
1454 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001455 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001456 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1457 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1458 VTableHolder, TemplateParams, Identifier));
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001459 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001460 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001461 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001462 AlignInBits, OffsetInBits, FlagsPOne, Elements, RuntimeLang,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001463 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001464 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001465 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1466 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1467 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001468 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001469 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1470 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1471 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001472 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001473 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1474 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1475 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001476 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001477 BaseType, SizeInBits, AlignInBits,
1478 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001479 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001480 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001481 BaseType, SizeInBits, AlignInBits,
1482 OffsetInBits, Flags, Elements, RuntimeLang,
1483 VTableHolder, TemplateParams, "other"));
1484
1485 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001486 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1487 BaseType, SizeInBits, AlignInBits,
1488 OffsetInBits, Flags, Elements, RuntimeLang,
1489 VTableHolder, TemplateParams, "")
1490 ->getRawIdentifier());
1491 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1492 BaseType, SizeInBits, AlignInBits,
1493 OffsetInBits, Flags, Elements, RuntimeLang,
1494 VTableHolder, TemplateParams)
1495 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001496
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001497 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001498 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001499}
1500
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001501TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001502 unsigned Tag = dwarf::DW_TAG_structure_type;
1503 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001504 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001505 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001506 DIScope *Scope = getSubprogram();
1507 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001508 uint64_t SizeInBits = UINT64_MAX;
Victor Leschuk197aa312016-10-18 14:31:22 +00001509 uint32_t AlignInBits = UINT32_MAX - 1;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001510 uint64_t OffsetInBits = UINT64_MAX - 2;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001511 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001512 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001513 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001514 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001515 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001516 StringRef Identifier = "some id";
1517
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001518 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001519 BaseType, SizeInBits, AlignInBits,
1520 OffsetInBits, Flags, Elements, RuntimeLang,
1521 VTableHolder, TemplateParams, Identifier);
1522 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1523 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1524 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1525}
1526
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001527TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001528 unsigned Tag = dwarf::DW_TAG_structure_type;
1529 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001530 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001531 unsigned Line = 1;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001532 DIScope *Scope = getSubprogram();
1533 DIType *BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001534 uint64_t SizeInBits = 2;
Victor Leschuk197aa312016-10-18 14:31:22 +00001535 uint32_t AlignInBits = 3;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001536 uint64_t OffsetInBits = 4;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001537 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001538 unsigned RuntimeLang = 6;
1539 StringRef Identifier = "some id";
1540
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001541 auto *N = DICompositeType::get(
1542 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1543 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001544
1545 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001546 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001547 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001548 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001549 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001550 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001551
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001552 DIType *VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001553 EXPECT_EQ(nullptr, N->getVTableHolder());
1554 N->replaceVTableHolder(VTableHolder);
1555 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Adrian Prantla8e56452017-11-08 22:04:43 +00001556 // As an extension, the containing type can be anything. This is
1557 // used by Rust to associate vtables with their concrete type.
1558 DIType *BasicType = getBasicType("basic");
1559 N->replaceVTableHolder(BasicType);
1560 EXPECT_EQ(BasicType, N->getVTableHolder());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001561 N->replaceVTableHolder(nullptr);
1562 EXPECT_EQ(nullptr, N->getVTableHolder());
1563
1564 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001565 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001566 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001567 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001568 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001569 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001570}
1571
Adrian Prantl8c599212018-02-06 23:45:59 +00001572TEST_F(DICompositeTypeTest, variant_part) {
1573 unsigned Tag = dwarf::DW_TAG_variant_part;
1574 StringRef Name = "some name";
1575 DIFile *File = getFile();
1576 unsigned Line = 1;
1577 DIScope *Scope = getSubprogram();
1578 DIType *BaseType = getCompositeType();
1579 uint64_t SizeInBits = 2;
1580 uint32_t AlignInBits = 3;
1581 uint64_t OffsetInBits = 4;
1582 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
1583 unsigned RuntimeLang = 6;
1584 StringRef Identifier = "some id";
1585 DIDerivedType *Discriminator = cast<DIDerivedType>(getDerivedType());
1586 DIDerivedType *Discriminator2 = cast<DIDerivedType>(getDerivedType());
1587
1588 EXPECT_NE(Discriminator, Discriminator2);
1589
1590 auto *N = DICompositeType::get(
1591 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1592 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1593 Discriminator);
1594
1595 // Test the hashing.
1596 auto *Same = DICompositeType::get(
1597 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1598 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1599 Discriminator);
1600 auto *Other = DICompositeType::get(
1601 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1602 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1603 Discriminator2);
1604 auto *NoDisc = DICompositeType::get(
1605 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1606 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
1607 nullptr);
1608
1609 EXPECT_EQ(N, Same);
1610 EXPECT_NE(Same, Other);
1611 EXPECT_NE(Same, NoDisc);
1612 EXPECT_NE(Other, NoDisc);
1613
1614 EXPECT_EQ(N->getDiscriminator(), Discriminator);
1615}
1616
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001617typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001618
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001619TEST_F(DISubroutineTypeTest, get) {
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001620 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(1);
1621 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001622 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001623
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001624 auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001625 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1626 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001627 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001628 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001629
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001630 EXPECT_NE(N, DISubroutineType::get(Context, FlagsPOne, 0, TypeArray));
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001631 EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple()));
1632
1633 // Test the hashing of calling conventions.
1634 auto *Fast = DISubroutineType::get(
1635 Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray);
1636 auto *Std = DISubroutineType::get(Context, Flags,
1637 dwarf::DW_CC_BORLAND_stdcall, TypeArray);
1638 EXPECT_EQ(Fast,
1639 DISubroutineType::get(Context, Flags,
1640 dwarf::DW_CC_BORLAND_msfastcall, TypeArray));
1641 EXPECT_EQ(Std, DISubroutineType::get(
1642 Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray));
1643
1644 EXPECT_NE(N, Fast);
1645 EXPECT_NE(N, Std);
1646 EXPECT_NE(Fast, Std);
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001647
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001648 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001649 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001650
1651 // Test always-empty operands.
1652 EXPECT_EQ(nullptr, N->getScope());
1653 EXPECT_EQ(nullptr, N->getFile());
1654 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001655}
1656
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001657typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001658
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001659TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001660 StringRef Filename = "file";
1661 StringRef Directory = "dir";
Scott Linder71603842018-02-12 19:45:54 +00001662 DIFile::ChecksumKind CSKind = DIFile::ChecksumKind::CSK_MD5;
1663 StringRef ChecksumString = "000102030405060708090a0b0c0d0e0f";
1664 DIFile::ChecksumInfo<StringRef> Checksum(CSKind, ChecksumString);
Scott Linder16c7bda2018-02-23 23:01:06 +00001665 StringRef Source = "source";
1666 auto *N = DIFile::get(Context, Filename, Directory, Checksum, Source);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001667
1668 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1669 EXPECT_EQ(Filename, N->getFilename());
1670 EXPECT_EQ(Directory, N->getDirectory());
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001671 EXPECT_EQ(Checksum, N->getChecksum());
Scott Linder16c7bda2018-02-23 23:01:06 +00001672 EXPECT_EQ(Source, N->getSource());
1673 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory, Checksum, Source));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001674
Scott Linder16c7bda2018-02-23 23:01:06 +00001675 EXPECT_NE(N, DIFile::get(Context, "other", Directory, Checksum, Source));
1676 EXPECT_NE(N, DIFile::get(Context, Filename, "other", Checksum, Source));
Scott Linder71603842018-02-12 19:45:54 +00001677 DIFile::ChecksumInfo<StringRef> OtherChecksum(DIFile::ChecksumKind::CSK_SHA1, ChecksumString);
Reid Kleckner26fa1bf2017-09-19 18:14:45 +00001678 EXPECT_NE(
Scott Linder71603842018-02-12 19:45:54 +00001679 N, DIFile::get(Context, Filename, Directory, OtherChecksum));
Scott Linder16c7bda2018-02-23 23:01:06 +00001680 StringRef OtherSource = "other";
1681 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum, OtherSource));
1682 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum));
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001683 EXPECT_NE(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001684
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001685 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001686 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001687}
1688
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001689TEST_F(DIFileTest, ScopeGetFile) {
1690 // Ensure that DIScope::getFile() returns itself.
1691 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001692 EXPECT_EQ(N, N->getFile());
1693}
1694
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001695typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001696
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001697TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001698 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001699 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001700 StringRef Producer = "some producer";
1701 bool IsOptimized = false;
1702 StringRef Flags = "flag after flag";
1703 unsigned RuntimeVersion = 2;
1704 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001705 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001706 MDTuple *EnumTypes = getTuple();
1707 MDTuple *RetainedTypes = getTuple();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001708 MDTuple *GlobalVariables = getTuple();
1709 MDTuple *ImportedEntities = getTuple();
Adrian Prantle3b49e02015-09-22 23:42:47 +00001710 uint64_t DWOId = 0x10000000c0ffee;
Amjad Abouda9bcf162015-12-10 12:56:35 +00001711 MDTuple *Macros = getTuple();
Adrian Prantl7b303702020-01-14 13:37:04 -08001712 StringRef SysRoot = "/";
Adrian Prantle4e7e442020-03-04 14:12:54 -08001713 StringRef SDK = "MacOSX.sdk";
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001714 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001715 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1716 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Dehao Chen0944a8c2017-02-01 22:45:09 +00001717 RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true,
Adrian Prantle4e7e442020-03-04 14:12:54 -08001718 false, DICompileUnit::DebugNameTableKind::Default, false, SysRoot, SDK);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001719
1720 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1721 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1722 EXPECT_EQ(File, N->getFile());
1723 EXPECT_EQ(Producer, N->getProducer());
1724 EXPECT_EQ(IsOptimized, N->isOptimized());
1725 EXPECT_EQ(Flags, N->getFlags());
1726 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1727 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1728 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001729 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1730 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001731 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1732 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001733 EXPECT_EQ(Macros, N->getMacros().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001734 EXPECT_EQ(DWOId, N->getDWOId());
Adrian Prantl7b303702020-01-14 13:37:04 -08001735 EXPECT_EQ(SysRoot, N->getSysRoot());
Adrian Prantle4e7e442020-03-04 14:12:54 -08001736 EXPECT_EQ(SDK, N->getSDK());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001737
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001738 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001739 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1740 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1741 EXPECT_EQ(File, Temp->getFile());
1742 EXPECT_EQ(Producer, Temp->getProducer());
1743 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1744 EXPECT_EQ(Flags, Temp->getFlags());
1745 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1746 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1747 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1748 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1749 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001750 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1751 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001752 EXPECT_EQ(Macros, Temp->getMacros().get());
Adrian Prantl7b303702020-01-14 13:37:04 -08001753 EXPECT_EQ(SysRoot, Temp->getSysRoot());
Adrian Prantle4e7e442020-03-04 14:12:54 -08001754 EXPECT_EQ(SDK, Temp->getSDK());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001755
1756 auto *TempAddress = Temp.get();
1757 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1758 EXPECT_TRUE(Clone->isDistinct());
1759 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001760}
1761
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001762TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001763 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001764 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001765 StringRef Producer = "some producer";
1766 bool IsOptimized = false;
1767 StringRef Flags = "flag after flag";
1768 unsigned RuntimeVersion = 2;
1769 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001770 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001771 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1772 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1773 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001774 uint64_t DWOId = 0xc0ffee;
Adrian Prantl7b303702020-01-14 13:37:04 -08001775 StringRef SysRoot = "/";
Adrian Prantle4e7e442020-03-04 14:12:54 -08001776 StringRef SDK = "MacOSX.sdk";
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001777 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001778 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1779 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001780 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true, false,
Adrian Prantle4e7e442020-03-04 14:12:54 -08001781 DICompileUnit::DebugNameTableKind::Default, false, SysRoot, SDK);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001782
1783 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001784 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001785 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001786 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001787 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001788 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001789
1790 auto *Macros = MDTuple::getDistinct(Context, None);
1791 EXPECT_EQ(nullptr, N->getMacros().get());
1792 N->replaceMacros(Macros);
1793 EXPECT_EQ(Macros, N->getMacros().get());
1794 N->replaceMacros(nullptr);
1795 EXPECT_EQ(nullptr, N->getMacros().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001796}
1797
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001799
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001800TEST_F(DISubprogramTest, get) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001801 DIScope *Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001802 StringRef Name = "name";
1803 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001804 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001805 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001806 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001807 bool IsLocalToUnit = false;
1808 bool IsDefinition = true;
1809 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00001810 DIType *ContainingType = getCompositeType();
Davide Italiano236e7442016-04-13 20:17:42 +00001811 unsigned Virtuality = 2;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001812 unsigned VirtualIndex = 5;
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001813 int ThisAdjustment = -3;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001814 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(6);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001815 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001816 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001817 DISubprogram *Declaration = getSubprogram();
Shiva Chen2c864552018-05-09 02:40:45 +00001818 MDTuple *RetainedNodes = getTuple();
Adrian Prantl1d12b882017-04-26 22:56:44 +00001819 MDTuple *ThrownTypes = getTuple();
Adrian Prantl75819ae2016-04-15 15:57:41 +00001820 DICompileUnit *Unit = getUnit();
Paul Robinsoncda54212018-11-19 18:29:28 +00001821 DISubprogram::DISPFlags SPFlags =
1822 static_cast<DISubprogram::DISPFlags>(Virtuality);
1823 assert(!IsLocalToUnit && IsDefinition && !IsOptimized &&
1824 "bools and SPFlags have to match");
1825 SPFlags |= DISubprogram::SPFlagDefinition;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001826
Adrian Prantl1d12b882017-04-26 22:56:44 +00001827 auto *N = DISubprogram::get(
Paul Robinsoncda54212018-11-19 18:29:28 +00001828 Context, Scope, Name, LinkageName, File, Line, Type, ScopeLine,
1829 ContainingType, VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit,
1830 TemplateParams, Declaration, RetainedNodes, ThrownTypes);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001831
1832 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1833 EXPECT_EQ(Scope, N->getScope());
1834 EXPECT_EQ(Name, N->getName());
1835 EXPECT_EQ(LinkageName, N->getLinkageName());
1836 EXPECT_EQ(File, N->getFile());
1837 EXPECT_EQ(Line, N->getLine());
1838 EXPECT_EQ(Type, N->getType());
1839 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1840 EXPECT_EQ(IsDefinition, N->isDefinition());
1841 EXPECT_EQ(ScopeLine, N->getScopeLine());
1842 EXPECT_EQ(ContainingType, N->getContainingType());
1843 EXPECT_EQ(Virtuality, N->getVirtuality());
1844 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001845 EXPECT_EQ(ThisAdjustment, N->getThisAdjustment());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001846 EXPECT_EQ(Flags, N->getFlags());
1847 EXPECT_EQ(IsOptimized, N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001848 EXPECT_EQ(Unit, N->getUnit());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001849 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001850 EXPECT_EQ(Declaration, N->getDeclaration());
Shiva Chen2c864552018-05-09 02:40:45 +00001851 EXPECT_EQ(RetainedNodes, N->getRetainedNodes().get());
Adrian Prantl1d12b882017-04-26 22:56:44 +00001852 EXPECT_EQ(ThrownTypes, N->getThrownTypes().get());
Paul Robinsoncda54212018-11-19 18:29:28 +00001853 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1854 Type, ScopeLine, ContainingType, VirtualIndex,
1855 ThisAdjustment, Flags, SPFlags, Unit,
1856 TemplateParams, Declaration, RetainedNodes,
1857 ThrownTypes));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001858
Paul Robinsoncda54212018-11-19 18:29:28 +00001859 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
1860 File, Line, Type, ScopeLine, ContainingType,
1861 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1862 Unit, TemplateParams, Declaration,
1863 RetainedNodes, ThrownTypes));
1864 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
1865 Line, Type, ScopeLine, ContainingType,
1866 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1867 Unit, TemplateParams, Declaration,
1868 RetainedNodes, ThrownTypes));
1869 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
1870 Type, ScopeLine, ContainingType, VirtualIndex,
1871 ThisAdjustment, Flags, SPFlags, Unit,
Shiva Chen2c864552018-05-09 02:40:45 +00001872 TemplateParams, Declaration, RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001873 ThrownTypes));
Paul Robinsoncda54212018-11-19 18:29:28 +00001874 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
1875 Line, Type, ScopeLine, ContainingType,
1876 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1877 Unit, TemplateParams, Declaration,
1878 RetainedNodes, ThrownTypes));
1879 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
1880 Line + 1, Type, ScopeLine, ContainingType,
1881 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1882 Unit, TemplateParams, Declaration,
1883 RetainedNodes, ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001884 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Paul Robinsoncda54212018-11-19 18:29:28 +00001885 getSubroutineType(), ScopeLine, ContainingType,
1886 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1887 Unit, TemplateParams, Declaration,
1888 RetainedNodes, ThrownTypes));
Adrian Prantl1d12b882017-04-26 22:56:44 +00001889 EXPECT_NE(N, DISubprogram::get(
1890 Context, Scope, Name, LinkageName, File, Line, Type,
Paul Robinsoncda54212018-11-19 18:29:28 +00001891 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1892 Flags, SPFlags ^ DISubprogram::SPFlagLocalToUnit, Unit,
1893 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
1894 EXPECT_NE(N, DISubprogram::get(
1895 Context, Scope, Name, LinkageName, File, Line, Type,
1896 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1897 Flags, SPFlags ^ DISubprogram::SPFlagDefinition, Unit,
1898 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
Adrian Prantl75819ae2016-04-15 15:57:41 +00001899 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Paul Robinsoncda54212018-11-19 18:29:28 +00001900 Type, ScopeLine + 1, ContainingType,
1901 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1902 Unit, TemplateParams, Declaration,
1903 RetainedNodes, ThrownTypes));
1904 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1905 Type, ScopeLine, getCompositeType(),
1906 VirtualIndex, ThisAdjustment, Flags, SPFlags,
1907 Unit, TemplateParams, Declaration,
1908 RetainedNodes, ThrownTypes));
1909 EXPECT_NE(N, DISubprogram::get(
1910 Context, Scope, Name, LinkageName, File, Line, Type,
1911 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1912 Flags, SPFlags ^ DISubprogram::SPFlagVirtual, Unit,
1913 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
1914 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1915 Type, ScopeLine, ContainingType,
1916 VirtualIndex + 1, ThisAdjustment, Flags,
1917 SPFlags, Unit, TemplateParams, Declaration,
1918 RetainedNodes, ThrownTypes));
1919 EXPECT_NE(N, DISubprogram::get(
1920 Context, Scope, Name, LinkageName, File, Line, Type,
1921 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
1922 Flags, SPFlags ^ DISubprogram::SPFlagOptimized, Unit,
1923 TemplateParams, Declaration, RetainedNodes, ThrownTypes));
1924 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1925 Type, ScopeLine, ContainingType, VirtualIndex,
1926 ThisAdjustment, Flags, SPFlags, nullptr,
1927 TemplateParams, Declaration, RetainedNodes,
1928 ThrownTypes));
1929 EXPECT_NE(N,
1930 DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1931 Type, ScopeLine, ContainingType, VirtualIndex,
1932 ThisAdjustment, Flags, SPFlags, Unit, getTuple(),
1933 Declaration, RetainedNodes, ThrownTypes));
1934 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1935 Type, ScopeLine, ContainingType, VirtualIndex,
1936 ThisAdjustment, Flags, SPFlags, Unit,
Shiva Chen2c864552018-05-09 02:40:45 +00001937 TemplateParams, getSubprogram(), RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +00001938 ThrownTypes));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001939 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Paul Robinsoncda54212018-11-19 18:29:28 +00001940 Type, ScopeLine, ContainingType, VirtualIndex,
1941 ThisAdjustment, Flags, SPFlags, Unit,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001942 TemplateParams, Declaration, getTuple()));
Paul Robinsoncda54212018-11-19 18:29:28 +00001943 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1944 Type, ScopeLine, ContainingType, VirtualIndex,
1945 ThisAdjustment, Flags, SPFlags, Unit,
1946 TemplateParams, Declaration, RetainedNodes,
1947 getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001948
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001949 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001950 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001951}
1952
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001953typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001954
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001955TEST_F(DILexicalBlockTest, get) {
1956 DILocalScope *Scope = getSubprogram();
1957 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001958 unsigned Line = 5;
1959 unsigned Column = 8;
1960
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001961 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001962
1963 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1964 EXPECT_EQ(Scope, N->getScope());
1965 EXPECT_EQ(File, N->getFile());
1966 EXPECT_EQ(Line, N->getLine());
1967 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001968 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001969
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001970 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001971 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1972 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1973 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1974 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001975
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001976 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001977 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001978}
1979
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +00001980TEST_F(DILexicalBlockTest, Overflow) {
1981 DISubprogram *SP = getSubprogram();
1982 DIFile *F = getFile();
1983 {
1984 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1985 EXPECT_EQ(2u, LB->getLine());
1986 EXPECT_EQ(7u, LB->getColumn());
1987 }
1988 unsigned U16 = 1u << 16;
1989 {
1990 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1991 EXPECT_EQ(UINT32_MAX, LB->getLine());
1992 EXPECT_EQ(U16 - 1, LB->getColumn());
1993 }
1994 {
1995 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1996 EXPECT_EQ(UINT32_MAX, LB->getLine());
1997 EXPECT_EQ(0u, LB->getColumn());
1998 }
1999 {
2000 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
2001 EXPECT_EQ(UINT32_MAX, LB->getLine());
2002 EXPECT_EQ(0u, LB->getColumn());
2003 }
2004}
2005
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002006typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002007
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002008TEST_F(DILexicalBlockFileTest, get) {
2009 DILocalScope *Scope = getSubprogram();
2010 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002011 unsigned Discriminator = 5;
2012
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002013 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002014
2015 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
2016 EXPECT_EQ(Scope, N->getScope());
2017 EXPECT_EQ(File, N->getFile());
2018 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002019 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002020
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002021 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00002022 Discriminator));
2023 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002024 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002025 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002026 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002027
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002028 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002029 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002030}
2031
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002032typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002033
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002034TEST_F(DINamespaceTest, get) {
2035 DIScope *Scope = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002036 StringRef Name = "namespace";
Adrian Prantldbfda632016-11-03 19:42:02 +00002037 bool ExportSymbols = true;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002038
Adrian Prantlfed4f392017-04-28 22:25:46 +00002039 auto *N = DINamespace::get(Context, Scope, Name, ExportSymbols);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002040
2041 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
2042 EXPECT_EQ(Scope, N->getScope());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002043 EXPECT_EQ(Name, N->getName());
Adrian Prantlfed4f392017-04-28 22:25:46 +00002044 EXPECT_EQ(N, DINamespace::get(Context, Scope, Name, ExportSymbols));
2045 EXPECT_NE(N, DINamespace::get(Context, getFile(), Name, ExportSymbols));
2046 EXPECT_NE(N, DINamespace::get(Context, Scope, "other", ExportSymbols));
2047 EXPECT_NE(N, DINamespace::get(Context, Scope, Name, !ExportSymbols));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002048
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002049 TempDINamespace Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002050 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002051}
2052
Adrian Prantlab1243f2015-06-29 23:03:47 +00002053typedef MetadataTest DIModuleTest;
2054
2055TEST_F(DIModuleTest, get) {
2056 DIScope *Scope = getFile();
2057 StringRef Name = "module";
2058 StringRef ConfigMacro = "-DNDEBUG";
2059 StringRef Includes = "-I.";
Adrian Prantlab1243f2015-06-29 23:03:47 +00002060
Adrian Prantl7b303702020-01-14 13:37:04 -08002061 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes);
Adrian Prantlab1243f2015-06-29 23:03:47 +00002062
2063 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
2064 EXPECT_EQ(Scope, N->getScope());
2065 EXPECT_EQ(Name, N->getName());
2066 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
2067 EXPECT_EQ(Includes, N->getIncludePath());
Adrian Prantl7b303702020-01-14 13:37:04 -08002068 EXPECT_EQ(N, DIModule::get(Context, Scope, Name, ConfigMacro, Includes));
2069 EXPECT_NE(N, DIModule::get(Context, getFile(), Name, ConfigMacro, Includes));
2070 EXPECT_NE(N, DIModule::get(Context, Scope, "other", ConfigMacro, Includes));
2071 EXPECT_NE(N, DIModule::get(Context, Scope, Name, "other", Includes));
2072 EXPECT_NE(N, DIModule::get(Context, Scope, Name, ConfigMacro, "other"));
Adrian Prantlab1243f2015-06-29 23:03:47 +00002073
2074 TempDIModule Temp = N->clone();
2075 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2076}
2077
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002078typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002079
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002080TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002081 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002082 DIType *Type = getBasicType("basic");
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302083 bool defaulted = false;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002084
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302085 auto *N = DITemplateTypeParameter::get(Context, Name, Type, defaulted);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002086
2087 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002088 EXPECT_EQ(Name, N->getName());
2089 EXPECT_EQ(Type, N->getType());
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302090 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type, defaulted));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002091
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302092 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type, defaulted));
2093 EXPECT_NE(N, DITemplateTypeParameter::get(Context, Name,
2094 getBasicType("other"), defaulted));
2095 EXPECT_NE(N, DITemplateTypeParameter::get(Context, Name, Type, true));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002096
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002097 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002098 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002099}
2100
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002101typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002102
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002103TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002104 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002105 StringRef Name = "template";
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002106 DIType *Type = getBasicType("basic");
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302107 bool defaulted = false;
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002108 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002109
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302110 auto *N =
2111 DITemplateValueParameter::get(Context, Tag, Name, Type, defaulted, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002112 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002113 EXPECT_EQ(Name, N->getName());
2114 EXPECT_EQ(Type, N->getType());
2115 EXPECT_EQ(Value, N->getValue());
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302116 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
2117 defaulted, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002118
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002119 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00002120 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302121 Type, defaulted, Value));
2122 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, "other", Type,
2123 defaulted, Value));
Hans Wennborg802b22b2020-03-02 09:26:00 +01002124 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Awanish Pandey7a42bab2020-03-02 10:52:12 +05302125 getBasicType("other"), defaulted,
2126 Value));
2127 EXPECT_NE(N,
2128 DITemplateValueParameter::get(Context, Tag, Name, Type, defaulted,
2129 getConstantAsMetadata()));
2130 EXPECT_NE(
2131 N, DITemplateValueParameter::get(Context, Tag, Name, Type, true, Value));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002132
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002133 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002134 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002135}
2136
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002137typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002138
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002139TEST_F(DIGlobalVariableTest, get) {
2140 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002141 StringRef Name = "name";
2142 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002143 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002144 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002145 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002146 bool IsLocalToUnit = false;
2147 bool IsDefinition = true;
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002148 MDTuple *templateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002149 DIDerivedType *StaticDataMemberDeclaration =
2150 cast<DIDerivedType>(getDerivedType());
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002151
Victor Leschuka37660c2016-10-26 21:32:29 +00002152 uint32_t AlignInBits = 8;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002153
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002154 auto *N = DIGlobalVariable::get(
2155 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2156 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
2157
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002158 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
2159 EXPECT_EQ(Scope, N->getScope());
2160 EXPECT_EQ(Name, N->getName());
2161 EXPECT_EQ(LinkageName, N->getLinkageName());
2162 EXPECT_EQ(File, N->getFile());
2163 EXPECT_EQ(Line, N->getLine());
2164 EXPECT_EQ(Type, N->getType());
2165 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
2166 EXPECT_EQ(IsDefinition, N->isDefinition());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002167 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002168 EXPECT_EQ(templateParams, N->getTemplateParams());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002169 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002170 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002171 Line, Type, IsLocalToUnit, IsDefinition,
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002172 StaticDataMemberDeclaration,
2173 templateParams, AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002174
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002175 EXPECT_NE(N, DIGlobalVariable::get(
2176 Context, getSubprogram(), Name, LinkageName, File, Line,
2177 Type, IsLocalToUnit, IsDefinition,
2178 StaticDataMemberDeclaration, templateParams, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002179 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002180 Line, Type, IsLocalToUnit, IsDefinition,
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002181 StaticDataMemberDeclaration,
2182 templateParams, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002183 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002184 Type, IsLocalToUnit, IsDefinition,
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002185 StaticDataMemberDeclaration,
2186 templateParams, AlignInBits));
2187 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName,
2188 getFile(), Line, Type, IsLocalToUnit,
2189 IsDefinition, StaticDataMemberDeclaration,
2190 templateParams, AlignInBits));
2191 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2192 Line + 1, Type, IsLocalToUnit,
2193 IsDefinition, StaticDataMemberDeclaration,
2194 templateParams, AlignInBits));
2195 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2196 Line, getDerivedType(), IsLocalToUnit,
2197 IsDefinition, StaticDataMemberDeclaration,
2198 templateParams, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002199 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002200 Line, Type, !IsLocalToUnit, IsDefinition,
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002201 StaticDataMemberDeclaration,
2202 templateParams, AlignInBits));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002203 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002204 Line, Type, IsLocalToUnit, !IsDefinition,
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002205 StaticDataMemberDeclaration,
2206 templateParams, AlignInBits));
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002207 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2208 Line, Type, IsLocalToUnit, IsDefinition,
2209 cast<DIDerivedType>(getDerivedType()),
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002210 templateParams, AlignInBits));
2211 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2212 Line, Type, IsLocalToUnit, IsDefinition,
2213 StaticDataMemberDeclaration, nullptr,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002214 AlignInBits));
Peter Collingbourned4135bb2016-09-13 01:12:59 +00002215 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
2216 Line, Type, IsLocalToUnit, IsDefinition,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002217 StaticDataMemberDeclaration,
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002218 templateParams, (AlignInBits << 1)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002219
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002220 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002221 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002222}
2223
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002224typedef MetadataTest DIGlobalVariableExpressionTest;
2225
2226TEST_F(DIGlobalVariableExpressionTest, get) {
2227 DIScope *Scope = getSubprogram();
2228 StringRef Name = "name";
2229 StringRef LinkageName = "linkage";
2230 DIFile *File = getFile();
2231 unsigned Line = 5;
2232 DIType *Type = getDerivedType();
2233 bool IsLocalToUnit = false;
2234 bool IsDefinition = true;
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002235 MDTuple *templateParams = getTuple();
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002236 auto *Expr = DIExpression::get(Context, {1, 2});
2237 auto *Expr2 = DIExpression::get(Context, {1, 2, 3});
2238 DIDerivedType *StaticDataMemberDeclaration =
2239 cast<DIDerivedType>(getDerivedType());
2240 uint32_t AlignInBits = 8;
2241
Matthew Vossf8ab35a2018-10-03 18:44:53 +00002242 auto *Var = DIGlobalVariable::get(
2243 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
2244 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
2245 auto *Var2 = DIGlobalVariable::get(
2246 Context, Scope, "other", LinkageName, File, Line, Type, IsLocalToUnit,
2247 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002248 auto *N = DIGlobalVariableExpression::get(Context, Var, Expr);
2249
2250 EXPECT_EQ(Var, N->getVariable());
2251 EXPECT_EQ(Expr, N->getExpression());
2252 EXPECT_EQ(N, DIGlobalVariableExpression::get(Context, Var, Expr));
2253 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var2, Expr));
2254 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var, Expr2));
2255
2256 TempDIGlobalVariableExpression Temp = N->clone();
2257 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2258}
2259
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002260typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002261
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002262TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002263 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002264 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002265 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002266 unsigned Line = 5;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002267 DIType *Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002268 unsigned Arg = 6;
Leny Kholodov5fcc4182016-09-06 10:46:28 +00002269 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
Victor Leschuka37660c2016-10-26 21:32:29 +00002270 uint32_t AlignInBits = 8;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002271
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002272 auto *N =
Victor Leschuk2ede1262016-10-20 00:13:12 +00002273 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags,
2274 AlignInBits);
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002275 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002276 EXPECT_EQ(Scope, N->getScope());
2277 EXPECT_EQ(Name, N->getName());
2278 EXPECT_EQ(File, N->getFile());
2279 EXPECT_EQ(Line, N->getLine());
2280 EXPECT_EQ(Type, N->getType());
2281 EXPECT_EQ(Arg, N->getArg());
2282 EXPECT_EQ(Flags, N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002283 EXPECT_EQ(AlignInBits, N->getAlignInBits());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002284 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002285 Flags, AlignInBits));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002286
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002287 EXPECT_FALSE(
Victor Leschuk2ede1262016-10-20 00:13:12 +00002288 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags,
2289 AlignInBits)->isParameter());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002290 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002291 Type, Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002292 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002293 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002294 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002295 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002296 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002297 Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002298 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002299 getDerivedType(), Arg, Flags, AlignInBits));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002300 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +00002301 Arg + 1, Flags, AlignInBits));
2302 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
2303 Arg, Flags, (AlignInBits << 1)));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002304
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002305 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002306 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002307}
2308
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002309TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002310 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002311 0, nullptr, 255, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002312 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002313 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002314 0, nullptr, 256, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002315 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002316 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002317 0, nullptr, 257, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002318 ->getArg());
2319 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002320 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
Victor Leschuk2ede1262016-10-20 00:13:12 +00002321 0, nullptr, Max, DINode::FlagZero, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00002322 ->getArg());
2323}
2324
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002325typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002326
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002327TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002328 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002329 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002330 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002331 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00002332
2333 EXPECT_EQ(5u, N->getNumElements());
2334 EXPECT_EQ(2u, N->getElement(0));
2335 EXPECT_EQ(6u, N->getElement(1));
2336 EXPECT_EQ(9u, N->getElement(2));
2337 EXPECT_EQ(78u, N->getElement(3));
2338 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002339
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002340 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002341 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Adrian Prantld1317012017-12-08 21:58:18 +00002342
2343 // Test DIExpression::prepend().
2344 uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
2345 auto *N0 = DIExpression::get(Context, Elts0);
Petar Jovanovice85bbf52019-05-20 10:35:57 +00002346 uint8_t DIExprFlags = DIExpression::ApplyOffset;
2347 DIExprFlags |= DIExpression::DerefBefore;
2348 DIExprFlags |= DIExpression::DerefAfter;
2349 DIExprFlags |= DIExpression::StackValue;
2350 auto *N0WithPrependedOps = DIExpression::prepend(N0, DIExprFlags, 64);
Adrian Prantld1317012017-12-08 21:58:18 +00002351 uint64_t Elts1[] = {dwarf::DW_OP_deref,
2352 dwarf::DW_OP_plus_uconst, 64,
2353 dwarf::DW_OP_deref,
2354 dwarf::DW_OP_stack_value,
2355 dwarf::DW_OP_LLVM_fragment, 0, 32};
2356 auto *N1 = DIExpression::get(Context, Elts1);
Vedant Kumarb572f642018-07-26 20:56:53 +00002357 EXPECT_EQ(N0WithPrependedOps, N1);
2358
2359 // Test DIExpression::append().
2360 uint64_t Elts2[] = {dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 64,
2361 dwarf::DW_OP_deref, dwarf::DW_OP_stack_value};
2362 auto *N2 = DIExpression::append(N0, Elts2);
2363 EXPECT_EQ(N0WithPrependedOps, N2);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002364}
2365
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002366TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002367#define EXPECT_VALID(...) \
2368 do { \
2369 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002370 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002371 } while (false)
2372#define EXPECT_INVALID(...) \
2373 do { \
2374 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002375 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002376 } while (false)
2377
2378 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002379 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002380
2381 // Valid constructions.
Florian Hahnc9c403c2017-06-13 16:54:44 +00002382 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6);
Florian Hahnffc498d2017-06-14 13:14:38 +00002383 EXPECT_VALID(dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus);
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002384 EXPECT_VALID(dwarf::DW_OP_deref);
Adrian Prantl941fa752016-12-05 18:04:47 +00002385 EXPECT_VALID(dwarf::DW_OP_LLVM_fragment, 3, 7);
Florian Hahnffc498d2017-06-14 13:14:38 +00002386 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref);
2387 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6);
Adrian Prantl941fa752016-12-05 18:04:47 +00002388 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_LLVM_fragment, 3, 7);
Florian Hahnffc498d2017-06-14 13:14:38 +00002389 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6,
Adrian Prantl941fa752016-12-05 18:04:47 +00002390 dwarf::DW_OP_LLVM_fragment, 3, 7);
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002391
2392 // Invalid constructions.
2393 EXPECT_INVALID(~0u);
Florian Hahnffc498d2017-06-14 13:14:38 +00002394 EXPECT_INVALID(dwarf::DW_OP_plus, 0);
Florian Hahnc9c403c2017-06-13 16:54:44 +00002395 EXPECT_INVALID(dwarf::DW_OP_plus_uconst);
Adrian Prantl941fa752016-12-05 18:04:47 +00002396 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment);
2397 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3);
Florian Hahnffc498d2017-06-14 13:14:38 +00002398 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_plus_uconst, 3);
Adrian Prantl941fa752016-12-05 18:04:47 +00002399 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_deref);
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00002400
2401#undef EXPECT_VALID
2402#undef EXPECT_INVALID
2403}
2404
stozer184d72a2019-11-22 16:40:32 +00002405TEST_F(DIExpressionTest, createFragmentExpression) {
2406#define EXPECT_VALID_FRAGMENT(Offset, Size, ...) \
2407 do { \
2408 uint64_t Elements[] = {__VA_ARGS__}; \
2409 DIExpression* Expression = DIExpression::get(Context, Elements); \
2410 EXPECT_TRUE(DIExpression::createFragmentExpression( \
2411 Expression, Offset, Size).hasValue()); \
2412 } while (false)
2413#define EXPECT_INVALID_FRAGMENT(Offset, Size, ...) \
2414 do { \
2415 uint64_t Elements[] = {__VA_ARGS__}; \
2416 DIExpression* Expression = DIExpression::get(Context, Elements); \
2417 EXPECT_FALSE(DIExpression::createFragmentExpression( \
2418 Expression, Offset, Size).hasValue()); \
2419 } while (false)
2420
2421 // createFragmentExpression adds correct ops.
2422 Optional<DIExpression*> R = DIExpression::createFragmentExpression(
2423 DIExpression::get(Context, {}), 0, 32);
2424 EXPECT_EQ(R.hasValue(), true);
2425 EXPECT_EQ(3u, (*R)->getNumElements());
2426 EXPECT_EQ(dwarf::DW_OP_LLVM_fragment, (*R)->getElement(0));
2427 EXPECT_EQ(0u, (*R)->getElement(1));
2428 EXPECT_EQ(32u, (*R)->getElement(2));
2429
2430 // Valid fragment expressions.
2431 EXPECT_VALID_FRAGMENT(0, 32, {});
2432 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_deref);
2433 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_LLVM_fragment, 0, 32);
2434 EXPECT_VALID_FRAGMENT(16, 16, dwarf::DW_OP_LLVM_fragment, 0, 32);
2435
2436 // Invalid fragment expressions (incompatible ops).
2437 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus);
2438 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 14, dwarf::DW_OP_minus);
2439 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 16, dwarf::DW_OP_shr);
2440 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 16, dwarf::DW_OP_shl);
2441 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 16, dwarf::DW_OP_shra);
2442 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6);
2443
2444#undef EXPECT_VALID_FRAGMENT
2445#undef EXPECT_INVALID_FRAGMENT
2446}
2447
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002448typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002449
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002450TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002451 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002452 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002453 unsigned Line = 5;
2454 StringRef GetterName = "getter";
2455 StringRef SetterName = "setter";
2456 unsigned Attributes = 7;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002457 DIType *Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002458
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002459 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002460 SetterName, Attributes, Type);
2461
2462 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
2463 EXPECT_EQ(Name, N->getName());
2464 EXPECT_EQ(File, N->getFile());
2465 EXPECT_EQ(Line, N->getLine());
2466 EXPECT_EQ(GetterName, N->getGetterName());
2467 EXPECT_EQ(SetterName, N->getSetterName());
2468 EXPECT_EQ(Attributes, N->getAttributes());
2469 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002470 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002471 SetterName, Attributes, Type));
2472
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002473 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002474 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002475 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002476 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002477 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002478 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002479 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002480 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002481 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002482 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002483 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002484 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002485 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002486 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00002487 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002488
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002489 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002490 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002491}
2492
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002493typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002494
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002495TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002496 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002497 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +00002498 DINode *Entity = getCompositeType();
Adrian Prantld63bfd22017-07-19 00:09:54 +00002499 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002500 unsigned Line = 5;
2501 StringRef Name = "name";
2502
Adrian Prantld63bfd22017-07-19 00:09:54 +00002503 auto *N =
2504 DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002505
2506 EXPECT_EQ(Tag, N->getTag());
2507 EXPECT_EQ(Scope, N->getScope());
2508 EXPECT_EQ(Entity, N->getEntity());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002509 EXPECT_EQ(File, N->getFile());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002510 EXPECT_EQ(Line, N->getLine());
2511 EXPECT_EQ(Name, N->getName());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002512 EXPECT_EQ(
2513 N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002514
2515 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002516 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Adrian Prantld63bfd22017-07-19 00:09:54 +00002517 Scope, Entity, File, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002518 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Adrian Prantld63bfd22017-07-19 00:09:54 +00002519 File, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002520 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Adrian Prantld63bfd22017-07-19 00:09:54 +00002521 File, Line, Name));
2522 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, nullptr, Line,
2523 Name));
2524 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File,
2525 Line + 1, Name));
2526 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
2527 "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002528
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002529 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002530 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002531}
2532
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002533typedef MetadataTest MetadataAsValueTest;
2534
2535TEST_F(MetadataAsValueTest, MDNode) {
2536 MDNode *N = MDNode::get(Context, None);
2537 auto *V = MetadataAsValue::get(Context, N);
2538 EXPECT_TRUE(V->getType()->isMetadataTy());
2539 EXPECT_EQ(N, V->getMetadata());
2540
2541 auto *V2 = MetadataAsValue::get(Context, N);
2542 EXPECT_EQ(V, V2);
2543}
2544
2545TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2546 MDNode *N = MDNode::get(Context, None);
2547 Metadata *Ops[] = {N};
2548 MDNode *N2 = MDNode::get(Context, Ops);
2549 auto *V = MetadataAsValue::get(Context, N2);
2550 EXPECT_TRUE(V->getType()->isMetadataTy());
2551 EXPECT_EQ(N2, V->getMetadata());
2552
2553 auto *V2 = MetadataAsValue::get(Context, N2);
2554 EXPECT_EQ(V, V2);
2555
2556 auto *V3 = MetadataAsValue::get(Context, N);
2557 EXPECT_TRUE(V3->getType()->isMetadataTy());
2558 EXPECT_NE(V, V3);
2559 EXPECT_EQ(N, V3->getMetadata());
2560}
2561
2562TEST_F(MetadataAsValueTest, MDNodeConstant) {
2563 auto *C = ConstantInt::getTrue(Context);
2564 auto *MD = ConstantAsMetadata::get(C);
2565 Metadata *Ops[] = {MD};
2566 auto *N = MDNode::get(Context, Ops);
2567
2568 auto *V = MetadataAsValue::get(Context, MD);
2569 EXPECT_TRUE(V->getType()->isMetadataTy());
2570 EXPECT_EQ(MD, V->getMetadata());
2571
2572 auto *V2 = MetadataAsValue::get(Context, N);
2573 EXPECT_EQ(MD, V2->getMetadata());
2574 EXPECT_EQ(V, V2);
2575}
2576
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002577typedef MetadataTest ValueAsMetadataTest;
2578
2579TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2580 Type *Ty = Type::getInt1PtrTy(Context);
2581 std::unique_ptr<GlobalVariable> GV0(
2582 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2583 auto *MD = ValueAsMetadata::get(GV0.get());
2584 EXPECT_TRUE(MD->getValue() == GV0.get());
2585 ASSERT_TRUE(GV0->use_empty());
2586
2587 std::unique_ptr<GlobalVariable> GV1(
2588 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2589 GV0->replaceAllUsesWith(GV1.get());
2590 EXPECT_TRUE(MD->getValue() == GV1.get());
2591}
2592
Adrian Prantlcbec1602016-02-08 17:02:34 +00002593TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2594 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002595 ConstantAsMetadata *CI =
2596 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Adrian Prantlcbec1602016-02-08 17:02:34 +00002597
Adrian Prantlcbec1602016-02-08 17:02:34 +00002598 auto Temp1 = MDTuple::getTemporary(Context, None);
Adrian Prantl817c47b2016-02-08 19:13:15 +00002599 auto Temp2 = MDTuple::getTemporary(Context, {CI});
2600 auto *N = MDTuple::get(Context, {Temp1.get()});
Adrian Prantlcbec1602016-02-08 17:02:34 +00002601
2602 // Test replacing a temporary node with another temporary node.
2603 Temp1->replaceAllUsesWith(Temp2.get());
2604 EXPECT_EQ(N->getOperand(0), Temp2.get());
2605
2606 // Clean up Temp2 for teardown.
2607 Temp2->replaceAllUsesWith(nullptr);
2608}
2609
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002610TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2611 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002612 ConstantAsMetadata *CI =
2613 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002614
2615 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002616 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002617
2618 // 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 +00002619 Metadata *Ops1[] = {CI, CI, Temp.get()};
2620 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002621
2622 auto *N1 = MDTuple::get(Context, Ops1);
2623 auto *N2 = MDTuple::get(Context, Ops2);
2624 ASSERT_NE(N1, N2);
2625
2626 // Tell metadata that the constant is getting deleted.
2627 //
2628 // After this, N1 will be invalid, so don't touch it.
2629 ValueAsMetadata::handleDeletion(CI->getValue());
2630 EXPECT_EQ(nullptr, N2->getOperand(0));
2631 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002632 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002633
2634 // Clean up Temp for teardown.
2635 Temp->replaceAllUsesWith(nullptr);
2636}
2637
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002638typedef MetadataTest TrackingMDRefTest;
2639
2640TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2641 Type *Ty = Type::getInt1PtrTy(Context);
2642 std::unique_ptr<GlobalVariable> GV0(
2643 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2644 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2645 EXPECT_TRUE(MD->getValue() == GV0.get());
2646 ASSERT_TRUE(GV0->use_empty());
2647
2648 std::unique_ptr<GlobalVariable> GV1(
2649 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2650 GV0->replaceAllUsesWith(GV1.get());
2651 EXPECT_TRUE(MD->getValue() == GV1.get());
2652
2653 // Reset it, so we don't inadvertently test deletion.
2654 MD.reset();
2655}
2656
2657TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2658 Type *Ty = Type::getInt1PtrTy(Context);
2659 std::unique_ptr<GlobalVariable> GV(
2660 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2661 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2662 EXPECT_TRUE(MD->getValue() == GV.get());
2663 ASSERT_TRUE(GV->use_empty());
2664
2665 GV.reset();
2666 EXPECT_TRUE(!MD);
2667}
2668
Devang Patel0924b332009-07-30 00:03:41 +00002669TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002670 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002671 ConstantAsMetadata *C =
2672 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2673 ConstantAsMetadata *C2 =
2674 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002675
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002676 Metadata *const V = C;
2677 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002678 MDNode *n = MDNode::get(Context, V);
2679 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002680
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002681 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002682 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002683 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2684 NMD->addOperand(n);
2685 NMD->addOperand(n2);
2686
Chris Lattnerbe354a62009-08-23 04:47:35 +00002687 std::string Str;
2688 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002689 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002690 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002691 oss.str().c_str());
2692}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002693
2694typedef MetadataTest FunctionAttachmentTest;
2695TEST_F(FunctionAttachmentTest, setMetadata) {
2696 Function *F = getFunction("foo");
2697 ASSERT_FALSE(F->hasMetadata());
2698 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2699 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2700 EXPECT_EQ(nullptr, F->getMetadata("other"));
2701
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002702 DISubprogram *SP1 = getSubprogram();
2703 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002704 ASSERT_NE(SP1, SP2);
2705
2706 F->setMetadata("dbg", SP1);
2707 EXPECT_TRUE(F->hasMetadata());
2708 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2709 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2710 EXPECT_EQ(nullptr, F->getMetadata("other"));
2711
2712 F->setMetadata(LLVMContext::MD_dbg, SP2);
2713 EXPECT_TRUE(F->hasMetadata());
2714 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2715 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2716 EXPECT_EQ(nullptr, F->getMetadata("other"));
2717
2718 F->setMetadata("dbg", nullptr);
2719 EXPECT_FALSE(F->hasMetadata());
2720 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2721 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2722 EXPECT_EQ(nullptr, F->getMetadata("other"));
2723
2724 MDTuple *T1 = getTuple();
2725 MDTuple *T2 = getTuple();
2726 ASSERT_NE(T1, T2);
2727
2728 F->setMetadata("other1", T1);
2729 F->setMetadata("other2", T2);
2730 EXPECT_TRUE(F->hasMetadata());
2731 EXPECT_EQ(T1, F->getMetadata("other1"));
2732 EXPECT_EQ(T2, F->getMetadata("other2"));
2733 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2734
2735 F->setMetadata("other1", T2);
2736 F->setMetadata("other2", T1);
2737 EXPECT_EQ(T2, F->getMetadata("other1"));
2738 EXPECT_EQ(T1, F->getMetadata("other2"));
2739
2740 F->setMetadata("other1", nullptr);
2741 F->setMetadata("other2", nullptr);
2742 EXPECT_FALSE(F->hasMetadata());
2743 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2744 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2745}
2746
2747TEST_F(FunctionAttachmentTest, getAll) {
2748 Function *F = getFunction("foo");
2749
2750 MDTuple *T1 = getTuple();
2751 MDTuple *T2 = getTuple();
2752 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002753 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002754
2755 F->setMetadata("other1", T2);
2756 F->setMetadata(LLVMContext::MD_dbg, SP);
2757 F->setMetadata("other2", T1);
2758 F->setMetadata(LLVMContext::MD_prof, P);
2759 F->setMetadata("other2", T2);
2760 F->setMetadata("other1", T1);
2761
2762 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2763 F->getAllMetadata(MDs);
2764 ASSERT_EQ(4u, MDs.size());
2765 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2766 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2767 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2768 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2769 EXPECT_EQ(SP, MDs[0].second);
2770 EXPECT_EQ(P, MDs[1].second);
2771 EXPECT_EQ(T1, MDs[2].second);
2772 EXPECT_EQ(T2, MDs[3].second);
2773}
2774
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002775TEST_F(FunctionAttachmentTest, Verifier) {
2776 Function *F = getFunction("foo");
2777 F->setMetadata("attach", getTuple());
Peter Collingbournebb738172016-06-06 23:21:27 +00002778 F->setIsMaterializable(true);
Peter Collingbournebb738172016-06-06 23:21:27 +00002779
Peter Collingbourne21521892016-06-21 23:42:48 +00002780 // Confirm this is materializable.
2781 ASSERT_TRUE(F->isMaterializable());
2782
2783 // Materializable functions cannot have metadata attachments.
2784 EXPECT_TRUE(verifyFunction(*F));
2785
2786 // Function declarations can.
Peter Collingbournebb738172016-06-06 23:21:27 +00002787 F->setIsMaterializable(false);
Peter Collingbourne21521892016-06-21 23:42:48 +00002788 EXPECT_FALSE(verifyModule(*F->getParent()));
2789 EXPECT_FALSE(verifyFunction(*F));
2790
2791 // So can definitions.
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002792 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2793 EXPECT_FALSE(verifyModule(*F->getParent()));
2794 EXPECT_FALSE(verifyFunction(*F));
2795}
2796
Diego Novillo2567f3d2015-05-13 15:13:45 +00002797TEST_F(FunctionAttachmentTest, EntryCount) {
2798 Function *F = getFunction("foo");
2799 EXPECT_FALSE(F->getEntryCount().hasValue());
Easwaran Ramane5b8de22018-01-17 22:24:23 +00002800 F->setEntryCount(12304, Function::PCT_Real);
2801 auto Count = F->getEntryCount();
2802 EXPECT_TRUE(Count.hasValue());
2803 EXPECT_EQ(12304u, Count.getCount());
2804 EXPECT_EQ(Function::PCT_Real, Count.getType());
2805
2806 // Repeat the same for synthetic counts.
2807 F = getFunction("bar");
2808 EXPECT_FALSE(F->getEntryCount().hasValue());
2809 F->setEntryCount(123, Function::PCT_Synthetic);
Xinliang David Li499c80b2019-04-24 19:51:16 +00002810 Count = F->getEntryCount(true /*allow synthetic*/);
Easwaran Ramane5b8de22018-01-17 22:24:23 +00002811 EXPECT_TRUE(Count.hasValue());
2812 EXPECT_EQ(123u, Count.getCount());
2813 EXPECT_EQ(Function::PCT_Synthetic, Count.getType());
Diego Novillo2567f3d2015-05-13 15:13:45 +00002814}
2815
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002816TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2817 Function *F = getFunction("foo");
2818 DISubprogram *SP = getSubprogram();
2819 F->setSubprogram(SP);
2820
2821 // Note that the static_cast confirms that F->getSubprogram() actually
2822 // returns an DISubprogram.
2823 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2824 EXPECT_EQ(SP, F->getMetadata("dbg"));
2825 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2826}
2827
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002828typedef MetadataTest DistinctMDOperandPlaceholderTest;
2829TEST_F(DistinctMDOperandPlaceholderTest, getID) {
2830 EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
2831}
2832
2833TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
2834 // Set up some placeholders.
2835 DistinctMDOperandPlaceholder PH0(7);
2836 DistinctMDOperandPlaceholder PH1(3);
2837 DistinctMDOperandPlaceholder PH2(0);
2838 Metadata *Ops[] = {&PH0, &PH1, &PH2};
2839 auto *D = MDTuple::getDistinct(Context, Ops);
2840 ASSERT_EQ(&PH0, D->getOperand(0));
2841 ASSERT_EQ(&PH1, D->getOperand(1));
2842 ASSERT_EQ(&PH2, D->getOperand(2));
2843
2844 // Replace them.
2845 auto *N0 = MDTuple::get(Context, None);
2846 auto *N1 = MDTuple::get(Context, N0);
2847 PH0.replaceUseWith(N0);
2848 PH1.replaceUseWith(N1);
2849 PH2.replaceUseWith(nullptr);
2850 EXPECT_EQ(N0, D->getOperand(0));
2851 EXPECT_EQ(N1, D->getOperand(1));
2852 EXPECT_EQ(nullptr, D->getOperand(2));
2853}
2854
2855TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
2856 // There is no user, but we can still call replace.
2857 DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None));
2858}
2859
Reid Kleckner6be1ed02017-08-10 21:14:07 +00002860// Test various assertions in metadata tracking. Don't run these tests if gtest
2861// will use SEH to recover from them. Two of these tests get halfway through
2862// inserting metadata into DenseMaps for tracking purposes, and then they
2863// assert, and we attempt to destroy an LLVMContext with broken invariants,
2864// leading to infinite loops.
2865#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) && !defined(GTEST_HAS_SEH)
Duncan P. N. Exon Smith4b1bc642016-04-23 04:15:56 +00002866TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
2867 // This shouldn't crash.
2868 DistinctMDOperandPlaceholder PH(7);
2869 EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
2870 "Unexpected callback to owner");
2871}
2872
2873TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
2874 // This shouldn't crash.
2875 DistinctMDOperandPlaceholder PH(7);
2876 EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
2877}
2878
2879TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
2880 // This shouldn't crash.
2881 DistinctMDOperandPlaceholder PH(7);
2882 MDTuple::getDistinct(Context, &PH);
2883 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2884 "Placeholders can only be used once");
2885}
2886
2887TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
2888 // TrackingMDRef doesn't install an owner callback, so it can't be detected
2889 // as an invalid use. However, using a placeholder in a TrackingMDRef *and*
2890 // a distinct node isn't possible and we should assert.
2891 //
2892 // (There's no positive test for using TrackingMDRef because it's not a
2893 // useful thing to do.)
2894 {
2895 DistinctMDOperandPlaceholder PH(7);
2896 MDTuple::getDistinct(Context, &PH);
2897 EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
2898 }
2899 {
2900 DistinctMDOperandPlaceholder PH(7);
2901 TrackingMDRef Ref(&PH);
2902 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2903 "Placeholders can only be used once");
2904 }
2905}
2906#endif
2907
stozer269a9af2019-12-03 12:24:41 +00002908typedef MetadataTest DebugVariableTest;
2909TEST_F(DebugVariableTest, DenseMap) {
2910 DenseMap<DebugVariable, uint64_t> DebugVariableMap;
2911
2912 DILocalScope *Scope = getSubprogram();
2913 DIFile *File = getFile();
2914 DIType *Type = getDerivedType();
2915 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
2916
2917 DILocation *InlinedLoc = DILocation::get(Context, 2, 7, Scope);
2918
2919 DILocalVariable *VarA =
2920 DILocalVariable::get(Context, Scope, "A", File, 5, Type, 2, Flags, 8);
2921 DILocalVariable *VarB =
2922 DILocalVariable::get(Context, Scope, "B", File, 7, Type, 3, Flags, 8);
2923
2924 DebugVariable DebugVariableA(VarA, NoneType(), nullptr);
2925 DebugVariable DebugVariableInlineA(VarA, NoneType(), InlinedLoc);
2926 DebugVariable DebugVariableB(VarB, NoneType(), nullptr);
2927 DebugVariable DebugVariableFragB(VarB, {{16, 16}}, nullptr);
2928
2929 DebugVariableMap.insert({DebugVariableA, 2});
2930 DebugVariableMap.insert({DebugVariableInlineA, 3});
2931 DebugVariableMap.insert({DebugVariableB, 6});
2932 DebugVariableMap.insert({DebugVariableFragB, 12});
2933
David Blaikie3e0d21d2019-12-03 11:36:12 -08002934 EXPECT_EQ(DebugVariableMap.count(DebugVariableA), 1u);
2935 EXPECT_EQ(DebugVariableMap.count(DebugVariableInlineA), 1u);
2936 EXPECT_EQ(DebugVariableMap.count(DebugVariableB), 1u);
2937 EXPECT_EQ(DebugVariableMap.count(DebugVariableFragB), 1u);
stozer269a9af2019-12-03 12:24:41 +00002938
David Blaikie3e0d21d2019-12-03 11:36:12 -08002939 EXPECT_EQ(DebugVariableMap.find(DebugVariableA)->second, 2u);
2940 EXPECT_EQ(DebugVariableMap.find(DebugVariableInlineA)->second, 3u);
2941 EXPECT_EQ(DebugVariableMap.find(DebugVariableB)->second, 6u);
2942 EXPECT_EQ(DebugVariableMap.find(DebugVariableFragB)->second, 12u);
stozer269a9af2019-12-03 12:24:41 +00002943}
2944
Duncan P. N. Exon Smith2923a432016-04-23 04:02:39 +00002945} // end namespace