blob: 22b909969d7544c3599c63527eb3b8e2b17f3a0b [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
23// Test that construction of MDString with different value produces different
24// MDString objects, even with the same string pointer and nulls in the string.
25TEST(MDStringTest, CreateDifferent) {
26 char x[3] = { 'f', 0, 'A' };
Daniel Dunbard43b86d2009-07-25 06:02:13 +000027 MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000028 x[2] = 'B';
Daniel Dunbard43b86d2009-07-25 06:02:13 +000029 MDString *s2 = getGlobalContext().getMDString(StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000030 EXPECT_NE(s1, s2);
31}
32
33// Test that creation of MDStrings with the same string contents produces the
34// same MDString object, even with different pointers.
35TEST(MDStringTest, CreateSame) {
36 char x[4] = { 'a', 'b', 'c', 'X' };
37 char y[4] = { 'a', 'b', 'c', 'Y' };
38
Daniel Dunbard43b86d2009-07-25 06:02:13 +000039 MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3));
40 MDString *s2 = getGlobalContext().getMDString(StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000041 EXPECT_EQ(s1, s2);
42}
43
44// Test that MDString prints out the string we fed it.
45TEST(MDStringTest, PrintingSimple) {
46 char *str = new char[13];
47 strncpy(str, "testing 1 2 3", 13);
Daniel Dunbard43b86d2009-07-25 06:02:13 +000048 MDString *s = getGlobalContext().getMDString(StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000049 strncpy(str, "aaaaaaaaaaaaa", 13);
50 delete[] str;
51
52 std::ostringstream oss;
53 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000054 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000055}
56
57// Test printing of MDString with non-printable characters.
58TEST(MDStringTest, PrintingComplex) {
59 char str[5] = {0, '\n', '"', '\\', -1};
Daniel Dunbard43b86d2009-07-25 06:02:13 +000060 MDString *s = getGlobalContext().getMDString(StringRef(str+0, 5));
Nick Lewycky49f89192009-04-04 07:22:01 +000061 std::ostringstream oss;
62 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000063 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000064}
65
66// Test the two constructors, and containing other Constants.
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000067TEST(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000068 char x[3] = { 'a', 'b', 'c' };
69 char y[3] = { '1', '2', '3' };
70
Daniel Dunbard43b86d2009-07-25 06:02:13 +000071 MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3));
72 MDString *s2 = getGlobalContext().getMDString(StringRef(&y[0], 3));
Owen Andersonedb4a702009-07-24 23:12:02 +000073 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
Nick Lewycky49f89192009-04-04 07:22:01 +000074
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000075 std::vector<Value *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +000076 V.push_back(s1);
77 V.push_back(CI);
78 V.push_back(s2);
79
Owen Andersonb6b25302009-07-14 23:09:55 +000080 MDNode *n1 = getGlobalContext().getMDNode(&V[0], 3);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000081 Value *const c1 = n1;
Owen Andersonb6b25302009-07-14 23:09:55 +000082 MDNode *n2 = getGlobalContext().getMDNode(&c1, 1);
83 MDNode *n3 = getGlobalContext().getMDNode(&V[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000084 EXPECT_NE(n1, n2);
85 EXPECT_EQ(n1, n3);
86
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000087 EXPECT_EQ(3u, n1->getNumElements());
88 EXPECT_EQ(s1, n1->getElement(0));
89 EXPECT_EQ(CI, n1->getElement(1));
90 EXPECT_EQ(s2, n1->getElement(2));
Nick Lewycky49f89192009-04-04 07:22:01 +000091
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000092 EXPECT_EQ(1u, n2->getNumElements());
93 EXPECT_EQ(n1, n2->getElement(0));
Nick Lewycky49f89192009-04-04 07:22:01 +000094
95 std::ostringstream oss1, oss2;
96 n1->print(oss1);
97 n2->print(oss2);
Devang Patelf04d63a2009-07-08 21:57:07 +000098 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Nick Lewyckyadbc2842009-05-30 05:06:04 +000099 oss1.str().c_str());
Devang Patelf04d63a2009-07-08 21:57:07 +0000100 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
101 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Nick Lewycky49f89192009-04-04 07:22:01 +0000102 oss2.str().c_str());
103}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000104
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000105TEST(MDNodeTest, Delete) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000106 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000107 Instruction *I = new BitCastInst(C, Type::Int32Ty);
108
109 Value *const V = I;
Owen Andersonb6b25302009-07-14 23:09:55 +0000110 MDNode *n = getGlobalContext().getMDNode(&V, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000111 WeakVH wvh = n;
112
113 EXPECT_EQ(n, wvh);
114
115 delete I;
116
117 std::ostringstream oss;
118 wvh->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000119 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000120}
Devang Patel0924b332009-07-30 00:03:41 +0000121
122TEST(NamedMDNodeTest, Search) {
123 Constant *C = ConstantInt::get(Type::Int32Ty, 1);
124 Constant *C2 = ConstantInt::get(Type::Int32Ty, 2);
125
126 Value *const V = C;
127 Value *const V2 = C2;
128 MDNode *n = getGlobalContext().getMDNode(&V, 1);
129 MDNode *n2 = getGlobalContext().getMDNode(&V2, 1);
130
131 MetadataBase *Nodes[2] = { n, n2 };
132
133 Module *M = new Module("MyModule", getGlobalContext());
134 const char *Name = "llvm.NMD1";
135 NamedMDNode *NMD = NamedMDNode::Create(Name, &Nodes[0], 2, M);
136 std::ostringstream oss;
137 NMD->print(oss);
138 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
139 "!1 = metadata !{i32 2}\n",
140 oss.str().c_str());
141}
Nick Lewycky49f89192009-04-04 07:22:01 +0000142}