IR: Stop printing 'metadata' in Metadata::print()
[oota-llvm.git] / unittests / IR / MetadataTest.cpp
1 //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
2 //
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
10 #include "llvm/IR/Metadata.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Type.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
18 using namespace llvm;
19
20 namespace {
21
22 class MetadataTest : public testing::Test {
23 protected:
24   LLVMContext Context;
25   MDNode *getNode() { return MDNode::get(Context, None); }
26   MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
27   MDNode *getNode(Metadata *MD1, Metadata *MD2) {
28     Metadata *MDs[] = {MD1, MD2};
29     return MDNode::get(Context, MDs);
30   }
31 };
32 typedef MetadataTest MDStringTest;
33
34 // Test that construction of MDString with different value produces different
35 // MDString objects, even with the same string pointer and nulls in the string.
36 TEST_F(MDStringTest, CreateDifferent) {
37   char x[3] = { 'f', 0, 'A' };
38   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
39   x[2] = 'B';
40   MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
41   EXPECT_NE(s1, s2);
42 }
43
44 // Test that creation of MDStrings with the same string contents produces the
45 // same MDString object, even with different pointers.
46 TEST_F(MDStringTest, CreateSame) {
47   char x[4] = { 'a', 'b', 'c', 'X' };
48   char y[4] = { 'a', 'b', 'c', 'Y' };
49
50   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
51   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
52   EXPECT_EQ(s1, s2);
53 }
54
55 // Test that MDString prints out the string we fed it.
56 TEST_F(MDStringTest, PrintingSimple) {
57   char *str = new char[13];
58   strncpy(str, "testing 1 2 3", 13);
59   MDString *s = MDString::get(Context, StringRef(str, 13));
60   strncpy(str, "aaaaaaaaaaaaa", 13);
61   delete[] str;
62
63   std::string Str;
64   raw_string_ostream oss(Str);
65   s->print(oss);
66   EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
67 }
68
69 // Test printing of MDString with non-printable characters.
70 TEST_F(MDStringTest, PrintingComplex) {
71   char str[5] = {0, '\n', '"', '\\', (char)-1};
72   MDString *s = MDString::get(Context, StringRef(str+0, 5));
73   std::string Str;
74   raw_string_ostream oss(Str);
75   s->print(oss);
76   EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
77 }
78
79 typedef MetadataTest MDNodeTest;
80
81 // Test the two constructors, and containing other Constants.
82 TEST_F(MDNodeTest, Simple) {
83   char x[3] = { 'a', 'b', 'c' };
84   char y[3] = { '1', '2', '3' };
85
86   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
87   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
88   ConstantAsMetadata *CI = ConstantAsMetadata::get(
89       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
90
91   std::vector<Metadata *> V;
92   V.push_back(s1);
93   V.push_back(CI);
94   V.push_back(s2);
95
96   MDNode *n1 = MDNode::get(Context, V);
97   Metadata *const c1 = n1;
98   MDNode *n2 = MDNode::get(Context, c1);
99   Metadata *const c2 = n2;
100   MDNode *n3 = MDNode::get(Context, V);
101   MDNode *n4 = MDNode::getIfExists(Context, V);
102   MDNode *n5 = MDNode::getIfExists(Context, c1);
103   MDNode *n6 = MDNode::getIfExists(Context, c2);
104   EXPECT_NE(n1, n2);
105   EXPECT_EQ(n1, n3);
106   EXPECT_EQ(n4, n1);
107   EXPECT_EQ(n5, n2);
108   EXPECT_EQ(n6, (Metadata *)nullptr);
109
110   EXPECT_EQ(3u, n1->getNumOperands());
111   EXPECT_EQ(s1, n1->getOperand(0));
112   EXPECT_EQ(CI, n1->getOperand(1));
113   EXPECT_EQ(s2, n1->getOperand(2));
114
115   EXPECT_EQ(1u, n2->getNumOperands());
116   EXPECT_EQ(n1, n2->getOperand(0));
117 }
118
119 TEST_F(MDNodeTest, Delete) {
120   Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
121   Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
122
123   Metadata *const V = LocalAsMetadata::get(I);
124   MDNode *n = MDNode::get(Context, V);
125   TrackingMDRef wvh(n);
126
127   EXPECT_EQ(n, wvh);
128
129   delete I;
130 }
131
132 TEST_F(MDNodeTest, SelfReference) {
133   // !0 = metadata !{metadata !0}
134   // !1 = metadata !{metadata !0}
135   {
136     MDNode *Temp = MDNode::getTemporary(Context, None);
137     Metadata *Args[] = {Temp};
138     MDNode *Self = MDNode::get(Context, Args);
139     Self->replaceOperandWith(0, Self);
140     MDNode::deleteTemporary(Temp);
141     ASSERT_EQ(Self, Self->getOperand(0));
142
143     // Self-references should be distinct, so MDNode::get() should grab a
144     // uniqued node that references Self, not Self.
145     Args[0] = Self;
146     MDNode *Ref1 = MDNode::get(Context, Args);
147     MDNode *Ref2 = MDNode::get(Context, Args);
148     EXPECT_NE(Self, Ref1);
149     EXPECT_EQ(Ref1, Ref2);
150   }
151
152   // !0 = metadata !{metadata !0, metadata !{}}
153   // !1 = metadata !{metadata !0, metadata !{}}
154   {
155     MDNode *Temp = MDNode::getTemporary(Context, None);
156     Metadata *Args[] = {Temp, MDNode::get(Context, None)};
157     MDNode *Self = MDNode::get(Context, Args);
158     Self->replaceOperandWith(0, Self);
159     MDNode::deleteTemporary(Temp);
160     ASSERT_EQ(Self, Self->getOperand(0));
161
162     // Self-references should be distinct, so MDNode::get() should grab a
163     // uniqued node that references Self, not Self itself.
164     Args[0] = Self;
165     MDNode *Ref1 = MDNode::get(Context, Args);
166     MDNode *Ref2 = MDNode::get(Context, Args);
167     EXPECT_NE(Self, Ref1);
168     EXPECT_EQ(Ref1, Ref2);
169   }
170 }
171
172 TEST_F(MDNodeTest, Print) {
173   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
174   MDString *S = MDString::get(Context, "foo");
175   MDNode *N0 = getNode();
176   MDNode *N1 = getNode(N0);
177   MDNode *N2 = getNode(N0, N1);
178
179   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
180   MDNode *N = MDNode::get(Context, Args);
181
182   std::string Expected;
183   {
184     raw_string_ostream OS(Expected);
185     OS << "!{";
186     C->printAsOperand(OS);
187     OS << ", ";
188     S->printAsOperand(OS);
189     OS << ", null";
190     MDNode *Nodes[] = {N0, N1, N2};
191     for (auto *Node : Nodes)
192       OS << ", <" << (void *)Node << ">";
193     OS << "}\n";
194   }
195
196   std::string Actual;
197   {
198     raw_string_ostream OS(Actual);
199     N->print(OS);
200   }
201
202   EXPECT_EQ(Expected, Actual);
203 }
204
205 typedef MetadataTest MetadataAsValueTest;
206
207 TEST_F(MetadataAsValueTest, MDNode) {
208   MDNode *N = MDNode::get(Context, None);
209   auto *V = MetadataAsValue::get(Context, N);
210   EXPECT_TRUE(V->getType()->isMetadataTy());
211   EXPECT_EQ(N, V->getMetadata());
212
213   auto *V2 = MetadataAsValue::get(Context, N);
214   EXPECT_EQ(V, V2);
215 }
216
217 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
218   MDNode *N = MDNode::get(Context, None);
219   Metadata *Ops[] = {N};
220   MDNode *N2 = MDNode::get(Context, Ops);
221   auto *V = MetadataAsValue::get(Context, N2);
222   EXPECT_TRUE(V->getType()->isMetadataTy());
223   EXPECT_EQ(N2, V->getMetadata());
224
225   auto *V2 = MetadataAsValue::get(Context, N2);
226   EXPECT_EQ(V, V2);
227
228   auto *V3 = MetadataAsValue::get(Context, N);
229   EXPECT_TRUE(V3->getType()->isMetadataTy());
230   EXPECT_NE(V, V3);
231   EXPECT_EQ(N, V3->getMetadata());
232 }
233
234 TEST_F(MetadataAsValueTest, MDNodeConstant) {
235   auto *C = ConstantInt::getTrue(Context);
236   auto *MD = ConstantAsMetadata::get(C);
237   Metadata *Ops[] = {MD};
238   auto *N = MDNode::get(Context, Ops);
239
240   auto *V = MetadataAsValue::get(Context, MD);
241   EXPECT_TRUE(V->getType()->isMetadataTy());
242   EXPECT_EQ(MD, V->getMetadata());
243
244   auto *V2 = MetadataAsValue::get(Context, N);
245   EXPECT_EQ(MD, V2->getMetadata());
246   EXPECT_EQ(V, V2);
247 }
248
249 typedef MetadataTest ValueAsMetadataTest;
250
251 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
252   Type *Ty = Type::getInt1PtrTy(Context);
253   std::unique_ptr<GlobalVariable> GV0(
254       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
255   auto *MD = ValueAsMetadata::get(GV0.get());
256   EXPECT_TRUE(MD->getValue() == GV0.get());
257   ASSERT_TRUE(GV0->use_empty());
258
259   std::unique_ptr<GlobalVariable> GV1(
260       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
261   GV0->replaceAllUsesWith(GV1.get());
262   EXPECT_TRUE(MD->getValue() == GV1.get());
263 }
264
265 typedef MetadataTest TrackingMDRefTest;
266
267 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
268   Type *Ty = Type::getInt1PtrTy(Context);
269   std::unique_ptr<GlobalVariable> GV0(
270       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
271   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
272   EXPECT_TRUE(MD->getValue() == GV0.get());
273   ASSERT_TRUE(GV0->use_empty());
274
275   std::unique_ptr<GlobalVariable> GV1(
276       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
277   GV0->replaceAllUsesWith(GV1.get());
278   EXPECT_TRUE(MD->getValue() == GV1.get());
279
280   // Reset it, so we don't inadvertently test deletion.
281   MD.reset();
282 }
283
284 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
285   Type *Ty = Type::getInt1PtrTy(Context);
286   std::unique_ptr<GlobalVariable> GV(
287       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
288   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
289   EXPECT_TRUE(MD->getValue() == GV.get());
290   ASSERT_TRUE(GV->use_empty());
291
292   GV.reset();
293   EXPECT_TRUE(!MD);
294 }
295
296 TEST(NamedMDNodeTest, Search) {
297   LLVMContext Context;
298   ConstantAsMetadata *C =
299       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
300   ConstantAsMetadata *C2 =
301       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
302
303   Metadata *const V = C;
304   Metadata *const V2 = C2;
305   MDNode *n = MDNode::get(Context, V);
306   MDNode *n2 = MDNode::get(Context, V2);
307
308   Module M("MyModule", Context);
309   const char *Name = "llvm.NMD1";
310   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
311   NMD->addOperand(n);
312   NMD->addOperand(n2);
313
314   std::string Str;
315   raw_string_ostream oss(Str);
316   NMD->print(oss);
317   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
318                oss.str().c_str());
319 }
320 }