blob: 11596160466cfef55fcc62d84ba7b388b75cb7c6 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +000012#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000013#include "llvm/IR/DebugInfoMetadata.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000014#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +000019#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000023#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024using namespace llvm;
25
26namespace {
27
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000028TEST(ContextAndReplaceableUsesTest, FromContext) {
29 LLVMContext Context;
30 ContextAndReplaceableUses CRU(Context);
31 EXPECT_EQ(&Context, &CRU.getContext());
32 EXPECT_FALSE(CRU.hasReplaceableUses());
33 EXPECT_FALSE(CRU.getReplaceableUses());
34}
35
36TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
37 LLVMContext Context;
38 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
39 EXPECT_EQ(&Context, &CRU.getContext());
40 EXPECT_TRUE(CRU.hasReplaceableUses());
41 EXPECT_TRUE(CRU.getReplaceableUses());
42}
43
44TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
45 LLVMContext Context;
46 ContextAndReplaceableUses CRU(Context);
47 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
48 EXPECT_EQ(&Context, &CRU.getContext());
49 EXPECT_TRUE(CRU.hasReplaceableUses());
50 EXPECT_TRUE(CRU.getReplaceableUses());
51}
52
53TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
54 LLVMContext Context;
55 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
56 auto *Ptr = ReplaceableUses.get();
57 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
58 ReplaceableUses = CRU.takeReplaceableUses();
59 EXPECT_EQ(&Context, &CRU.getContext());
60 EXPECT_FALSE(CRU.hasReplaceableUses());
61 EXPECT_FALSE(CRU.getReplaceableUses());
62 EXPECT_EQ(Ptr, ReplaceableUses.get());
63}
64
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000065class MetadataTest : public testing::Test {
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000066public:
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000067 MetadataTest() : M("test", Context), Counter(0) {}
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000068
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000069protected:
70 LLVMContext Context;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000071 Module M;
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000072 int Counter;
73
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000074 MDNode *getNode() { return MDNode::get(Context, None); }
75 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
76 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
77 Metadata *MDs[] = {MD1, MD2};
78 return MDNode::get(Context, MDs);
79 }
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000080
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +000081 MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000082 DISubroutineType *getSubroutineType() {
83 return DISubroutineType::getDistinct(Context, 0, getNode(nullptr));
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +000084 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000085 DISubprogram *getSubprogram() {
86 return DISubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
Adrian Prantl75819ae2016-04-15 15:57:41 +000087 nullptr, false, false, 0, nullptr,
88 0, 0, 0, false, nullptr);
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +000089 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000090 DIScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
91 DIFile *getFile() {
92 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +000093 }
Adrian Prantl75819ae2016-04-15 15:57:41 +000094 DICompileUnit *getUnit() {
95 return DICompileUnit::getDistinct(Context, 1, getFile(), "clang", false,
96 "-g", 2, "", DICompileUnit::FullDebug,
97 getTuple(), getTuple(), getTuple(),
98 getTuple(), getTuple(), 0);
99 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000100 DITypeRef getBasicType(StringRef Name) {
101 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000102 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000103 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000104 DITypeRef getDerivedType() {
105 return DIDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000106 nullptr, 0, nullptr,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000107 getBasicType("basictype"), 1, 2, 0, 0)
108 ->getRef();
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000109 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000110 Constant *getConstant() {
111 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
112 }
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000113 ConstantAsMetadata *getConstantAsMetadata() {
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000114 return ConstantAsMetadata::get(getConstant());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000115 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000116 DITypeRef getCompositeType() {
117 return DICompositeType::getDistinct(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000118 Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
119 nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
120 ->getRef();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000121 }
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +0000122 Function *getFunction(StringRef Name) {
123 return cast<Function>(M.getOrInsertFunction(
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000124 Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
125 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000126};
127typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +0000128
Nick Lewycky49f89192009-04-04 07:22:01 +0000129// Test that construction of MDString with different value produces different
130// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000131TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000132 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +0000133 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000134 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +0000135 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000136 EXPECT_NE(s1, s2);
137}
138
139// Test that creation of MDStrings with the same string contents produces the
140// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000141TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000142 char x[4] = { 'a', 'b', 'c', 'X' };
143 char y[4] = { 'a', 'b', 'c', 'Y' };
144
Owen Anderson23587322009-07-31 21:38:10 +0000145 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
146 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +0000147 EXPECT_EQ(s1, s2);
148}
149
150// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000151TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000152 char *str = new char[13];
153 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +0000154 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +0000155 strncpy(str, "aaaaaaaaaaaaa", 13);
156 delete[] str;
157
Chris Lattnerbe354a62009-08-23 04:47:35 +0000158 std::string Str;
159 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000160 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000161 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000162}
163
164// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000165TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000166 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000167 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000168 std::string Str;
169 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000170 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000171 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000172}
173
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000174typedef MetadataTest MDNodeTest;
175
Nick Lewycky49f89192009-04-04 07:22:01 +0000176// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000177TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000178 char x[3] = { 'a', 'b', 'c' };
179 char y[3] = { '1', '2', '3' };
180
Owen Anderson23587322009-07-31 21:38:10 +0000181 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
182 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Mehdi Amini03b42e42016-04-14 21:59:01 +0000183 ConstantAsMetadata *CI =
184 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000185
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000186 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000187 V.push_back(s1);
188 V.push_back(CI);
189 V.push_back(s2);
190
Jay Foad5514afe2011-04-21 19:59:31 +0000191 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000192 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000193 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000194 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000195 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000196 MDNode *n4 = MDNode::getIfExists(Context, V);
197 MDNode *n5 = MDNode::getIfExists(Context, c1);
198 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000199 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000200 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000201 EXPECT_EQ(n4, n1);
202 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000203 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000204
Chris Lattner9b493022009-12-31 01:22:29 +0000205 EXPECT_EQ(3u, n1->getNumOperands());
206 EXPECT_EQ(s1, n1->getOperand(0));
207 EXPECT_EQ(CI, n1->getOperand(1));
208 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000209
Chris Lattner9b493022009-12-31 01:22:29 +0000210 EXPECT_EQ(1u, n2->getNumOperands());
211 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000212}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000213
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000214TEST_F(MDNodeTest, Delete) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000215 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
216 Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000217
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000218 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000219 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000220 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000221
222 EXPECT_EQ(n, wvh);
223
224 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000225}
Devang Patel0924b332009-07-30 00:03:41 +0000226
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000227TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000228 // !0 = !{!0}
229 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000230 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000231 auto Temp = MDNode::getTemporary(Context, None);
232 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000233 MDNode *Self = MDNode::get(Context, Args);
234 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000235 ASSERT_EQ(Self, Self->getOperand(0));
236
237 // Self-references should be distinct, so MDNode::get() should grab a
238 // uniqued node that references Self, not Self.
239 Args[0] = Self;
240 MDNode *Ref1 = MDNode::get(Context, Args);
241 MDNode *Ref2 = MDNode::get(Context, Args);
242 EXPECT_NE(Self, Ref1);
243 EXPECT_EQ(Ref1, Ref2);
244 }
245
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000246 // !0 = !{!0, !{}}
247 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000248 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000249 auto Temp = MDNode::getTemporary(Context, None);
250 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000251 MDNode *Self = MDNode::get(Context, Args);
252 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000253 ASSERT_EQ(Self, Self->getOperand(0));
254
255 // Self-references should be distinct, so MDNode::get() should grab a
256 // uniqued node that references Self, not Self itself.
257 Args[0] = Self;
258 MDNode *Ref1 = MDNode::get(Context, Args);
259 MDNode *Ref2 = MDNode::get(Context, Args);
260 EXPECT_NE(Self, Ref1);
261 EXPECT_EQ(Ref1, Ref2);
262 }
263}
264
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000265TEST_F(MDNodeTest, Print) {
266 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
267 MDString *S = MDString::get(Context, "foo");
268 MDNode *N0 = getNode();
269 MDNode *N1 = getNode(N0);
270 MDNode *N2 = getNode(N0, N1);
271
272 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
273 MDNode *N = MDNode::get(Context, Args);
274
275 std::string Expected;
276 {
277 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000278 OS << "<" << (void *)N << "> = !{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000279 C->printAsOperand(OS);
280 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000281 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000282 OS << ", null";
283 MDNode *Nodes[] = {N0, N1, N2};
284 for (auto *Node : Nodes)
285 OS << ", <" << (void *)Node << ">";
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +0000286 OS << "}";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000287 }
288
289 std::string Actual;
290 {
291 raw_string_ostream OS(Actual);
292 N->print(OS);
293 }
294
295 EXPECT_EQ(Expected, Actual);
296}
297
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000298#define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
299 do { \
300 std::string Actual_; \
301 raw_string_ostream OS(Actual_); \
302 PRINT; \
303 OS.flush(); \
304 std::string Expected_(EXPECTED); \
305 EXPECT_EQ(Expected_, Actual_); \
306 } while (false)
307
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +0000308TEST_F(MDNodeTest, PrintTemporary) {
309 MDNode *Arg = getNode();
310 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
311 MDNode *N = getNode(Temp.get());
312 Module M("test", Context);
313 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
314 NMD->addOperand(N);
315
316 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
317 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
318 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
319
320 // Cleanup.
321 Temp->replaceAllUsesWith(Arg);
322}
323
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000324TEST_F(MDNodeTest, PrintFromModule) {
325 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
326 MDString *S = MDString::get(Context, "foo");
327 MDNode *N0 = getNode();
328 MDNode *N1 = getNode(N0);
329 MDNode *N2 = getNode(N0, N1);
330
331 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
332 MDNode *N = MDNode::get(Context, Args);
333 Module M("test", Context);
334 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
335 NMD->addOperand(N);
336
337 std::string Expected;
338 {
339 raw_string_ostream OS(Expected);
340 OS << "!0 = !{";
341 C->printAsOperand(OS);
342 OS << ", ";
343 S->printAsOperand(OS);
344 OS << ", null, !1, !2, !3}";
345 }
346
347 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
348}
349
350TEST_F(MDNodeTest, PrintFromFunction) {
351 Module M("test", Context);
352 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
353 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
354 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
355 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
356 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
357 auto *R0 = ReturnInst::Create(Context, BB0);
358 auto *R1 = ReturnInst::Create(Context, BB1);
359 auto *N0 = MDNode::getDistinct(Context, None);
360 auto *N1 = MDNode::getDistinct(Context, None);
361 R0->setMetadata("md", N0);
362 R1->setMetadata("md", N1);
363
364 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
365 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000366
367 ModuleSlotTracker MST(&M);
368 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
369 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000370}
371
372TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
373 Module M("test", Context);
374
375 auto *Intrinsic =
376 Function::Create(FunctionType::get(Type::getVoidTy(Context),
377 Type::getMetadataTy(Context), false),
378 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
379
380 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
381 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
382 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
383 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
384 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
385 auto *N0 = MDNode::getDistinct(Context, None);
386 auto *N1 = MDNode::getDistinct(Context, None);
387 auto *MAV0 = MetadataAsValue::get(Context, N0);
388 auto *MAV1 = MetadataAsValue::get(Context, N1);
389 CallInst::Create(Intrinsic, MAV0, "", BB0);
390 CallInst::Create(Intrinsic, MAV1, "", BB1);
391
392 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
393 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
394 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
395 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
396 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
397 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +0000398
399 ModuleSlotTracker MST(&M);
400 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
401 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
402 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
403 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
404 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
405 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000406}
407#undef EXPECT_PRINTER_EQ
408
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000409TEST_F(MDNodeTest, NullOperand) {
410 // metadata !{}
411 MDNode *Empty = MDNode::get(Context, None);
412
413 // metadata !{metadata !{}}
414 Metadata *Ops[] = {Empty};
415 MDNode *N = MDNode::get(Context, Ops);
416 ASSERT_EQ(Empty, N->getOperand(0));
417
418 // metadata !{metadata !{}} => metadata !{null}
419 N->replaceOperandWith(0, nullptr);
420 ASSERT_EQ(nullptr, N->getOperand(0));
421
422 // metadata !{null}
423 Ops[0] = nullptr;
424 MDNode *NullOp = MDNode::get(Context, Ops);
425 ASSERT_EQ(nullptr, NullOp->getOperand(0));
426 EXPECT_EQ(N, NullOp);
427}
428
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000429TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
430 // !{}
431 MDNode *Empty = MDNode::get(Context, None);
432 ASSERT_TRUE(Empty->isResolved());
433 EXPECT_FALSE(Empty->isDistinct());
434
435 // !{!{}}
436 Metadata *Wrapped1Ops[] = {Empty};
437 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
438 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
439 ASSERT_TRUE(Wrapped1->isResolved());
440 EXPECT_FALSE(Wrapped1->isDistinct());
441
442 // !{!{!{}}}
443 Metadata *Wrapped2Ops[] = {Wrapped1};
444 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
445 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
446 ASSERT_TRUE(Wrapped2->isResolved());
447 EXPECT_FALSE(Wrapped2->isDistinct());
448
449 // !{!{!{}}} => !{!{}}
450 Wrapped2->replaceOperandWith(0, Empty);
451 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
452 EXPECT_TRUE(Wrapped2->isDistinct());
453 EXPECT_FALSE(Wrapped1->isDistinct());
454}
455
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000456TEST_F(MDNodeTest, getDistinct) {
457 // !{}
458 MDNode *Empty = MDNode::get(Context, None);
459 ASSERT_TRUE(Empty->isResolved());
460 ASSERT_FALSE(Empty->isDistinct());
461 ASSERT_EQ(Empty, MDNode::get(Context, None));
462
463 // distinct !{}
464 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
465 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
466 EXPECT_TRUE(Distinct1->isResolved());
467 EXPECT_TRUE(Distinct2->isDistinct());
468 EXPECT_NE(Empty, Distinct1);
469 EXPECT_NE(Empty, Distinct2);
470 EXPECT_NE(Distinct1, Distinct2);
471
472 // !{}
473 ASSERT_EQ(Empty, MDNode::get(Context, None));
474}
475
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000476TEST_F(MDNodeTest, isUniqued) {
477 MDNode *U = MDTuple::get(Context, None);
478 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000479 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000480 EXPECT_TRUE(U->isUniqued());
481 EXPECT_FALSE(D->isUniqued());
482 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000483}
484
485TEST_F(MDNodeTest, isDistinct) {
486 MDNode *U = MDTuple::get(Context, None);
487 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000488 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000489 EXPECT_FALSE(U->isDistinct());
490 EXPECT_TRUE(D->isDistinct());
491 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000492}
493
494TEST_F(MDNodeTest, isTemporary) {
495 MDNode *U = MDTuple::get(Context, None);
496 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000497 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000498 EXPECT_FALSE(U->isTemporary());
499 EXPECT_FALSE(D->isTemporary());
500 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000501}
502
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000503TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
504 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000505 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000506 ASSERT_FALSE(Temp->isResolved());
507
508 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000509 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000510 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
511 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000512 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000513
514 // temporary !{} => !{}
515 MDNode *Empty = MDNode::get(Context, None);
516 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000517 EXPECT_EQ(Empty, Distinct->getOperand(0));
518}
519
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000520TEST_F(MDNodeTest, handleChangedOperandRecursion) {
521 // !0 = !{}
522 MDNode *N0 = MDNode::get(Context, None);
523
524 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000525 auto Temp3 = MDTuple::getTemporary(Context, None);
526 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000527 MDNode *N1 = MDNode::get(Context, Ops1);
528
529 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000530 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000531 MDNode *N2 = MDNode::get(Context, Ops2);
532
533 // !3 = !{!2}
534 Metadata *Ops3[] = {N2};
535 MDNode *N3 = MDNode::get(Context, Ops3);
536 Temp3->replaceAllUsesWith(N3);
537
538 // !4 = !{!1}
539 Metadata *Ops4[] = {N1};
540 MDNode *N4 = MDNode::get(Context, Ops4);
541
542 // Confirm that the cycle prevented RAUW from getting dropped.
543 EXPECT_TRUE(N0->isResolved());
544 EXPECT_FALSE(N1->isResolved());
545 EXPECT_FALSE(N2->isResolved());
546 EXPECT_FALSE(N3->isResolved());
547 EXPECT_FALSE(N4->isResolved());
548
549 // Create a couple of distinct nodes to observe what's going on.
550 //
551 // !5 = distinct !{!2}
552 // !6 = distinct !{!3}
553 Metadata *Ops5[] = {N2};
554 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
555 Metadata *Ops6[] = {N3};
556 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
557
558 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
559 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
560 // references !3, this can cause a re-entry of handleChangedOperand() when !3
561 // is not ready for it.
562 //
563 // !2->replaceOperandWith(1, nullptr)
564 // !2: !{!3, !0} => !{!3, null}
565 // !2->replaceAllUsesWith(!1)
566 // !3: !{!2] => !{!1}
567 // !3->replaceAllUsesWith(!4)
568 N2->replaceOperandWith(1, nullptr);
569
570 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
571 // under us. Just check that the other nodes are sane.
572 //
573 // !1 = !{!4, null}
574 // !4 = !{!1}
575 // !5 = distinct !{!1}
576 // !6 = distinct !{!4}
577 EXPECT_EQ(N4, N1->getOperand(0));
578 EXPECT_EQ(N1, N4->getOperand(0));
579 EXPECT_EQ(N1, N5->getOperand(0));
580 EXPECT_EQ(N4, N6->getOperand(0));
581}
582
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000583TEST_F(MDNodeTest, replaceResolvedOperand) {
584 // Check code for replacing one resolved operand with another. If doing this
585 // directly (via replaceOperandWith()) becomes illegal, change the operand to
586 // a global value that gets RAUW'ed.
587 //
588 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000589 auto Temp = MDTuple::getTemporary(Context, None);
590 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000591
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000592 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000593 MDNode *N = MDTuple::get(Context, Ops);
594 EXPECT_EQ(nullptr, N->getOperand(0));
595 ASSERT_FALSE(N->isResolved());
596
597 // Check code for replacing resolved nodes.
598 N->replaceOperandWith(0, Empty);
599 EXPECT_EQ(Empty, N->getOperand(0));
600
601 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000602 N->replaceOperandWith(0, Temp.get());
603 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000604
605 // Remove the references to Temp; required for teardown.
606 Temp->replaceAllUsesWith(nullptr);
607}
608
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000609TEST_F(MDNodeTest, replaceWithUniqued) {
610 auto *Empty = MDTuple::get(Context, None);
611 MDTuple *FirstUniqued;
612 {
613 Metadata *Ops[] = {Empty};
614 auto Temp = MDTuple::getTemporary(Context, Ops);
615 EXPECT_TRUE(Temp->isTemporary());
616
617 // Don't expect a collision.
618 auto *Current = Temp.get();
619 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
620 EXPECT_TRUE(FirstUniqued->isUniqued());
621 EXPECT_TRUE(FirstUniqued->isResolved());
622 EXPECT_EQ(Current, FirstUniqued);
623 }
624 {
625 Metadata *Ops[] = {Empty};
626 auto Temp = MDTuple::getTemporary(Context, Ops);
627 EXPECT_TRUE(Temp->isTemporary());
628
629 // Should collide with Uniqued above this time.
630 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
631 EXPECT_TRUE(Uniqued->isUniqued());
632 EXPECT_TRUE(Uniqued->isResolved());
633 EXPECT_EQ(FirstUniqued, Uniqued);
634 }
635 {
636 auto Unresolved = MDTuple::getTemporary(Context, None);
637 Metadata *Ops[] = {Unresolved.get()};
638 auto Temp = MDTuple::getTemporary(Context, Ops);
639 EXPECT_TRUE(Temp->isTemporary());
640
641 // Shouldn't be resolved.
642 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
643 EXPECT_TRUE(Uniqued->isUniqued());
644 EXPECT_FALSE(Uniqued->isResolved());
645
646 // Should be a different node.
647 EXPECT_NE(FirstUniqued, Uniqued);
648
649 // Should resolve when we update its node (note: be careful to avoid a
650 // collision with any other nodes above).
651 Uniqued->replaceOperandWith(0, nullptr);
652 EXPECT_TRUE(Uniqued->isResolved());
653 }
654}
655
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000656TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000657 // temp !{}
658 MDTuple *Op = MDTuple::getTemporary(Context, None).release();
659 EXPECT_FALSE(Op->isResolved());
660
661 // temp !{temp !{}}
662 Metadata *Ops[] = {Op};
663 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
664 EXPECT_FALSE(N->isResolved());
665
666 // temp !{temp !{}} => !{temp !{}}
667 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
668 EXPECT_FALSE(N->isResolved());
669
670 // !{temp !{}} => !{!{}}
671 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
672 EXPECT_TRUE(Op->isResolved());
673 EXPECT_TRUE(N->isResolved());
674}
675
Duncan P. N. Exon Smith014f1b82015-03-31 21:05:06 +0000676TEST_F(MDNodeTest, replaceWithUniquedChangingOperand) {
Duncan P. N. Exon Smithcb33d6f2015-03-31 20:50:50 +0000677 // i1* @GV
678 Type *Ty = Type::getInt1PtrTy(Context);
679 std::unique_ptr<GlobalVariable> GV(
680 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
681 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
682
683 // temp !{i1* @GV}
684 Metadata *Ops[] = {Op};
685 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
686
687 // temp !{i1* @GV} => !{i1* @GV}
688 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
689 ASSERT_TRUE(N->isUniqued());
690
691 // !{i1* @GV} => !{null}
692 GV.reset();
693 ASSERT_TRUE(N->isUniqued());
694 Metadata *NullOps[] = {nullptr};
695 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
696}
697
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000698TEST_F(MDNodeTest, replaceWithDistinct) {
699 {
700 auto *Empty = MDTuple::get(Context, None);
701 Metadata *Ops[] = {Empty};
702 auto Temp = MDTuple::getTemporary(Context, Ops);
703 EXPECT_TRUE(Temp->isTemporary());
704
705 // Don't expect a collision.
706 auto *Current = Temp.get();
707 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
708 EXPECT_TRUE(Distinct->isDistinct());
709 EXPECT_TRUE(Distinct->isResolved());
710 EXPECT_EQ(Current, Distinct);
711 }
712 {
713 auto Unresolved = MDTuple::getTemporary(Context, None);
714 Metadata *Ops[] = {Unresolved.get()};
715 auto Temp = MDTuple::getTemporary(Context, Ops);
716 EXPECT_TRUE(Temp->isTemporary());
717
718 // Don't expect a collision.
719 auto *Current = Temp.get();
720 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
721 EXPECT_TRUE(Distinct->isDistinct());
722 EXPECT_TRUE(Distinct->isResolved());
723 EXPECT_EQ(Current, Distinct);
724
725 // Cleanup; required for teardown.
726 Unresolved->replaceAllUsesWith(nullptr);
727 }
728}
729
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000730TEST_F(MDNodeTest, replaceWithPermanent) {
731 Metadata *Ops[] = {nullptr};
732 auto Temp = MDTuple::getTemporary(Context, Ops);
733 auto *T = Temp.get();
734
735 // U is a normal, uniqued node that references T.
736 auto *U = MDTuple::get(Context, T);
737 EXPECT_TRUE(U->isUniqued());
738
739 // Make Temp self-referencing.
740 Temp->replaceOperandWith(0, T);
741
742 // Try to uniquify Temp. This should, despite the name in the API, give a
743 // 'distinct' node, since self-references aren't allowed to be uniqued.
744 //
745 // Since it's distinct, N should have the same address as when it was a
746 // temporary (i.e., be equal to T not U).
747 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
748 EXPECT_EQ(N, T);
749 EXPECT_TRUE(N->isDistinct());
750
751 // U should be the canonical unique node with N as the argument.
752 EXPECT_EQ(U, MDTuple::get(Context, N));
753 EXPECT_TRUE(U->isUniqued());
754
755 // This temporary should collide with U when replaced, but it should still be
756 // uniqued.
757 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
758 EXPECT_TRUE(U->isUniqued());
759
760 // This temporary should become a new uniqued node.
761 auto Temp2 = MDTuple::getTemporary(Context, U);
762 auto *V = Temp2.get();
763 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
764 EXPECT_TRUE(V->isUniqued());
765 EXPECT_EQ(U, V->getOperand(0));
766}
767
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000768TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
769 TrackingMDRef Ref;
770 EXPECT_EQ(nullptr, Ref.get());
771 {
772 auto Temp = MDTuple::getTemporary(Context, None);
773 Ref.reset(Temp.get());
774 EXPECT_EQ(Temp.get(), Ref.get());
775 }
776 EXPECT_EQ(nullptr, Ref.get());
777}
778
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000779typedef MetadataTest DILocationTest;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000780
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000781TEST_F(DILocationTest, Overflow) {
782 DISubprogram *N = getSubprogram();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000783 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000784 DILocation *L = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000785 EXPECT_EQ(2u, L->getLine());
786 EXPECT_EQ(7u, L->getColumn());
787 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000788 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000789 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000790 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000791 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000792 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000793 }
794 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000795 DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000796 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000797 EXPECT_EQ(0u, L->getColumn());
798 }
799 {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000800 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000801 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000802 EXPECT_EQ(0u, L->getColumn());
803 }
804}
805
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000806TEST_F(DILocationTest, getDistinct) {
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000807 MDNode *N = getSubprogram();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000808 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000809 EXPECT_TRUE(L0->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000810 DILocation *L1 = DILocation::get(Context, 2, 7, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000811 EXPECT_FALSE(L1->isDistinct());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000812 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000813}
814
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000815TEST_F(DILocationTest, getTemporary) {
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000816 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000817 auto L = DILocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000818 EXPECT_TRUE(L->isTemporary());
819 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000820}
821
Teresa Johnsond98152b62015-12-07 15:05:44 +0000822TEST_F(DILocationTest, cloneTemporary) {
823 MDNode *N = MDNode::get(Context, None);
824 auto L = DILocation::getTemporary(Context, 2, 7, N);
825 EXPECT_TRUE(L->isTemporary());
826 auto L2 = L->clone();
827 EXPECT_TRUE(L2->isTemporary());
828}
829
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000830typedef MetadataTest GenericDINodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000831
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000832TEST_F(GenericDINodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000833 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000834 auto *Empty = MDNode::get(Context, None);
835 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000836 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000837 EXPECT_EQ(15u, N->getTag());
838 EXPECT_EQ(2u, N->getNumOperands());
839 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000840 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000841 EXPECT_EQ(1u, N->getNumDwarfOperands());
842 EXPECT_EQ(Empty, N->getDwarfOperand(0));
843 EXPECT_EQ(Empty, N->getOperand(1));
844 ASSERT_TRUE(N->isUniqued());
845
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000846 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000847
848 N->replaceOperandWith(1, nullptr);
849 EXPECT_EQ(15u, N->getTag());
850 EXPECT_EQ(Header, N->getHeader());
851 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
852 ASSERT_TRUE(N->isUniqued());
853
854 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000855 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000856
857 N->replaceDwarfOperandWith(0, Empty);
858 EXPECT_EQ(15u, N->getTag());
859 EXPECT_EQ(Header, N->getHeader());
860 EXPECT_EQ(Empty, N->getDwarfOperand(0));
861 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000862 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000863
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000864 TempGenericDINode Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000865 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000866}
867
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000868TEST_F(GenericDINodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000869 // Canonicalize !"" to null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000870 auto *N = GenericDINode::get(Context, 15, StringRef(), None);
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000871 EXPECT_EQ(StringRef(), N->getHeader());
872 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000873}
874
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000875typedef MetadataTest DISubrangeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000876
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000877TEST_F(DISubrangeTest, get) {
878 auto *N = DISubrange::get(Context, 5, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000879 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
880 EXPECT_EQ(5, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000881 EXPECT_EQ(7, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000882 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
883 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000884
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000885 TempDISubrange Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000886 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000887}
888
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000889TEST_F(DISubrangeTest, getEmptyArray) {
890 auto *N = DISubrange::get(Context, -1, 0);
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000891 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
892 EXPECT_EQ(-1, N->getCount());
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000893 EXPECT_EQ(0, N->getLowerBound());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000894 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +0000895}
896
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000897typedef MetadataTest DIEnumeratorTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000898
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000899TEST_F(DIEnumeratorTest, get) {
900 auto *N = DIEnumerator::get(Context, 7, "name");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000901 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
902 EXPECT_EQ(7, N->getValue());
903 EXPECT_EQ("name", N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000904 EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000905
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000906 EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
907 EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000908
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000909 TempDIEnumerator Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000910 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000911}
912
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000913typedef MetadataTest DIBasicTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000914
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000915TEST_F(DIBasicTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000916 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000917 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000918 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
919 EXPECT_EQ("special", N->getName());
920 EXPECT_EQ(33u, N->getSizeInBits());
921 EXPECT_EQ(26u, N->getAlignInBits());
922 EXPECT_EQ(7u, N->getEncoding());
923 EXPECT_EQ(0u, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000924 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000925 26, 7));
926
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000927 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000928 "special", 33, 26, 7));
929 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000930 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
931 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000932 26, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000933 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000934 25, 7));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000935 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000936 26, 6));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000937
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000938 TempDIBasicType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +0000939 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000940}
941
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000942TEST_F(DIBasicTypeTest, getWithLargeValues) {
943 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000944 UINT64_MAX, UINT64_MAX - 1, 7);
945 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
946 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
947}
948
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000949TEST_F(DIBasicTypeTest, getUnspecified) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000950 auto *N =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000951 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000952 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
953 EXPECT_EQ("unspecified", N->getName());
954 EXPECT_EQ(0u, N->getSizeInBits());
955 EXPECT_EQ(0u, N->getAlignInBits());
956 EXPECT_EQ(0u, N->getEncoding());
957 EXPECT_EQ(0u, N->getLine());
958}
959
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000960typedef MetadataTest DITypeTest;
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000961
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000962TEST_F(DITypeTest, clone) {
963 // Check that DIType has a specialized clone that returns TempDIType.
964 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000965 dwarf::DW_ATE_signed);
966
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000967 TempDIType Temp = N->clone();
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000968 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
969}
970
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000971TEST_F(DITypeTest, setFlags) {
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000972 // void (void)
973 Metadata *TypesOps[] = {nullptr};
974 Metadata *Types = MDTuple::get(Context, TypesOps);
975
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000976 DIType *D = DISubroutineType::getDistinct(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000977 EXPECT_EQ(0u, D->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000978 D->setFlags(DINode::FlagRValueReference);
979 EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000980 D->setFlags(0u);
981 EXPECT_EQ(0u, D->getFlags());
982
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000983 TempDIType T = DISubroutineType::getTemporary(Context, 0u, Types);
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000984 EXPECT_EQ(0u, T->getFlags());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000985 T->setFlags(DINode::FlagRValueReference);
986 EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
Duncan P. N. Exon Smithb3538492015-03-03 16:45:34 +0000987 T->setFlags(0u);
988 EXPECT_EQ(0u, T->getFlags());
989}
990
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000991typedef MetadataTest DIDerivedTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000992
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000993TEST_F(DIDerivedTypeTest, get) {
994 DIFile *File = getFile();
995 DIScopeRef Scope = getSubprogramRef();
996 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000997 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000998
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000999 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001000 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
1001 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
1002 EXPECT_EQ("something", N->getName());
1003 EXPECT_EQ(File, N->getFile());
1004 EXPECT_EQ(1u, N->getLine());
1005 EXPECT_EQ(Scope, N->getScope());
1006 EXPECT_EQ(BaseType, N->getBaseType());
1007 EXPECT_EQ(2u, N->getSizeInBits());
1008 EXPECT_EQ(3u, N->getAlignInBits());
1009 EXPECT_EQ(4u, N->getOffsetInBits());
1010 EXPECT_EQ(5u, N->getFlags());
1011 EXPECT_EQ(ExtraData, N->getExtraData());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001012 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001013 "something", File, 1, Scope, BaseType, 2, 3,
1014 4, 5, ExtraData));
1015
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001016 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001017 "something", File, 1, Scope, BaseType, 2, 3,
1018 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001019 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001020 File, 1, Scope, BaseType, 2, 3, 4, 5,
1021 ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001022 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001023 "something", getFile(), 1, Scope, BaseType, 2,
1024 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001025 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001026 "something", File, 2, Scope, BaseType, 2, 3,
1027 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001028 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001029 "something", File, 1, getSubprogramRef(),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001030 BaseType, 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001031 EXPECT_NE(N, DIDerivedType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001032 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1033 Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001034 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001035 "something", File, 1, Scope, BaseType, 3, 3,
1036 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001037 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001038 "something", File, 1, Scope, BaseType, 2, 2,
1039 4, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001040 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001041 "something", File, 1, Scope, BaseType, 2, 3,
1042 5, 5, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001043 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001044 "something", File, 1, Scope, BaseType, 2, 3,
1045 4, 4, ExtraData));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001046 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001047 "something", File, 1, Scope, BaseType, 2, 3,
1048 4, 5, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001049
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001050 TempDIDerivedType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001051 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001052}
1053
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001054TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1055 DIFile *File = getFile();
1056 DIScopeRef Scope = getSubprogramRef();
1057 DITypeRef BaseType = getBasicType("basic");
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001058 MDTuple *ExtraData = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001059
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001060 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001061 File, 1, Scope, BaseType, UINT64_MAX,
1062 UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1063 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1064 EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1065 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1066}
1067
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001068typedef MetadataTest DICompositeTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001069
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001070TEST_F(DICompositeTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001071 unsigned Tag = dwarf::DW_TAG_structure_type;
1072 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001073 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001074 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001075 DIScopeRef Scope = getSubprogramRef();
1076 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001077 uint64_t SizeInBits = 2;
1078 uint64_t AlignInBits = 3;
1079 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001080 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001081 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001082 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001083 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001084 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001085 StringRef Identifier = "some id";
1086
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001087 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001088 BaseType, SizeInBits, AlignInBits,
1089 OffsetInBits, Flags, Elements, RuntimeLang,
1090 VTableHolder, TemplateParams, Identifier);
1091 EXPECT_EQ(Tag, N->getTag());
1092 EXPECT_EQ(Name, N->getName());
1093 EXPECT_EQ(File, N->getFile());
1094 EXPECT_EQ(Line, N->getLine());
1095 EXPECT_EQ(Scope, N->getScope());
1096 EXPECT_EQ(BaseType, N->getBaseType());
1097 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1098 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1099 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1100 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001101 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001102 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1103 EXPECT_EQ(VTableHolder, N->getVTableHolder());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001104 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001105 EXPECT_EQ(Identifier, N->getIdentifier());
1106
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001107 EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001108 BaseType, SizeInBits, AlignInBits,
1109 OffsetInBits, Flags, Elements, RuntimeLang,
1110 VTableHolder, TemplateParams, Identifier));
1111
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001112 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001113 BaseType, SizeInBits, AlignInBits,
1114 OffsetInBits, Flags, Elements, RuntimeLang,
1115 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001116 EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001117 BaseType, SizeInBits, AlignInBits,
1118 OffsetInBits, Flags, Elements, RuntimeLang,
1119 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001120 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001121 BaseType, SizeInBits, AlignInBits,
1122 OffsetInBits, Flags, Elements, RuntimeLang,
1123 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001124 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001125 BaseType, SizeInBits, AlignInBits,
1126 OffsetInBits, Flags, Elements, RuntimeLang,
1127 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001128 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001129 Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001130 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1131 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001132 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001133 Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1134 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1135 RuntimeLang, VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001136 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001137 BaseType, SizeInBits + 1, AlignInBits,
1138 OffsetInBits, Flags, Elements, RuntimeLang,
1139 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001140 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001141 BaseType, SizeInBits, AlignInBits + 1,
1142 OffsetInBits, Flags, Elements, RuntimeLang,
1143 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001144 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001145 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1146 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1147 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001148 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001149 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1150 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1151 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001152 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001153 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1154 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1155 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001156 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001157 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1158 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1159 VTableHolder, TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001160 EXPECT_NE(N, DICompositeType::get(
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001161 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1162 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1163 getCompositeType(), TemplateParams, Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001164 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001165 BaseType, SizeInBits, AlignInBits,
1166 OffsetInBits, Flags, Elements, RuntimeLang,
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001167 VTableHolder, getTuple(), Identifier));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001168 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001169 BaseType, SizeInBits, AlignInBits,
1170 OffsetInBits, Flags, Elements, RuntimeLang,
1171 VTableHolder, TemplateParams, "other"));
1172
1173 // Be sure that missing identifiers get null pointers.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001174 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1175 BaseType, SizeInBits, AlignInBits,
1176 OffsetInBits, Flags, Elements, RuntimeLang,
1177 VTableHolder, TemplateParams, "")
1178 ->getRawIdentifier());
1179 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1180 BaseType, SizeInBits, AlignInBits,
1181 OffsetInBits, Flags, Elements, RuntimeLang,
1182 VTableHolder, TemplateParams)
1183 ->getRawIdentifier());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001184
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001185 TempDICompositeType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001186 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001187}
1188
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001189TEST_F(DICompositeTypeTest, getWithLargeValues) {
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001190 unsigned Tag = dwarf::DW_TAG_structure_type;
1191 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001192 DIFile *File = getFile();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001193 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001194 DIScopeRef Scope = getSubprogramRef();
1195 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001196 uint64_t SizeInBits = UINT64_MAX;
1197 uint64_t AlignInBits = UINT64_MAX - 1;
1198 uint64_t OffsetInBits = UINT64_MAX - 2;
1199 unsigned Flags = 5;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001200 MDTuple *Elements = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001201 unsigned RuntimeLang = 6;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001202 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001203 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001204 StringRef Identifier = "some id";
1205
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001206 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001207 BaseType, SizeInBits, AlignInBits,
1208 OffsetInBits, Flags, Elements, RuntimeLang,
1209 VTableHolder, TemplateParams, Identifier);
1210 EXPECT_EQ(SizeInBits, N->getSizeInBits());
1211 EXPECT_EQ(AlignInBits, N->getAlignInBits());
1212 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1213}
1214
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001215TEST_F(DICompositeTypeTest, replaceOperands) {
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001216 unsigned Tag = dwarf::DW_TAG_structure_type;
1217 StringRef Name = "some name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001218 DIFile *File = getFile();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001219 unsigned Line = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001220 DIScopeRef Scope = getSubprogramRef();
1221 DITypeRef BaseType = getCompositeType();
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00001222 uint64_t SizeInBits = 2;
1223 uint64_t AlignInBits = 3;
1224 uint64_t OffsetInBits = 4;
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001225 unsigned Flags = 5;
1226 unsigned RuntimeLang = 6;
1227 StringRef Identifier = "some id";
1228
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001229 auto *N = DICompositeType::get(
1230 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1231 OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001232
1233 auto *Elements = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001234 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001235 N->replaceElements(Elements);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001236 EXPECT_EQ(Elements, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001237 N->replaceElements(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001238 EXPECT_EQ(nullptr, N->getElements().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001239
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001240 DITypeRef VTableHolder = getCompositeType();
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001241 EXPECT_EQ(nullptr, N->getVTableHolder());
1242 N->replaceVTableHolder(VTableHolder);
1243 EXPECT_EQ(VTableHolder, N->getVTableHolder());
1244 N->replaceVTableHolder(nullptr);
1245 EXPECT_EQ(nullptr, N->getVTableHolder());
1246
1247 auto *TemplateParams = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001248 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001249 N->replaceTemplateParams(TemplateParams);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001250 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001251 N->replaceTemplateParams(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001252 EXPECT_EQ(nullptr, N->getTemplateParams().get());
Duncan P. N. Exon Smithf51e00d2015-02-18 20:47:52 +00001253}
1254
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001255typedef MetadataTest DISubroutineTypeTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001256
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001257TEST_F(DISubroutineTypeTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001258 unsigned Flags = 1;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001259 MDTuple *TypeArray = getTuple();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001260
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001261 auto *N = DISubroutineType::get(Context, Flags, TypeArray);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001262 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1263 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001264 EXPECT_EQ(TypeArray, N->getTypeArray().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001265 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, TypeArray));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001266
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001267 EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, TypeArray));
1268 EXPECT_NE(N, DISubroutineType::get(Context, Flags, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001269
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001270 TempDISubroutineType Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001271 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smitha9f0a8d2015-02-19 23:25:21 +00001272
1273 // Test always-empty operands.
1274 EXPECT_EQ(nullptr, N->getScope());
1275 EXPECT_EQ(nullptr, N->getFile());
1276 EXPECT_EQ("", N->getName());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001277}
1278
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001279typedef MetadataTest DIFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001280
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001281TEST_F(DIFileTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001282 StringRef Filename = "file";
1283 StringRef Directory = "dir";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001284 auto *N = DIFile::get(Context, Filename, Directory);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001285
1286 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1287 EXPECT_EQ(Filename, N->getFilename());
1288 EXPECT_EQ(Directory, N->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001289 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001290
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001291 EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1292 EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001293
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001294 TempDIFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001295 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001296}
1297
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001298TEST_F(DIFileTest, ScopeGetFile) {
1299 // Ensure that DIScope::getFile() returns itself.
1300 DIScope *N = DIFile::get(Context, "file", "dir");
Duncan P. N. Exon Smith2c6a0a92015-02-28 21:47:02 +00001301 EXPECT_EQ(N, N->getFile());
1302}
1303
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001304typedef MetadataTest DICompileUnitTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001305
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001306TEST_F(DICompileUnitTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001307 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001308 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001309 StringRef Producer = "some producer";
1310 bool IsOptimized = false;
1311 StringRef Flags = "flag after flag";
1312 unsigned RuntimeVersion = 2;
1313 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001314 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001315 MDTuple *EnumTypes = getTuple();
1316 MDTuple *RetainedTypes = getTuple();
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001317 MDTuple *GlobalVariables = getTuple();
1318 MDTuple *ImportedEntities = getTuple();
Adrian Prantle3b49e02015-09-22 23:42:47 +00001319 uint64_t DWOId = 0x10000000c0ffee;
Amjad Abouda9bcf162015-12-10 12:56:35 +00001320 MDTuple *Macros = getTuple();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001321 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001322 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1323 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001324 RetainedTypes, GlobalVariables, ImportedEntities, Macros,
Amjad Abouda9bcf162015-12-10 12:56:35 +00001325 DWOId);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001326
1327 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1328 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1329 EXPECT_EQ(File, N->getFile());
1330 EXPECT_EQ(Producer, N->getProducer());
1331 EXPECT_EQ(IsOptimized, N->isOptimized());
1332 EXPECT_EQ(Flags, N->getFlags());
1333 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1334 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1335 EXPECT_EQ(EmissionKind, N->getEmissionKind());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001336 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1337 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001338 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1339 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001340 EXPECT_EQ(Macros, N->getMacros().get());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001341 EXPECT_EQ(DWOId, N->getDWOId());
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001342
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001343 TempDICompileUnit Temp = N->clone();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001344 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1345 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1346 EXPECT_EQ(File, Temp->getFile());
1347 EXPECT_EQ(Producer, Temp->getProducer());
1348 EXPECT_EQ(IsOptimized, Temp->isOptimized());
1349 EXPECT_EQ(Flags, Temp->getFlags());
1350 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1351 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1352 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1353 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1354 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001355 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1356 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001357 EXPECT_EQ(Macros, Temp->getMacros().get());
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001358 EXPECT_EQ(DWOId, Temp->getDWOId());
1359
1360 auto *TempAddress = Temp.get();
1361 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1362 EXPECT_TRUE(Clone->isDistinct());
1363 EXPECT_EQ(TempAddress, Clone);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001364}
1365
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001366TEST_F(DICompileUnitTest, replaceArrays) {
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001367 unsigned SourceLanguage = 1;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001368 DIFile *File = getFile();
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001369 StringRef Producer = "some producer";
1370 bool IsOptimized = false;
1371 StringRef Flags = "flag after flag";
1372 unsigned RuntimeVersion = 2;
1373 StringRef SplitDebugFilename = "another/file";
Adrian Prantlb939a252016-03-31 23:56:58 +00001374 auto EmissionKind = DICompileUnit::FullDebug;
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001375 MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1376 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1377 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
Adrian Prantl1f599f92015-05-21 20:37:30 +00001378 uint64_t DWOId = 0xc0ffee;
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001379 auto *N = DICompileUnit::getDistinct(
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001380 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1381 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001382 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId);
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001383
1384 auto *GlobalVariables = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001385 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001386 N->replaceGlobalVariables(GlobalVariables);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001387 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001388 N->replaceGlobalVariables(nullptr);
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001389 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001390
1391 auto *Macros = MDTuple::getDistinct(Context, None);
1392 EXPECT_EQ(nullptr, N->getMacros().get());
1393 N->replaceMacros(Macros);
1394 EXPECT_EQ(Macros, N->getMacros().get());
1395 N->replaceMacros(nullptr);
1396 EXPECT_EQ(nullptr, N->getMacros().get());
Duncan P. N. Exon Smith94bbbf02015-02-18 20:36:09 +00001397}
1398
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001399typedef MetadataTest DISubprogramTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001400
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001401TEST_F(DISubprogramTest, get) {
1402 DIScopeRef Scope = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001403 StringRef Name = "name";
1404 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001405 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001406 unsigned Line = 2;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001407 DISubroutineType *Type = getSubroutineType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001408 bool IsLocalToUnit = false;
1409 bool IsDefinition = true;
1410 unsigned ScopeLine = 3;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001411 DITypeRef ContainingType = getCompositeType();
Davide Italiano236e7442016-04-13 20:17:42 +00001412 unsigned Virtuality = 2;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001413 unsigned VirtualIndex = 5;
1414 unsigned Flags = 6;
Davide Italiano236e7442016-04-13 20:17:42 +00001415 unsigned NotFlags = (~Flags) & ((1 << 27) - 1);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001416 bool IsOptimized = false;
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001417 MDTuple *TemplateParams = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001418 DISubprogram *Declaration = getSubprogram();
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001419 MDTuple *Variables = getTuple();
Adrian Prantl75819ae2016-04-15 15:57:41 +00001420 DICompileUnit *Unit = getUnit();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001421
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001422 auto *N = DISubprogram::get(
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001423 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1424 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001425 IsOptimized, Unit, TemplateParams, Declaration, Variables);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001426
1427 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1428 EXPECT_EQ(Scope, N->getScope());
1429 EXPECT_EQ(Name, N->getName());
1430 EXPECT_EQ(LinkageName, N->getLinkageName());
1431 EXPECT_EQ(File, N->getFile());
1432 EXPECT_EQ(Line, N->getLine());
1433 EXPECT_EQ(Type, N->getType());
1434 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1435 EXPECT_EQ(IsDefinition, N->isDefinition());
1436 EXPECT_EQ(ScopeLine, N->getScopeLine());
1437 EXPECT_EQ(ContainingType, N->getContainingType());
1438 EXPECT_EQ(Virtuality, N->getVirtuality());
1439 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1440 EXPECT_EQ(Flags, N->getFlags());
1441 EXPECT_EQ(IsOptimized, N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001442 EXPECT_EQ(Unit, N->getUnit());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001443 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001444 EXPECT_EQ(Declaration, N->getDeclaration());
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +00001445 EXPECT_EQ(Variables, N->getVariables().get());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001446 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001447 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1448 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001449 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001450 Declaration, Variables));
1451
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001452 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001453 File, Line, Type, IsLocalToUnit, IsDefinition,
1454 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001455 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001456 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001457 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001458 Line, Type, IsLocalToUnit, IsDefinition,
1459 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001460 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001461 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001462 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001463 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1464 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001465 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001466 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001467 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001468 Line, Type, IsLocalToUnit, IsDefinition,
1469 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001470 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001471 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001472 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001473 Line + 1, Type, IsLocalToUnit, IsDefinition,
1474 ScopeLine, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001475 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001476 TemplateParams, Declaration, Variables));
Peter Collingbourned4bff302015-11-05 22:03:56 +00001477 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1478 getSubroutineType(), IsLocalToUnit,
1479 IsDefinition, ScopeLine, ContainingType,
1480 Virtuality, VirtualIndex, Flags, IsOptimized,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001481 Unit, TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001482 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001483 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1484 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001485 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001486 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001487 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001488 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1489 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001490 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001491 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001492 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001493 Type, IsLocalToUnit, IsDefinition,
1494 ScopeLine + 1, ContainingType, Virtuality,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001495 VirtualIndex, Flags, IsOptimized, Unit,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001496 TemplateParams, Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001497 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001498 Type, IsLocalToUnit, IsDefinition, ScopeLine,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001499 getCompositeType(), Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001500 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001501 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001502 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001503 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1504 ContainingType, Virtuality + 1, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001505 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001506 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001507 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001508 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1509 ContainingType, Virtuality, VirtualIndex + 1,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001510 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001511 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001512 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001513 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1514 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001515 NotFlags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001516 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001517 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001518 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1519 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001520 Flags, !IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001521 Declaration, Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001522 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001523 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1524 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001525 Flags, IsOptimized, nullptr, TemplateParams,
1526 Declaration, Variables));
1527 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1528 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1529 ContainingType, Virtuality, VirtualIndex,
1530 Flags, IsOptimized, Unit, getTuple(),
1531 Declaration, Variables));
1532 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1533 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1534 ContainingType, Virtuality, VirtualIndex,
1535 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001536 getSubprogram(), Variables));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001537 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001538 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1539 ContainingType, Virtuality, VirtualIndex,
Adrian Prantl75819ae2016-04-15 15:57:41 +00001540 Flags, IsOptimized, Unit, TemplateParams,
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001541 Declaration, getTuple()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001542
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001543 TempDISubprogram Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001544 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001545}
1546
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001547typedef MetadataTest DILexicalBlockTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001548
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001549TEST_F(DILexicalBlockTest, get) {
1550 DILocalScope *Scope = getSubprogram();
1551 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001552 unsigned Line = 5;
1553 unsigned Column = 8;
1554
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001555 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001556
1557 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1558 EXPECT_EQ(Scope, N->getScope());
1559 EXPECT_EQ(File, N->getFile());
1560 EXPECT_EQ(Line, N->getLine());
1561 EXPECT_EQ(Column, N->getColumn());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001562 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001563
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001564 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001565 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1566 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1567 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1568 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001569
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001570 TempDILexicalBlock Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001571 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001572}
1573
Duncan P. N. Exon Smithb09eb9f2015-08-28 22:58:50 +00001574TEST_F(DILexicalBlockTest, Overflow) {
1575 DISubprogram *SP = getSubprogram();
1576 DIFile *F = getFile();
1577 {
1578 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1579 EXPECT_EQ(2u, LB->getLine());
1580 EXPECT_EQ(7u, LB->getColumn());
1581 }
1582 unsigned U16 = 1u << 16;
1583 {
1584 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1585 EXPECT_EQ(UINT32_MAX, LB->getLine());
1586 EXPECT_EQ(U16 - 1, LB->getColumn());
1587 }
1588 {
1589 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1590 EXPECT_EQ(UINT32_MAX, LB->getLine());
1591 EXPECT_EQ(0u, LB->getColumn());
1592 }
1593 {
1594 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1595 EXPECT_EQ(UINT32_MAX, LB->getLine());
1596 EXPECT_EQ(0u, LB->getColumn());
1597 }
1598}
1599
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001600typedef MetadataTest DILexicalBlockFileTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001601
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001602TEST_F(DILexicalBlockFileTest, get) {
1603 DILocalScope *Scope = getSubprogram();
1604 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001605 unsigned Discriminator = 5;
1606
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001607 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001608
1609 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1610 EXPECT_EQ(Scope, N->getScope());
1611 EXPECT_EQ(File, N->getFile());
1612 EXPECT_EQ(Discriminator, N->getDiscriminator());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001613 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001614
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001615 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001616 Discriminator));
1617 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001618 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001619 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001620 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001621
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001622 TempDILexicalBlockFile Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001623 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001624}
1625
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001626typedef MetadataTest DINamespaceTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001627
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001628TEST_F(DINamespaceTest, get) {
1629 DIScope *Scope = getFile();
1630 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001631 StringRef Name = "namespace";
1632 unsigned Line = 5;
1633
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001634 auto *N = DINamespace::get(Context, Scope, File, Name, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001635
1636 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1637 EXPECT_EQ(Scope, N->getScope());
1638 EXPECT_EQ(File, N->getFile());
1639 EXPECT_EQ(Name, N->getName());
1640 EXPECT_EQ(Line, N->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001641 EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001642
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001643 EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1644 EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1645 EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1646 EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001647
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001648 TempDINamespace 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 Smith01fc1762015-02-10 00:52:32 +00001650}
1651
Adrian Prantlab1243f2015-06-29 23:03:47 +00001652typedef MetadataTest DIModuleTest;
1653
1654TEST_F(DIModuleTest, get) {
1655 DIScope *Scope = getFile();
1656 StringRef Name = "module";
1657 StringRef ConfigMacro = "-DNDEBUG";
1658 StringRef Includes = "-I.";
1659 StringRef Sysroot = "/";
1660
1661 auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1662
1663 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1664 EXPECT_EQ(Scope, N->getScope());
1665 EXPECT_EQ(Name, N->getName());
1666 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1667 EXPECT_EQ(Includes, N->getIncludePath());
1668 EXPECT_EQ(Sysroot, N->getISysRoot());
1669 EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1670 ConfigMacro, Includes, Sysroot));
1671 EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1672 ConfigMacro, Includes, Sysroot));
1673 EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1674 ConfigMacro, Includes, Sysroot));
1675 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1676 "other", Includes, Sysroot));
1677 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1678 ConfigMacro, "other", Sysroot));
1679 EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1680 ConfigMacro, Includes, "other"));
1681
1682 TempDIModule Temp = N->clone();
1683 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1684}
1685
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001686typedef MetadataTest DITemplateTypeParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001687
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001688TEST_F(DITemplateTypeParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001689 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001690 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001691
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001692 auto *N = DITemplateTypeParameter::get(Context, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001693
1694 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001695 EXPECT_EQ(Name, N->getName());
1696 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001697 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001698
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001699 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001700 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001701 DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001702
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001703 TempDITemplateTypeParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001704 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001705}
1706
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001707typedef MetadataTest DITemplateValueParameterTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001708
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001709TEST_F(DITemplateValueParameterTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001710 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001711 StringRef Name = "template";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001712 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001713 Metadata *Value = getConstantAsMetadata();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001714
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001715 auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001716 EXPECT_EQ(Tag, N->getTag());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001717 EXPECT_EQ(Name, N->getName());
1718 EXPECT_EQ(Type, N->getType());
1719 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001720 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001721
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001722 EXPECT_NE(N, DITemplateValueParameter::get(
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00001723 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1724 Type, Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001725 EXPECT_NE(N,
1726 DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1727 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001728 getBasicType("other"), Value));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001729 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001730 getConstantAsMetadata()));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001731
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001732 TempDITemplateValueParameter Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001733 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001734}
1735
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001736typedef MetadataTest DIGlobalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001737
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001738TEST_F(DIGlobalVariableTest, get) {
1739 DIScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001740 StringRef Name = "name";
1741 StringRef LinkageName = "linkage";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001742 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001743 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001744 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001745 bool IsLocalToUnit = false;
1746 bool IsDefinition = true;
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001747 Constant *Variable = getConstant();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001748 DIDerivedType *StaticDataMemberDeclaration =
1749 cast<DIDerivedType>(getDerivedType());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001750
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001751 auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001752 Type, IsLocalToUnit, IsDefinition, Variable,
1753 StaticDataMemberDeclaration);
1754 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1755 EXPECT_EQ(Scope, N->getScope());
1756 EXPECT_EQ(Name, N->getName());
1757 EXPECT_EQ(LinkageName, N->getLinkageName());
1758 EXPECT_EQ(File, N->getFile());
1759 EXPECT_EQ(Line, N->getLine());
1760 EXPECT_EQ(Type, N->getType());
1761 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1762 EXPECT_EQ(IsDefinition, N->isDefinition());
1763 EXPECT_EQ(Variable, N->getVariable());
1764 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001765 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001766 Line, Type, IsLocalToUnit, IsDefinition,
1767 Variable, StaticDataMemberDeclaration));
1768
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001769 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001770 DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001771 File, Line, Type, IsLocalToUnit, IsDefinition,
1772 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001773 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001774 Line, Type, IsLocalToUnit, IsDefinition,
1775 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001776 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001777 Type, IsLocalToUnit, IsDefinition,
1778 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001779 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001780 DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001781 Line, Type, IsLocalToUnit, IsDefinition,
1782 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001783 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001784 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001785 Line + 1, Type, IsLocalToUnit, IsDefinition,
1786 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001787 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001788 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001789 getDerivedType(), IsLocalToUnit, IsDefinition,
1790 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001791 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001792 Line, Type, !IsLocalToUnit, IsDefinition,
1793 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001794 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001795 Line, Type, IsLocalToUnit, !IsDefinition,
1796 Variable, StaticDataMemberDeclaration));
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001797 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith7ad0bd52015-04-11 20:27:40 +00001799 Type, IsLocalToUnit, IsDefinition,
1800 getConstant(), StaticDataMemberDeclaration));
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001801 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001802 DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001803 Type, IsLocalToUnit, IsDefinition, Variable,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001804 cast<DIDerivedType>(getDerivedType())));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001805
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001806 TempDIGlobalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001807 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001808}
1809
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001810typedef MetadataTest DILocalVariableTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001811
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001812TEST_F(DILocalVariableTest, get) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001813 DILocalScope *Scope = getSubprogram();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001814 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001815 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001816 unsigned Line = 5;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001817 DITypeRef Type = getDerivedType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001818 unsigned Arg = 6;
1819 unsigned Flags = 7;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001820
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001821 auto *N =
1822 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags);
1823 EXPECT_TRUE(N->isParameter());
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001824 EXPECT_EQ(Scope, N->getScope());
1825 EXPECT_EQ(Name, N->getName());
1826 EXPECT_EQ(File, N->getFile());
1827 EXPECT_EQ(Line, N->getLine());
1828 EXPECT_EQ(Type, N->getType());
1829 EXPECT_EQ(Arg, N->getArg());
1830 EXPECT_EQ(Flags, N->getFlags());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001831 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1832 Flags));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001833
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001834 EXPECT_FALSE(
1835 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags)
1836 ->isParameter());
1837 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001838 Type, Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001839 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
1840 Arg, Flags));
1841 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
1842 Arg, Flags));
1843 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
1844 Arg, Flags));
1845 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001846 getDerivedType(), Arg, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001847 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001848 Arg + 1, Flags));
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001849 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1850 ~Flags));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001851
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001852 TempDILocalVariable Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001853 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001854}
1855
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001856TEST_F(DILocalVariableTest, getArg256) {
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001857 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1858 0, nullptr, 255, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001859 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001860 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1861 0, nullptr, 256, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001862 ->getArg());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001863 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1864 0, nullptr, 257, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001865 ->getArg());
1866 unsigned Max = UINT16_MAX;
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001867 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1868 0, nullptr, Max, 0)
Duncan P. N. Exon Smith1ec75ae2015-04-28 01:07:33 +00001869 ->getArg());
1870}
1871
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001872typedef MetadataTest DIExpressionTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001873
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001874TEST_F(DIExpressionTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001875 uint64_t Elements[] = {2, 6, 9, 78, 0};
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001876 auto *N = DIExpression::get(Context, Elements);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001877 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001878 EXPECT_EQ(N, DIExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001879
1880 EXPECT_EQ(5u, N->getNumElements());
1881 EXPECT_EQ(2u, N->getElement(0));
1882 EXPECT_EQ(6u, N->getElement(1));
1883 EXPECT_EQ(9u, N->getElement(2));
1884 EXPECT_EQ(78u, N->getElement(3));
1885 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001886
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001887 TempDIExpression Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001888 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001889}
1890
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001891TEST_F(DIExpressionTest, isValid) {
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001892#define EXPECT_VALID(...) \
1893 do { \
1894 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001895 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001896 } while (false)
1897#define EXPECT_INVALID(...) \
1898 do { \
1899 uint64_t Elements[] = {__VA_ARGS__}; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001900 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001901 } while (false)
1902
1903 // Empty expression should be valid.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001904 EXPECT_TRUE(DIExpression::get(Context, None));
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +00001905
1906 // Valid constructions.
1907 EXPECT_VALID(dwarf::DW_OP_plus, 6);
1908 EXPECT_VALID(dwarf::DW_OP_deref);
1909 EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1910 EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1911 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1912 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1913 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1914
1915 // Invalid constructions.
1916 EXPECT_INVALID(~0u);
1917 EXPECT_INVALID(dwarf::DW_OP_plus);
1918 EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1919 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1920 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1921 EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1922
1923#undef EXPECT_VALID
1924#undef EXPECT_INVALID
1925}
1926
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001927typedef MetadataTest DIObjCPropertyTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001928
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001929TEST_F(DIObjCPropertyTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001930 StringRef Name = "name";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001931 DIFile *File = getFile();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001932 unsigned Line = 5;
1933 StringRef GetterName = "getter";
1934 StringRef SetterName = "setter";
1935 unsigned Attributes = 7;
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001936 DITypeRef Type = getBasicType("basic");
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001937
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001938 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001939 SetterName, Attributes, Type);
1940
1941 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1942 EXPECT_EQ(Name, N->getName());
1943 EXPECT_EQ(File, N->getFile());
1944 EXPECT_EQ(Line, N->getLine());
1945 EXPECT_EQ(GetterName, N->getGetterName());
1946 EXPECT_EQ(SetterName, N->getSetterName());
1947 EXPECT_EQ(Attributes, N->getAttributes());
1948 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001949 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001950 SetterName, Attributes, Type));
1951
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001952 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001953 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001954 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001955 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001956 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001957 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001958 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001959 SetterName, Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001960 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001961 "other", Attributes, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001962 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001963 SetterName, Attributes + 1, Type));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001964 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001965 SetterName, Attributes,
Adrian Prantl8ff53b32015-06-15 23:18:03 +00001966 getBasicType("other")));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001967
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001968 TempDIObjCProperty Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00001969 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001970}
1971
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001972typedef MetadataTest DIImportedEntityTest;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001973
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001974TEST_F(DIImportedEntityTest, get) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001975 unsigned Tag = dwarf::DW_TAG_imported_module;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001976 DIScope *Scope = getSubprogram();
1977 DINodeRef Entity = getCompositeType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001978 unsigned Line = 5;
1979 StringRef Name = "name";
1980
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001981 auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001982
1983 EXPECT_EQ(Tag, N->getTag());
1984 EXPECT_EQ(Scope, N->getScope());
1985 EXPECT_EQ(Entity, N->getEntity());
1986 EXPECT_EQ(Line, N->getLine());
1987 EXPECT_EQ(Name, N->getName());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001988 EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001989
1990 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001991 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001992 Scope, Entity, Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001993 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001994 Line, Name));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001995 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001996 Line, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001997 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001998 DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001999 EXPECT_NE(N,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002000 DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002001
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002002 TempDIImportedEntity Temp = N->clone();
Duncan P. N. Exon Smith6873fea2015-02-17 23:10:13 +00002003 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002004}
2005
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002006typedef MetadataTest MetadataAsValueTest;
2007
2008TEST_F(MetadataAsValueTest, MDNode) {
2009 MDNode *N = MDNode::get(Context, None);
2010 auto *V = MetadataAsValue::get(Context, N);
2011 EXPECT_TRUE(V->getType()->isMetadataTy());
2012 EXPECT_EQ(N, V->getMetadata());
2013
2014 auto *V2 = MetadataAsValue::get(Context, N);
2015 EXPECT_EQ(V, V2);
2016}
2017
2018TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2019 MDNode *N = MDNode::get(Context, None);
2020 Metadata *Ops[] = {N};
2021 MDNode *N2 = MDNode::get(Context, Ops);
2022 auto *V = MetadataAsValue::get(Context, N2);
2023 EXPECT_TRUE(V->getType()->isMetadataTy());
2024 EXPECT_EQ(N2, V->getMetadata());
2025
2026 auto *V2 = MetadataAsValue::get(Context, N2);
2027 EXPECT_EQ(V, V2);
2028
2029 auto *V3 = MetadataAsValue::get(Context, N);
2030 EXPECT_TRUE(V3->getType()->isMetadataTy());
2031 EXPECT_NE(V, V3);
2032 EXPECT_EQ(N, V3->getMetadata());
2033}
2034
2035TEST_F(MetadataAsValueTest, MDNodeConstant) {
2036 auto *C = ConstantInt::getTrue(Context);
2037 auto *MD = ConstantAsMetadata::get(C);
2038 Metadata *Ops[] = {MD};
2039 auto *N = MDNode::get(Context, Ops);
2040
2041 auto *V = MetadataAsValue::get(Context, MD);
2042 EXPECT_TRUE(V->getType()->isMetadataTy());
2043 EXPECT_EQ(MD, V->getMetadata());
2044
2045 auto *V2 = MetadataAsValue::get(Context, N);
2046 EXPECT_EQ(MD, V2->getMetadata());
2047 EXPECT_EQ(V, V2);
2048}
2049
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002050typedef MetadataTest ValueAsMetadataTest;
2051
2052TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2053 Type *Ty = Type::getInt1PtrTy(Context);
2054 std::unique_ptr<GlobalVariable> GV0(
2055 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2056 auto *MD = ValueAsMetadata::get(GV0.get());
2057 EXPECT_TRUE(MD->getValue() == GV0.get());
2058 ASSERT_TRUE(GV0->use_empty());
2059
2060 std::unique_ptr<GlobalVariable> GV1(
2061 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2062 GV0->replaceAllUsesWith(GV1.get());
2063 EXPECT_TRUE(MD->getValue() == GV1.get());
2064}
2065
Adrian Prantlcbec1602016-02-08 17:02:34 +00002066TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2067 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002068 ConstantAsMetadata *CI =
2069 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Adrian Prantlcbec1602016-02-08 17:02:34 +00002070
Adrian Prantlcbec1602016-02-08 17:02:34 +00002071 auto Temp1 = MDTuple::getTemporary(Context, None);
Adrian Prantl817c47b2016-02-08 19:13:15 +00002072 auto Temp2 = MDTuple::getTemporary(Context, {CI});
2073 auto *N = MDTuple::get(Context, {Temp1.get()});
Adrian Prantlcbec1602016-02-08 17:02:34 +00002074
2075 // Test replacing a temporary node with another temporary node.
2076 Temp1->replaceAllUsesWith(Temp2.get());
2077 EXPECT_EQ(N->getOperand(0), Temp2.get());
2078
2079 // Clean up Temp2 for teardown.
2080 Temp2->replaceAllUsesWith(nullptr);
2081}
2082
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002083TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2084 // Create a constant.
Mehdi Amini03b42e42016-04-14 21:59:01 +00002085 ConstantAsMetadata *CI =
2086 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002087
2088 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002089 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002090
2091 // 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 +00002092 Metadata *Ops1[] = {CI, CI, Temp.get()};
2093 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002094
2095 auto *N1 = MDTuple::get(Context, Ops1);
2096 auto *N2 = MDTuple::get(Context, Ops2);
2097 ASSERT_NE(N1, N2);
2098
2099 // Tell metadata that the constant is getting deleted.
2100 //
2101 // After this, N1 will be invalid, so don't touch it.
2102 ValueAsMetadata::handleDeletion(CI->getValue());
2103 EXPECT_EQ(nullptr, N2->getOperand(0));
2104 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00002105 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00002106
2107 // Clean up Temp for teardown.
2108 Temp->replaceAllUsesWith(nullptr);
2109}
2110
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00002111typedef MetadataTest TrackingMDRefTest;
2112
2113TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2114 Type *Ty = Type::getInt1PtrTy(Context);
2115 std::unique_ptr<GlobalVariable> GV0(
2116 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2117 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2118 EXPECT_TRUE(MD->getValue() == GV0.get());
2119 ASSERT_TRUE(GV0->use_empty());
2120
2121 std::unique_ptr<GlobalVariable> GV1(
2122 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2123 GV0->replaceAllUsesWith(GV1.get());
2124 EXPECT_TRUE(MD->getValue() == GV1.get());
2125
2126 // Reset it, so we don't inadvertently test deletion.
2127 MD.reset();
2128}
2129
2130TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2131 Type *Ty = Type::getInt1PtrTy(Context);
2132 std::unique_ptr<GlobalVariable> GV(
2133 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2134 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2135 EXPECT_TRUE(MD->getValue() == GV.get());
2136 ASSERT_TRUE(GV->use_empty());
2137
2138 GV.reset();
2139 EXPECT_TRUE(!MD);
2140}
2141
Devang Patel0924b332009-07-30 00:03:41 +00002142TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00002143 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002144 ConstantAsMetadata *C =
2145 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2146 ConstantAsMetadata *C2 =
2147 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00002148
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002149 Metadata *const V = C;
2150 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00002151 MDNode *n = MDNode::get(Context, V);
2152 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00002153
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00002154 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00002155 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00002156 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2157 NMD->addOperand(n);
2158 NMD->addOperand(n2);
2159
Chris Lattnerbe354a62009-08-23 04:47:35 +00002160 std::string Str;
2161 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00002162 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00002163 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00002164 oss.str().c_str());
2165}
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002166
2167typedef MetadataTest FunctionAttachmentTest;
2168TEST_F(FunctionAttachmentTest, setMetadata) {
2169 Function *F = getFunction("foo");
2170 ASSERT_FALSE(F->hasMetadata());
2171 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2172 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2173 EXPECT_EQ(nullptr, F->getMetadata("other"));
2174
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002175 DISubprogram *SP1 = getSubprogram();
2176 DISubprogram *SP2 = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002177 ASSERT_NE(SP1, SP2);
2178
2179 F->setMetadata("dbg", SP1);
2180 EXPECT_TRUE(F->hasMetadata());
2181 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2182 EXPECT_EQ(SP1, F->getMetadata("dbg"));
2183 EXPECT_EQ(nullptr, F->getMetadata("other"));
2184
2185 F->setMetadata(LLVMContext::MD_dbg, SP2);
2186 EXPECT_TRUE(F->hasMetadata());
2187 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2188 EXPECT_EQ(SP2, F->getMetadata("dbg"));
2189 EXPECT_EQ(nullptr, F->getMetadata("other"));
2190
2191 F->setMetadata("dbg", nullptr);
2192 EXPECT_FALSE(F->hasMetadata());
2193 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2194 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2195 EXPECT_EQ(nullptr, F->getMetadata("other"));
2196
2197 MDTuple *T1 = getTuple();
2198 MDTuple *T2 = getTuple();
2199 ASSERT_NE(T1, T2);
2200
2201 F->setMetadata("other1", T1);
2202 F->setMetadata("other2", T2);
2203 EXPECT_TRUE(F->hasMetadata());
2204 EXPECT_EQ(T1, F->getMetadata("other1"));
2205 EXPECT_EQ(T2, F->getMetadata("other2"));
2206 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2207
2208 F->setMetadata("other1", T2);
2209 F->setMetadata("other2", T1);
2210 EXPECT_EQ(T2, F->getMetadata("other1"));
2211 EXPECT_EQ(T1, F->getMetadata("other2"));
2212
2213 F->setMetadata("other1", nullptr);
2214 F->setMetadata("other2", nullptr);
2215 EXPECT_FALSE(F->hasMetadata());
2216 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2217 EXPECT_EQ(nullptr, F->getMetadata("other2"));
2218}
2219
2220TEST_F(FunctionAttachmentTest, getAll) {
2221 Function *F = getFunction("foo");
2222
2223 MDTuple *T1 = getTuple();
2224 MDTuple *T2 = getTuple();
2225 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002226 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002227
2228 F->setMetadata("other1", T2);
2229 F->setMetadata(LLVMContext::MD_dbg, SP);
2230 F->setMetadata("other2", T1);
2231 F->setMetadata(LLVMContext::MD_prof, P);
2232 F->setMetadata("other2", T2);
2233 F->setMetadata("other1", T1);
2234
2235 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2236 F->getAllMetadata(MDs);
2237 ASSERT_EQ(4u, MDs.size());
2238 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2239 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2240 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2241 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2242 EXPECT_EQ(SP, MDs[0].second);
2243 EXPECT_EQ(P, MDs[1].second);
2244 EXPECT_EQ(T1, MDs[2].second);
2245 EXPECT_EQ(T2, MDs[3].second);
2246}
2247
2248TEST_F(FunctionAttachmentTest, dropUnknownMetadata) {
2249 Function *F = getFunction("foo");
2250
2251 MDTuple *T1 = getTuple();
2252 MDTuple *T2 = getTuple();
2253 MDTuple *P = getTuple();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002254 DISubprogram *SP = getSubprogram();
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00002255
2256 F->setMetadata("other1", T1);
2257 F->setMetadata(LLVMContext::MD_dbg, SP);
2258 F->setMetadata("other2", T2);
2259 F->setMetadata(LLVMContext::MD_prof, P);
2260
2261 unsigned Known[] = {Context.getMDKindID("other2"), LLVMContext::MD_prof};
2262 F->dropUnknownMetadata(Known);
2263
2264 EXPECT_EQ(T2, F->getMetadata("other2"));
2265 EXPECT_EQ(P, F->getMetadata(LLVMContext::MD_prof));
2266 EXPECT_EQ(nullptr, F->getMetadata("other1"));
2267 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2268
2269 F->setMetadata("other2", nullptr);
2270 F->setMetadata(LLVMContext::MD_prof, nullptr);
2271 EXPECT_FALSE(F->hasMetadata());
2272}
2273
Duncan P. N. Exon Smith327e9bd2015-04-24 21:53:27 +00002274TEST_F(FunctionAttachmentTest, Verifier) {
2275 Function *F = getFunction("foo");
2276 F->setMetadata("attach", getTuple());
2277
2278 // Confirm this has no body.
2279 ASSERT_TRUE(F->empty());
2280
2281 // Functions without a body cannot have metadata attachments (they also can't
2282 // be verified directly, so check that the module fails to verify).
2283 EXPECT_TRUE(verifyModule(*F->getParent()));
2284
2285 // Functions with a body can.
2286 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2287 EXPECT_FALSE(verifyModule(*F->getParent()));
2288 EXPECT_FALSE(verifyFunction(*F));
2289}
2290
Diego Novillo2567f3d2015-05-13 15:13:45 +00002291TEST_F(FunctionAttachmentTest, EntryCount) {
2292 Function *F = getFunction("foo");
2293 EXPECT_FALSE(F->getEntryCount().hasValue());
2294 F->setEntryCount(12304);
2295 EXPECT_TRUE(F->getEntryCount().hasValue());
2296 EXPECT_EQ(12304u, *F->getEntryCount());
2297}
2298
Duncan P. N. Exon Smithb56b5af2015-08-28 21:55:35 +00002299TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2300 Function *F = getFunction("foo");
2301 DISubprogram *SP = getSubprogram();
2302 F->setSubprogram(SP);
2303
2304 // Note that the static_cast confirms that F->getSubprogram() actually
2305 // returns an DISubprogram.
2306 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2307 EXPECT_EQ(SP, F->getMetadata("dbg"));
2308 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2309}
2310
Nick Lewycky49f89192009-04-04 07:22:01 +00002311}