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
class MetadataTest : public testing::Test {
27
typedef MetadataTest MDStringTest;
29
// Test that construction of MDString with different value produces different
30
// MDString objects, even with the same string pointer and nulls in the string.
31
TEST_F(MDStringTest, CreateDifferent) {
32
char x[3] = { 'f', 0, 'A' };
33
MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
35
MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
39
// Test that creation of MDStrings with the same string contents produces the
40
// same MDString object, even with different pointers.
41
TEST_F(MDStringTest, CreateSame) {
42
char x[4] = { 'a', 'b', 'c', 'X' };
43
char y[4] = { 'a', 'b', 'c', 'Y' };
45
MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
46
MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
50
// Test that MDString prints out the string we fed it.
51
TEST_F(MDStringTest, PrintingSimple) {
52
char *str = new char[13];
53
strncpy(str, "testing 1 2 3", 13);
54
MDString *s = MDString::get(Context, StringRef(str, 13));
55
strncpy(str, "aaaaaaaaaaaaa", 13);
59
raw_string_ostream oss(Str);
61
EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
64
// Test printing of MDString with non-printable characters.
65
TEST_F(MDStringTest, PrintingComplex) {
66
char str[5] = {0, '\n', '"', '\\', -1};
67
MDString *s = MDString::get(Context, StringRef(str+0, 5));
69
raw_string_ostream oss(Str);
71
EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
74
typedef MetadataTest MDNodeTest;
76
// Test the two constructors, and containing other Constants.
77
TEST_F(MDNodeTest, Simple) {
78
char x[3] = { 'a', 'b', 'c' };
79
char y[3] = { '1', '2', '3' };
81
MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
82
MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
83
ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
85
std::vector<Value *> V;
90
MDNode *n1 = MDNode::get(Context, &V[0], 3);
92
MDNode *n2 = MDNode::get(Context, &c1, 1);
93
MDNode *n3 = MDNode::get(Context, &V[0], 3);
95
#ifdef ENABLE_MDNODE_UNIQUING
101
EXPECT_EQ(3u, n1->getNumOperands());
102
EXPECT_EQ(s1, n1->getOperand(0));
103
EXPECT_EQ(CI, n1->getOperand(1));
104
EXPECT_EQ(s2, n1->getOperand(2));
106
EXPECT_EQ(1u, n2->getNumOperands());
107
EXPECT_EQ(n1, n2->getOperand(0));
110
TEST_F(MDNodeTest, Delete) {
111
Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
112
Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
115
MDNode *n = MDNode::get(Context, &V, 1);
123
TEST(NamedMDNodeTest, Search) {
125
Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
126
Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2);
129
Value *const V2 = C2;
130
MDNode *n = MDNode::get(Context, &V, 1);
131
MDNode *n2 = MDNode::get(Context, &V2, 1);
133
MDNode *Nodes[2] = { n, n2 };
135
Module *M = new Module("MyModule", Context);
136
const char *Name = "llvm.NMD1";
137
NamedMDNode *NMD = NamedMDNode::Create(Context, Name, &Nodes[0], 2, M);
139
raw_string_ostream oss(Str);
141
EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",