1 //===- llvm/unittest/VMCore/Metadata.cpp - Metadata unit tests ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/Constants.h"
12 #include "llvm/Instructions.h"
13 #include "llvm/Metadata.h"
14 #include "llvm/Module.h"
15 #include "llvm/Type.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/Support/ValueHandle.h"
22 LLVMContext
&Context
= getGlobalContext();
24 // Test that construction of MDString with different value produces different
25 // MDString objects, even with the same string pointer and nulls in the string.
26 TEST(MDStringTest
, CreateDifferent
) {
27 char x
[3] = { 'f', 0, 'A' };
28 MDString
*s1
= MDString::get(Context
, StringRef(&x
[0], 3));
30 MDString
*s2
= MDString::get(Context
, StringRef(&x
[0], 3));
34 // Test that creation of MDStrings with the same string contents produces the
35 // same MDString object, even with different pointers.
36 TEST(MDStringTest
, CreateSame
) {
37 char x
[4] = { 'a', 'b', 'c', 'X' };
38 char y
[4] = { 'a', 'b', 'c', 'Y' };
40 MDString
*s1
= MDString::get(Context
, StringRef(&x
[0], 3));
41 MDString
*s2
= MDString::get(Context
, StringRef(&y
[0], 3));
45 // Test that MDString prints out the string we fed it.
46 TEST(MDStringTest
, PrintingSimple
) {
47 char *str
= new char[13];
48 strncpy(str
, "testing 1 2 3", 13);
49 MDString
*s
= MDString::get(Context
, StringRef(str
, 13));
50 strncpy(str
, "aaaaaaaaaaaaa", 13);
54 raw_string_ostream
oss(Str
);
56 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss
.str().c_str());
59 // Test printing of MDString with non-printable characters.
60 TEST(MDStringTest
, PrintingComplex
) {
61 char str
[5] = {0, '\n', '"', '\\', -1};
62 MDString
*s
= MDString::get(Context
, StringRef(str
+0, 5));
64 raw_string_ostream
oss(Str
);
66 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss
.str().c_str());
69 // Test the two constructors, and containing other Constants.
70 TEST(MDNodeTest
, Simple
) {
71 char x
[3] = { 'a', 'b', 'c' };
72 char y
[3] = { '1', '2', '3' };
74 MDString
*s1
= MDString::get(Context
, StringRef(&x
[0], 3));
75 MDString
*s2
= MDString::get(Context
, StringRef(&y
[0], 3));
76 ConstantInt
*CI
= ConstantInt::get(getGlobalContext(), APInt(8, 0));
78 std::vector
<Value
*> V
;
83 MDNode
*n1
= MDNode::get(Context
, &V
[0], 3);
85 MDNode
*n2
= MDNode::get(Context
, &c1
, 1);
86 MDNode
*n3
= MDNode::get(Context
, &V
[0], 3);
88 #ifdef ENABLE_MDNODE_UNIQUING
94 EXPECT_EQ(3u, n1
->getNumElements());
95 EXPECT_EQ(s1
, n1
->getElement(0));
96 EXPECT_EQ(CI
, n1
->getElement(1));
97 EXPECT_EQ(s2
, n1
->getElement(2));
99 EXPECT_EQ(1u, n2
->getNumElements());
100 EXPECT_EQ(n1
, n2
->getElement(0));
103 raw_string_ostream
oss(Str
);
105 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
109 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
110 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
114 TEST(MDNodeTest
, Delete
) {
115 Constant
*C
= ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
116 Instruction
*I
= new BitCastInst(C
, Type::getInt32Ty(getGlobalContext()));
119 MDNode
*n
= MDNode::get(Context
, &V
, 1);
127 raw_string_ostream
oss(Str
);
129 EXPECT_STREQ("!0 = metadata !{null}\n", oss
.str().c_str());
132 TEST(NamedMDNodeTest
, Search
) {
133 Constant
*C
= ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
134 Constant
*C2
= ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
137 Value
*const V2
= C2
;
138 MDNode
*n
= MDNode::get(Context
, &V
, 1);
139 MDNode
*n2
= MDNode::get(Context
, &V2
, 1);
141 MetadataBase
*Nodes
[2] = { n
, n2
};
143 Module
*M
= new Module("MyModule", getGlobalContext());
144 const char *Name
= "llvm.NMD1";
145 NamedMDNode
*NMD
= NamedMDNode::Create(getGlobalContext(), Name
, &Nodes
[0], 2, M
);
147 raw_string_ostream
oss(Str
);
149 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
150 "!1 = metadata !{i32 2}\n",