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/LLVMContext.h"
14 #include "llvm/Metadata.h"
15 #include "llvm/Module.h"
16 #include "llvm/Type.h"
17 #include "llvm/Support/raw_ostream.h"
18 #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);
55 raw_string_ostream oss(Str);
57 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
60 // Test printing of MDString with non-printable characters.
61 TEST(MDStringTest, PrintingComplex) {
62 char str[5] = {0, '\n', '"', '\\', -1};
63 MDString *s = MDString::get(Context, StringRef(str+0, 5));
65 raw_string_ostream oss(Str);
67 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
70 // Test the two constructors, and containing other Constants.
71 TEST(MDNodeTest, Simple) {
72 char x[3] = { 'a', 'b', 'c' };
73 char y[3] = { '1', '2', '3' };
75 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
76 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
77 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
79 std::vector<Value *> V;
84 MDNode *n1 = MDNode::get(Context, &V[0], 3);
86 MDNode *n2 = MDNode::get(Context, &c1, 1);
87 MDNode *n3 = MDNode::get(Context, &V[0], 3);
89 #ifdef ENABLE_MDNODE_UNIQUING
95 EXPECT_EQ(3u, n1->getNumOperands());
96 EXPECT_EQ(s1, n1->getOperand(0));
97 EXPECT_EQ(CI, n1->getOperand(1));
98 EXPECT_EQ(s2, n1->getOperand(2));
100 EXPECT_EQ(1u, n2->getNumOperands());
101 EXPECT_EQ(n1, n2->getOperand(0));
104 TEST(MDNodeTest, Delete) {
105 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
106 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
109 MDNode *n = MDNode::get(Context, &V, 1);
117 TEST(NamedMDNodeTest, Search) {
118 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
119 Constant *C2 = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
122 Value *const V2 = C2;
123 MDNode *n = MDNode::get(Context, &V, 1);
124 MDNode *n2 = MDNode::get(Context, &V2, 1);
126 MDNode *Nodes[2] = { n, n2 };
128 Module *M = new Module("MyModule", getGlobalContext());
129 const char *Name = "llvm.NMD1";
130 NamedMDNode *NMD = NamedMDNode::Create(getGlobalContext(), Name, &Nodes[0], 2, M);
132 raw_string_ostream oss(Str);
134 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",