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/ValueHandle.h"
23 LLVMContext &Context = getGlobalContext();
25 // Test that construction of MDString with different value produces different
26 // MDString objects, even with the same string pointer and nulls in the string.
27 TEST(MDStringTest, CreateDifferent) {
28 char x[3] = { 'f', 0, 'A' };
29 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
31 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
35 // Test that creation of MDStrings with the same string contents produces the
36 // same MDString object, even with different pointers.
37 TEST(MDStringTest, CreateSame) {
38 char x[4] = { 'a', 'b', 'c', 'X' };
39 char y[4] = { 'a', 'b', 'c', 'Y' };
41 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
42 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
46 // Test that MDString prints out the string we fed it.
47 TEST(MDStringTest, PrintingSimple) {
48 char *str = new char[13];
49 strncpy(str, "testing 1 2 3", 13);
50 MDString *s = MDString::get(Context, StringRef(str, 13));
51 strncpy(str, "aaaaaaaaaaaaa", 13);
54 std::ostringstream oss;
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));
63 std::ostringstream oss;
65 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
68 // Test the two constructors, and containing other Constants.
69 TEST(MDNodeTest, Simple) {
70 char x[3] = { 'a', 'b', 'c' };
71 char y[3] = { '1', '2', '3' };
73 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
74 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
75 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
77 std::vector<Value *> V;
82 MDNode *n1 = MDNode::get(Context, &V[0], 3);
84 MDNode *n2 = MDNode::get(Context, &c1, 1);
85 MDNode *n3 = MDNode::get(Context, &V[0], 3);
89 EXPECT_EQ(3u, n1->getNumElements());
90 EXPECT_EQ(s1, n1->getElement(0));
91 EXPECT_EQ(CI, n1->getElement(1));
92 EXPECT_EQ(s2, n1->getElement(2));
94 EXPECT_EQ(1u, n2->getNumElements());
95 EXPECT_EQ(n1, n2->getElement(0));
97 std::ostringstream oss1, oss2;
100 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
102 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
103 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
107 TEST(MDNodeTest, Delete) {
108 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
109 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
112 MDNode *n = MDNode::get(Context, &V, 1);
119 std::ostringstream oss;
121 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
124 TEST(NamedMDNodeTest, Search) {
125 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
126 Constant *C2 = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
129 Value *const V2 = C2;
130 MDNode *n = MDNode::get(Context, &V, 1);
131 MDNode *n2 = MDNode::get(Context, &V2, 1);
133 MetadataBase *Nodes[2] = { n, n2 };
135 Module *M = new Module("MyModule", getGlobalContext());
136 const char *Name = "llvm.NMD1";
137 NamedMDNode *NMD = NamedMDNode::Create(getGlobalContext(), Name, &Nodes[0], 2, M);
138 std::ostringstream oss;
140 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
141 "!1 = metadata !{i32 2}\n",