Duncan P. N. Exon Smith | 71db642 | 2015-02-02 18:20:15 +0000 | [diff] [blame] | 1 | //===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===// |
Nick Lewycky | 49f8919 | 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 | |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 11 | #include "llvm/IR/Constants.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DebugInfoMetadata.h" |
Duncan P. N. Exon Smith | df52349 | 2015-02-18 20:32:57 +0000 | [diff] [blame] | 13 | #include "llvm/IR/Function.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Instructions.h" |
| 15 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/IR/Type.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
Duncan P. N. Exon Smith | 2711ca7 | 2015-01-19 19:02:06 +0000 | [diff] [blame] | 25 | TEST(ContextAndReplaceableUsesTest, FromContext) { |
| 26 | LLVMContext Context; |
| 27 | ContextAndReplaceableUses CRU(Context); |
| 28 | EXPECT_EQ(&Context, &CRU.getContext()); |
| 29 | EXPECT_FALSE(CRU.hasReplaceableUses()); |
| 30 | EXPECT_FALSE(CRU.getReplaceableUses()); |
| 31 | } |
| 32 | |
| 33 | TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) { |
| 34 | LLVMContext Context; |
| 35 | ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context)); |
| 36 | EXPECT_EQ(&Context, &CRU.getContext()); |
| 37 | EXPECT_TRUE(CRU.hasReplaceableUses()); |
| 38 | EXPECT_TRUE(CRU.getReplaceableUses()); |
| 39 | } |
| 40 | |
| 41 | TEST(ContextAndReplaceableUsesTest, makeReplaceable) { |
| 42 | LLVMContext Context; |
| 43 | ContextAndReplaceableUses CRU(Context); |
| 44 | CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context)); |
| 45 | EXPECT_EQ(&Context, &CRU.getContext()); |
| 46 | EXPECT_TRUE(CRU.hasReplaceableUses()); |
| 47 | EXPECT_TRUE(CRU.getReplaceableUses()); |
| 48 | } |
| 49 | |
| 50 | TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) { |
| 51 | LLVMContext Context; |
| 52 | auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context); |
| 53 | auto *Ptr = ReplaceableUses.get(); |
| 54 | ContextAndReplaceableUses CRU(std::move(ReplaceableUses)); |
| 55 | ReplaceableUses = CRU.takeReplaceableUses(); |
| 56 | EXPECT_EQ(&Context, &CRU.getContext()); |
| 57 | EXPECT_FALSE(CRU.hasReplaceableUses()); |
| 58 | EXPECT_FALSE(CRU.getReplaceableUses()); |
| 59 | EXPECT_EQ(Ptr, ReplaceableUses.get()); |
| 60 | } |
| 61 | |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 62 | class MetadataTest : public testing::Test { |
| 63 | protected: |
| 64 | LLVMContext Context; |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 65 | MDNode *getNode() { return MDNode::get(Context, None); } |
| 66 | MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); } |
| 67 | MDNode *getNode(Metadata *MD1, Metadata *MD2) { |
| 68 | Metadata *MDs[] = {MD1, MD2}; |
| 69 | return MDNode::get(Context, MDs); |
| 70 | } |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 71 | }; |
| 72 | typedef MetadataTest MDStringTest; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 73 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 74 | // Test that construction of MDString with different value produces different |
| 75 | // MDString objects, even with the same string pointer and nulls in the string. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 76 | TEST_F(MDStringTest, CreateDifferent) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 77 | char x[3] = { 'f', 0, 'A' }; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 78 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 79 | x[2] = 'B'; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 80 | MDString *s2 = MDString::get(Context, StringRef(&x[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 81 | EXPECT_NE(s1, s2); |
| 82 | } |
| 83 | |
| 84 | // Test that creation of MDStrings with the same string contents produces the |
| 85 | // same MDString object, even with different pointers. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 86 | TEST_F(MDStringTest, CreateSame) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 87 | char x[4] = { 'a', 'b', 'c', 'X' }; |
| 88 | char y[4] = { 'a', 'b', 'c', 'Y' }; |
| 89 | |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 90 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
| 91 | MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 92 | EXPECT_EQ(s1, s2); |
| 93 | } |
| 94 | |
| 95 | // Test that MDString prints out the string we fed it. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 96 | TEST_F(MDStringTest, PrintingSimple) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 97 | char *str = new char[13]; |
| 98 | strncpy(str, "testing 1 2 3", 13); |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 99 | MDString *s = MDString::get(Context, StringRef(str, 13)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 100 | strncpy(str, "aaaaaaaaaaaaa", 13); |
| 101 | delete[] str; |
| 102 | |
Chris Lattner | be354a6 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 103 | std::string Str; |
| 104 | raw_string_ostream oss(Str); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 105 | s->print(oss); |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 106 | EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str()); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Test printing of MDString with non-printable characters. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 110 | TEST_F(MDStringTest, PrintingComplex) { |
Jeffrey Yasskin | 065c357 | 2011-08-30 20:53:29 +0000 | [diff] [blame] | 111 | char str[5] = {0, '\n', '"', '\\', (char)-1}; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 112 | MDString *s = MDString::get(Context, StringRef(str+0, 5)); |
Chris Lattner | be354a6 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 113 | std::string Str; |
| 114 | raw_string_ostream oss(Str); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 115 | s->print(oss); |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 116 | EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 119 | typedef MetadataTest MDNodeTest; |
| 120 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 121 | // Test the two constructors, and containing other Constants. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 122 | TEST_F(MDNodeTest, Simple) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 123 | char x[3] = { 'a', 'b', 'c' }; |
| 124 | char y[3] = { '1', '2', '3' }; |
| 125 | |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 126 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
| 127 | MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 128 | ConstantAsMetadata *CI = ConstantAsMetadata::get( |
| 129 | ConstantInt::get(getGlobalContext(), APInt(8, 0))); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 130 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 131 | std::vector<Metadata *> V; |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 132 | V.push_back(s1); |
| 133 | V.push_back(CI); |
| 134 | V.push_back(s2); |
| 135 | |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 136 | MDNode *n1 = MDNode::get(Context, V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 137 | Metadata *const c1 = n1; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 138 | MDNode *n2 = MDNode::get(Context, c1); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 139 | Metadata *const c2 = n2; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 140 | MDNode *n3 = MDNode::get(Context, V); |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 141 | MDNode *n4 = MDNode::getIfExists(Context, V); |
| 142 | MDNode *n5 = MDNode::getIfExists(Context, c1); |
| 143 | MDNode *n6 = MDNode::getIfExists(Context, c2); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 144 | EXPECT_NE(n1, n2); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 145 | EXPECT_EQ(n1, n3); |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 146 | EXPECT_EQ(n4, n1); |
| 147 | EXPECT_EQ(n5, n2); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 148 | EXPECT_EQ(n6, (Metadata *)nullptr); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 150 | EXPECT_EQ(3u, n1->getNumOperands()); |
| 151 | EXPECT_EQ(s1, n1->getOperand(0)); |
| 152 | EXPECT_EQ(CI, n1->getOperand(1)); |
| 153 | EXPECT_EQ(s2, n1->getOperand(2)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 155 | EXPECT_EQ(1u, n2->getNumOperands()); |
| 156 | EXPECT_EQ(n1, n2->getOperand(0)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 157 | } |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 158 | |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 159 | TEST_F(MDNodeTest, Delete) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 160 | Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1); |
| 161 | Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext())); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 162 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 163 | Metadata *const V = LocalAsMetadata::get(I); |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 164 | MDNode *n = MDNode::get(Context, V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 165 | TrackingMDRef wvh(n); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 166 | |
| 167 | EXPECT_EQ(n, wvh); |
| 168 | |
| 169 | delete I; |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 170 | } |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 171 | |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 172 | TEST_F(MDNodeTest, SelfReference) { |
Duncan P. N. Exon Smith | 8c66273 | 2014-12-16 07:45:05 +0000 | [diff] [blame] | 173 | // !0 = !{!0} |
| 174 | // !1 = !{!0} |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 175 | { |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 176 | auto Temp = MDNode::getTemporary(Context, None); |
| 177 | Metadata *Args[] = {Temp.get()}; |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 178 | MDNode *Self = MDNode::get(Context, Args); |
| 179 | Self->replaceOperandWith(0, Self); |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 180 | ASSERT_EQ(Self, Self->getOperand(0)); |
| 181 | |
| 182 | // Self-references should be distinct, so MDNode::get() should grab a |
| 183 | // uniqued node that references Self, not Self. |
| 184 | Args[0] = Self; |
| 185 | MDNode *Ref1 = MDNode::get(Context, Args); |
| 186 | MDNode *Ref2 = MDNode::get(Context, Args); |
| 187 | EXPECT_NE(Self, Ref1); |
| 188 | EXPECT_EQ(Ref1, Ref2); |
| 189 | } |
| 190 | |
Duncan P. N. Exon Smith | 8c66273 | 2014-12-16 07:45:05 +0000 | [diff] [blame] | 191 | // !0 = !{!0, !{}} |
| 192 | // !1 = !{!0, !{}} |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 193 | { |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 194 | auto Temp = MDNode::getTemporary(Context, None); |
| 195 | Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)}; |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 196 | MDNode *Self = MDNode::get(Context, Args); |
| 197 | Self->replaceOperandWith(0, Self); |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 198 | ASSERT_EQ(Self, Self->getOperand(0)); |
| 199 | |
| 200 | // Self-references should be distinct, so MDNode::get() should grab a |
| 201 | // uniqued node that references Self, not Self itself. |
| 202 | Args[0] = Self; |
| 203 | MDNode *Ref1 = MDNode::get(Context, Args); |
| 204 | MDNode *Ref2 = MDNode::get(Context, Args); |
| 205 | EXPECT_NE(Self, Ref1); |
| 206 | EXPECT_EQ(Ref1, Ref2); |
| 207 | } |
| 208 | } |
| 209 | |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 210 | TEST_F(MDNodeTest, Print) { |
| 211 | Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7); |
| 212 | MDString *S = MDString::get(Context, "foo"); |
| 213 | MDNode *N0 = getNode(); |
| 214 | MDNode *N1 = getNode(N0); |
| 215 | MDNode *N2 = getNode(N0, N1); |
| 216 | |
| 217 | Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2}; |
| 218 | MDNode *N = MDNode::get(Context, Args); |
| 219 | |
| 220 | std::string Expected; |
| 221 | { |
| 222 | raw_string_ostream OS(Expected); |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 223 | OS << "!{"; |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 224 | C->printAsOperand(OS); |
| 225 | OS << ", "; |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 226 | S->printAsOperand(OS); |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 227 | OS << ", null"; |
| 228 | MDNode *Nodes[] = {N0, N1, N2}; |
| 229 | for (auto *Node : Nodes) |
| 230 | OS << ", <" << (void *)Node << ">"; |
| 231 | OS << "}\n"; |
| 232 | } |
| 233 | |
| 234 | std::string Actual; |
| 235 | { |
| 236 | raw_string_ostream OS(Actual); |
| 237 | N->print(OS); |
| 238 | } |
| 239 | |
| 240 | EXPECT_EQ(Expected, Actual); |
| 241 | } |
| 242 | |
Duncan P. N. Exon Smith | bcd960a | 2015-01-05 23:31:54 +0000 | [diff] [blame] | 243 | TEST_F(MDNodeTest, NullOperand) { |
| 244 | // metadata !{} |
| 245 | MDNode *Empty = MDNode::get(Context, None); |
| 246 | |
| 247 | // metadata !{metadata !{}} |
| 248 | Metadata *Ops[] = {Empty}; |
| 249 | MDNode *N = MDNode::get(Context, Ops); |
| 250 | ASSERT_EQ(Empty, N->getOperand(0)); |
| 251 | |
| 252 | // metadata !{metadata !{}} => metadata !{null} |
| 253 | N->replaceOperandWith(0, nullptr); |
| 254 | ASSERT_EQ(nullptr, N->getOperand(0)); |
| 255 | |
| 256 | // metadata !{null} |
| 257 | Ops[0] = nullptr; |
| 258 | MDNode *NullOp = MDNode::get(Context, Ops); |
| 259 | ASSERT_EQ(nullptr, NullOp->getOperand(0)); |
| 260 | EXPECT_EQ(N, NullOp); |
| 261 | } |
| 262 | |
Duncan P. N. Exon Smith | 136ea3f | 2015-01-07 21:35:38 +0000 | [diff] [blame] | 263 | TEST_F(MDNodeTest, DistinctOnUniquingCollision) { |
| 264 | // !{} |
| 265 | MDNode *Empty = MDNode::get(Context, None); |
| 266 | ASSERT_TRUE(Empty->isResolved()); |
| 267 | EXPECT_FALSE(Empty->isDistinct()); |
| 268 | |
| 269 | // !{!{}} |
| 270 | Metadata *Wrapped1Ops[] = {Empty}; |
| 271 | MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops); |
| 272 | ASSERT_EQ(Empty, Wrapped1->getOperand(0)); |
| 273 | ASSERT_TRUE(Wrapped1->isResolved()); |
| 274 | EXPECT_FALSE(Wrapped1->isDistinct()); |
| 275 | |
| 276 | // !{!{!{}}} |
| 277 | Metadata *Wrapped2Ops[] = {Wrapped1}; |
| 278 | MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops); |
| 279 | ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0)); |
| 280 | ASSERT_TRUE(Wrapped2->isResolved()); |
| 281 | EXPECT_FALSE(Wrapped2->isDistinct()); |
| 282 | |
| 283 | // !{!{!{}}} => !{!{}} |
| 284 | Wrapped2->replaceOperandWith(0, Empty); |
| 285 | ASSERT_EQ(Empty, Wrapped2->getOperand(0)); |
| 286 | EXPECT_TRUE(Wrapped2->isDistinct()); |
| 287 | EXPECT_FALSE(Wrapped1->isDistinct()); |
| 288 | } |
| 289 | |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 290 | TEST_F(MDNodeTest, getDistinct) { |
| 291 | // !{} |
| 292 | MDNode *Empty = MDNode::get(Context, None); |
| 293 | ASSERT_TRUE(Empty->isResolved()); |
| 294 | ASSERT_FALSE(Empty->isDistinct()); |
| 295 | ASSERT_EQ(Empty, MDNode::get(Context, None)); |
| 296 | |
| 297 | // distinct !{} |
| 298 | MDNode *Distinct1 = MDNode::getDistinct(Context, None); |
| 299 | MDNode *Distinct2 = MDNode::getDistinct(Context, None); |
| 300 | EXPECT_TRUE(Distinct1->isResolved()); |
| 301 | EXPECT_TRUE(Distinct2->isDistinct()); |
| 302 | EXPECT_NE(Empty, Distinct1); |
| 303 | EXPECT_NE(Empty, Distinct2); |
| 304 | EXPECT_NE(Distinct1, Distinct2); |
| 305 | |
| 306 | // !{} |
| 307 | ASSERT_EQ(Empty, MDNode::get(Context, None)); |
| 308 | } |
| 309 | |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 310 | TEST_F(MDNodeTest, isUniqued) { |
| 311 | MDNode *U = MDTuple::get(Context, None); |
| 312 | MDNode *D = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 313 | auto T = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 314 | EXPECT_TRUE(U->isUniqued()); |
| 315 | EXPECT_FALSE(D->isUniqued()); |
| 316 | EXPECT_FALSE(T->isUniqued()); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | TEST_F(MDNodeTest, isDistinct) { |
| 320 | MDNode *U = MDTuple::get(Context, None); |
| 321 | MDNode *D = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 322 | auto T = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 323 | EXPECT_FALSE(U->isDistinct()); |
| 324 | EXPECT_TRUE(D->isDistinct()); |
| 325 | EXPECT_FALSE(T->isDistinct()); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | TEST_F(MDNodeTest, isTemporary) { |
| 329 | MDNode *U = MDTuple::get(Context, None); |
| 330 | MDNode *D = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 331 | auto T = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 332 | EXPECT_FALSE(U->isTemporary()); |
| 333 | EXPECT_FALSE(D->isTemporary()); |
| 334 | EXPECT_TRUE(T->isTemporary()); |
Duncan P. N. Exon Smith | d1474ee | 2015-01-12 18:41:26 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 337 | TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) { |
| 338 | // temporary !{} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 339 | auto Temp = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 340 | ASSERT_FALSE(Temp->isResolved()); |
| 341 | |
| 342 | // distinct !{temporary !{}} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 343 | Metadata *Ops[] = {Temp.get()}; |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 344 | MDNode *Distinct = MDNode::getDistinct(Context, Ops); |
| 345 | EXPECT_TRUE(Distinct->isResolved()); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 346 | EXPECT_EQ(Temp.get(), Distinct->getOperand(0)); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 347 | |
| 348 | // temporary !{} => !{} |
| 349 | MDNode *Empty = MDNode::get(Context, None); |
| 350 | Temp->replaceAllUsesWith(Empty); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 351 | EXPECT_EQ(Empty, Distinct->getOperand(0)); |
| 352 | } |
| 353 | |
Duncan P. N. Exon Smith | 5f46189 | 2015-01-12 19:22:04 +0000 | [diff] [blame] | 354 | TEST_F(MDNodeTest, handleChangedOperandRecursion) { |
| 355 | // !0 = !{} |
| 356 | MDNode *N0 = MDNode::get(Context, None); |
| 357 | |
| 358 | // !1 = !{!3, null} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 359 | auto Temp3 = MDTuple::getTemporary(Context, None); |
| 360 | Metadata *Ops1[] = {Temp3.get(), nullptr}; |
Duncan P. N. Exon Smith | 5f46189 | 2015-01-12 19:22:04 +0000 | [diff] [blame] | 361 | MDNode *N1 = MDNode::get(Context, Ops1); |
| 362 | |
| 363 | // !2 = !{!3, !0} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 364 | Metadata *Ops2[] = {Temp3.get(), N0}; |
Duncan P. N. Exon Smith | 5f46189 | 2015-01-12 19:22:04 +0000 | [diff] [blame] | 365 | MDNode *N2 = MDNode::get(Context, Ops2); |
| 366 | |
| 367 | // !3 = !{!2} |
| 368 | Metadata *Ops3[] = {N2}; |
| 369 | MDNode *N3 = MDNode::get(Context, Ops3); |
| 370 | Temp3->replaceAllUsesWith(N3); |
| 371 | |
| 372 | // !4 = !{!1} |
| 373 | Metadata *Ops4[] = {N1}; |
| 374 | MDNode *N4 = MDNode::get(Context, Ops4); |
| 375 | |
| 376 | // Confirm that the cycle prevented RAUW from getting dropped. |
| 377 | EXPECT_TRUE(N0->isResolved()); |
| 378 | EXPECT_FALSE(N1->isResolved()); |
| 379 | EXPECT_FALSE(N2->isResolved()); |
| 380 | EXPECT_FALSE(N3->isResolved()); |
| 381 | EXPECT_FALSE(N4->isResolved()); |
| 382 | |
| 383 | // Create a couple of distinct nodes to observe what's going on. |
| 384 | // |
| 385 | // !5 = distinct !{!2} |
| 386 | // !6 = distinct !{!3} |
| 387 | Metadata *Ops5[] = {N2}; |
| 388 | MDNode *N5 = MDNode::getDistinct(Context, Ops5); |
| 389 | Metadata *Ops6[] = {N3}; |
| 390 | MDNode *N6 = MDNode::getDistinct(Context, Ops6); |
| 391 | |
| 392 | // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW). |
| 393 | // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2 |
| 394 | // references !3, this can cause a re-entry of handleChangedOperand() when !3 |
| 395 | // is not ready for it. |
| 396 | // |
| 397 | // !2->replaceOperandWith(1, nullptr) |
| 398 | // !2: !{!3, !0} => !{!3, null} |
| 399 | // !2->replaceAllUsesWith(!1) |
| 400 | // !3: !{!2] => !{!1} |
| 401 | // !3->replaceAllUsesWith(!4) |
| 402 | N2->replaceOperandWith(1, nullptr); |
| 403 | |
| 404 | // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from |
| 405 | // under us. Just check that the other nodes are sane. |
| 406 | // |
| 407 | // !1 = !{!4, null} |
| 408 | // !4 = !{!1} |
| 409 | // !5 = distinct !{!1} |
| 410 | // !6 = distinct !{!4} |
| 411 | EXPECT_EQ(N4, N1->getOperand(0)); |
| 412 | EXPECT_EQ(N1, N4->getOperand(0)); |
| 413 | EXPECT_EQ(N1, N5->getOperand(0)); |
| 414 | EXPECT_EQ(N4, N6->getOperand(0)); |
| 415 | } |
| 416 | |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 417 | TEST_F(MDNodeTest, replaceResolvedOperand) { |
| 418 | // Check code for replacing one resolved operand with another. If doing this |
| 419 | // directly (via replaceOperandWith()) becomes illegal, change the operand to |
| 420 | // a global value that gets RAUW'ed. |
| 421 | // |
| 422 | // Use a temporary node to keep N from being resolved. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 423 | auto Temp = MDTuple::getTemporary(Context, None); |
| 424 | Metadata *Ops[] = {nullptr, Temp.get()}; |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 425 | |
NAKAMURA Takumi | 2f8f054 | 2015-01-13 08:13:46 +0000 | [diff] [blame] | 426 | MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>()); |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 427 | MDNode *N = MDTuple::get(Context, Ops); |
| 428 | EXPECT_EQ(nullptr, N->getOperand(0)); |
| 429 | ASSERT_FALSE(N->isResolved()); |
| 430 | |
| 431 | // Check code for replacing resolved nodes. |
| 432 | N->replaceOperandWith(0, Empty); |
| 433 | EXPECT_EQ(Empty, N->getOperand(0)); |
| 434 | |
| 435 | // Check code for adding another unresolved operand. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 436 | N->replaceOperandWith(0, Temp.get()); |
| 437 | EXPECT_EQ(Temp.get(), N->getOperand(0)); |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 438 | |
| 439 | // Remove the references to Temp; required for teardown. |
| 440 | Temp->replaceAllUsesWith(nullptr); |
| 441 | } |
| 442 | |
Duncan P. N. Exon Smith | e335309 | 2015-01-19 22:24:52 +0000 | [diff] [blame] | 443 | TEST_F(MDNodeTest, replaceWithUniqued) { |
| 444 | auto *Empty = MDTuple::get(Context, None); |
| 445 | MDTuple *FirstUniqued; |
| 446 | { |
| 447 | Metadata *Ops[] = {Empty}; |
| 448 | auto Temp = MDTuple::getTemporary(Context, Ops); |
| 449 | EXPECT_TRUE(Temp->isTemporary()); |
| 450 | |
| 451 | // Don't expect a collision. |
| 452 | auto *Current = Temp.get(); |
| 453 | FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp)); |
| 454 | EXPECT_TRUE(FirstUniqued->isUniqued()); |
| 455 | EXPECT_TRUE(FirstUniqued->isResolved()); |
| 456 | EXPECT_EQ(Current, FirstUniqued); |
| 457 | } |
| 458 | { |
| 459 | Metadata *Ops[] = {Empty}; |
| 460 | auto Temp = MDTuple::getTemporary(Context, Ops); |
| 461 | EXPECT_TRUE(Temp->isTemporary()); |
| 462 | |
| 463 | // Should collide with Uniqued above this time. |
| 464 | auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp)); |
| 465 | EXPECT_TRUE(Uniqued->isUniqued()); |
| 466 | EXPECT_TRUE(Uniqued->isResolved()); |
| 467 | EXPECT_EQ(FirstUniqued, Uniqued); |
| 468 | } |
| 469 | { |
| 470 | auto Unresolved = MDTuple::getTemporary(Context, None); |
| 471 | Metadata *Ops[] = {Unresolved.get()}; |
| 472 | auto Temp = MDTuple::getTemporary(Context, Ops); |
| 473 | EXPECT_TRUE(Temp->isTemporary()); |
| 474 | |
| 475 | // Shouldn't be resolved. |
| 476 | auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp)); |
| 477 | EXPECT_TRUE(Uniqued->isUniqued()); |
| 478 | EXPECT_FALSE(Uniqued->isResolved()); |
| 479 | |
| 480 | // Should be a different node. |
| 481 | EXPECT_NE(FirstUniqued, Uniqued); |
| 482 | |
| 483 | // Should resolve when we update its node (note: be careful to avoid a |
| 484 | // collision with any other nodes above). |
| 485 | Uniqued->replaceOperandWith(0, nullptr); |
| 486 | EXPECT_TRUE(Uniqued->isResolved()); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | TEST_F(MDNodeTest, replaceWithDistinct) { |
| 491 | { |
| 492 | auto *Empty = MDTuple::get(Context, None); |
| 493 | Metadata *Ops[] = {Empty}; |
| 494 | auto Temp = MDTuple::getTemporary(Context, Ops); |
| 495 | EXPECT_TRUE(Temp->isTemporary()); |
| 496 | |
| 497 | // Don't expect a collision. |
| 498 | auto *Current = Temp.get(); |
| 499 | auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp)); |
| 500 | EXPECT_TRUE(Distinct->isDistinct()); |
| 501 | EXPECT_TRUE(Distinct->isResolved()); |
| 502 | EXPECT_EQ(Current, Distinct); |
| 503 | } |
| 504 | { |
| 505 | auto Unresolved = MDTuple::getTemporary(Context, None); |
| 506 | Metadata *Ops[] = {Unresolved.get()}; |
| 507 | auto Temp = MDTuple::getTemporary(Context, Ops); |
| 508 | EXPECT_TRUE(Temp->isTemporary()); |
| 509 | |
| 510 | // Don't expect a collision. |
| 511 | auto *Current = Temp.get(); |
| 512 | auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp)); |
| 513 | EXPECT_TRUE(Distinct->isDistinct()); |
| 514 | EXPECT_TRUE(Distinct->isResolved()); |
| 515 | EXPECT_EQ(Current, Distinct); |
| 516 | |
| 517 | // Cleanup; required for teardown. |
| 518 | Unresolved->replaceAllUsesWith(nullptr); |
| 519 | } |
| 520 | } |
| 521 | |
Duncan P. N. Exon Smith | 4ee4a98 | 2015-02-10 19:13:46 +0000 | [diff] [blame] | 522 | TEST_F(MDNodeTest, replaceWithPermanent) { |
| 523 | Metadata *Ops[] = {nullptr}; |
| 524 | auto Temp = MDTuple::getTemporary(Context, Ops); |
| 525 | auto *T = Temp.get(); |
| 526 | |
| 527 | // U is a normal, uniqued node that references T. |
| 528 | auto *U = MDTuple::get(Context, T); |
| 529 | EXPECT_TRUE(U->isUniqued()); |
| 530 | |
| 531 | // Make Temp self-referencing. |
| 532 | Temp->replaceOperandWith(0, T); |
| 533 | |
| 534 | // Try to uniquify Temp. This should, despite the name in the API, give a |
| 535 | // 'distinct' node, since self-references aren't allowed to be uniqued. |
| 536 | // |
| 537 | // Since it's distinct, N should have the same address as when it was a |
| 538 | // temporary (i.e., be equal to T not U). |
| 539 | auto *N = MDNode::replaceWithPermanent(std::move(Temp)); |
| 540 | EXPECT_EQ(N, T); |
| 541 | EXPECT_TRUE(N->isDistinct()); |
| 542 | |
| 543 | // U should be the canonical unique node with N as the argument. |
| 544 | EXPECT_EQ(U, MDTuple::get(Context, N)); |
| 545 | EXPECT_TRUE(U->isUniqued()); |
| 546 | |
| 547 | // This temporary should collide with U when replaced, but it should still be |
| 548 | // uniqued. |
| 549 | EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N))); |
| 550 | EXPECT_TRUE(U->isUniqued()); |
| 551 | |
| 552 | // This temporary should become a new uniqued node. |
| 553 | auto Temp2 = MDTuple::getTemporary(Context, U); |
| 554 | auto *V = Temp2.get(); |
| 555 | EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2))); |
| 556 | EXPECT_TRUE(V->isUniqued()); |
| 557 | EXPECT_EQ(U, V->getOperand(0)); |
| 558 | } |
| 559 | |
Duncan P. N. Exon Smith | 8d53697 | 2015-01-22 21:36:45 +0000 | [diff] [blame] | 560 | TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) { |
| 561 | TrackingMDRef Ref; |
| 562 | EXPECT_EQ(nullptr, Ref.get()); |
| 563 | { |
| 564 | auto Temp = MDTuple::getTemporary(Context, None); |
| 565 | Ref.reset(Temp.get()); |
| 566 | EXPECT_EQ(Temp.get(), Ref.get()); |
| 567 | } |
| 568 | EXPECT_EQ(nullptr, Ref.get()); |
| 569 | } |
| 570 | |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 571 | typedef MetadataTest MDLocationTest; |
| 572 | |
| 573 | TEST_F(MDLocationTest, Overflow) { |
| 574 | MDNode *N = MDNode::get(Context, None); |
| 575 | { |
| 576 | MDLocation *L = MDLocation::get(Context, 2, 7, N); |
| 577 | EXPECT_EQ(2u, L->getLine()); |
| 578 | EXPECT_EQ(7u, L->getColumn()); |
| 579 | } |
Duncan P. N. Exon Smith | 2f5bb31 | 2015-01-16 17:33:08 +0000 | [diff] [blame] | 580 | unsigned U16 = 1u << 16; |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 581 | { |
Duncan P. N. Exon Smith | af677eb | 2015-02-06 22:50:13 +0000 | [diff] [blame] | 582 | MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N); |
| 583 | EXPECT_EQ(UINT32_MAX, L->getLine()); |
Duncan P. N. Exon Smith | 2f5bb31 | 2015-01-16 17:33:08 +0000 | [diff] [blame] | 584 | EXPECT_EQ(U16 - 1, L->getColumn()); |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 585 | } |
| 586 | { |
Duncan P. N. Exon Smith | af677eb | 2015-02-06 22:50:13 +0000 | [diff] [blame] | 587 | MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N); |
| 588 | EXPECT_EQ(UINT32_MAX, L->getLine()); |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 589 | EXPECT_EQ(0u, L->getColumn()); |
| 590 | } |
| 591 | { |
Duncan P. N. Exon Smith | af677eb | 2015-02-06 22:50:13 +0000 | [diff] [blame] | 592 | MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N); |
| 593 | EXPECT_EQ(UINT32_MAX, L->getLine()); |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 594 | EXPECT_EQ(0u, L->getColumn()); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | TEST_F(MDLocationTest, getDistinct) { |
| 599 | MDNode *N = MDNode::get(Context, None); |
| 600 | MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N); |
| 601 | EXPECT_TRUE(L0->isDistinct()); |
| 602 | MDLocation *L1 = MDLocation::get(Context, 2, 7, N); |
| 603 | EXPECT_FALSE(L1->isDistinct()); |
| 604 | EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N)); |
| 605 | } |
| 606 | |
Duncan P. N. Exon Smith | 799e56a | 2015-01-19 20:37:44 +0000 | [diff] [blame] | 607 | TEST_F(MDLocationTest, getTemporary) { |
| 608 | MDNode *N = MDNode::get(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 609 | auto L = MDLocation::getTemporary(Context, 2, 7, N); |
Duncan P. N. Exon Smith | 799e56a | 2015-01-19 20:37:44 +0000 | [diff] [blame] | 610 | EXPECT_TRUE(L->isTemporary()); |
| 611 | EXPECT_FALSE(L->isResolved()); |
Duncan P. N. Exon Smith | 799e56a | 2015-01-19 20:37:44 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Duncan P. N. Exon Smith | e8b5e49 | 2015-01-22 22:47:44 +0000 | [diff] [blame] | 614 | typedef MetadataTest GenericDebugNodeTest; |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 615 | |
Duncan P. N. Exon Smith | e8b5e49 | 2015-01-22 22:47:44 +0000 | [diff] [blame] | 616 | TEST_F(GenericDebugNodeTest, get) { |
Duncan P. N. Exon Smith | 68ab023 | 2015-01-22 23:10:55 +0000 | [diff] [blame] | 617 | StringRef Header = "header"; |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 618 | auto *Empty = MDNode::get(Context, None); |
| 619 | Metadata *Ops1[] = {Empty}; |
Duncan P. N. Exon Smith | e8b5e49 | 2015-01-22 22:47:44 +0000 | [diff] [blame] | 620 | auto *N = GenericDebugNode::get(Context, 15, Header, Ops1); |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 621 | EXPECT_EQ(15u, N->getTag()); |
| 622 | EXPECT_EQ(2u, N->getNumOperands()); |
| 623 | EXPECT_EQ(Header, N->getHeader()); |
Duncan P. N. Exon Smith | 68ab023 | 2015-01-22 23:10:55 +0000 | [diff] [blame] | 624 | EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0)); |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 625 | EXPECT_EQ(1u, N->getNumDwarfOperands()); |
| 626 | EXPECT_EQ(Empty, N->getDwarfOperand(0)); |
| 627 | EXPECT_EQ(Empty, N->getOperand(1)); |
| 628 | ASSERT_TRUE(N->isUniqued()); |
| 629 | |
Duncan P. N. Exon Smith | e8b5e49 | 2015-01-22 22:47:44 +0000 | [diff] [blame] | 630 | EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1)); |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 631 | |
| 632 | N->replaceOperandWith(1, nullptr); |
| 633 | EXPECT_EQ(15u, N->getTag()); |
| 634 | EXPECT_EQ(Header, N->getHeader()); |
| 635 | EXPECT_EQ(nullptr, N->getDwarfOperand(0)); |
| 636 | ASSERT_TRUE(N->isUniqued()); |
| 637 | |
| 638 | Metadata *Ops2[] = {nullptr}; |
Duncan P. N. Exon Smith | e8b5e49 | 2015-01-22 22:47:44 +0000 | [diff] [blame] | 639 | EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2)); |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 640 | |
| 641 | N->replaceDwarfOperandWith(0, Empty); |
| 642 | EXPECT_EQ(15u, N->getTag()); |
| 643 | EXPECT_EQ(Header, N->getHeader()); |
| 644 | EXPECT_EQ(Empty, N->getDwarfOperand(0)); |
| 645 | ASSERT_TRUE(N->isUniqued()); |
Duncan P. N. Exon Smith | e8b5e49 | 2015-01-22 22:47:44 +0000 | [diff] [blame] | 646 | EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 647 | |
| 648 | TempGenericDebugNode Temp = N->clone(); |
| 649 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Duncan P. N. Exon Smith | e8b5e49 | 2015-01-22 22:47:44 +0000 | [diff] [blame] | 652 | TEST_F(GenericDebugNodeTest, getEmptyHeader) { |
Duncan P. N. Exon Smith | 2da09e4 | 2015-01-20 00:58:46 +0000 | [diff] [blame] | 653 | // Canonicalize !"" to null. |
Duncan P. N. Exon Smith | 68ab023 | 2015-01-22 23:10:55 +0000 | [diff] [blame] | 654 | auto *N = GenericDebugNode::get(Context, 15, StringRef(), None); |
| 655 | EXPECT_EQ(StringRef(), N->getHeader()); |
| 656 | EXPECT_EQ(nullptr, N->getOperand(0)); |
Duncan P. N. Exon Smith | 2da09e4 | 2015-01-20 00:58:46 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 659 | typedef MetadataTest MDSubrangeTest; |
| 660 | |
| 661 | TEST_F(MDSubrangeTest, get) { |
| 662 | auto *N = MDSubrange::get(Context, 5, 7); |
| 663 | EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag()); |
| 664 | EXPECT_EQ(5, N->getCount()); |
| 665 | EXPECT_EQ(7, N->getLo()); |
| 666 | EXPECT_EQ(N, MDSubrange::get(Context, 5, 7)); |
| 667 | EXPECT_EQ(MDSubrange::get(Context, 5, 0), MDSubrange::get(Context, 5)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 668 | |
| 669 | TempMDSubrange Temp = N->clone(); |
| 670 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | typedef MetadataTest MDEnumeratorTest; |
| 674 | |
| 675 | TEST_F(MDEnumeratorTest, get) { |
| 676 | auto *N = MDEnumerator::get(Context, 7, "name"); |
| 677 | EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag()); |
| 678 | EXPECT_EQ(7, N->getValue()); |
| 679 | EXPECT_EQ("name", N->getName()); |
| 680 | EXPECT_EQ(N, MDEnumerator::get(Context, 7, "name")); |
| 681 | |
| 682 | EXPECT_NE(N, MDEnumerator::get(Context, 8, "name")); |
| 683 | EXPECT_NE(N, MDEnumerator::get(Context, 7, "nam")); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 684 | |
| 685 | TempMDEnumerator Temp = N->clone(); |
| 686 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | typedef MetadataTest MDBasicTypeTest; |
| 690 | |
| 691 | TEST_F(MDBasicTypeTest, get) { |
| 692 | auto *N = |
| 693 | MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7); |
| 694 | EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag()); |
| 695 | EXPECT_EQ("special", N->getName()); |
| 696 | EXPECT_EQ(33u, N->getSizeInBits()); |
| 697 | EXPECT_EQ(26u, N->getAlignInBits()); |
| 698 | EXPECT_EQ(7u, N->getEncoding()); |
| 699 | EXPECT_EQ(0u, N->getLine()); |
| 700 | EXPECT_EQ(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, |
| 701 | 26, 7)); |
| 702 | |
| 703 | EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, |
| 704 | "special", 33, 26, 7)); |
| 705 | EXPECT_NE(N, |
| 706 | MDBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7)); |
| 707 | EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32, |
| 708 | 26, 7)); |
| 709 | EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, |
| 710 | 25, 7)); |
| 711 | EXPECT_NE(N, MDBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, |
| 712 | 26, 6)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 713 | |
| 714 | TempMDBasicType Temp = N->clone(); |
| 715 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | typedef MetadataTest MDDerivedTypeTest; |
| 719 | |
| 720 | TEST_F(MDDerivedTypeTest, get) { |
| 721 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 722 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 723 | Metadata *BaseType = MDTuple::getDistinct(Context, None); |
| 724 | Metadata *ExtraData = MDTuple::getDistinct(Context, None); |
| 725 | |
| 726 | auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", |
| 727 | File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData); |
| 728 | EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag()); |
| 729 | EXPECT_EQ("something", N->getName()); |
| 730 | EXPECT_EQ(File, N->getFile()); |
| 731 | EXPECT_EQ(1u, N->getLine()); |
| 732 | EXPECT_EQ(Scope, N->getScope()); |
| 733 | EXPECT_EQ(BaseType, N->getBaseType()); |
| 734 | EXPECT_EQ(2u, N->getSizeInBits()); |
| 735 | EXPECT_EQ(3u, N->getAlignInBits()); |
| 736 | EXPECT_EQ(4u, N->getOffsetInBits()); |
| 737 | EXPECT_EQ(5u, N->getFlags()); |
| 738 | EXPECT_EQ(ExtraData, N->getExtraData()); |
| 739 | EXPECT_EQ(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, |
| 740 | "something", File, 1, Scope, BaseType, 2, 3, |
| 741 | 4, 5, ExtraData)); |
| 742 | |
| 743 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_reference_type, |
| 744 | "something", File, 1, Scope, BaseType, 2, 3, |
| 745 | 4, 5, ExtraData)); |
| 746 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else", |
| 747 | File, 1, Scope, BaseType, 2, 3, 4, 5, |
| 748 | ExtraData)); |
| 749 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, |
| 750 | "something", Scope, 1, Scope, BaseType, 2, 3, |
| 751 | 4, 5, ExtraData)); |
| 752 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, |
| 753 | "something", File, 2, Scope, BaseType, 2, 3, |
| 754 | 4, 5, ExtraData)); |
| 755 | EXPECT_NE(N, |
| 756 | MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", |
| 757 | File, 1, File, BaseType, 2, 3, 4, 5, ExtraData)); |
| 758 | EXPECT_NE(N, |
| 759 | MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", |
| 760 | File, 1, Scope, File, 2, 3, 4, 5, ExtraData)); |
| 761 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, |
| 762 | "something", File, 1, Scope, BaseType, 3, 3, |
| 763 | 4, 5, ExtraData)); |
| 764 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, |
| 765 | "something", File, 1, Scope, BaseType, 2, 2, |
| 766 | 4, 5, ExtraData)); |
| 767 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, |
| 768 | "something", File, 1, Scope, BaseType, 2, 3, |
| 769 | 5, 5, ExtraData)); |
| 770 | EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, |
| 771 | "something", File, 1, Scope, BaseType, 2, 3, |
| 772 | 4, 4, ExtraData)); |
| 773 | EXPECT_NE(N, |
| 774 | MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", |
| 775 | File, 1, Scope, BaseType, 2, 3, 4, 5, File)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 776 | |
| 777 | TempMDDerivedType Temp = N->clone(); |
| 778 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | typedef MetadataTest MDCompositeTypeTest; |
| 782 | |
| 783 | TEST_F(MDCompositeTypeTest, get) { |
| 784 | unsigned Tag = dwarf::DW_TAG_structure_type; |
| 785 | StringRef Name = "some name"; |
| 786 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 787 | unsigned Line = 1; |
| 788 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 789 | Metadata *BaseType = MDTuple::getDistinct(Context, None); |
| 790 | unsigned SizeInBits = 2; |
| 791 | unsigned AlignInBits = 3; |
| 792 | unsigned OffsetInBits = 4; |
| 793 | unsigned Flags = 5; |
| 794 | Metadata *Elements = MDTuple::getDistinct(Context, None); |
| 795 | unsigned RuntimeLang = 6; |
| 796 | Metadata *VTableHolder = MDTuple::getDistinct(Context, None); |
| 797 | Metadata *TemplateParams = MDTuple::getDistinct(Context, None); |
| 798 | StringRef Identifier = "some id"; |
| 799 | |
| 800 | auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 801 | BaseType, SizeInBits, AlignInBits, |
| 802 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 803 | VTableHolder, TemplateParams, Identifier); |
| 804 | EXPECT_EQ(Tag, N->getTag()); |
| 805 | EXPECT_EQ(Name, N->getName()); |
| 806 | EXPECT_EQ(File, N->getFile()); |
| 807 | EXPECT_EQ(Line, N->getLine()); |
| 808 | EXPECT_EQ(Scope, N->getScope()); |
| 809 | EXPECT_EQ(BaseType, N->getBaseType()); |
| 810 | EXPECT_EQ(SizeInBits, N->getSizeInBits()); |
| 811 | EXPECT_EQ(AlignInBits, N->getAlignInBits()); |
| 812 | EXPECT_EQ(OffsetInBits, N->getOffsetInBits()); |
| 813 | EXPECT_EQ(Flags, N->getFlags()); |
| 814 | EXPECT_EQ(Elements, N->getElements()); |
| 815 | EXPECT_EQ(RuntimeLang, N->getRuntimeLang()); |
| 816 | EXPECT_EQ(VTableHolder, N->getVTableHolder()); |
| 817 | EXPECT_EQ(TemplateParams, N->getTemplateParams()); |
| 818 | EXPECT_EQ(Identifier, N->getIdentifier()); |
| 819 | |
| 820 | EXPECT_EQ(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 821 | BaseType, SizeInBits, AlignInBits, |
| 822 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 823 | VTableHolder, TemplateParams, Identifier)); |
| 824 | |
| 825 | EXPECT_NE(N, MDCompositeType::get(Context, Tag + 1, Name, File, Line, Scope, |
| 826 | BaseType, SizeInBits, AlignInBits, |
| 827 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 828 | VTableHolder, TemplateParams, Identifier)); |
| 829 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, "abc", File, Line, Scope, |
| 830 | BaseType, SizeInBits, AlignInBits, |
| 831 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 832 | VTableHolder, TemplateParams, Identifier)); |
| 833 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, Scope, Line, Scope, |
| 834 | BaseType, SizeInBits, AlignInBits, |
| 835 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 836 | VTableHolder, TemplateParams, Identifier)); |
| 837 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line + 1, Scope, |
| 838 | BaseType, SizeInBits, AlignInBits, |
| 839 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 840 | VTableHolder, TemplateParams, Identifier)); |
| 841 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, File, |
| 842 | BaseType, SizeInBits, AlignInBits, |
| 843 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 844 | VTableHolder, TemplateParams, Identifier)); |
| 845 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File, |
| 846 | SizeInBits, AlignInBits, OffsetInBits, |
| 847 | Flags, Elements, RuntimeLang, VTableHolder, |
| 848 | TemplateParams, Identifier)); |
| 849 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 850 | BaseType, SizeInBits + 1, AlignInBits, |
| 851 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 852 | VTableHolder, TemplateParams, Identifier)); |
| 853 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 854 | BaseType, SizeInBits, AlignInBits + 1, |
| 855 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 856 | VTableHolder, TemplateParams, Identifier)); |
| 857 | EXPECT_NE(N, MDCompositeType::get( |
| 858 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 859 | AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang, |
| 860 | VTableHolder, TemplateParams, Identifier)); |
| 861 | EXPECT_NE(N, MDCompositeType::get( |
| 862 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 863 | AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang, |
| 864 | VTableHolder, TemplateParams, Identifier)); |
| 865 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 866 | BaseType, SizeInBits, AlignInBits, |
| 867 | OffsetInBits, Flags, File, RuntimeLang, |
| 868 | VTableHolder, TemplateParams, Identifier)); |
| 869 | EXPECT_NE(N, MDCompositeType::get( |
| 870 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 871 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1, |
| 872 | VTableHolder, TemplateParams, Identifier)); |
| 873 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 874 | BaseType, SizeInBits, AlignInBits, |
| 875 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 876 | File, TemplateParams, Identifier)); |
| 877 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 878 | BaseType, SizeInBits, AlignInBits, |
| 879 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 880 | VTableHolder, File, Identifier)); |
| 881 | EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, |
| 882 | BaseType, SizeInBits, AlignInBits, |
| 883 | OffsetInBits, Flags, Elements, RuntimeLang, |
| 884 | VTableHolder, TemplateParams, "other")); |
| 885 | |
| 886 | // Be sure that missing identifiers get null pointers. |
| 887 | EXPECT_FALSE(MDCompositeType::get( |
| 888 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 889 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
| 890 | VTableHolder, TemplateParams, "")->getRawIdentifier()); |
| 891 | EXPECT_FALSE(MDCompositeType::get( |
| 892 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 893 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
| 894 | VTableHolder, TemplateParams)->getRawIdentifier()); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 895 | |
| 896 | TempMDCompositeType Temp = N->clone(); |
| 897 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | typedef MetadataTest MDSubroutineTypeTest; |
| 901 | |
| 902 | TEST_F(MDSubroutineTypeTest, get) { |
| 903 | unsigned Flags = 1; |
| 904 | Metadata *TypeArray = MDTuple::getDistinct(Context, None); |
| 905 | |
| 906 | auto *N = MDSubroutineType::get(Context, Flags, TypeArray); |
| 907 | EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag()); |
| 908 | EXPECT_EQ(Flags, N->getFlags()); |
| 909 | EXPECT_EQ(TypeArray, N->getTypeArray()); |
| 910 | EXPECT_EQ(N, MDSubroutineType::get(Context, Flags, TypeArray)); |
| 911 | |
| 912 | EXPECT_NE(N, MDSubroutineType::get(Context, Flags + 1, TypeArray)); |
| 913 | EXPECT_NE(N, MDSubroutineType::get(Context, Flags, |
| 914 | MDTuple::getDistinct(Context, None))); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 915 | |
| 916 | TempMDSubroutineType Temp = N->clone(); |
| 917 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | typedef MetadataTest MDFileTest; |
| 921 | |
| 922 | TEST_F(MDFileTest, get) { |
| 923 | StringRef Filename = "file"; |
| 924 | StringRef Directory = "dir"; |
| 925 | auto *N = MDFile::get(Context, Filename, Directory); |
| 926 | |
| 927 | EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag()); |
| 928 | EXPECT_EQ(Filename, N->getFilename()); |
| 929 | EXPECT_EQ(Directory, N->getDirectory()); |
| 930 | EXPECT_EQ(N, MDFile::get(Context, Filename, Directory)); |
| 931 | |
| 932 | EXPECT_NE(N, MDFile::get(Context, "other", Directory)); |
| 933 | EXPECT_NE(N, MDFile::get(Context, Filename, "other")); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 934 | |
| 935 | TempMDFile Temp = N->clone(); |
| 936 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | typedef MetadataTest MDCompileUnitTest; |
| 940 | |
| 941 | TEST_F(MDCompileUnitTest, get) { |
| 942 | unsigned SourceLanguage = 1; |
| 943 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 944 | StringRef Producer = "some producer"; |
| 945 | bool IsOptimized = false; |
| 946 | StringRef Flags = "flag after flag"; |
| 947 | unsigned RuntimeVersion = 2; |
| 948 | StringRef SplitDebugFilename = "another/file"; |
| 949 | unsigned EmissionKind = 3; |
| 950 | Metadata *EnumTypes = MDTuple::getDistinct(Context, None); |
| 951 | Metadata *RetainedTypes = MDTuple::getDistinct(Context, None); |
| 952 | Metadata *Subprograms = MDTuple::getDistinct(Context, None); |
| 953 | Metadata *GlobalVariables = MDTuple::getDistinct(Context, None); |
| 954 | Metadata *ImportedEntities = MDTuple::getDistinct(Context, None); |
| 955 | auto *N = MDCompileUnit::get( |
| 956 | Context, SourceLanguage, File, Producer, IsOptimized, Flags, |
| 957 | RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, |
| 958 | RetainedTypes, Subprograms, GlobalVariables, ImportedEntities); |
| 959 | |
| 960 | EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag()); |
| 961 | EXPECT_EQ(SourceLanguage, N->getSourceLanguage()); |
| 962 | EXPECT_EQ(File, N->getFile()); |
| 963 | EXPECT_EQ(Producer, N->getProducer()); |
| 964 | EXPECT_EQ(IsOptimized, N->isOptimized()); |
| 965 | EXPECT_EQ(Flags, N->getFlags()); |
| 966 | EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion()); |
| 967 | EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename()); |
| 968 | EXPECT_EQ(EmissionKind, N->getEmissionKind()); |
| 969 | EXPECT_EQ(EnumTypes, N->getEnumTypes()); |
| 970 | EXPECT_EQ(RetainedTypes, N->getRetainedTypes()); |
| 971 | EXPECT_EQ(Subprograms, N->getSubprograms()); |
| 972 | EXPECT_EQ(GlobalVariables, N->getGlobalVariables()); |
| 973 | EXPECT_EQ(ImportedEntities, N->getImportedEntities()); |
| 974 | EXPECT_EQ(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, |
| 975 | IsOptimized, Flags, RuntimeVersion, |
| 976 | SplitDebugFilename, EmissionKind, EnumTypes, |
| 977 | RetainedTypes, Subprograms, GlobalVariables, |
| 978 | ImportedEntities)); |
| 979 | |
| 980 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage + 1, File, Producer, |
| 981 | IsOptimized, Flags, RuntimeVersion, |
| 982 | SplitDebugFilename, EmissionKind, EnumTypes, |
| 983 | RetainedTypes, Subprograms, GlobalVariables, |
| 984 | ImportedEntities)); |
| 985 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, EnumTypes, Producer, |
| 986 | IsOptimized, Flags, RuntimeVersion, |
| 987 | SplitDebugFilename, EmissionKind, EnumTypes, |
| 988 | RetainedTypes, Subprograms, GlobalVariables, |
| 989 | ImportedEntities)); |
| 990 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, "other", |
| 991 | IsOptimized, Flags, RuntimeVersion, |
| 992 | SplitDebugFilename, EmissionKind, EnumTypes, |
| 993 | RetainedTypes, Subprograms, GlobalVariables, |
| 994 | ImportedEntities)); |
| 995 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, |
| 996 | !IsOptimized, Flags, RuntimeVersion, |
| 997 | SplitDebugFilename, EmissionKind, EnumTypes, |
| 998 | RetainedTypes, Subprograms, GlobalVariables, |
| 999 | ImportedEntities)); |
| 1000 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, |
| 1001 | IsOptimized, "other", RuntimeVersion, |
| 1002 | SplitDebugFilename, EmissionKind, EnumTypes, |
| 1003 | RetainedTypes, Subprograms, GlobalVariables, |
| 1004 | ImportedEntities)); |
| 1005 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, |
| 1006 | IsOptimized, Flags, RuntimeVersion + 1, |
| 1007 | SplitDebugFilename, EmissionKind, EnumTypes, |
| 1008 | RetainedTypes, Subprograms, GlobalVariables, |
| 1009 | ImportedEntities)); |
| 1010 | EXPECT_NE(N, |
| 1011 | MDCompileUnit::get(Context, SourceLanguage, File, Producer, |
| 1012 | IsOptimized, Flags, RuntimeVersion, "other", |
| 1013 | EmissionKind, EnumTypes, RetainedTypes, |
| 1014 | Subprograms, GlobalVariables, ImportedEntities)); |
| 1015 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, |
| 1016 | IsOptimized, Flags, RuntimeVersion, |
| 1017 | SplitDebugFilename, EmissionKind + 1, |
| 1018 | EnumTypes, RetainedTypes, Subprograms, |
| 1019 | GlobalVariables, ImportedEntities)); |
| 1020 | EXPECT_NE(N, MDCompileUnit::get(Context, SourceLanguage, File, Producer, |
| 1021 | IsOptimized, Flags, RuntimeVersion, |
| 1022 | SplitDebugFilename, EmissionKind, File, |
| 1023 | RetainedTypes, Subprograms, GlobalVariables, |
| 1024 | ImportedEntities)); |
| 1025 | EXPECT_NE(N, MDCompileUnit::get( |
| 1026 | Context, SourceLanguage, File, Producer, IsOptimized, Flags, |
| 1027 | RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, |
| 1028 | File, Subprograms, GlobalVariables, ImportedEntities)); |
| 1029 | EXPECT_NE(N, MDCompileUnit::get( |
| 1030 | Context, SourceLanguage, File, Producer, IsOptimized, Flags, |
| 1031 | RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, |
| 1032 | RetainedTypes, File, GlobalVariables, ImportedEntities)); |
| 1033 | EXPECT_NE(N, MDCompileUnit::get( |
| 1034 | Context, SourceLanguage, File, Producer, IsOptimized, Flags, |
| 1035 | RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, |
| 1036 | RetainedTypes, Subprograms, File, ImportedEntities)); |
| 1037 | EXPECT_NE(N, MDCompileUnit::get( |
| 1038 | Context, SourceLanguage, File, Producer, IsOptimized, Flags, |
| 1039 | RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, |
| 1040 | RetainedTypes, Subprograms, GlobalVariables, File)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1041 | |
| 1042 | TempMDCompileUnit Temp = N->clone(); |
| 1043 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Duncan P. N. Exon Smith | 94bbbf0 | 2015-02-18 20:36:09 +0000 | [diff] [blame^] | 1046 | TEST_F(MDCompileUnitTest, replaceArrays) { |
| 1047 | unsigned SourceLanguage = 1; |
| 1048 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1049 | StringRef Producer = "some producer"; |
| 1050 | bool IsOptimized = false; |
| 1051 | StringRef Flags = "flag after flag"; |
| 1052 | unsigned RuntimeVersion = 2; |
| 1053 | StringRef SplitDebugFilename = "another/file"; |
| 1054 | unsigned EmissionKind = 3; |
| 1055 | Metadata *EnumTypes = MDTuple::getDistinct(Context, None); |
| 1056 | Metadata *RetainedTypes = MDTuple::getDistinct(Context, None); |
| 1057 | Metadata *ImportedEntities = MDTuple::getDistinct(Context, None); |
| 1058 | auto *N = MDCompileUnit::get( |
| 1059 | Context, SourceLanguage, File, Producer, IsOptimized, Flags, |
| 1060 | RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes, |
| 1061 | RetainedTypes, nullptr, nullptr, ImportedEntities); |
| 1062 | |
| 1063 | auto *Subprograms = MDTuple::getDistinct(Context, None); |
| 1064 | EXPECT_EQ(nullptr, N->getSubprograms()); |
| 1065 | N->replaceSubprograms(Subprograms); |
| 1066 | EXPECT_EQ(Subprograms, N->getSubprograms()); |
| 1067 | N->replaceSubprograms(nullptr); |
| 1068 | EXPECT_EQ(nullptr, N->getSubprograms()); |
| 1069 | |
| 1070 | auto *GlobalVariables = MDTuple::getDistinct(Context, None); |
| 1071 | EXPECT_EQ(nullptr, N->getGlobalVariables()); |
| 1072 | N->replaceGlobalVariables(GlobalVariables); |
| 1073 | EXPECT_EQ(GlobalVariables, N->getGlobalVariables()); |
| 1074 | N->replaceGlobalVariables(nullptr); |
| 1075 | EXPECT_EQ(nullptr, N->getGlobalVariables()); |
| 1076 | } |
| 1077 | |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1078 | typedef MetadataTest MDSubprogramTest; |
| 1079 | |
| 1080 | TEST_F(MDSubprogramTest, get) { |
| 1081 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1082 | StringRef Name = "name"; |
| 1083 | StringRef LinkageName = "linkage"; |
| 1084 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1085 | unsigned Line = 2; |
| 1086 | Metadata *Type = MDTuple::getDistinct(Context, None); |
| 1087 | bool IsLocalToUnit = false; |
| 1088 | bool IsDefinition = true; |
| 1089 | unsigned ScopeLine = 3; |
| 1090 | Metadata *ContainingType = MDTuple::getDistinct(Context, None); |
| 1091 | unsigned Virtuality = 4; |
| 1092 | unsigned VirtualIndex = 5; |
| 1093 | unsigned Flags = 6; |
| 1094 | bool IsOptimized = false; |
| 1095 | Metadata *Function = MDTuple::getDistinct(Context, None); |
| 1096 | Metadata *TemplateParams = MDTuple::getDistinct(Context, None); |
| 1097 | Metadata *Declaration = MDTuple::getDistinct(Context, None); |
| 1098 | Metadata *Variables = MDTuple::getDistinct(Context, None); |
| 1099 | |
| 1100 | auto *N = MDSubprogram::get( |
| 1101 | Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, |
| 1102 | IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, |
| 1103 | IsOptimized, Function, TemplateParams, Declaration, Variables); |
| 1104 | |
| 1105 | EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag()); |
| 1106 | EXPECT_EQ(Scope, N->getScope()); |
| 1107 | EXPECT_EQ(Name, N->getName()); |
| 1108 | EXPECT_EQ(LinkageName, N->getLinkageName()); |
| 1109 | EXPECT_EQ(File, N->getFile()); |
| 1110 | EXPECT_EQ(Line, N->getLine()); |
| 1111 | EXPECT_EQ(Type, N->getType()); |
| 1112 | EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit()); |
| 1113 | EXPECT_EQ(IsDefinition, N->isDefinition()); |
| 1114 | EXPECT_EQ(ScopeLine, N->getScopeLine()); |
| 1115 | EXPECT_EQ(ContainingType, N->getContainingType()); |
| 1116 | EXPECT_EQ(Virtuality, N->getVirtuality()); |
| 1117 | EXPECT_EQ(VirtualIndex, N->getVirtualIndex()); |
| 1118 | EXPECT_EQ(Flags, N->getFlags()); |
| 1119 | EXPECT_EQ(IsOptimized, N->isOptimized()); |
| 1120 | EXPECT_EQ(Function, N->getFunction()); |
| 1121 | EXPECT_EQ(TemplateParams, N->getTemplateParams()); |
| 1122 | EXPECT_EQ(Declaration, N->getDeclaration()); |
| 1123 | EXPECT_EQ(Variables, N->getVariables()); |
| 1124 | EXPECT_EQ(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1125 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1126 | ContainingType, Virtuality, VirtualIndex, |
| 1127 | Flags, IsOptimized, Function, TemplateParams, |
| 1128 | Declaration, Variables)); |
| 1129 | |
| 1130 | EXPECT_NE(N, MDSubprogram::get(Context, File, Name, LinkageName, File, Line, |
| 1131 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1132 | ContainingType, Virtuality, VirtualIndex, |
| 1133 | Flags, IsOptimized, Function, TemplateParams, |
| 1134 | Declaration, Variables)); |
| 1135 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, "other", LinkageName, File, |
| 1136 | Line, Type, IsLocalToUnit, IsDefinition, |
| 1137 | ScopeLine, ContainingType, Virtuality, |
| 1138 | VirtualIndex, Flags, IsOptimized, Function, |
| 1139 | TemplateParams, Declaration, Variables)); |
| 1140 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, "other", File, Line, |
| 1141 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1142 | ContainingType, Virtuality, VirtualIndex, |
| 1143 | Flags, IsOptimized, Function, TemplateParams, |
| 1144 | Declaration, Variables)); |
| 1145 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, Scope, Line, |
| 1146 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1147 | ContainingType, Virtuality, VirtualIndex, |
| 1148 | Flags, IsOptimized, Function, TemplateParams, |
| 1149 | Declaration, Variables)); |
| 1150 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, |
| 1151 | Line + 1, Type, IsLocalToUnit, IsDefinition, |
| 1152 | ScopeLine, ContainingType, Virtuality, |
| 1153 | VirtualIndex, Flags, IsOptimized, Function, |
| 1154 | TemplateParams, Declaration, Variables)); |
| 1155 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1156 | Scope, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1157 | ContainingType, Virtuality, VirtualIndex, |
| 1158 | Flags, IsOptimized, Function, TemplateParams, |
| 1159 | Declaration, Variables)); |
| 1160 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1161 | Type, !IsLocalToUnit, IsDefinition, ScopeLine, |
| 1162 | ContainingType, Virtuality, VirtualIndex, |
| 1163 | Flags, IsOptimized, Function, TemplateParams, |
| 1164 | Declaration, Variables)); |
| 1165 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1166 | Type, IsLocalToUnit, !IsDefinition, ScopeLine, |
| 1167 | ContainingType, Virtuality, VirtualIndex, |
| 1168 | Flags, IsOptimized, Function, TemplateParams, |
| 1169 | Declaration, Variables)); |
| 1170 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1171 | Type, IsLocalToUnit, IsDefinition, |
| 1172 | ScopeLine + 1, ContainingType, Virtuality, |
| 1173 | VirtualIndex, Flags, IsOptimized, Function, |
| 1174 | TemplateParams, Declaration, Variables)); |
| 1175 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1176 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1177 | Type, Virtuality, VirtualIndex, Flags, |
| 1178 | IsOptimized, Function, TemplateParams, |
| 1179 | Declaration, Variables)); |
| 1180 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1181 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1182 | ContainingType, Virtuality + 1, VirtualIndex, |
| 1183 | Flags, IsOptimized, Function, TemplateParams, |
| 1184 | Declaration, Variables)); |
| 1185 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1186 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1187 | ContainingType, Virtuality, VirtualIndex + 1, |
| 1188 | Flags, IsOptimized, Function, TemplateParams, |
| 1189 | Declaration, Variables)); |
| 1190 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1191 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1192 | ContainingType, Virtuality, VirtualIndex, |
| 1193 | ~Flags, IsOptimized, Function, TemplateParams, |
| 1194 | Declaration, Variables)); |
| 1195 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1196 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1197 | ContainingType, Virtuality, VirtualIndex, |
| 1198 | Flags, !IsOptimized, Function, TemplateParams, |
| 1199 | Declaration, Variables)); |
| 1200 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1201 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1202 | ContainingType, Virtuality, VirtualIndex, |
| 1203 | Flags, IsOptimized, Type, TemplateParams, |
| 1204 | Declaration, Variables)); |
| 1205 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1206 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1207 | ContainingType, Virtuality, VirtualIndex, |
| 1208 | Flags, IsOptimized, Function, Type, |
| 1209 | Declaration, Variables)); |
| 1210 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1211 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1212 | ContainingType, Virtuality, VirtualIndex, |
| 1213 | Flags, IsOptimized, Function, TemplateParams, |
| 1214 | Type, Variables)); |
| 1215 | EXPECT_NE(N, MDSubprogram::get(Context, Scope, Name, LinkageName, File, Line, |
| 1216 | Type, IsLocalToUnit, IsDefinition, ScopeLine, |
| 1217 | ContainingType, Virtuality, VirtualIndex, |
| 1218 | Flags, IsOptimized, Function, TemplateParams, |
| 1219 | Declaration, Type)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1220 | |
| 1221 | TempMDSubprogram Temp = N->clone(); |
| 1222 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Duncan P. N. Exon Smith | df52349 | 2015-02-18 20:32:57 +0000 | [diff] [blame] | 1225 | TEST_F(MDSubprogramTest, replaceFunction) { |
| 1226 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1227 | StringRef Name = "name"; |
| 1228 | StringRef LinkageName = "linkage"; |
| 1229 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1230 | unsigned Line = 2; |
| 1231 | Metadata *Type = MDTuple::getDistinct(Context, None); |
| 1232 | bool IsLocalToUnit = false; |
| 1233 | bool IsDefinition = true; |
| 1234 | unsigned ScopeLine = 3; |
| 1235 | Metadata *ContainingType = MDTuple::getDistinct(Context, None); |
| 1236 | unsigned Virtuality = 4; |
| 1237 | unsigned VirtualIndex = 5; |
| 1238 | unsigned Flags = 6; |
| 1239 | bool IsOptimized = false; |
| 1240 | Metadata *TemplateParams = MDTuple::getDistinct(Context, None); |
| 1241 | Metadata *Declaration = MDTuple::getDistinct(Context, None); |
| 1242 | Metadata *Variables = MDTuple::getDistinct(Context, None); |
| 1243 | |
| 1244 | auto *N = MDSubprogram::get( |
| 1245 | Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, |
| 1246 | IsDefinition, ScopeLine, ContainingType, Virtuality, VirtualIndex, Flags, |
| 1247 | IsOptimized, nullptr, TemplateParams, Declaration, Variables); |
| 1248 | |
| 1249 | EXPECT_EQ(nullptr, N->getFunction()); |
| 1250 | |
| 1251 | std::unique_ptr<Function> F( |
| 1252 | Function::Create(FunctionType::get(Type::getVoidTy(Context), false), |
| 1253 | GlobalValue::ExternalLinkage)); |
| 1254 | N->replaceFunction(F.get()); |
| 1255 | EXPECT_EQ(ConstantAsMetadata::get(F.get()), N->getFunction()); |
| 1256 | |
| 1257 | N->replaceFunction(nullptr); |
| 1258 | EXPECT_EQ(nullptr, N->getFunction()); |
| 1259 | } |
| 1260 | |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1261 | typedef MetadataTest MDLexicalBlockTest; |
| 1262 | |
| 1263 | TEST_F(MDLexicalBlockTest, get) { |
| 1264 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1265 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1266 | unsigned Line = 5; |
| 1267 | unsigned Column = 8; |
| 1268 | |
| 1269 | auto *N = MDLexicalBlock::get(Context, Scope, File, Line, Column); |
| 1270 | |
| 1271 | EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag()); |
| 1272 | EXPECT_EQ(Scope, N->getScope()); |
| 1273 | EXPECT_EQ(File, N->getFile()); |
| 1274 | EXPECT_EQ(Line, N->getLine()); |
| 1275 | EXPECT_EQ(Column, N->getColumn()); |
| 1276 | EXPECT_EQ(N, MDLexicalBlock::get(Context, Scope, File, Line, Column)); |
| 1277 | |
| 1278 | EXPECT_NE(N, MDLexicalBlock::get(Context, File, File, Line, Column)); |
| 1279 | EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, Scope, Line, Column)); |
| 1280 | EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line + 1, Column)); |
| 1281 | EXPECT_NE(N, MDLexicalBlock::get(Context, Scope, File, Line, Column + 1)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1282 | |
| 1283 | TempMDLexicalBlock Temp = N->clone(); |
| 1284 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | typedef MetadataTest MDLexicalBlockFileTest; |
| 1288 | |
| 1289 | TEST_F(MDLexicalBlockFileTest, get) { |
| 1290 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1291 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1292 | unsigned Discriminator = 5; |
| 1293 | |
| 1294 | auto *N = MDLexicalBlockFile::get(Context, Scope, File, Discriminator); |
| 1295 | |
| 1296 | EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag()); |
| 1297 | EXPECT_EQ(Scope, N->getScope()); |
| 1298 | EXPECT_EQ(File, N->getFile()); |
| 1299 | EXPECT_EQ(Discriminator, N->getDiscriminator()); |
| 1300 | EXPECT_EQ(N, MDLexicalBlockFile::get(Context, Scope, File, Discriminator)); |
| 1301 | |
| 1302 | EXPECT_NE(N, MDLexicalBlockFile::get(Context, File, File, Discriminator)); |
| 1303 | EXPECT_NE(N, MDLexicalBlockFile::get(Context, Scope, Scope, Discriminator)); |
| 1304 | EXPECT_NE(N, |
| 1305 | MDLexicalBlockFile::get(Context, Scope, File, Discriminator + 1)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1306 | |
| 1307 | TempMDLexicalBlockFile Temp = N->clone(); |
| 1308 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | typedef MetadataTest MDNamespaceTest; |
| 1312 | |
| 1313 | TEST_F(MDNamespaceTest, get) { |
| 1314 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1315 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1316 | StringRef Name = "namespace"; |
| 1317 | unsigned Line = 5; |
| 1318 | |
| 1319 | auto *N = MDNamespace::get(Context, Scope, File, Name, Line); |
| 1320 | |
| 1321 | EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag()); |
| 1322 | EXPECT_EQ(Scope, N->getScope()); |
| 1323 | EXPECT_EQ(File, N->getFile()); |
| 1324 | EXPECT_EQ(Name, N->getName()); |
| 1325 | EXPECT_EQ(Line, N->getLine()); |
| 1326 | EXPECT_EQ(N, MDNamespace::get(Context, Scope, File, Name, Line)); |
| 1327 | |
| 1328 | EXPECT_NE(N, MDNamespace::get(Context, File, File, Name, Line)); |
| 1329 | EXPECT_NE(N, MDNamespace::get(Context, Scope, Scope, Name, Line)); |
| 1330 | EXPECT_NE(N, MDNamespace::get(Context, Scope, File, "other", Line)); |
| 1331 | EXPECT_NE(N, MDNamespace::get(Context, Scope, File, Name, Line + 1)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1332 | |
| 1333 | TempMDNamespace Temp = N->clone(); |
| 1334 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | typedef MetadataTest MDTemplateTypeParameterTest; |
| 1338 | |
| 1339 | TEST_F(MDTemplateTypeParameterTest, get) { |
| 1340 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1341 | StringRef Name = "template"; |
| 1342 | Metadata *Type = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1343 | |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1344 | auto *N = MDTemplateTypeParameter::get(Context, Scope, Name, Type); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1345 | |
| 1346 | EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag()); |
| 1347 | EXPECT_EQ(Scope, N->getScope()); |
| 1348 | EXPECT_EQ(Name, N->getName()); |
| 1349 | EXPECT_EQ(Type, N->getType()); |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1350 | EXPECT_EQ(N, MDTemplateTypeParameter::get(Context, Scope, Name, Type)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1351 | |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1352 | EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Type, Name, Type)); |
| 1353 | EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, "other", Type)); |
| 1354 | EXPECT_NE(N, MDTemplateTypeParameter::get(Context, Scope, Name, Scope)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1355 | |
| 1356 | TempMDTemplateTypeParameter Temp = N->clone(); |
| 1357 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | typedef MetadataTest MDTemplateValueParameterTest; |
| 1361 | |
| 1362 | TEST_F(MDTemplateValueParameterTest, get) { |
| 1363 | unsigned Tag = dwarf::DW_TAG_template_value_parameter; |
| 1364 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1365 | StringRef Name = "template"; |
| 1366 | Metadata *Type = MDTuple::getDistinct(Context, None); |
| 1367 | Metadata *Value = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1368 | |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1369 | auto *N = |
| 1370 | MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1371 | EXPECT_EQ(Tag, N->getTag()); |
| 1372 | EXPECT_EQ(Scope, N->getScope()); |
| 1373 | EXPECT_EQ(Name, N->getName()); |
| 1374 | EXPECT_EQ(Type, N->getType()); |
| 1375 | EXPECT_EQ(Value, N->getValue()); |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1376 | EXPECT_EQ( |
| 1377 | N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Value)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1378 | |
| 1379 | EXPECT_NE(N, MDTemplateValueParameter::get( |
| 1380 | Context, dwarf::DW_TAG_GNU_template_template_param, Scope, |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1381 | Name, Type, Value)); |
| 1382 | EXPECT_NE( |
| 1383 | N, MDTemplateValueParameter::get(Context, Tag, Type, Name, Type, Value)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1384 | EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, "other", Type, |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1385 | Value)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1386 | EXPECT_NE(N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Scope, |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 1387 | Value)); |
| 1388 | EXPECT_NE( |
| 1389 | N, MDTemplateValueParameter::get(Context, Tag, Scope, Name, Type, Scope)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1390 | |
| 1391 | TempMDTemplateValueParameter Temp = N->clone(); |
| 1392 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | typedef MetadataTest MDGlobalVariableTest; |
| 1396 | |
| 1397 | TEST_F(MDGlobalVariableTest, get) { |
| 1398 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1399 | StringRef Name = "name"; |
| 1400 | StringRef LinkageName = "linkage"; |
| 1401 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1402 | unsigned Line = 5; |
| 1403 | Metadata *Type = MDTuple::getDistinct(Context, None); |
| 1404 | bool IsLocalToUnit = false; |
| 1405 | bool IsDefinition = true; |
| 1406 | Metadata *Variable = MDTuple::getDistinct(Context, None); |
| 1407 | Metadata *StaticDataMemberDeclaration = MDTuple::getDistinct(Context, None); |
| 1408 | |
| 1409 | auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line, |
| 1410 | Type, IsLocalToUnit, IsDefinition, Variable, |
| 1411 | StaticDataMemberDeclaration); |
| 1412 | EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag()); |
| 1413 | EXPECT_EQ(Scope, N->getScope()); |
| 1414 | EXPECT_EQ(Name, N->getName()); |
| 1415 | EXPECT_EQ(LinkageName, N->getLinkageName()); |
| 1416 | EXPECT_EQ(File, N->getFile()); |
| 1417 | EXPECT_EQ(Line, N->getLine()); |
| 1418 | EXPECT_EQ(Type, N->getType()); |
| 1419 | EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit()); |
| 1420 | EXPECT_EQ(IsDefinition, N->isDefinition()); |
| 1421 | EXPECT_EQ(Variable, N->getVariable()); |
| 1422 | EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration()); |
| 1423 | EXPECT_EQ(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, |
| 1424 | Line, Type, IsLocalToUnit, IsDefinition, |
| 1425 | Variable, StaticDataMemberDeclaration)); |
| 1426 | |
| 1427 | EXPECT_NE(N, MDGlobalVariable::get(Context, File, Name, LinkageName, File, |
| 1428 | Line, Type, IsLocalToUnit, IsDefinition, |
| 1429 | Variable, StaticDataMemberDeclaration)); |
| 1430 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, "other", LinkageName, File, |
| 1431 | Line, Type, IsLocalToUnit, IsDefinition, |
| 1432 | Variable, StaticDataMemberDeclaration)); |
| 1433 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, "other", File, Line, |
| 1434 | Type, IsLocalToUnit, IsDefinition, |
| 1435 | Variable, StaticDataMemberDeclaration)); |
| 1436 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, Scope, |
| 1437 | Line, Type, IsLocalToUnit, IsDefinition, |
| 1438 | Variable, StaticDataMemberDeclaration)); |
| 1439 | EXPECT_NE(N, |
| 1440 | MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, |
| 1441 | Line + 1, Type, IsLocalToUnit, IsDefinition, |
| 1442 | Variable, StaticDataMemberDeclaration)); |
| 1443 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, |
| 1444 | Line, Scope, IsLocalToUnit, IsDefinition, |
| 1445 | Variable, StaticDataMemberDeclaration)); |
| 1446 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, |
| 1447 | Line, Type, !IsLocalToUnit, IsDefinition, |
| 1448 | Variable, StaticDataMemberDeclaration)); |
| 1449 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, |
| 1450 | Line, Type, IsLocalToUnit, !IsDefinition, |
| 1451 | Variable, StaticDataMemberDeclaration)); |
| 1452 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, |
| 1453 | Line, Type, IsLocalToUnit, IsDefinition, |
| 1454 | Type, StaticDataMemberDeclaration)); |
| 1455 | EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, |
| 1456 | Line, Type, IsLocalToUnit, IsDefinition, |
| 1457 | Variable, Type)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1458 | |
| 1459 | TempMDGlobalVariable Temp = N->clone(); |
| 1460 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | typedef MetadataTest MDLocalVariableTest; |
| 1464 | |
| 1465 | TEST_F(MDLocalVariableTest, get) { |
| 1466 | unsigned Tag = dwarf::DW_TAG_arg_variable; |
| 1467 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1468 | StringRef Name = "name"; |
| 1469 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1470 | unsigned Line = 5; |
| 1471 | Metadata *Type = MDTuple::getDistinct(Context, None); |
| 1472 | unsigned Arg = 6; |
| 1473 | unsigned Flags = 7; |
| 1474 | Metadata *InlinedAt = MDTuple::getDistinct(Context, None); |
| 1475 | |
| 1476 | auto *N = MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, |
| 1477 | Arg, Flags, InlinedAt); |
| 1478 | EXPECT_EQ(Tag, N->getTag()); |
| 1479 | EXPECT_EQ(Scope, N->getScope()); |
| 1480 | EXPECT_EQ(Name, N->getName()); |
| 1481 | EXPECT_EQ(File, N->getFile()); |
| 1482 | EXPECT_EQ(Line, N->getLine()); |
| 1483 | EXPECT_EQ(Type, N->getType()); |
| 1484 | EXPECT_EQ(Arg, N->getArg()); |
| 1485 | EXPECT_EQ(Flags, N->getFlags()); |
| 1486 | EXPECT_EQ(InlinedAt, N->getInlinedAt()); |
| 1487 | EXPECT_EQ(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, |
| 1488 | Arg, Flags, InlinedAt)); |
| 1489 | |
| 1490 | EXPECT_NE(N, MDLocalVariable::get(Context, dwarf::DW_TAG_auto_variable, Scope, |
| 1491 | Name, File, Line, Type, Arg, Flags, |
| 1492 | InlinedAt)); |
| 1493 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, File, Name, File, Line, |
| 1494 | Type, Arg, Flags, InlinedAt)); |
| 1495 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, "other", File, Line, |
| 1496 | Type, Arg, Flags, InlinedAt)); |
| 1497 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, Scope, Line, |
| 1498 | Type, Arg, Flags, InlinedAt)); |
| 1499 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1, |
| 1500 | Type, Arg, Flags, InlinedAt)); |
| 1501 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, |
| 1502 | Scope, Arg, Flags, InlinedAt)); |
| 1503 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, |
| 1504 | Arg + 1, Flags, InlinedAt)); |
| 1505 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, |
| 1506 | Arg, ~Flags, InlinedAt)); |
| 1507 | EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type, |
| 1508 | Arg, Flags, Scope)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1509 | |
| 1510 | TempMDLocalVariable Temp = N->clone(); |
| 1511 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | typedef MetadataTest MDExpressionTest; |
| 1515 | |
| 1516 | TEST_F(MDExpressionTest, get) { |
| 1517 | uint64_t Elements[] = {2, 6, 9, 78, 0}; |
| 1518 | auto *N = MDExpression::get(Context, Elements); |
| 1519 | EXPECT_EQ(makeArrayRef(Elements), N->getElements()); |
| 1520 | EXPECT_EQ(N, MDExpression::get(Context, Elements)); |
Duncan P. N. Exon Smith | dddc537 | 2015-02-10 01:36:46 +0000 | [diff] [blame] | 1521 | |
| 1522 | EXPECT_EQ(5u, N->getNumElements()); |
| 1523 | EXPECT_EQ(2u, N->getElement(0)); |
| 1524 | EXPECT_EQ(6u, N->getElement(1)); |
| 1525 | EXPECT_EQ(9u, N->getElement(2)); |
| 1526 | EXPECT_EQ(78u, N->getElement(3)); |
| 1527 | EXPECT_EQ(0u, N->getElement(4)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1528 | |
| 1529 | TempMDExpression Temp = N->clone(); |
| 1530 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 1533 | TEST_F(MDExpressionTest, isValid) { |
| 1534 | #define EXPECT_VALID(...) \ |
| 1535 | do { \ |
| 1536 | uint64_t Elements[] = {__VA_ARGS__}; \ |
| 1537 | EXPECT_TRUE(MDExpression::get(Context, Elements)->isValid()); \ |
| 1538 | } while (false) |
| 1539 | #define EXPECT_INVALID(...) \ |
| 1540 | do { \ |
| 1541 | uint64_t Elements[] = {__VA_ARGS__}; \ |
| 1542 | EXPECT_FALSE(MDExpression::get(Context, Elements)->isValid()); \ |
| 1543 | } while (false) |
| 1544 | |
| 1545 | // Empty expression should be valid. |
| 1546 | EXPECT_TRUE(MDExpression::get(Context, None)); |
| 1547 | |
| 1548 | // Valid constructions. |
| 1549 | EXPECT_VALID(dwarf::DW_OP_plus, 6); |
| 1550 | EXPECT_VALID(dwarf::DW_OP_deref); |
| 1551 | EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7); |
| 1552 | EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref); |
| 1553 | EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6); |
| 1554 | EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7); |
| 1555 | EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7); |
| 1556 | |
| 1557 | // Invalid constructions. |
| 1558 | EXPECT_INVALID(~0u); |
| 1559 | EXPECT_INVALID(dwarf::DW_OP_plus); |
| 1560 | EXPECT_INVALID(dwarf::DW_OP_bit_piece); |
| 1561 | EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3); |
| 1562 | EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3); |
| 1563 | EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref); |
| 1564 | |
| 1565 | #undef EXPECT_VALID |
| 1566 | #undef EXPECT_INVALID |
| 1567 | } |
| 1568 | |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1569 | typedef MetadataTest MDObjCPropertyTest; |
| 1570 | |
| 1571 | TEST_F(MDObjCPropertyTest, get) { |
| 1572 | StringRef Name = "name"; |
| 1573 | Metadata *File = MDTuple::getDistinct(Context, None); |
| 1574 | unsigned Line = 5; |
| 1575 | StringRef GetterName = "getter"; |
| 1576 | StringRef SetterName = "setter"; |
| 1577 | unsigned Attributes = 7; |
| 1578 | Metadata *Type = MDTuple::getDistinct(Context, None); |
| 1579 | |
| 1580 | auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName, |
| 1581 | SetterName, Attributes, Type); |
| 1582 | |
| 1583 | EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag()); |
| 1584 | EXPECT_EQ(Name, N->getName()); |
| 1585 | EXPECT_EQ(File, N->getFile()); |
| 1586 | EXPECT_EQ(Line, N->getLine()); |
| 1587 | EXPECT_EQ(GetterName, N->getGetterName()); |
| 1588 | EXPECT_EQ(SetterName, N->getSetterName()); |
| 1589 | EXPECT_EQ(Attributes, N->getAttributes()); |
| 1590 | EXPECT_EQ(Type, N->getType()); |
| 1591 | EXPECT_EQ(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, |
| 1592 | SetterName, Attributes, Type)); |
| 1593 | |
| 1594 | EXPECT_NE(N, MDObjCProperty::get(Context, "other", File, Line, GetterName, |
| 1595 | SetterName, Attributes, Type)); |
| 1596 | EXPECT_NE(N, MDObjCProperty::get(Context, Name, Type, Line, GetterName, |
| 1597 | SetterName, Attributes, Type)); |
| 1598 | EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line + 1, GetterName, |
| 1599 | SetterName, Attributes, Type)); |
| 1600 | EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, "other", |
| 1601 | SetterName, Attributes, Type)); |
| 1602 | EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, |
| 1603 | "other", Attributes, Type)); |
| 1604 | EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, |
| 1605 | SetterName, Attributes + 1, Type)); |
| 1606 | EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName, |
| 1607 | SetterName, Attributes, File)); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1608 | |
| 1609 | TempMDObjCProperty Temp = N->clone(); |
| 1610 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | typedef MetadataTest MDImportedEntityTest; |
| 1614 | |
| 1615 | TEST_F(MDImportedEntityTest, get) { |
| 1616 | unsigned Tag = dwarf::DW_TAG_imported_module; |
| 1617 | Metadata *Scope = MDTuple::getDistinct(Context, None); |
| 1618 | Metadata *Entity = MDTuple::getDistinct(Context, None); |
| 1619 | unsigned Line = 5; |
| 1620 | StringRef Name = "name"; |
| 1621 | |
| 1622 | auto *N = MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name); |
| 1623 | |
| 1624 | EXPECT_EQ(Tag, N->getTag()); |
| 1625 | EXPECT_EQ(Scope, N->getScope()); |
| 1626 | EXPECT_EQ(Entity, N->getEntity()); |
| 1627 | EXPECT_EQ(Line, N->getLine()); |
| 1628 | EXPECT_EQ(Name, N->getName()); |
| 1629 | EXPECT_EQ(N, MDImportedEntity::get(Context, Tag, Scope, Entity, Line, Name)); |
| 1630 | |
| 1631 | EXPECT_NE(N, |
| 1632 | MDImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration, |
| 1633 | Scope, Entity, Line, Name)); |
| 1634 | EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Entity, Entity, Line, Name)); |
| 1635 | EXPECT_NE(N, MDImportedEntity::get(Context, Tag, Scope, Scope, Line, Name)); |
| 1636 | EXPECT_NE(N, |
| 1637 | MDImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name)); |
| 1638 | EXPECT_NE(N, |
| 1639 | MDImportedEntity::get(Context, Tag, Scope, Entity, Line, "other")); |
Duncan P. N. Exon Smith | 6873fea | 2015-02-17 23:10:13 +0000 | [diff] [blame] | 1640 | |
| 1641 | TempMDImportedEntity Temp = N->clone(); |
| 1642 | EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp))); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1645 | typedef MetadataTest MetadataAsValueTest; |
| 1646 | |
| 1647 | TEST_F(MetadataAsValueTest, MDNode) { |
| 1648 | MDNode *N = MDNode::get(Context, None); |
| 1649 | auto *V = MetadataAsValue::get(Context, N); |
| 1650 | EXPECT_TRUE(V->getType()->isMetadataTy()); |
| 1651 | EXPECT_EQ(N, V->getMetadata()); |
| 1652 | |
| 1653 | auto *V2 = MetadataAsValue::get(Context, N); |
| 1654 | EXPECT_EQ(V, V2); |
| 1655 | } |
| 1656 | |
| 1657 | TEST_F(MetadataAsValueTest, MDNodeMDNode) { |
| 1658 | MDNode *N = MDNode::get(Context, None); |
| 1659 | Metadata *Ops[] = {N}; |
| 1660 | MDNode *N2 = MDNode::get(Context, Ops); |
| 1661 | auto *V = MetadataAsValue::get(Context, N2); |
| 1662 | EXPECT_TRUE(V->getType()->isMetadataTy()); |
| 1663 | EXPECT_EQ(N2, V->getMetadata()); |
| 1664 | |
| 1665 | auto *V2 = MetadataAsValue::get(Context, N2); |
| 1666 | EXPECT_EQ(V, V2); |
| 1667 | |
| 1668 | auto *V3 = MetadataAsValue::get(Context, N); |
| 1669 | EXPECT_TRUE(V3->getType()->isMetadataTy()); |
| 1670 | EXPECT_NE(V, V3); |
| 1671 | EXPECT_EQ(N, V3->getMetadata()); |
| 1672 | } |
| 1673 | |
| 1674 | TEST_F(MetadataAsValueTest, MDNodeConstant) { |
| 1675 | auto *C = ConstantInt::getTrue(Context); |
| 1676 | auto *MD = ConstantAsMetadata::get(C); |
| 1677 | Metadata *Ops[] = {MD}; |
| 1678 | auto *N = MDNode::get(Context, Ops); |
| 1679 | |
| 1680 | auto *V = MetadataAsValue::get(Context, MD); |
| 1681 | EXPECT_TRUE(V->getType()->isMetadataTy()); |
| 1682 | EXPECT_EQ(MD, V->getMetadata()); |
| 1683 | |
| 1684 | auto *V2 = MetadataAsValue::get(Context, N); |
| 1685 | EXPECT_EQ(MD, V2->getMetadata()); |
| 1686 | EXPECT_EQ(V, V2); |
| 1687 | } |
| 1688 | |
Duncan P. N. Exon Smith | 121eeff | 2014-12-12 19:24:33 +0000 | [diff] [blame] | 1689 | typedef MetadataTest ValueAsMetadataTest; |
| 1690 | |
| 1691 | TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) { |
| 1692 | Type *Ty = Type::getInt1PtrTy(Context); |
| 1693 | std::unique_ptr<GlobalVariable> GV0( |
| 1694 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 1695 | auto *MD = ValueAsMetadata::get(GV0.get()); |
| 1696 | EXPECT_TRUE(MD->getValue() == GV0.get()); |
| 1697 | ASSERT_TRUE(GV0->use_empty()); |
| 1698 | |
| 1699 | std::unique_ptr<GlobalVariable> GV1( |
| 1700 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 1701 | GV0->replaceAllUsesWith(GV1.get()); |
| 1702 | EXPECT_TRUE(MD->getValue() == GV1.get()); |
| 1703 | } |
| 1704 | |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 1705 | TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) { |
| 1706 | // Create a constant. |
| 1707 | ConstantAsMetadata *CI = ConstantAsMetadata::get( |
| 1708 | ConstantInt::get(getGlobalContext(), APInt(8, 0))); |
| 1709 | |
| 1710 | // Create a temporary to prevent nodes from resolving. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 1711 | auto Temp = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 1712 | |
| 1713 | // When the first operand of N1 gets reset to nullptr, it'll collide with N2. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 1714 | Metadata *Ops1[] = {CI, CI, Temp.get()}; |
| 1715 | Metadata *Ops2[] = {nullptr, CI, Temp.get()}; |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 1716 | |
| 1717 | auto *N1 = MDTuple::get(Context, Ops1); |
| 1718 | auto *N2 = MDTuple::get(Context, Ops2); |
| 1719 | ASSERT_NE(N1, N2); |
| 1720 | |
| 1721 | // Tell metadata that the constant is getting deleted. |
| 1722 | // |
| 1723 | // After this, N1 will be invalid, so don't touch it. |
| 1724 | ValueAsMetadata::handleDeletion(CI->getValue()); |
| 1725 | EXPECT_EQ(nullptr, N2->getOperand(0)); |
| 1726 | EXPECT_EQ(nullptr, N2->getOperand(1)); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 1727 | EXPECT_EQ(Temp.get(), N2->getOperand(2)); |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 1728 | |
| 1729 | // Clean up Temp for teardown. |
| 1730 | Temp->replaceAllUsesWith(nullptr); |
| 1731 | } |
| 1732 | |
Duncan P. N. Exon Smith | 121eeff | 2014-12-12 19:24:33 +0000 | [diff] [blame] | 1733 | typedef MetadataTest TrackingMDRefTest; |
| 1734 | |
| 1735 | TEST_F(TrackingMDRefTest, UpdatesOnRAUW) { |
| 1736 | Type *Ty = Type::getInt1PtrTy(Context); |
| 1737 | std::unique_ptr<GlobalVariable> GV0( |
| 1738 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 1739 | TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get())); |
| 1740 | EXPECT_TRUE(MD->getValue() == GV0.get()); |
| 1741 | ASSERT_TRUE(GV0->use_empty()); |
| 1742 | |
| 1743 | std::unique_ptr<GlobalVariable> GV1( |
| 1744 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 1745 | GV0->replaceAllUsesWith(GV1.get()); |
| 1746 | EXPECT_TRUE(MD->getValue() == GV1.get()); |
| 1747 | |
| 1748 | // Reset it, so we don't inadvertently test deletion. |
| 1749 | MD.reset(); |
| 1750 | } |
| 1751 | |
| 1752 | TEST_F(TrackingMDRefTest, UpdatesOnDeletion) { |
| 1753 | Type *Ty = Type::getInt1PtrTy(Context); |
| 1754 | std::unique_ptr<GlobalVariable> GV( |
| 1755 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 1756 | TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get())); |
| 1757 | EXPECT_TRUE(MD->getValue() == GV.get()); |
| 1758 | ASSERT_TRUE(GV->use_empty()); |
| 1759 | |
| 1760 | GV.reset(); |
| 1761 | EXPECT_TRUE(!MD); |
| 1762 | } |
| 1763 | |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 1764 | TEST(NamedMDNodeTest, Search) { |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 1765 | LLVMContext Context; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1766 | ConstantAsMetadata *C = |
| 1767 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1)); |
| 1768 | ConstantAsMetadata *C2 = |
| 1769 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2)); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 1770 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1771 | Metadata *const V = C; |
| 1772 | Metadata *const V2 = C2; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 1773 | MDNode *n = MDNode::get(Context, V); |
| 1774 | MDNode *n2 = MDNode::get(Context, V2); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 1775 | |
Jeffrey Yasskin | 3d73d1a | 2010-03-13 01:39:20 +0000 | [diff] [blame] | 1776 | Module M("MyModule", Context); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 1777 | const char *Name = "llvm.NMD1"; |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1778 | NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name); |
| 1779 | NMD->addOperand(n); |
| 1780 | NMD->addOperand(n2); |
| 1781 | |
Chris Lattner | be354a6 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 1782 | std::string Str; |
| 1783 | raw_string_ostream oss(Str); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 1784 | NMD->print(oss); |
Chris Lattner | 1e6e367 | 2009-12-31 02:12:13 +0000 | [diff] [blame] | 1785 | EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 1786 | oss.str().c_str()); |
| 1787 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1788 | } |