blob: e8e6d68c5fa36e049000b9d3626082d46c8b9337 [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 Smithd9901ff2015-02-02 18:53:21 +000012#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/Instructions.h"
14#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Module.h"
17#include "llvm/IR/Type.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000018#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000019#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000020using namespace llvm;
21
22namespace {
23
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000024TEST(ContextAndReplaceableUsesTest, FromContext) {
25 LLVMContext Context;
26 ContextAndReplaceableUses CRU(Context);
27 EXPECT_EQ(&Context, &CRU.getContext());
28 EXPECT_FALSE(CRU.hasReplaceableUses());
29 EXPECT_FALSE(CRU.getReplaceableUses());
30}
31
32TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
33 LLVMContext Context;
34 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
35 EXPECT_EQ(&Context, &CRU.getContext());
36 EXPECT_TRUE(CRU.hasReplaceableUses());
37 EXPECT_TRUE(CRU.getReplaceableUses());
38}
39
40TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
41 LLVMContext Context;
42 ContextAndReplaceableUses CRU(Context);
43 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
44 EXPECT_EQ(&Context, &CRU.getContext());
45 EXPECT_TRUE(CRU.hasReplaceableUses());
46 EXPECT_TRUE(CRU.getReplaceableUses());
47}
48
49TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
50 LLVMContext Context;
51 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
52 auto *Ptr = ReplaceableUses.get();
53 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
54 ReplaceableUses = CRU.takeReplaceableUses();
55 EXPECT_EQ(&Context, &CRU.getContext());
56 EXPECT_FALSE(CRU.hasReplaceableUses());
57 EXPECT_FALSE(CRU.getReplaceableUses());
58 EXPECT_EQ(Ptr, ReplaceableUses.get());
59}
60
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000061class MetadataTest : public testing::Test {
62protected:
63 LLVMContext Context;
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000064 MDNode *getNode() { return MDNode::get(Context, None); }
65 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
66 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
67 Metadata *MDs[] = {MD1, MD2};
68 return MDNode::get(Context, MDs);
69 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000070};
71typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +000072
Nick Lewycky49f89192009-04-04 07:22:01 +000073// Test that construction of MDString with different value produces different
74// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000075TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +000076 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000077 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000078 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000079 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000080 EXPECT_NE(s1, s2);
81}
82
83// Test that creation of MDStrings with the same string contents produces the
84// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000085TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +000086 char x[4] = { 'a', 'b', 'c', 'X' };
87 char y[4] = { 'a', 'b', 'c', 'Y' };
88
Owen Anderson23587322009-07-31 21:38:10 +000089 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
90 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000091 EXPECT_EQ(s1, s2);
92}
93
94// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000095TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000096 char *str = new char[13];
97 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000098 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000099 strncpy(str, "aaaaaaaaaaaaa", 13);
100 delete[] str;
101
Chris Lattnerbe354a62009-08-23 04:47:35 +0000102 std::string Str;
103 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000104 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000105 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000106}
107
108// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000109TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000110 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000111 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000112 std::string Str;
113 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000114 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000115 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000116}
117
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000118typedef MetadataTest MDNodeTest;
119
Nick Lewycky49f89192009-04-04 07:22:01 +0000120// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000121TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000122 char x[3] = { 'a', 'b', 'c' };
123 char y[3] = { '1', '2', '3' };
124
Owen Anderson23587322009-07-31 21:38:10 +0000125 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
126 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000127 ConstantAsMetadata *CI = ConstantAsMetadata::get(
128 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000129
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000130 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000131 V.push_back(s1);
132 V.push_back(CI);
133 V.push_back(s2);
134
Jay Foad5514afe2011-04-21 19:59:31 +0000135 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000136 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000137 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000138 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000139 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000140 MDNode *n4 = MDNode::getIfExists(Context, V);
141 MDNode *n5 = MDNode::getIfExists(Context, c1);
142 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000143 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000144 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000145 EXPECT_EQ(n4, n1);
146 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000147 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000148
Chris Lattner9b493022009-12-31 01:22:29 +0000149 EXPECT_EQ(3u, n1->getNumOperands());
150 EXPECT_EQ(s1, n1->getOperand(0));
151 EXPECT_EQ(CI, n1->getOperand(1));
152 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000153
Chris Lattner9b493022009-12-31 01:22:29 +0000154 EXPECT_EQ(1u, n2->getNumOperands());
155 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000156}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000157
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000158TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000159 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
160 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000161
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000162 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000163 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000164 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000165
166 EXPECT_EQ(n, wvh);
167
168 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000169}
Devang Patel0924b332009-07-30 00:03:41 +0000170
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000171TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000172 // !0 = !{!0}
173 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000174 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000175 auto Temp = MDNode::getTemporary(Context, None);
176 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000177 MDNode *Self = MDNode::get(Context, Args);
178 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000179 ASSERT_EQ(Self, Self->getOperand(0));
180
181 // Self-references should be distinct, so MDNode::get() should grab a
182 // uniqued node that references Self, not Self.
183 Args[0] = Self;
184 MDNode *Ref1 = MDNode::get(Context, Args);
185 MDNode *Ref2 = MDNode::get(Context, Args);
186 EXPECT_NE(Self, Ref1);
187 EXPECT_EQ(Ref1, Ref2);
188 }
189
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000190 // !0 = !{!0, !{}}
191 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000192 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000193 auto Temp = MDNode::getTemporary(Context, None);
194 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000195 MDNode *Self = MDNode::get(Context, Args);
196 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000197 ASSERT_EQ(Self, Self->getOperand(0));
198
199 // Self-references should be distinct, so MDNode::get() should grab a
200 // uniqued node that references Self, not Self itself.
201 Args[0] = Self;
202 MDNode *Ref1 = MDNode::get(Context, Args);
203 MDNode *Ref2 = MDNode::get(Context, Args);
204 EXPECT_NE(Self, Ref1);
205 EXPECT_EQ(Ref1, Ref2);
206 }
207}
208
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000209TEST_F(MDNodeTest, Print) {
210 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
211 MDString *S = MDString::get(Context, "foo");
212 MDNode *N0 = getNode();
213 MDNode *N1 = getNode(N0);
214 MDNode *N2 = getNode(N0, N1);
215
216 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
217 MDNode *N = MDNode::get(Context, Args);
218
219 std::string Expected;
220 {
221 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000222 OS << "!{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000223 C->printAsOperand(OS);
224 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000225 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000226 OS << ", null";
227 MDNode *Nodes[] = {N0, N1, N2};
228 for (auto *Node : Nodes)
229 OS << ", <" << (void *)Node << ">";
230 OS << "}\n";
231 }
232
233 std::string Actual;
234 {
235 raw_string_ostream OS(Actual);
236 N->print(OS);
237 }
238
239 EXPECT_EQ(Expected, Actual);
240}
241
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000242TEST_F(MDNodeTest, NullOperand) {
243 // metadata !{}
244 MDNode *Empty = MDNode::get(Context, None);
245
246 // metadata !{metadata !{}}
247 Metadata *Ops[] = {Empty};
248 MDNode *N = MDNode::get(Context, Ops);
249 ASSERT_EQ(Empty, N->getOperand(0));
250
251 // metadata !{metadata !{}} => metadata !{null}
252 N->replaceOperandWith(0, nullptr);
253 ASSERT_EQ(nullptr, N->getOperand(0));
254
255 // metadata !{null}
256 Ops[0] = nullptr;
257 MDNode *NullOp = MDNode::get(Context, Ops);
258 ASSERT_EQ(nullptr, NullOp->getOperand(0));
259 EXPECT_EQ(N, NullOp);
260}
261
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000262TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
263 // !{}
264 MDNode *Empty = MDNode::get(Context, None);
265 ASSERT_TRUE(Empty->isResolved());
266 EXPECT_FALSE(Empty->isDistinct());
267
268 // !{!{}}
269 Metadata *Wrapped1Ops[] = {Empty};
270 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
271 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
272 ASSERT_TRUE(Wrapped1->isResolved());
273 EXPECT_FALSE(Wrapped1->isDistinct());
274
275 // !{!{!{}}}
276 Metadata *Wrapped2Ops[] = {Wrapped1};
277 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
278 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
279 ASSERT_TRUE(Wrapped2->isResolved());
280 EXPECT_FALSE(Wrapped2->isDistinct());
281
282 // !{!{!{}}} => !{!{}}
283 Wrapped2->replaceOperandWith(0, Empty);
284 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
285 EXPECT_TRUE(Wrapped2->isDistinct());
286 EXPECT_FALSE(Wrapped1->isDistinct());
287}
288
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000289TEST_F(MDNodeTest, getDistinct) {
290 // !{}
291 MDNode *Empty = MDNode::get(Context, None);
292 ASSERT_TRUE(Empty->isResolved());
293 ASSERT_FALSE(Empty->isDistinct());
294 ASSERT_EQ(Empty, MDNode::get(Context, None));
295
296 // distinct !{}
297 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
298 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
299 EXPECT_TRUE(Distinct1->isResolved());
300 EXPECT_TRUE(Distinct2->isDistinct());
301 EXPECT_NE(Empty, Distinct1);
302 EXPECT_NE(Empty, Distinct2);
303 EXPECT_NE(Distinct1, Distinct2);
304
305 // !{}
306 ASSERT_EQ(Empty, MDNode::get(Context, None));
307}
308
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000309TEST_F(MDNodeTest, isUniqued) {
310 MDNode *U = MDTuple::get(Context, None);
311 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000312 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000313 EXPECT_TRUE(U->isUniqued());
314 EXPECT_FALSE(D->isUniqued());
315 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000316}
317
318TEST_F(MDNodeTest, isDistinct) {
319 MDNode *U = MDTuple::get(Context, None);
320 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000321 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000322 EXPECT_FALSE(U->isDistinct());
323 EXPECT_TRUE(D->isDistinct());
324 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000325}
326
327TEST_F(MDNodeTest, isTemporary) {
328 MDNode *U = MDTuple::get(Context, None);
329 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000330 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000331 EXPECT_FALSE(U->isTemporary());
332 EXPECT_FALSE(D->isTemporary());
333 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000334}
335
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000336TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
337 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000338 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000339 ASSERT_FALSE(Temp->isResolved());
340
341 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000342 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000343 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
344 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000345 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000346
347 // temporary !{} => !{}
348 MDNode *Empty = MDNode::get(Context, None);
349 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000350 EXPECT_EQ(Empty, Distinct->getOperand(0));
351}
352
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000353TEST_F(MDNodeTest, handleChangedOperandRecursion) {
354 // !0 = !{}
355 MDNode *N0 = MDNode::get(Context, None);
356
357 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000358 auto Temp3 = MDTuple::getTemporary(Context, None);
359 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000360 MDNode *N1 = MDNode::get(Context, Ops1);
361
362 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000363 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000364 MDNode *N2 = MDNode::get(Context, Ops2);
365
366 // !3 = !{!2}
367 Metadata *Ops3[] = {N2};
368 MDNode *N3 = MDNode::get(Context, Ops3);
369 Temp3->replaceAllUsesWith(N3);
370
371 // !4 = !{!1}
372 Metadata *Ops4[] = {N1};
373 MDNode *N4 = MDNode::get(Context, Ops4);
374
375 // Confirm that the cycle prevented RAUW from getting dropped.
376 EXPECT_TRUE(N0->isResolved());
377 EXPECT_FALSE(N1->isResolved());
378 EXPECT_FALSE(N2->isResolved());
379 EXPECT_FALSE(N3->isResolved());
380 EXPECT_FALSE(N4->isResolved());
381
382 // Create a couple of distinct nodes to observe what's going on.
383 //
384 // !5 = distinct !{!2}
385 // !6 = distinct !{!3}
386 Metadata *Ops5[] = {N2};
387 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
388 Metadata *Ops6[] = {N3};
389 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
390
391 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
392 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
393 // references !3, this can cause a re-entry of handleChangedOperand() when !3
394 // is not ready for it.
395 //
396 // !2->replaceOperandWith(1, nullptr)
397 // !2: !{!3, !0} => !{!3, null}
398 // !2->replaceAllUsesWith(!1)
399 // !3: !{!2] => !{!1}
400 // !3->replaceAllUsesWith(!4)
401 N2->replaceOperandWith(1, nullptr);
402
403 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
404 // under us. Just check that the other nodes are sane.
405 //
406 // !1 = !{!4, null}
407 // !4 = !{!1}
408 // !5 = distinct !{!1}
409 // !6 = distinct !{!4}
410 EXPECT_EQ(N4, N1->getOperand(0));
411 EXPECT_EQ(N1, N4->getOperand(0));
412 EXPECT_EQ(N1, N5->getOperand(0));
413 EXPECT_EQ(N4, N6->getOperand(0));
414}
415
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000416TEST_F(MDNodeTest, replaceResolvedOperand) {
417 // Check code for replacing one resolved operand with another. If doing this
418 // directly (via replaceOperandWith()) becomes illegal, change the operand to
419 // a global value that gets RAUW'ed.
420 //
421 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000422 auto Temp = MDTuple::getTemporary(Context, None);
423 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000424
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000425 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000426 MDNode *N = MDTuple::get(Context, Ops);
427 EXPECT_EQ(nullptr, N->getOperand(0));
428 ASSERT_FALSE(N->isResolved());
429
430 // Check code for replacing resolved nodes.
431 N->replaceOperandWith(0, Empty);
432 EXPECT_EQ(Empty, N->getOperand(0));
433
434 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000435 N->replaceOperandWith(0, Temp.get());
436 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000437
438 // Remove the references to Temp; required for teardown.
439 Temp->replaceAllUsesWith(nullptr);
440}
441
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000442TEST_F(MDNodeTest, replaceWithUniqued) {
443 auto *Empty = MDTuple::get(Context, None);
444 MDTuple *FirstUniqued;
445 {
446 Metadata *Ops[] = {Empty};
447 auto Temp = MDTuple::getTemporary(Context, Ops);
448 EXPECT_TRUE(Temp->isTemporary());
449
450 // Don't expect a collision.
451 auto *Current = Temp.get();
452 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
453 EXPECT_TRUE(FirstUniqued->isUniqued());
454 EXPECT_TRUE(FirstUniqued->isResolved());
455 EXPECT_EQ(Current, FirstUniqued);
456 }
457 {
458 Metadata *Ops[] = {Empty};
459 auto Temp = MDTuple::getTemporary(Context, Ops);
460 EXPECT_TRUE(Temp->isTemporary());
461
462 // Should collide with Uniqued above this time.
463 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
464 EXPECT_TRUE(Uniqued->isUniqued());
465 EXPECT_TRUE(Uniqued->isResolved());
466 EXPECT_EQ(FirstUniqued, Uniqued);
467 }
468 {
469 auto Unresolved = MDTuple::getTemporary(Context, None);
470 Metadata *Ops[] = {Unresolved.get()};
471 auto Temp = MDTuple::getTemporary(Context, Ops);
472 EXPECT_TRUE(Temp->isTemporary());
473
474 // Shouldn't be resolved.
475 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
476 EXPECT_TRUE(Uniqued->isUniqued());
477 EXPECT_FALSE(Uniqued->isResolved());
478
479 // Should be a different node.
480 EXPECT_NE(FirstUniqued, Uniqued);
481
482 // Should resolve when we update its node (note: be careful to avoid a
483 // collision with any other nodes above).
484 Uniqued->replaceOperandWith(0, nullptr);
485 EXPECT_TRUE(Uniqued->isResolved());
486 }
487}
488
489TEST_F(MDNodeTest, replaceWithDistinct) {
490 {
491 auto *Empty = MDTuple::get(Context, None);
492 Metadata *Ops[] = {Empty};
493 auto Temp = MDTuple::getTemporary(Context, Ops);
494 EXPECT_TRUE(Temp->isTemporary());
495
496 // Don't expect a collision.
497 auto *Current = Temp.get();
498 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
499 EXPECT_TRUE(Distinct->isDistinct());
500 EXPECT_TRUE(Distinct->isResolved());
501 EXPECT_EQ(Current, Distinct);
502 }
503 {
504 auto Unresolved = MDTuple::getTemporary(Context, None);
505 Metadata *Ops[] = {Unresolved.get()};
506 auto Temp = MDTuple::getTemporary(Context, Ops);
507 EXPECT_TRUE(Temp->isTemporary());
508
509 // Don't expect a collision.
510 auto *Current = Temp.get();
511 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
512 EXPECT_TRUE(Distinct->isDistinct());
513 EXPECT_TRUE(Distinct->isResolved());
514 EXPECT_EQ(Current, Distinct);
515
516 // Cleanup; required for teardown.
517 Unresolved->replaceAllUsesWith(nullptr);
518 }
519}
520
Duncan P. N. Exon Smith4ee4a982015-02-10 19:13:46 +0000521TEST_F(MDNodeTest, replaceWithPermanent) {
522 Metadata *Ops[] = {nullptr};
523 auto Temp = MDTuple::getTemporary(Context, Ops);
524 auto *T = Temp.get();
525
526 // U is a normal, uniqued node that references T.
527 auto *U = MDTuple::get(Context, T);
528 EXPECT_TRUE(U->isUniqued());
529
530 // Make Temp self-referencing.
531 Temp->replaceOperandWith(0, T);
532
533 // Try to uniquify Temp. This should, despite the name in the API, give a
534 // 'distinct' node, since self-references aren't allowed to be uniqued.
535 //
536 // Since it's distinct, N should have the same address as when it was a
537 // temporary (i.e., be equal to T not U).
538 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
539 EXPECT_EQ(N, T);
540 EXPECT_TRUE(N->isDistinct());
541
542 // U should be the canonical unique node with N as the argument.
543 EXPECT_EQ(U, MDTuple::get(Context, N));
544 EXPECT_TRUE(U->isUniqued());
545
546 // This temporary should collide with U when replaced, but it should still be
547 // uniqued.
548 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
549 EXPECT_TRUE(U->isUniqued());
550
551 // This temporary should become a new uniqued node.
552 auto Temp2 = MDTuple::getTemporary(Context, U);
553 auto *V = Temp2.get();
554 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
555 EXPECT_TRUE(V->isUniqued());
556 EXPECT_EQ(U, V->getOperand(0));
557}
558
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000559TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
560 TrackingMDRef Ref;
561 EXPECT_EQ(nullptr, Ref.get());
562 {
563 auto Temp = MDTuple::getTemporary(Context, None);
564 Ref.reset(Temp.get());
565 EXPECT_EQ(Temp.get(), Ref.get());
566 }
567 EXPECT_EQ(nullptr, Ref.get());
568}
569
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000570typedef MetadataTest MDLocationTest;
571
572TEST_F(MDLocationTest, Overflow) {
573 MDNode *N = MDNode::get(Context, None);
574 {
575 MDLocation *L = MDLocation::get(Context, 2, 7, N);
576 EXPECT_EQ(2u, L->getLine());
577 EXPECT_EQ(7u, L->getColumn());
578 }
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000579 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000580 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000581 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
582 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000583 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000584 }
585 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000586 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
587 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000588 EXPECT_EQ(0u, L->getColumn());
589 }
590 {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +0000591 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
592 EXPECT_EQ(UINT32_MAX, L->getLine());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000593 EXPECT_EQ(0u, L->getColumn());
594 }
595}
596
597TEST_F(MDLocationTest, getDistinct) {
598 MDNode *N = MDNode::get(Context, None);
599 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
600 EXPECT_TRUE(L0->isDistinct());
601 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
602 EXPECT_FALSE(L1->isDistinct());
603 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
604}
605
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000606TEST_F(MDLocationTest, getTemporary) {
607 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000608 auto L = MDLocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000609 EXPECT_TRUE(L->isTemporary());
610 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000611}
612
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000613typedef MetadataTest GenericDebugNodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000614
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000615TEST_F(GenericDebugNodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000616 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000617 auto *Empty = MDNode::get(Context, None);
618 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000619 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000620 EXPECT_EQ(15u, N->getTag());
621 EXPECT_EQ(2u, N->getNumOperands());
622 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000623 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000624 EXPECT_EQ(1u, N->getNumDwarfOperands());
625 EXPECT_EQ(Empty, N->getDwarfOperand(0));
626 EXPECT_EQ(Empty, N->getOperand(1));
627 ASSERT_TRUE(N->isUniqued());
628
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000629 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000630
631 N->replaceOperandWith(1, nullptr);
632 EXPECT_EQ(15u, N->getTag());
633 EXPECT_EQ(Header, N->getHeader());
634 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
635 ASSERT_TRUE(N->isUniqued());
636
637 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000638 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000639
640 N->replaceDwarfOperandWith(0, Empty);
641 EXPECT_EQ(15u, N->getTag());
642 EXPECT_EQ(Header, N->getHeader());
643 EXPECT_EQ(Empty, N->getDwarfOperand(0));
644 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000645 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000646}
647
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000648TEST_F(GenericDebugNodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000649 // Canonicalize !"" to null.
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000650 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
651 EXPECT_EQ(StringRef(), N->getHeader());
652 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000653}
654
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000655typedef MetadataTest MDSubrangeTest;
656
657TEST_F(MDSubrangeTest, get) {
658 auto *N = MDSubrange::get(Context, 5, 7);
659 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
660 EXPECT_EQ(5, N->getCount());
661 EXPECT_EQ(7, N->getLo());
662 EXPECT_EQ(N, MDSubrange::get(Context, 5, 7));
663 EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5));
664}
665
666typedef MetadataTest MDEnumeratorTest;
667
668TEST_F(MDEnumeratorTest, get) {
669 auto *N = MDEnumerator::get(Context, 7, "name");
670 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
671 EXPECT_EQ(7, N->getValue());
672 EXPECT_EQ("name", N->getName());
673 EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name"));
674
675 EXPECT_NE(N, MDEnumerator::get(Context, 8, "name"));
676 EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam"));
677}
678
679typedef MetadataTest MDBasicTypeTest;
680
681TEST_F(MDBasicTypeTest, get) {
682 auto *N =
683 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
684 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
685 EXPECT_EQ("special", N->getName());
686 EXPECT_EQ(33u, N->getSizeInBits());
687 EXPECT_EQ(26u, N->getAlignInBits());
688 EXPECT_EQ(7u, N->getEncoding());
689 EXPECT_EQ(0u, N->getLine());
690 EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
691 26, 7));
692
693 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
694 "special", 33, 26, 7));
695 EXPECT_NE(N,
696 MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
697 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
698 26, 7));
699 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
700 25, 7));
701 EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
702 26, 6));
703}
704
705typedef MetadataTest MDDerivedTypeTest;
706
707TEST_F(MDDerivedTypeTest, get) {
708 Metadata *File = MDTuple::getDistinct(Context, None);
709 Metadata *Scope = MDTuple::getDistinct(Context, None);
710 Metadata *BaseType = MDTuple::getDistinct(Context, None);
711 Metadata *ExtraData = MDTuple::getDistinct(Context, None);
712
713 auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
714 File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
715 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
716 EXPECT_EQ("something", N->getName());
717 EXPECT_EQ(File, N->getFile());
718 EXPECT_EQ(1u, N->getLine());
719 EXPECT_EQ(Scope, N->getScope());
720 EXPECT_EQ(BaseType, N->getBaseType());
721 EXPECT_EQ(2u, N->getSizeInBits());
722 EXPECT_EQ(3u, N->getAlignInBits());
723 EXPECT_EQ(4u, N->getOffsetInBits());
724 EXPECT_EQ(5u, N->getFlags());
725 EXPECT_EQ(ExtraData, N->getExtraData());
726 EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
727 "something", File, 1, Scope, BaseType, 2, 3,
728 4, 5, ExtraData));
729
730 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type,
731 "something", File, 1, Scope, BaseType, 2, 3,
732 4, 5, ExtraData));
733 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
734 File, 1, Scope, BaseType, 2, 3, 4, 5,
735 ExtraData));
736 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
737 "something", Scope, 1, Scope, BaseType, 2, 3,
738 4, 5, ExtraData));
739 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
740 "something", File, 2, Scope, BaseType, 2, 3,
741 4, 5, ExtraData));
742 EXPECT_NE(N,
743 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
744 File, 1, File, BaseType, 2, 3, 4, 5, ExtraData));
745 EXPECT_NE(N,
746 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
747 File, 1, Scope, File, 2, 3, 4, 5, ExtraData));
748 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
749 "something", File, 1, Scope, BaseType, 3, 3,
750 4, 5, ExtraData));
751 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
752 "something", File, 1, Scope, BaseType, 2, 2,
753 4, 5, ExtraData));
754 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
755 "something", File, 1, Scope, BaseType, 2, 3,
756 5, 5, ExtraData));
757 EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
758 "something", File, 1, Scope, BaseType, 2, 3,
759 4, 4, ExtraData));
760 EXPECT_NE(N,
761 MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
762 File, 1, Scope, BaseType, 2, 3, 4, 5, File));
763}
764
765typedef MetadataTest MDCompositeTypeTest;
766
767TEST_F(MDCompositeTypeTest, get) {
768 unsigned Tag = dwarf::DW_TAG_structure_type;
769 StringRef Name = "some name";
770 Metadata *File = MDTuple::getDistinct(Context, None);
771 unsigned Line = 1;
772 Metadata *Scope = MDTuple::getDistinct(Context, None);
773 Metadata *BaseType = MDTuple::getDistinct(Context, None);
774 unsigned SizeInBits = 2;
775 unsigned AlignInBits = 3;
776 unsigned OffsetInBits = 4;
777 unsigned Flags = 5;
778 Metadata *Elements = MDTuple::getDistinct(Context, None);
779 unsigned RuntimeLang = 6;
780 Metadata *VTableHolder = MDTuple::getDistinct(Context, None);
781 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
782 StringRef Identifier = "some id";
783
784 auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
785 BaseType, SizeInBits, AlignInBits,
786 OffsetInBits, Flags, Elements, RuntimeLang,
787 VTableHolder, TemplateParams, Identifier);
788 EXPECT_EQ(Tag, N->getTag());
789 EXPECT_EQ(Name, N->getName());
790 EXPECT_EQ(File, N->getFile());
791 EXPECT_EQ(Line, N->getLine());
792 EXPECT_EQ(Scope, N->getScope());
793 EXPECT_EQ(BaseType, N->getBaseType());
794 EXPECT_EQ(SizeInBits, N->getSizeInBits());
795 EXPECT_EQ(AlignInBits, N->getAlignInBits());
796 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
797 EXPECT_EQ(Flags, N->getFlags());
798 EXPECT_EQ(Elements, N->getElements());
799 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
800 EXPECT_EQ(VTableHolder, N->getVTableHolder());
801 EXPECT_EQ(TemplateParams, N->getTemplateParams());
802 EXPECT_EQ(Identifier, N->getIdentifier());
803
804 EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
805 BaseType, SizeInBits, AlignInBits,
806 OffsetInBits, Flags, Elements, RuntimeLang,
807 VTableHolder, TemplateParams, Identifier));
808
809 EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
810 BaseType, SizeInBits, AlignInBits,
811 OffsetInBits, Flags, Elements, RuntimeLang,
812 VTableHolder, TemplateParams, Identifier));
813 EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope,
814 BaseType, SizeInBits, AlignInBits,
815 OffsetInBits, Flags, Elements, RuntimeLang,
816 VTableHolder, TemplateParams, Identifier));
817 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope,
818 BaseType, SizeInBits, AlignInBits,
819 OffsetInBits, Flags, Elements, RuntimeLang,
820 VTableHolder, TemplateParams, Identifier));
821 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
822 BaseType, SizeInBits, AlignInBits,
823 OffsetInBits, Flags, Elements, RuntimeLang,
824 VTableHolder, TemplateParams, Identifier));
825 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File,
826 BaseType, SizeInBits, AlignInBits,
827 OffsetInBits, Flags, Elements, RuntimeLang,
828 VTableHolder, TemplateParams, Identifier));
829 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
830 SizeInBits, AlignInBits, OffsetInBits,
831 Flags, Elements, RuntimeLang, VTableHolder,
832 TemplateParams, Identifier));
833 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
834 BaseType, SizeInBits + 1, AlignInBits,
835 OffsetInBits, Flags, Elements, RuntimeLang,
836 VTableHolder, TemplateParams, Identifier));
837 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
838 BaseType, SizeInBits, AlignInBits + 1,
839 OffsetInBits, Flags, Elements, RuntimeLang,
840 VTableHolder, TemplateParams, Identifier));
841 EXPECT_NE(N, MDCompositeType::get(
842 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
843 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
844 VTableHolder, TemplateParams, Identifier));
845 EXPECT_NE(N, MDCompositeType::get(
846 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
847 AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
848 VTableHolder, TemplateParams, Identifier));
849 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
850 BaseType, SizeInBits, AlignInBits,
851 OffsetInBits, Flags, File, RuntimeLang,
852 VTableHolder, TemplateParams, Identifier));
853 EXPECT_NE(N, MDCompositeType::get(
854 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
855 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
856 VTableHolder, TemplateParams, Identifier));
857 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
858 BaseType, SizeInBits, AlignInBits,
859 OffsetInBits, Flags, Elements, RuntimeLang,
860 File, TemplateParams, Identifier));
861 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
862 BaseType, SizeInBits, AlignInBits,
863 OffsetInBits, Flags, Elements, RuntimeLang,
864 VTableHolder, File, Identifier));
865 EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
866 BaseType, SizeInBits, AlignInBits,
867 OffsetInBits, Flags, Elements, RuntimeLang,
868 VTableHolder, TemplateParams, "other"));
869
870 // Be sure that missing identifiers get null pointers.
871 EXPECT_FALSE(MDCompositeType::get(
872 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
873 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
874 VTableHolder, TemplateParams, "")->getRawIdentifier());
875 EXPECT_FALSE(MDCompositeType::get(
876 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
877 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
878 VTableHolder, TemplateParams)->getRawIdentifier());
879}
880
881typedef MetadataTest MDSubroutineTypeTest;
882
883TEST_F(MDSubroutineTypeTest, get) {
884 unsigned Flags = 1;
885 Metadata *TypeArray = MDTuple::getDistinct(Context, None);
886
887 auto *N = MDSubroutineType::get(Context, Flags, TypeArray);
888 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
889 EXPECT_EQ(Flags, N->getFlags());
890 EXPECT_EQ(TypeArray, N->getTypeArray());
891 EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray));
892
893 EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray));
894 EXPECT_NE(N, MDSubroutineType::get(Context, Flags,
895 MDTuple::getDistinct(Context, None)));
896}
897
898typedef MetadataTest MDFileTest;
899
900TEST_F(MDFileTest, get) {
901 StringRef Filename = "file";
902 StringRef Directory = "dir";
903 auto *N = MDFile::get(Context, Filename, Directory);
904
905 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
906 EXPECT_EQ(Filename, N->getFilename());
907 EXPECT_EQ(Directory, N->getDirectory());
908 EXPECT_EQ(N, MDFile::get(Context, Filename, Directory));
909
910 EXPECT_NE(N, MDFile::get(Context, "other", Directory));
911 EXPECT_NE(N, MDFile::get(Context, Filename, "other"));
912}
913
914typedef MetadataTest MDCompileUnitTest;
915
916TEST_F(MDCompileUnitTest, get) {
917 unsigned SourceLanguage = 1;
918 Metadata *File = MDTuple::getDistinct(Context, None);
919 StringRef Producer = "some producer";
920 bool IsOptimized = false;
921 StringRef Flags = "flag after flag";
922 unsigned RuntimeVersion = 2;
923 StringRef SplitDebugFilename = "another/file";
924 unsigned EmissionKind = 3;
925 Metadata *EnumTypes = MDTuple::getDistinct(Context, None);
926 Metadata *RetainedTypes = MDTuple::getDistinct(Context, None);
927 Metadata *Subprograms = MDTuple::getDistinct(Context, None);
928 Metadata *GlobalVariables = MDTuple::getDistinct(Context, None);
929 Metadata *ImportedEntities = MDTuple::getDistinct(Context, None);
930 auto *N = MDCompileUnit::get(
931 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
932 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
933 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities);
934
935 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
936 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
937 EXPECT_EQ(File, N->getFile());
938 EXPECT_EQ(Producer, N->getProducer());
939 EXPECT_EQ(IsOptimized, N->isOptimized());
940 EXPECT_EQ(Flags, N->getFlags());
941 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
942 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
943 EXPECT_EQ(EmissionKind, N->getEmissionKind());
944 EXPECT_EQ(EnumTypes, N->getEnumTypes());
945 EXPECT_EQ(RetainedTypes, N->getRetainedTypes());
946 EXPECT_EQ(Subprograms, N->getSubprograms());
947 EXPECT_EQ(GlobalVariables, N->getGlobalVariables());
948 EXPECT_EQ(ImportedEntities, N->getImportedEntities());
949 EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
950 IsOptimized, Flags, RuntimeVersion,
951 SplitDebugFilename, EmissionKind, EnumTypes,
952 RetainedTypes, Subprograms, GlobalVariables,
953 ImportedEntities));
954
955 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer,
956 IsOptimized, Flags, RuntimeVersion,
957 SplitDebugFilename, EmissionKind, EnumTypes,
958 RetainedTypes, Subprograms, GlobalVariables,
959 ImportedEntities));
960 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer,
961 IsOptimized, Flags, RuntimeVersion,
962 SplitDebugFilename, EmissionKind, EnumTypes,
963 RetainedTypes, Subprograms, GlobalVariables,
964 ImportedEntities));
965 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other",
966 IsOptimized, Flags, RuntimeVersion,
967 SplitDebugFilename, EmissionKind, EnumTypes,
968 RetainedTypes, Subprograms, GlobalVariables,
969 ImportedEntities));
970 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
971 !IsOptimized, Flags, RuntimeVersion,
972 SplitDebugFilename, EmissionKind, EnumTypes,
973 RetainedTypes, Subprograms, GlobalVariables,
974 ImportedEntities));
975 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
976 IsOptimized, "other", RuntimeVersion,
977 SplitDebugFilename, EmissionKind, EnumTypes,
978 RetainedTypes, Subprograms, GlobalVariables,
979 ImportedEntities));
980 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
981 IsOptimized, Flags, RuntimeVersion + 1,
982 SplitDebugFilename, EmissionKind, EnumTypes,
983 RetainedTypes, Subprograms, GlobalVariables,
984 ImportedEntities));
985 EXPECT_NE(N,
986 MDCompileUnit::get(Context, SourceLanguage, File, Producer,
987 IsOptimized, Flags, RuntimeVersion, "other",
988 EmissionKind, EnumTypes, RetainedTypes,
989 Subprograms, GlobalVariables, ImportedEntities));
990 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
991 IsOptimized, Flags, RuntimeVersion,
992 SplitDebugFilename, EmissionKind + 1,
993 EnumTypes, RetainedTypes, Subprograms,
994 GlobalVariables, ImportedEntities));
995 EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer,
996 IsOptimized, Flags, RuntimeVersion,
997 SplitDebugFilename, EmissionKind, File,
998 RetainedTypes, Subprograms, GlobalVariables,
999 ImportedEntities));
1000 EXPECT_NE(N, MDCompileUnit::get(
1001 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1002 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1003 File, Subprograms, GlobalVariables, ImportedEntities));
1004 EXPECT_NE(N, MDCompileUnit::get(
1005 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1006 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1007 RetainedTypes, File, GlobalVariables, ImportedEntities));
1008 EXPECT_NE(N, MDCompileUnit::get(
1009 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1010 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1011 RetainedTypes, Subprograms, File, ImportedEntities));
1012 EXPECT_NE(N, MDCompileUnit::get(
1013 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1014 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1015 RetainedTypes, Subprograms, GlobalVariables, File));
1016}
1017
1018typedef MetadataTest MDSubprogramTest;
1019
1020TEST_F(MDSubprogramTest, get) {
1021 Metadata *Scope = MDTuple::getDistinct(Context, None);
1022 StringRef Name = "name";
1023 StringRef LinkageName = "linkage";
1024 Metadata *File = MDTuple::getDistinct(Context, None);
1025 unsigned Line = 2;
1026 Metadata *Type = MDTuple::getDistinct(Context, None);
1027 bool IsLocalToUnit = false;
1028 bool IsDefinition = true;
1029 unsigned ScopeLine = 3;
1030 Metadata *ContainingType = MDTuple::getDistinct(Context, None);
1031 unsigned Virtuality = 4;
1032 unsigned VirtualIndex = 5;
1033 unsigned Flags = 6;
1034 bool IsOptimized = false;
1035 Metadata *Function = MDTuple::getDistinct(Context, None);
1036 Metadata *TemplateParams = MDTuple::getDistinct(Context, None);
1037 Metadata *Declaration = MDTuple::getDistinct(Context, None);
1038 Metadata *Variables = MDTuple::getDistinct(Context, None);
1039
1040 auto *N = MDSubprogram::get(
1041 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
1042 IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags,
1043 IsOptimized, Function, TemplateParams, Declaration, Variables);
1044
1045 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1046 EXPECT_EQ(Scope, N->getScope());
1047 EXPECT_EQ(Name, N->getName());
1048 EXPECT_EQ(LinkageName, N->getLinkageName());
1049 EXPECT_EQ(File, N->getFile());
1050 EXPECT_EQ(Line, N->getLine());
1051 EXPECT_EQ(Type, N->getType());
1052 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1053 EXPECT_EQ(IsDefinition, N->isDefinition());
1054 EXPECT_EQ(ScopeLine, N->getScopeLine());
1055 EXPECT_EQ(ContainingType, N->getContainingType());
1056 EXPECT_EQ(Virtuality, N->getVirtuality());
1057 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1058 EXPECT_EQ(Flags, N->getFlags());
1059 EXPECT_EQ(IsOptimized, N->isOptimized());
1060 EXPECT_EQ(Function, N->getFunction());
1061 EXPECT_EQ(TemplateParams, N->getTemplateParams());
1062 EXPECT_EQ(Declaration, N->getDeclaration());
1063 EXPECT_EQ(Variables, N->getVariables());
1064 EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1065 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1066 ContainingType, Virtuality, VirtualIndex,
1067 Flags, IsOptimized, Function, TemplateParams,
1068 Declaration, Variables));
1069
1070 EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line,
1071 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1072 ContainingType, Virtuality, VirtualIndex,
1073 Flags, IsOptimized, Function, TemplateParams,
1074 Declaration, Variables));
1075 EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File,
1076 Line, Type, IsLocalToUnit, IsDefinition,
1077 ScopeLine, ContainingType, Virtuality,
1078 VirtualIndex, Flags, IsOptimized, Function,
1079 TemplateParams, Declaration, Variables));
1080 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line,
1081 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1082 ContainingType, Virtuality, VirtualIndex,
1083 Flags, IsOptimized, Function, TemplateParams,
1084 Declaration, Variables));
1085 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line,
1086 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1087 ContainingType, Virtuality, VirtualIndex,
1088 Flags, IsOptimized, Function, TemplateParams,
1089 Declaration, Variables));
1090 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File,
1091 Line + 1, Type, IsLocalToUnit, IsDefinition,
1092 ScopeLine, ContainingType, Virtuality,
1093 VirtualIndex, Flags, IsOptimized, Function,
1094 TemplateParams, Declaration, Variables));
1095 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1096 Scope, IsLocalToUnit, IsDefinition, ScopeLine,
1097 ContainingType, Virtuality, VirtualIndex,
1098 Flags, IsOptimized, Function, TemplateParams,
1099 Declaration, Variables));
1100 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1101 Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1102 ContainingType, Virtuality, VirtualIndex,
1103 Flags, IsOptimized, Function, TemplateParams,
1104 Declaration, Variables));
1105 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1106 Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1107 ContainingType, Virtuality, VirtualIndex,
1108 Flags, IsOptimized, Function, TemplateParams,
1109 Declaration, Variables));
1110 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1111 Type, IsLocalToUnit, IsDefinition,
1112 ScopeLine + 1, ContainingType, Virtuality,
1113 VirtualIndex, Flags, IsOptimized, Function,
1114 TemplateParams, Declaration, Variables));
1115 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1116 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1117 Type, Virtuality, VirtualIndex, Flags,
1118 IsOptimized, Function, TemplateParams,
1119 Declaration, Variables));
1120 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1121 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1122 ContainingType, Virtuality + 1, VirtualIndex,
1123 Flags, IsOptimized, Function, TemplateParams,
1124 Declaration, Variables));
1125 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1126 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1127 ContainingType, Virtuality, VirtualIndex + 1,
1128 Flags, IsOptimized, Function, TemplateParams,
1129 Declaration, Variables));
1130 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1131 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1132 ContainingType, Virtuality, VirtualIndex,
1133 ~Flags, IsOptimized, Function, TemplateParams,
1134 Declaration, Variables));
1135 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1136 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1137 ContainingType, Virtuality, VirtualIndex,
1138 Flags, !IsOptimized, Function, TemplateParams,
1139 Declaration, Variables));
1140 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1141 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1142 ContainingType, Virtuality, VirtualIndex,
1143 Flags, IsOptimized, Type, TemplateParams,
1144 Declaration, Variables));
1145 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1146 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1147 ContainingType, Virtuality, VirtualIndex,
1148 Flags, IsOptimized, Function, Type,
1149 Declaration, Variables));
1150 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1151 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1152 ContainingType, Virtuality, VirtualIndex,
1153 Flags, IsOptimized, Function, TemplateParams,
1154 Type, Variables));
1155 EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1156 Type, IsLocalToUnit, IsDefinition, ScopeLine,
1157 ContainingType, Virtuality, VirtualIndex,
1158 Flags, IsOptimized, Function, TemplateParams,
1159 Declaration, Type));
1160}
1161
1162typedef MetadataTest MDLexicalBlockTest;
1163
1164TEST_F(MDLexicalBlockTest, get) {
1165 Metadata *Scope = MDTuple::getDistinct(Context, None);
1166 Metadata *File = MDTuple::getDistinct(Context, None);
1167 unsigned Line = 5;
1168 unsigned Column = 8;
1169
1170 auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column);
1171
1172 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1173 EXPECT_EQ(Scope, N->getScope());
1174 EXPECT_EQ(File, N->getFile());
1175 EXPECT_EQ(Line, N->getLine());
1176 EXPECT_EQ(Column, N->getColumn());
1177 EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column));
1178
1179 EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column));
1180 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column));
1181 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column));
1182 EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1));
1183}
1184
1185typedef MetadataTest MDLexicalBlockFileTest;
1186
1187TEST_F(MDLexicalBlockFileTest, get) {
1188 Metadata *Scope = MDTuple::getDistinct(Context, None);
1189 Metadata *File = MDTuple::getDistinct(Context, None);
1190 unsigned Discriminator = 5;
1191
1192 auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator);
1193
1194 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1195 EXPECT_EQ(Scope, N->getScope());
1196 EXPECT_EQ(File, N->getFile());
1197 EXPECT_EQ(Discriminator, N->getDiscriminator());
1198 EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator));
1199
1200 EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator));
1201 EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator));
1202 EXPECT_NE(N,
1203 MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1204}
1205
1206typedef MetadataTest MDNamespaceTest;
1207
1208TEST_F(MDNamespaceTest, get) {
1209 Metadata *Scope = MDTuple::getDistinct(Context, None);
1210 Metadata *File = MDTuple::getDistinct(Context, None);
1211 StringRef Name = "namespace";
1212 unsigned Line = 5;
1213
1214 auto *N = MDNamespace::get(Context, Scope, File, Name, Line);
1215
1216 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1217 EXPECT_EQ(Scope, N->getScope());
1218 EXPECT_EQ(File, N->getFile());
1219 EXPECT_EQ(Name, N->getName());
1220 EXPECT_EQ(Line, N->getLine());
1221 EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line));
1222
1223 EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line));
1224 EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line));
1225 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line));
1226 EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1));
1227}
1228
1229typedef MetadataTest MDTemplateTypeParameterTest;
1230
1231TEST_F(MDTemplateTypeParameterTest, get) {
1232 Metadata *Scope = MDTuple::getDistinct(Context, None);
1233 StringRef Name = "template";
1234 Metadata *Type = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001235
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001236 auto *N = MDTemplateTypeParameter::get(Context, Scope, Name, Type);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001237
1238 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1239 EXPECT_EQ(Scope, N->getScope());
1240 EXPECT_EQ(Name, N->getName());
1241 EXPECT_EQ(Type, N->getType());
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001242 EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Scope, Name, Type));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001243
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001244 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Type, Name, Type));
1245 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, "other", Type));
1246 EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, Name, Scope));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001247}
1248
1249typedef MetadataTest MDTemplateValueParameterTest;
1250
1251TEST_F(MDTemplateValueParameterTest, get) {
1252 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1253 Metadata *Scope = MDTuple::getDistinct(Context, None);
1254 StringRef Name = "template";
1255 Metadata *Type = MDTuple::getDistinct(Context, None);
1256 Metadata *Value = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001257
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001258 auto *N =
1259 MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001260 EXPECT_EQ(Tag, N->getTag());
1261 EXPECT_EQ(Scope, N->getScope());
1262 EXPECT_EQ(Name, N->getName());
1263 EXPECT_EQ(Type, N->getType());
1264 EXPECT_EQ(Value, N->getValue());
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001265 EXPECT_EQ(
1266 N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001267
1268 EXPECT_NE(N, MDTemplateValueParameter::get(
1269 Context, dwarf::DW_TAG_GNU_template_template_param, Scope,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001270 Name, Type, Value));
1271 EXPECT_NE(
1272 N, MDTemplateValueParameter::get(Context, Tag, Type, Name, Type, Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001273 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, "other", Type,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001274 Value));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001275 EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Scope,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +00001276 Value));
1277 EXPECT_NE(
1278 N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Scope));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001279}
1280
1281typedef MetadataTest MDGlobalVariableTest;
1282
1283TEST_F(MDGlobalVariableTest, get) {
1284 Metadata *Scope = MDTuple::getDistinct(Context, None);
1285 StringRef Name = "name";
1286 StringRef LinkageName = "linkage";
1287 Metadata *File = MDTuple::getDistinct(Context, None);
1288 unsigned Line = 5;
1289 Metadata *Type = MDTuple::getDistinct(Context, None);
1290 bool IsLocalToUnit = false;
1291 bool IsDefinition = true;
1292 Metadata *Variable = MDTuple::getDistinct(Context, None);
1293 Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None);
1294
1295 auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1296 Type, IsLocalToUnit, IsDefinition, Variable,
1297 StaticDataMemberDeclaration);
1298 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1299 EXPECT_EQ(Scope, N->getScope());
1300 EXPECT_EQ(Name, N->getName());
1301 EXPECT_EQ(LinkageName, N->getLinkageName());
1302 EXPECT_EQ(File, N->getFile());
1303 EXPECT_EQ(Line, N->getLine());
1304 EXPECT_EQ(Type, N->getType());
1305 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1306 EXPECT_EQ(IsDefinition, N->isDefinition());
1307 EXPECT_EQ(Variable, N->getVariable());
1308 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1309 EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1310 Line, Type, IsLocalToUnit, IsDefinition,
1311 Variable, StaticDataMemberDeclaration));
1312
1313 EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File,
1314 Line, Type, IsLocalToUnit, IsDefinition,
1315 Variable, StaticDataMemberDeclaration));
1316 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1317 Line, Type, IsLocalToUnit, IsDefinition,
1318 Variable, StaticDataMemberDeclaration));
1319 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1320 Type, IsLocalToUnit, IsDefinition,
1321 Variable, StaticDataMemberDeclaration));
1322 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope,
1323 Line, Type, IsLocalToUnit, IsDefinition,
1324 Variable, StaticDataMemberDeclaration));
1325 EXPECT_NE(N,
1326 MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1327 Line + 1, Type, IsLocalToUnit, IsDefinition,
1328 Variable, StaticDataMemberDeclaration));
1329 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1330 Line, Scope, IsLocalToUnit, IsDefinition,
1331 Variable, StaticDataMemberDeclaration));
1332 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1333 Line, Type, !IsLocalToUnit, IsDefinition,
1334 Variable, StaticDataMemberDeclaration));
1335 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1336 Line, Type, IsLocalToUnit, !IsDefinition,
1337 Variable, StaticDataMemberDeclaration));
1338 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1339 Line, Type, IsLocalToUnit, IsDefinition,
1340 Type, StaticDataMemberDeclaration));
1341 EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1342 Line, Type, IsLocalToUnit, IsDefinition,
1343 Variable, Type));
1344}
1345
1346typedef MetadataTest MDLocalVariableTest;
1347
1348TEST_F(MDLocalVariableTest, get) {
1349 unsigned Tag = dwarf::DW_TAG_arg_variable;
1350 Metadata *Scope = MDTuple::getDistinct(Context, None);
1351 StringRef Name = "name";
1352 Metadata *File = MDTuple::getDistinct(Context, None);
1353 unsigned Line = 5;
1354 Metadata *Type = MDTuple::getDistinct(Context, None);
1355 unsigned Arg = 6;
1356 unsigned Flags = 7;
1357 Metadata *InlinedAt = MDTuple::getDistinct(Context, None);
1358
1359 auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1360 Arg, Flags, InlinedAt);
1361 EXPECT_EQ(Tag, N->getTag());
1362 EXPECT_EQ(Scope, N->getScope());
1363 EXPECT_EQ(Name, N->getName());
1364 EXPECT_EQ(File, N->getFile());
1365 EXPECT_EQ(Line, N->getLine());
1366 EXPECT_EQ(Type, N->getType());
1367 EXPECT_EQ(Arg, N->getArg());
1368 EXPECT_EQ(Flags, N->getFlags());
1369 EXPECT_EQ(InlinedAt, N->getInlinedAt());
1370 EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1371 Arg, Flags, InlinedAt));
1372
1373 EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope,
1374 Name, File, Line, Type, Arg, Flags,
1375 InlinedAt));
1376 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line,
1377 Type, Arg, Flags, InlinedAt));
1378 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line,
1379 Type, Arg, Flags, InlinedAt));
1380 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line,
1381 Type, Arg, Flags, InlinedAt));
1382 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
1383 Type, Arg, Flags, InlinedAt));
1384 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
1385 Scope, Arg, Flags, InlinedAt));
1386 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1387 Arg + 1, Flags, InlinedAt));
1388 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1389 Arg, ~Flags, InlinedAt));
1390 EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
1391 Arg, Flags, Scope));
1392}
1393
1394typedef MetadataTest MDExpressionTest;
1395
1396TEST_F(MDExpressionTest, get) {
1397 uint64_t Elements[] = {2, 6, 9, 78, 0};
1398 auto *N = MDExpression::get(Context, Elements);
1399 EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1400 EXPECT_EQ(N, MDExpression::get(Context, Elements));
Duncan P. N. Exon Smithdddc5372015-02-10 01:36:46 +00001401
1402 EXPECT_EQ(5u, N->getNumElements());
1403 EXPECT_EQ(2u, N->getElement(0));
1404 EXPECT_EQ(6u, N->getElement(1));
1405 EXPECT_EQ(9u, N->getElement(2));
1406 EXPECT_EQ(78u, N->getElement(3));
1407 EXPECT_EQ(0u, N->getElement(4));
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001408}
1409
1410typedef MetadataTest MDObjCPropertyTest;
1411
1412TEST_F(MDObjCPropertyTest, get) {
1413 StringRef Name = "name";
1414 Metadata *File = MDTuple::getDistinct(Context, None);
1415 unsigned Line = 5;
1416 StringRef GetterName = "getter";
1417 StringRef SetterName = "setter";
1418 unsigned Attributes = 7;
1419 Metadata *Type = MDTuple::getDistinct(Context, None);
1420
1421 auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
1422 SetterName, Attributes, Type);
1423
1424 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
1425 EXPECT_EQ(Name, N->getName());
1426 EXPECT_EQ(File, N->getFile());
1427 EXPECT_EQ(Line, N->getLine());
1428 EXPECT_EQ(GetterName, N->getGetterName());
1429 EXPECT_EQ(SetterName, N->getSetterName());
1430 EXPECT_EQ(Attributes, N->getAttributes());
1431 EXPECT_EQ(Type, N->getType());
1432 EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1433 SetterName, Attributes, Type));
1434
1435 EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName,
1436 SetterName, Attributes, Type));
1437 EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName,
1438 SetterName, Attributes, Type));
1439 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName,
1440 SetterName, Attributes, Type));
1441 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other",
1442 SetterName, Attributes, Type));
1443 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1444 "other", Attributes, Type));
1445 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1446 SetterName, Attributes + 1, Type));
1447 EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
1448 SetterName, Attributes, File));
1449}
1450
1451typedef MetadataTest MDImportedEntityTest;
1452
1453TEST_F(MDImportedEntityTest, get) {
1454 unsigned Tag = dwarf::DW_TAG_imported_module;
1455 Metadata *Scope = MDTuple::getDistinct(Context, None);
1456 Metadata *Entity = MDTuple::getDistinct(Context, None);
1457 unsigned Line = 5;
1458 StringRef Name = "name";
1459
1460 auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
1461
1462 EXPECT_EQ(Tag, N->getTag());
1463 EXPECT_EQ(Scope, N->getScope());
1464 EXPECT_EQ(Entity, N->getEntity());
1465 EXPECT_EQ(Line, N->getLine());
1466 EXPECT_EQ(Name, N->getName());
1467 EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
1468
1469 EXPECT_NE(N,
1470 MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
1471 Scope, Entity, Line, Name));
1472 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name));
1473 EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name));
1474 EXPECT_NE(N,
1475 MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
1476 EXPECT_NE(N,
1477 MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
1478}
1479
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001480typedef MetadataTest MetadataAsValueTest;
1481
1482TEST_F(MetadataAsValueTest, MDNode) {
1483 MDNode *N = MDNode::get(Context, None);
1484 auto *V = MetadataAsValue::get(Context, N);
1485 EXPECT_TRUE(V->getType()->isMetadataTy());
1486 EXPECT_EQ(N, V->getMetadata());
1487
1488 auto *V2 = MetadataAsValue::get(Context, N);
1489 EXPECT_EQ(V, V2);
1490}
1491
1492TEST_F(MetadataAsValueTest, MDNodeMDNode) {
1493 MDNode *N = MDNode::get(Context, None);
1494 Metadata *Ops[] = {N};
1495 MDNode *N2 = MDNode::get(Context, Ops);
1496 auto *V = MetadataAsValue::get(Context, N2);
1497 EXPECT_TRUE(V->getType()->isMetadataTy());
1498 EXPECT_EQ(N2, V->getMetadata());
1499
1500 auto *V2 = MetadataAsValue::get(Context, N2);
1501 EXPECT_EQ(V, V2);
1502
1503 auto *V3 = MetadataAsValue::get(Context, N);
1504 EXPECT_TRUE(V3->getType()->isMetadataTy());
1505 EXPECT_NE(V, V3);
1506 EXPECT_EQ(N, V3->getMetadata());
1507}
1508
1509TEST_F(MetadataAsValueTest, MDNodeConstant) {
1510 auto *C = ConstantInt::getTrue(Context);
1511 auto *MD = ConstantAsMetadata::get(C);
1512 Metadata *Ops[] = {MD};
1513 auto *N = MDNode::get(Context, Ops);
1514
1515 auto *V = MetadataAsValue::get(Context, MD);
1516 EXPECT_TRUE(V->getType()->isMetadataTy());
1517 EXPECT_EQ(MD, V->getMetadata());
1518
1519 auto *V2 = MetadataAsValue::get(Context, N);
1520 EXPECT_EQ(MD, V2->getMetadata());
1521 EXPECT_EQ(V, V2);
1522}
1523
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001524typedef MetadataTest ValueAsMetadataTest;
1525
1526TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
1527 Type *Ty = Type::getInt1PtrTy(Context);
1528 std::unique_ptr<GlobalVariable> GV0(
1529 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1530 auto *MD = ValueAsMetadata::get(GV0.get());
1531 EXPECT_TRUE(MD->getValue() == GV0.get());
1532 ASSERT_TRUE(GV0->use_empty());
1533
1534 std::unique_ptr<GlobalVariable> GV1(
1535 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1536 GV0->replaceAllUsesWith(GV1.get());
1537 EXPECT_TRUE(MD->getValue() == GV1.get());
1538}
1539
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001540TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
1541 // Create a constant.
1542 ConstantAsMetadata *CI = ConstantAsMetadata::get(
1543 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
1544
1545 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001546 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001547
1548 // 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 +00001549 Metadata *Ops1[] = {CI, CI, Temp.get()};
1550 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001551
1552 auto *N1 = MDTuple::get(Context, Ops1);
1553 auto *N2 = MDTuple::get(Context, Ops2);
1554 ASSERT_NE(N1, N2);
1555
1556 // Tell metadata that the constant is getting deleted.
1557 //
1558 // After this, N1 will be invalid, so don't touch it.
1559 ValueAsMetadata::handleDeletion(CI->getValue());
1560 EXPECT_EQ(nullptr, N2->getOperand(0));
1561 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +00001562 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +00001563
1564 // Clean up Temp for teardown.
1565 Temp->replaceAllUsesWith(nullptr);
1566}
1567
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +00001568typedef MetadataTest TrackingMDRefTest;
1569
1570TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
1571 Type *Ty = Type::getInt1PtrTy(Context);
1572 std::unique_ptr<GlobalVariable> GV0(
1573 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1574 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
1575 EXPECT_TRUE(MD->getValue() == GV0.get());
1576 ASSERT_TRUE(GV0->use_empty());
1577
1578 std::unique_ptr<GlobalVariable> GV1(
1579 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1580 GV0->replaceAllUsesWith(GV1.get());
1581 EXPECT_TRUE(MD->getValue() == GV1.get());
1582
1583 // Reset it, so we don't inadvertently test deletion.
1584 MD.reset();
1585}
1586
1587TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
1588 Type *Ty = Type::getInt1PtrTy(Context);
1589 std::unique_ptr<GlobalVariable> GV(
1590 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
1591 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
1592 EXPECT_TRUE(MD->getValue() == GV.get());
1593 ASSERT_TRUE(GV->use_empty());
1594
1595 GV.reset();
1596 EXPECT_TRUE(!MD);
1597}
1598
Devang Patel0924b332009-07-30 00:03:41 +00001599TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +00001600 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001601 ConstantAsMetadata *C =
1602 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
1603 ConstantAsMetadata *C2 =
1604 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +00001605
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001606 Metadata *const V = C;
1607 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +00001608 MDNode *n = MDNode::get(Context, V);
1609 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +00001610
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +00001611 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +00001612 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +00001613 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
1614 NMD->addOperand(n);
1615 NMD->addOperand(n2);
1616
Chris Lattnerbe354a62009-08-23 04:47:35 +00001617 std::string Str;
1618 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +00001619 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +00001620 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +00001621 oss.str().c_str());
1622}
Nick Lewycky49f89192009-04-04 07:22:01 +00001623}