blob: 352e83ee662ee2b276ab06d4a7dacd5900991cc4 [file] [log] [blame]
Chandler Carruthc779e962013-01-07 15:35:46 +00001//===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
Nick Lewycky21cc4462009-04-04 07:22:01 +00002//
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
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000010#include "llvm/IR/Metadata.h"
11#include "llvm/IR/Constants.h"
12#include "llvm/IR/Instructions.h"
13#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
15#include "llvm/IR/Type.h"
Nick Lewyckycb337992009-05-10 20:57:05 +000016#include "llvm/Support/ValueHandle.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000017#include "llvm/Support/raw_ostream.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000018#include "gtest/gtest.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000019using namespace llvm;
20
21namespace {
22
Jeffrey Yasskine5790a42010-03-04 23:24:19 +000023class MetadataTest : public testing::Test {
24protected:
25 LLVMContext Context;
26};
27typedef MetadataTest MDStringTest;
Owen Anderson5d0bf1b2009-07-31 21:38:10 +000028
Nick Lewycky21cc4462009-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 Yasskine5790a42010-03-04 23:24:19 +000031TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky21cc4462009-04-04 07:22:01 +000032 char x[3] = { 'f', 0, 'A' };
Owen Anderson5d0bf1b2009-07-31 21:38:10 +000033 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky21cc4462009-04-04 07:22:01 +000034 x[2] = 'B';
Owen Anderson5d0bf1b2009-07-31 21:38:10 +000035 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky21cc4462009-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 Yasskine5790a42010-03-04 23:24:19 +000041TEST_F(MDStringTest, CreateSame) {
Nick Lewycky21cc4462009-04-04 07:22:01 +000042 char x[4] = { 'a', 'b', 'c', 'X' };
43 char y[4] = { 'a', 'b', 'c', 'Y' };
44
Owen Anderson5d0bf1b2009-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 Lewycky21cc4462009-04-04 07:22:01 +000047 EXPECT_EQ(s1, s2);
48}
49
50// Test that MDString prints out the string we fed it.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +000051TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky21cc4462009-04-04 07:22:01 +000052 char *str = new char[13];
53 strncpy(str, "testing 1 2 3", 13);
Owen Anderson5d0bf1b2009-07-31 21:38:10 +000054 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky21cc4462009-04-04 07:22:01 +000055 strncpy(str, "aaaaaaaaaaaaa", 13);
56 delete[] str;
57
Chris Lattner0c47a412009-08-23 04:47:35 +000058 std::string Str;
59 raw_string_ostream oss(Str);
Nick Lewycky21cc4462009-04-04 07:22:01 +000060 s->print(oss);
Nick Lewycky7a0370f2009-05-30 05:06:04 +000061 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky21cc4462009-04-04 07:22:01 +000062}
63
64// Test printing of MDString with non-printable characters.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +000065TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskincda2a142011-08-30 20:53:29 +000066 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson5d0bf1b2009-07-31 21:38:10 +000067 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattner0c47a412009-08-23 04:47:35 +000068 std::string Str;
69 raw_string_ostream oss(Str);
Nick Lewycky21cc4462009-04-04 07:22:01 +000070 s->print(oss);
Nick Lewycky7a0370f2009-05-30 05:06:04 +000071 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky21cc4462009-04-04 07:22:01 +000072}
73
Jeffrey Yasskine5790a42010-03-04 23:24:19 +000074typedef MetadataTest MDNodeTest;
75
Nick Lewycky21cc4462009-04-04 07:22:01 +000076// Test the two constructors, and containing other Constants.
Jeffrey Yasskine5790a42010-03-04 23:24:19 +000077TEST_F(MDNodeTest, Simple) {
Nick Lewycky21cc4462009-04-04 07:22:01 +000078 char x[3] = { 'a', 'b', 'c' };
79 char y[3] = { '1', '2', '3' };
80
Owen Anderson5d0bf1b2009-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 Andersoneed707b2009-07-24 23:12:02 +000083 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
Nick Lewycky21cc4462009-04-04 07:22:01 +000084
Nick Lewyckycb337992009-05-10 20:57:05 +000085 std::vector<Value *> V;
Nick Lewycky21cc4462009-04-04 07:22:01 +000086 V.push_back(s1);
87 V.push_back(CI);
88 V.push_back(s2);
89
Jay Foadec9186b2011-04-21 19:59:31 +000090 MDNode *n1 = MDNode::get(Context, V);
Nick Lewyckycb337992009-05-10 20:57:05 +000091 Value *const c1 = n1;
Jay Foadec9186b2011-04-21 19:59:31 +000092 MDNode *n2 = MDNode::get(Context, c1);
Duncan Sands4000afe2012-03-31 08:20:11 +000093 Value *const c2 = n2;
Jay Foadec9186b2011-04-21 19:59:31 +000094 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands4000afe2012-03-31 08:20:11 +000095 MDNode *n4 = MDNode::getIfExists(Context, V);
96 MDNode *n5 = MDNode::getIfExists(Context, c1);
97 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky21cc4462009-04-04 07:22:01 +000098 EXPECT_NE(n1, n2);
Daniel Dunbar7b26b582009-09-07 04:19:02 +000099#ifdef ENABLE_MDNODE_UNIQUING
Devang Patel5f4ac842009-09-03 01:39:20 +0000100 EXPECT_EQ(n1, n3);
Daniel Dunbar7b26b582009-09-07 04:19:02 +0000101#else
102 (void) n3;
103#endif
Duncan Sands4000afe2012-03-31 08:20:11 +0000104 EXPECT_EQ(n4, n1);
105 EXPECT_EQ(n5, n2);
106 EXPECT_EQ(n6, (Value*)0);
Nick Lewycky21cc4462009-04-04 07:22:01 +0000107
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000108 EXPECT_EQ(3u, n1->getNumOperands());
109 EXPECT_EQ(s1, n1->getOperand(0));
110 EXPECT_EQ(CI, n1->getOperand(1));
111 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000112
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000113 EXPECT_EQ(1u, n2->getNumOperands());
114 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky21cc4462009-04-04 07:22:01 +0000115}
Nick Lewyckycb337992009-05-10 20:57:05 +0000116
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000117TEST_F(MDNodeTest, Delete) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000118 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
119 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckycb337992009-05-10 20:57:05 +0000120
121 Value *const V = I;
Jay Foadec9186b2011-04-21 19:59:31 +0000122 MDNode *n = MDNode::get(Context, V);
Nick Lewyckycb337992009-05-10 20:57:05 +0000123 WeakVH wvh = n;
124
125 EXPECT_EQ(n, wvh);
126
127 delete I;
Nick Lewyckycb337992009-05-10 20:57:05 +0000128}
Devang Patelfa7c4dc2009-07-30 00:03:41 +0000129
130TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskine5790a42010-03-04 23:24:19 +0000131 LLVMContext Context;
132 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
133 Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2);
Devang Patelfa7c4dc2009-07-30 00:03:41 +0000134
135 Value *const V = C;
136 Value *const V2 = C2;
Jay Foadec9186b2011-04-21 19:59:31 +0000137 MDNode *n = MDNode::get(Context, V);
138 MDNode *n2 = MDNode::get(Context, V2);
Devang Patelfa7c4dc2009-07-30 00:03:41 +0000139
Jeffrey Yasskin9d0b3dd2010-03-13 01:39:20 +0000140 Module M("MyModule", Context);
Devang Patelfa7c4dc2009-07-30 00:03:41 +0000141 const char *Name = "llvm.NMD1";
Dan Gohman17aa92c2010-07-21 23:38:33 +0000142 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
143 NMD->addOperand(n);
144 NMD->addOperand(n2);
145
Chris Lattner0c47a412009-08-23 04:47:35 +0000146 std::string Str;
147 raw_string_ostream oss(Str);
Devang Patelfa7c4dc2009-07-30 00:03:41 +0000148 NMD->print(oss);
Chris Lattnerab2f2f12009-12-31 02:12:13 +0000149 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patelfa7c4dc2009-07-30 00:03:41 +0000150 oss.str().c_str());
151}
Nick Lewycky21cc4462009-04-04 07:22:01 +0000152}