blob: 22519f9f30731d7eb3088125c0da4a12266a997b [file] [log] [blame]
Nick Lewycky49f89192009-04-04 07:22:01 +00001//===- llvm/unittest/VMCore/Metadata.cpp - Metadata unit tests ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11#include "llvm/Constants.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000012#include "llvm/Instructions.h"
Benjamin Kramer28983a42009-07-28 22:03:24 +000013#include "llvm/Metadata.h"
Devang Patel0924b332009-07-30 00:03:41 +000014#include "llvm/Module.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000015#include "llvm/Type.h"
16#include "llvm/Support/ValueHandle.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000017#include <sstream>
18
19using namespace llvm;
20
21namespace {
22
Owen Anderson23587322009-07-31 21:38:10 +000023LLVMContext &Context = getGlobalContext();
24
Nick Lewycky49f89192009-04-04 07:22:01 +000025// Test that construction of MDString with different value produces different
26// MDString objects, even with the same string pointer and nulls in the string.
27TEST(MDStringTest, CreateDifferent) {
28 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000029 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000030 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000031 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000032 EXPECT_NE(s1, s2);
33}
34
35// Test that creation of MDStrings with the same string contents produces the
36// same MDString object, even with different pointers.
37TEST(MDStringTest, CreateSame) {
38 char x[4] = { 'a', 'b', 'c', 'X' };
39 char y[4] = { 'a', 'b', 'c', 'Y' };
40
Owen Anderson23587322009-07-31 21:38:10 +000041 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
42 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000043 EXPECT_EQ(s1, s2);
44}
45
46// Test that MDString prints out the string we fed it.
47TEST(MDStringTest, PrintingSimple) {
48 char *str = new char[13];
49 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000050 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000051 strncpy(str, "aaaaaaaaaaaaa", 13);
52 delete[] str;
53
54 std::ostringstream oss;
55 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000056 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000057}
58
59// Test printing of MDString with non-printable characters.
60TEST(MDStringTest, PrintingComplex) {
61 char str[5] = {0, '\n', '"', '\\', -1};
Owen Anderson23587322009-07-31 21:38:10 +000062 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Nick Lewycky49f89192009-04-04 07:22:01 +000063 std::ostringstream oss;
64 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000065 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000066}
67
68// Test the two constructors, and containing other Constants.
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000069TEST(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000070 char x[3] = { 'a', 'b', 'c' };
71 char y[3] = { '1', '2', '3' };
72
Owen Anderson23587322009-07-31 21:38:10 +000073 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
74 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Owen Andersonedb4a702009-07-24 23:12:02 +000075 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
Nick Lewycky49f89192009-04-04 07:22:01 +000076
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000077 std::vector<Value *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +000078 V.push_back(s1);
79 V.push_back(CI);
80 V.push_back(s2);
81
Owen Anderson23587322009-07-31 21:38:10 +000082 MDNode *n1 = MDNode::get(Context, &V[0], 3);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000083 Value *const c1 = n1;
Owen Anderson23587322009-07-31 21:38:10 +000084 MDNode *n2 = MDNode::get(Context, &c1, 1);
85 MDNode *n3 = MDNode::get(Context, &V[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000086 EXPECT_NE(n1, n2);
87 EXPECT_EQ(n1, n3);
88
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000089 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));
Nick Lewycky49f89192009-04-04 07:22:01 +000093
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000094 EXPECT_EQ(1u, n2->getNumElements());
95 EXPECT_EQ(n1, n2->getElement(0));
Nick Lewycky49f89192009-04-04 07:22:01 +000096
97 std::ostringstream oss1, oss2;
98 n1->print(oss1);
99 n2->print(oss2);
Devang Patelf04d63a2009-07-08 21:57:07 +0000100 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000101 oss1.str().c_str());
Devang Patelf04d63a2009-07-08 21:57:07 +0000102 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
103 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Nick Lewycky49f89192009-04-04 07:22:01 +0000104 oss2.str().c_str());
105}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000106
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000107TEST(MDNodeTest, Delete) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000108 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000109 Instruction *I = new BitCastInst(C, Type::Int32Ty);
110
111 Value *const V = I;
Owen Anderson23587322009-07-31 21:38:10 +0000112 MDNode *n = MDNode::get(Context, &V, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000113 WeakVH wvh = n;
114
115 EXPECT_EQ(n, wvh);
116
117 delete I;
118
119 std::ostringstream oss;
120 wvh->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000121 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000122}
Devang Patel0924b332009-07-30 00:03:41 +0000123
124TEST(NamedMDNodeTest, Search) {
125 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
126 Constant *C2 = ConstantInt::get(Type::Int32Ty, 2);
127
128 Value *const V = C;
129 Value *const V2 = C2;
Owen Anderson23587322009-07-31 21:38:10 +0000130 MDNode *n = MDNode::get(Context, &V, 1);
131 MDNode *n2 = MDNode::get(Context, &V2, 1);
Devang Patel0924b332009-07-30 00:03:41 +0000132
133 MetadataBase *Nodes[2] = { n, n2 };
134
135 Module *M = new Module("MyModule", getGlobalContext());
136 const char *Name = "llvm.NMD1";
137 NamedMDNode *NMD = NamedMDNode::Create(Name, &Nodes[0], 2, M);
138 std::ostringstream oss;
139 NMD->print(oss);
140 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
141 "!1 = metadata !{i32 2}\n",
142 oss.str().c_str());
143}
Nick Lewycky49f89192009-04-04 07:22:01 +0000144}