blob: cdf5a6e6b90ede6608352bc11f5242f5370c9256 [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"
Chris Lattnerbe354a62009-08-23 04:47:35 +000016#include "llvm/Support/raw_ostream.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000017#include "llvm/Support/ValueHandle.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000018using namespace llvm;
19
20namespace {
21
Owen Anderson23587322009-07-31 21:38:10 +000022LLVMContext &Context = getGlobalContext();
23
Nick Lewycky49f89192009-04-04 07:22:01 +000024// Test that construction of MDString with different value produces different
25// MDString objects, even with the same string pointer and nulls in the string.
26TEST(MDStringTest, CreateDifferent) {
27 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000028 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000029 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000030 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000031 EXPECT_NE(s1, s2);
32}
33
34// Test that creation of MDStrings with the same string contents produces the
35// same MDString object, even with different pointers.
36TEST(MDStringTest, CreateSame) {
37 char x[4] = { 'a', 'b', 'c', 'X' };
38 char y[4] = { 'a', 'b', 'c', 'Y' };
39
Owen Anderson23587322009-07-31 21:38:10 +000040 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
41 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000042 EXPECT_EQ(s1, s2);
43}
44
45// Test that MDString prints out the string we fed it.
46TEST(MDStringTest, PrintingSimple) {
47 char *str = new char[13];
48 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000049 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000050 strncpy(str, "aaaaaaaaaaaaa", 13);
51 delete[] str;
52
Chris Lattnerbe354a62009-08-23 04:47:35 +000053 std::string Str;
54 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +000055 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));
Chris Lattnerbe354a62009-08-23 04:47:35 +000063 std::string Str;
64 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +000065 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000066 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000067}
68
69// Test the two constructors, and containing other Constants.
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000070TEST(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000071 char x[3] = { 'a', 'b', 'c' };
72 char y[3] = { '1', '2', '3' };
73
Owen Anderson23587322009-07-31 21:38:10 +000074 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
75 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Owen Andersonedb4a702009-07-24 23:12:02 +000076 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
Nick Lewycky49f89192009-04-04 07:22:01 +000077
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000078 std::vector<Value *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +000079 V.push_back(s1);
80 V.push_back(CI);
81 V.push_back(s2);
82
Owen Anderson23587322009-07-31 21:38:10 +000083 MDNode *n1 = MDNode::get(Context, &V[0], 3);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000084 Value *const c1 = n1;
Owen Anderson23587322009-07-31 21:38:10 +000085 MDNode *n2 = MDNode::get(Context, &c1, 1);
86 MDNode *n3 = MDNode::get(Context, &V[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000087 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +000088 EXPECT_EQ(n1, n3);
Nick Lewycky49f89192009-04-04 07:22:01 +000089
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000090 EXPECT_EQ(3u, n1->getNumElements());
91 EXPECT_EQ(s1, n1->getElement(0));
92 EXPECT_EQ(CI, n1->getElement(1));
93 EXPECT_EQ(s2, n1->getElement(2));
Nick Lewycky49f89192009-04-04 07:22:01 +000094
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000095 EXPECT_EQ(1u, n2->getNumElements());
96 EXPECT_EQ(n1, n2->getElement(0));
Nick Lewycky49f89192009-04-04 07:22:01 +000097
Chris Lattnerbe354a62009-08-23 04:47:35 +000098 std::string Str;
99 raw_string_ostream oss(Str);
100 n1->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000101 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Chris Lattnerbe354a62009-08-23 04:47:35 +0000102 oss.str().c_str());
103 Str.clear();
104 n2->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000105 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
106 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Chris Lattnerbe354a62009-08-23 04:47:35 +0000107 oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000108}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000109
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000110TEST(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000111 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
112 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000113
114 Value *const V = I;
Owen Anderson23587322009-07-31 21:38:10 +0000115 MDNode *n = MDNode::get(Context, &V, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000116 WeakVH wvh = n;
117
118 EXPECT_EQ(n, wvh);
119
120 delete I;
121
Chris Lattnerbe354a62009-08-23 04:47:35 +0000122 std::string Str;
123 raw_string_ostream oss(Str);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000124 wvh->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000125 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000126}
Devang Patel0924b332009-07-30 00:03:41 +0000127
128TEST(NamedMDNodeTest, Search) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000129 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
130 Constant *C2 = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
Devang Patel0924b332009-07-30 00:03:41 +0000131
132 Value *const V = C;
133 Value *const V2 = C2;
Owen Anderson23587322009-07-31 21:38:10 +0000134 MDNode *n = MDNode::get(Context, &V, 1);
135 MDNode *n2 = MDNode::get(Context, &V2, 1);
Devang Patel0924b332009-07-30 00:03:41 +0000136
137 MetadataBase *Nodes[2] = { n, n2 };
138
139 Module *M = new Module("MyModule", getGlobalContext());
140 const char *Name = "llvm.NMD1";
Owen Anderson55f1c092009-08-13 21:58:54 +0000141 NamedMDNode *NMD = NamedMDNode::Create(getGlobalContext(), Name, &Nodes[0], 2, M);
Chris Lattnerbe354a62009-08-23 04:47:35 +0000142 std::string Str;
143 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +0000144 NMD->print(oss);
145 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
146 "!1 = metadata !{i32 2}\n",
147 oss.str().c_str());
148}
Nick Lewycky49f89192009-04-04 07:22:01 +0000149}