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 // Test that construction of MDString with different value produces different
24 // MDString objects, even with the same string pointer and nulls in the string.
25 TEST(MDStringTest, CreateDifferent) {
26 char x[3] = { 'f', 0, 'A' };
27 MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3));
29 MDString *s2 = getGlobalContext().getMDString(StringRef(&x[0], 3));
33 // Test that creation of MDStrings with the same string contents produces the
34 // same MDString object, even with different pointers.
35 TEST(MDStringTest, CreateSame) {
36 char x[4] = { 'a', 'b', 'c', 'X' };
37 char y[4] = { 'a', 'b', 'c', 'Y' };
39 MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3));
40 MDString *s2 = getGlobalContext().getMDString(StringRef(&y[0], 3));
44 // Test that MDString prints out the string we fed it.
45 TEST(MDStringTest, PrintingSimple) {
46 char *str = new char[13];
47 strncpy(str, "testing 1 2 3", 13);
48 MDString *s = getGlobalContext().getMDString(StringRef(str, 13));
49 strncpy(str, "aaaaaaaaaaaaa", 13);
52 std::ostringstream oss;
54 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
57 // Test printing of MDString with non-printable characters.
58 TEST(MDStringTest, PrintingComplex) {
59 char str[5] = {0, '\n', '"', '\\', -1};
60 MDString *s = getGlobalContext().getMDString(StringRef(str+0, 5));
61 std::ostringstream oss;
63 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
66 // Test the two constructors, and containing other Constants.
67 TEST(MDNodeTest, Simple) {
68 char x[3] = { 'a', 'b', 'c' };
69 char y[3] = { '1', '2', '3' };
71 MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3));
72 MDString *s2 = getGlobalContext().getMDString(StringRef(&y[0], 3));
73 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
75 std::vector<Value *> V;
80 MDNode *n1 = getGlobalContext().getMDNode(&V[0], 3);
82 MDNode *n2 = getGlobalContext().getMDNode(&c1, 1);
83 MDNode *n3 = getGlobalContext().getMDNode(&V[0], 3);
87 EXPECT_EQ(3u, n1->getNumElements());
88 EXPECT_EQ(s1, n1->getElement(0));
89 EXPECT_EQ(CI, n1->getElement(1));
90 EXPECT_EQ(s2, n1->getElement(2));
92 EXPECT_EQ(1u, n2->getNumElements());
93 EXPECT_EQ(n1, n2->getElement(0));
95 std::ostringstream oss1, oss2;
98 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
100 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
101 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
105 TEST(MDNodeTest, Delete) {
106 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
107 Instruction *I = new BitCastInst(C, Type::Int32Ty);
110 MDNode *n = getGlobalContext().getMDNode(&V, 1);
117 std::ostringstream oss;
119 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
122 TEST(NamedMDNodeTest, Search) {
123 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
124 Constant *C2 = ConstantInt::get(Type::Int32Ty, 2);
127 Value *const V2 = C2;
128 MDNode *n = getGlobalContext().getMDNode(&V, 1);
129 MDNode *n2 = getGlobalContext().getMDNode(&V2, 1);
131 MetadataBase *Nodes[2] = { n, n2 };
133 Module *M = new Module("MyModule", getGlobalContext());
134 const char *Name = "llvm.NMD1";
135 NamedMDNode *NMD = NamedMDNode::Create(Name, &Nodes[0], 2, M);
136 std::ostringstream oss;
138 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
139 "!1 = metadata !{i32 2}\n",