Chandler Carruth | c779e96 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===// |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 10 | #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 Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ValueHandle.h" |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
Jeffrey Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 23 | class MetadataTest : public testing::Test { |
| 24 | protected: |
| 25 | LLVMContext Context; |
| 26 | }; |
| 27 | typedef MetadataTest MDStringTest; |
Owen Anderson | 5d0bf1b | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 28 | |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 29 | // 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 Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 31 | TEST_F(MDStringTest, CreateDifferent) { |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 32 | char x[3] = { 'f', 0, 'A' }; |
Owen Anderson | 5d0bf1b | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 33 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 34 | x[2] = 'B'; |
Owen Anderson | 5d0bf1b | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 35 | MDString *s2 = MDString::get(Context, StringRef(&x[0], 3)); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 36 | 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 Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 41 | TEST_F(MDStringTest, CreateSame) { |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 42 | char x[4] = { 'a', 'b', 'c', 'X' }; |
| 43 | char y[4] = { 'a', 'b', 'c', 'Y' }; |
| 44 | |
Owen Anderson | 5d0bf1b | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 45 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
| 46 | MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 47 | EXPECT_EQ(s1, s2); |
| 48 | } |
| 49 | |
| 50 | // Test that MDString prints out the string we fed it. |
Jeffrey Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 51 | TEST_F(MDStringTest, PrintingSimple) { |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 52 | char *str = new char[13]; |
| 53 | strncpy(str, "testing 1 2 3", 13); |
Owen Anderson | 5d0bf1b | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 54 | MDString *s = MDString::get(Context, StringRef(str, 13)); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 55 | strncpy(str, "aaaaaaaaaaaaa", 13); |
| 56 | delete[] str; |
| 57 | |
Chris Lattner | 0c47a41 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 58 | std::string Str; |
| 59 | raw_string_ostream oss(Str); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 60 | s->print(oss); |
Nick Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 61 | EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str()); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Test printing of MDString with non-printable characters. |
Jeffrey Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 65 | TEST_F(MDStringTest, PrintingComplex) { |
Jeffrey Yasskin | cda2a14 | 2011-08-30 20:53:29 +0000 | [diff] [blame] | 66 | char str[5] = {0, '\n', '"', '\\', (char)-1}; |
Owen Anderson | 5d0bf1b | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 67 | MDString *s = MDString::get(Context, StringRef(str+0, 5)); |
Chris Lattner | 0c47a41 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 68 | std::string Str; |
| 69 | raw_string_ostream oss(Str); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 70 | s->print(oss); |
Nick Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 71 | EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Jeffrey Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 74 | typedef MetadataTest MDNodeTest; |
| 75 | |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 76 | // Test the two constructors, and containing other Constants. |
Jeffrey Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 77 | TEST_F(MDNodeTest, Simple) { |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 78 | char x[3] = { 'a', 'b', 'c' }; |
| 79 | char y[3] = { '1', '2', '3' }; |
| 80 | |
Owen Anderson | 5d0bf1b | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 81 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
| 82 | MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 83 | ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0)); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 84 | |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 85 | std::vector<Value *> V; |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 86 | V.push_back(s1); |
| 87 | V.push_back(CI); |
| 88 | V.push_back(s2); |
| 89 | |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 90 | MDNode *n1 = MDNode::get(Context, V); |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 91 | Value *const c1 = n1; |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 92 | MDNode *n2 = MDNode::get(Context, c1); |
Duncan Sands | 4000afe | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 93 | Value *const c2 = n2; |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 94 | MDNode *n3 = MDNode::get(Context, V); |
Duncan Sands | 4000afe | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 95 | MDNode *n4 = MDNode::getIfExists(Context, V); |
| 96 | MDNode *n5 = MDNode::getIfExists(Context, c1); |
| 97 | MDNode *n6 = MDNode::getIfExists(Context, c2); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 98 | EXPECT_NE(n1, n2); |
Daniel Dunbar | 7b26b58 | 2009-09-07 04:19:02 +0000 | [diff] [blame] | 99 | #ifdef ENABLE_MDNODE_UNIQUING |
Devang Patel | 5f4ac84 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 100 | EXPECT_EQ(n1, n3); |
Daniel Dunbar | 7b26b58 | 2009-09-07 04:19:02 +0000 | [diff] [blame] | 101 | #else |
| 102 | (void) n3; |
| 103 | #endif |
Duncan Sands | 4000afe | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 104 | EXPECT_EQ(n4, n1); |
| 105 | EXPECT_EQ(n5, n2); |
| 106 | EXPECT_EQ(n6, (Value*)0); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 108 | 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 Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 113 | EXPECT_EQ(1u, n2->getNumOperands()); |
| 114 | EXPECT_EQ(n1, n2->getOperand(0)); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 115 | } |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 116 | |
Jeffrey Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 117 | TEST_F(MDNodeTest, Delete) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 118 | Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1); |
| 119 | Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext())); |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 120 | |
| 121 | Value *const V = I; |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 122 | MDNode *n = MDNode::get(Context, V); |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 123 | WeakVH wvh = n; |
| 124 | |
| 125 | EXPECT_EQ(n, wvh); |
| 126 | |
| 127 | delete I; |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 128 | } |
Devang Patel | fa7c4dc | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 129 | |
| 130 | TEST(NamedMDNodeTest, Search) { |
Jeffrey Yasskin | e5790a4 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 131 | LLVMContext Context; |
| 132 | Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1); |
| 133 | Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2); |
Devang Patel | fa7c4dc | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 134 | |
| 135 | Value *const V = C; |
| 136 | Value *const V2 = C2; |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 137 | MDNode *n = MDNode::get(Context, V); |
| 138 | MDNode *n2 = MDNode::get(Context, V2); |
Devang Patel | fa7c4dc | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 139 | |
Jeffrey Yasskin | 9d0b3dd | 2010-03-13 01:39:20 +0000 | [diff] [blame] | 140 | Module M("MyModule", Context); |
Devang Patel | fa7c4dc | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 141 | const char *Name = "llvm.NMD1"; |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 142 | NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name); |
| 143 | NMD->addOperand(n); |
| 144 | NMD->addOperand(n2); |
| 145 | |
Chris Lattner | 0c47a41 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 146 | std::string Str; |
| 147 | raw_string_ostream oss(Str); |
Devang Patel | fa7c4dc | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 148 | NMD->print(oss); |
Chris Lattner | ab2f2f1 | 2009-12-31 02:12:13 +0000 | [diff] [blame] | 149 | EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", |
Devang Patel | fa7c4dc | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 150 | oss.str().c_str()); |
| 151 | } |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 152 | } |