blob: ce3a1aba8cf1fec324026fa0a14def38f1fd22da [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"
Chris Lattner26a7ae42009-10-27 17:08:31 +000013#include "llvm/LLVMContext.h"
Benjamin Kramer28983a42009-07-28 22:03:24 +000014#include "llvm/Metadata.h"
Devang Patel0924b332009-07-30 00:03:41 +000015#include "llvm/Module.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000016#include "llvm/Type.h"
Chris Lattnerbe354a62009-08-23 04:47:35 +000017#include "llvm/Support/raw_ostream.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000018#include "llvm/Support/ValueHandle.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000019using 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
Chris Lattnerbe354a62009-08-23 04:47:35 +000054 std::string Str;
55 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +000056 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000057 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000058}
59
60// Test printing of MDString with non-printable characters.
61TEST(MDStringTest, PrintingComplex) {
62 char str[5] = {0, '\n', '"', '\\', -1};
Owen Anderson23587322009-07-31 21:38:10 +000063 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +000064 std::string Str;
65 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +000066 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000067 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000068}
69
70// Test the two constructors, and containing other Constants.
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000071TEST(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000072 char x[3] = { 'a', 'b', 'c' };
73 char y[3] = { '1', '2', '3' };
74
Owen Anderson23587322009-07-31 21:38:10 +000075 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
76 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Owen Andersonedb4a702009-07-24 23:12:02 +000077 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
Nick Lewycky49f89192009-04-04 07:22:01 +000078
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000079 std::vector<Value *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +000080 V.push_back(s1);
81 V.push_back(CI);
82 V.push_back(s2);
83
Owen Anderson23587322009-07-31 21:38:10 +000084 MDNode *n1 = MDNode::get(Context, &V[0], 3);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000085 Value *const c1 = n1;
Owen Anderson23587322009-07-31 21:38:10 +000086 MDNode *n2 = MDNode::get(Context, &c1, 1);
87 MDNode *n3 = MDNode::get(Context, &V[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000088 EXPECT_NE(n1, n2);
Daniel Dunbar8feee902009-09-07 04:19:02 +000089#ifdef ENABLE_MDNODE_UNIQUING
Devang Patelf7188322009-09-03 01:39:20 +000090 EXPECT_EQ(n1, n3);
Daniel Dunbar8feee902009-09-07 04:19:02 +000091#else
92 (void) n3;
93#endif
Nick Lewycky49f89192009-04-04 07:22:01 +000094
Chris Lattner9b493022009-12-31 01:22:29 +000095 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));
Nick Lewycky49f89192009-04-04 07:22:01 +000099
Chris Lattner9b493022009-12-31 01:22:29 +0000100 EXPECT_EQ(1u, n2->getNumOperands());
101 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000102
Chris Lattnerbe354a62009-08-23 04:47:35 +0000103 std::string Str;
104 raw_string_ostream oss(Str);
105 n1->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000106 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Chris Lattnerbe354a62009-08-23 04:47:35 +0000107 oss.str().c_str());
108 Str.clear();
109 n2->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000110 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
111 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Chris Lattnerbe354a62009-08-23 04:47:35 +0000112 oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000113}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000114
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000115TEST(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000116 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
117 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000118
119 Value *const V = I;
Owen Anderson23587322009-07-31 21:38:10 +0000120 MDNode *n = MDNode::get(Context, &V, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000121 WeakVH wvh = n;
122
123 EXPECT_EQ(n, wvh);
124
125 delete I;
126
Chris Lattnerbe354a62009-08-23 04:47:35 +0000127 std::string Str;
128 raw_string_ostream oss(Str);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000129 wvh->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000130 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000131}
Devang Patel0924b332009-07-30 00:03:41 +0000132
133TEST(NamedMDNodeTest, Search) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000134 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
135 Constant *C2 = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
Devang Patel0924b332009-07-30 00:03:41 +0000136
137 Value *const V = C;
138 Value *const V2 = C2;
Owen Anderson23587322009-07-31 21:38:10 +0000139 MDNode *n = MDNode::get(Context, &V, 1);
140 MDNode *n2 = MDNode::get(Context, &V2, 1);
Devang Patel0924b332009-07-30 00:03:41 +0000141
142 MetadataBase *Nodes[2] = { n, n2 };
143
144 Module *M = new Module("MyModule", getGlobalContext());
145 const char *Name = "llvm.NMD1";
Owen Anderson55f1c092009-08-13 21:58:54 +0000146 NamedMDNode *NMD = NamedMDNode::Create(getGlobalContext(), Name, &Nodes[0], 2, M);
Chris Lattnerbe354a62009-08-23 04:47:35 +0000147 std::string Str;
148 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +0000149 NMD->print(oss);
150 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
151 "!1 = metadata !{i32 2}\n",
152 oss.str().c_str());
153}
Nick Lewycky49f89192009-04-04 07:22:01 +0000154}