blob: 12ac2e704c8e8793c1c02242f504d1ed29f6204e [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
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000023class MetadataTest : public testing::Test {
24protected:
25 LLVMContext Context;
26};
27typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +000028
Nick Lewycky49f89192009-04-04 07:22:01 +000029// Test that construction of MDString with different value produces different
30// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000031TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +000032 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000033 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000034 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000035 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000036 EXPECT_NE(s1, s2);
37}
38
39// Test that creation of MDStrings with the same string contents produces the
40// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000041TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +000042 char x[4] = { 'a', 'b', 'c', 'X' };
43 char y[4] = { 'a', 'b', 'c', 'Y' };
44
Owen Anderson23587322009-07-31 21:38:10 +000045 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
46 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000047 EXPECT_EQ(s1, s2);
48}
49
50// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000051TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000052 char *str = new char[13];
53 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000054 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000055 strncpy(str, "aaaaaaaaaaaaa", 13);
56 delete[] str;
57
Chris Lattnerbe354a62009-08-23 04:47:35 +000058 std::string Str;
59 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +000060 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000061 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000062}
63
64// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000065TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +000066 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +000067 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +000068 std::string Str;
69 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +000070 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000071 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000072}
73
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000074typedef MetadataTest MDNodeTest;
75
Nick Lewycky49f89192009-04-04 07:22:01 +000076// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000077TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000078 char x[3] = { 'a', 'b', 'c' };
79 char y[3] = { '1', '2', '3' };
80
Owen Anderson23587322009-07-31 21:38:10 +000081 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
82 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Owen Andersonedb4a702009-07-24 23:12:02 +000083 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
Nick Lewycky49f89192009-04-04 07:22:01 +000084
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000085 std::vector<Value *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +000086 V.push_back(s1);
87 V.push_back(CI);
88 V.push_back(s2);
89
Jay Foad5514afe2011-04-21 19:59:31 +000090 MDNode *n1 = MDNode::get(Context, V);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000091 Value *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +000092 MDNode *n2 = MDNode::get(Context, c1);
93 MDNode *n3 = MDNode::get(Context, V);
Nick Lewycky49f89192009-04-04 07:22:01 +000094 EXPECT_NE(n1, n2);
Daniel Dunbar8feee902009-09-07 04:19:02 +000095#ifdef ENABLE_MDNODE_UNIQUING
Devang Patelf7188322009-09-03 01:39:20 +000096 EXPECT_EQ(n1, n3);
Daniel Dunbar8feee902009-09-07 04:19:02 +000097#else
98 (void) n3;
99#endif
Nick Lewycky49f89192009-04-04 07:22:01 +0000100
Chris Lattner9b493022009-12-31 01:22:29 +0000101 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));
Nick Lewycky49f89192009-04-04 07:22:01 +0000105
Chris Lattner9b493022009-12-31 01:22:29 +0000106 EXPECT_EQ(1u, n2->getNumOperands());
107 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000108}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000109
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000110TEST_F(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;
Jay Foad5514afe2011-04-21 19:59:31 +0000115 MDNode *n = MDNode::get(Context, V);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000116 WeakVH wvh = n;
117
118 EXPECT_EQ(n, wvh);
119
120 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000121}
Devang Patel0924b332009-07-30 00:03:41 +0000122
123TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000124 LLVMContext Context;
125 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
126 Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2);
Devang Patel0924b332009-07-30 00:03:41 +0000127
128 Value *const V = C;
129 Value *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +0000130 MDNode *n = MDNode::get(Context, V);
131 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +0000132
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +0000133 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +0000134 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +0000135 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
136 NMD->addOperand(n);
137 NMD->addOperand(n2);
138
Chris Lattnerbe354a62009-08-23 04:47:35 +0000139 std::string Str;
140 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +0000141 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +0000142 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +0000143 oss.str().c_str());
144}
Nick Lewycky49f89192009-04-04 07:22:01 +0000145}