Debian Codegraph @

llvm/unittests/IR/MetadataTest.cpp

raw
1 //===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Metadata.h"
10 #include "../lib/IR/LLVMContextImpl.h"
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DIBuilder.h"
15 #include "llvm/IR/DebugInfo.h"
16 #include "llvm/IR/DebugInfoMetadata.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ModuleSlotTracker.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "gtest/gtest.h"
27 #include <optional>
28 using namespace llvm;
29
30 namespace llvm {
31 LLVM_ABI extern cl::opt<bool> PickMergedSourceLocations;
32 } // namespace llvm
33
34 namespace {
35
36 TEST(ContextAndReplaceableUsesTest, FromContext) {
37 LLVMContext Context;
38 ContextAndReplaceableUses CRU(Context);
39 EXPECT_EQ(&Context, &CRU.getContext());
40 EXPECT_FALSE(CRU.hasReplaceableUses());
41 EXPECT_FALSE(CRU.getReplaceableUses());
42 }
43
44 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
45 LLVMContext Context;
46 ContextAndReplaceableUses CRU(std::make_unique<ReplaceableMetadataImpl>(Context));
47 EXPECT_EQ(&Context, &CRU.getContext());
48 EXPECT_TRUE(CRU.hasReplaceableUses());
49 EXPECT_TRUE(CRU.getReplaceableUses());
50 }
51
52 TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
53 LLVMContext Context;
54 ContextAndReplaceableUses CRU(Context);
55 CRU.makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(Context));
56 EXPECT_EQ(&Context, &CRU.getContext());
57 EXPECT_TRUE(CRU.hasReplaceableUses());
58 EXPECT_TRUE(CRU.getReplaceableUses());
59 }
60
61 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
62 LLVMContext Context;
63 auto ReplaceableUses = std::make_unique<ReplaceableMetadataImpl>(Context);
64 auto *Ptr = ReplaceableUses.get();
65 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
66 ReplaceableUses = CRU.takeReplaceableUses();
67 EXPECT_EQ(&Context, &CRU.getContext());
68 EXPECT_FALSE(CRU.hasReplaceableUses());
69 EXPECT_FALSE(CRU.getReplaceableUses());
70 EXPECT_EQ(Ptr, ReplaceableUses.get());
71 }
72
73 class MetadataTest : public testing::Test {
74 public:
75 MetadataTest() : M("test", Context), Counter(0) {}
76
77 protected:
78 LLVMContext Context;
79 Module M;
80 int Counter;
81
82 MDNode *getNode() { return MDNode::get(Context, {}); }
83 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
84 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
85 Metadata *MDs[] = {MD1, MD2};
86 return MDNode::get(Context, MDs);
87 }
88
89 MDTuple *getTuple() { return MDTuple::getDistinct(Context, {}); }
90 DISubroutineType *getSubroutineType() {
91 return DISubroutineType::getDistinct(Context, DINode::FlagZero, 0,
92 getNode(nullptr));
93 }
94 DISubprogram *getSubprogram(DIFile *F = nullptr) {
95 return DISubprogram::getDistinct(Context, nullptr, "", "", F, 0, nullptr, 0,
96 nullptr, 0, 0, DINode::FlagZero,
97 DISubprogram::SPFlagZero, nullptr);
98 }
99 DIFile *getFile() {
100 return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
101 }
102 DICompileUnit *getUnit() {
103 return DICompileUnit::getDistinct(
104 Context, 1, getFile(), "clang", false, "-g", 2, "",
105 DICompileUnit::FullDebug, getTuple(), getTuple(), getTuple(),
106 getTuple(), getTuple(), 0, true, false,
107 DICompileUnit::DebugNameTableKind::Default, false, "/", "");
108 }
109 DIType *getBasicType(StringRef Name) {
110 return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
111 }
112 DIType *getDerivedType() {
113 return DIDerivedType::getDistinct(
114 Context, dwarf::DW_TAG_pointer_type, "", nullptr, 0, nullptr,
115 getBasicType("basictype"), 1, 2, 0, std::nullopt, {}, DINode::FlagZero);
116 }
117 Constant *getConstant() {
118 return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
119 }
120 ConstantAsMetadata *getConstantAsMetadata() {
121 return ConstantAsMetadata::get(getConstant());
122 }
123 DIType *getCompositeType() {
124 return DICompositeType::getDistinct(Context, dwarf::DW_TAG_structure_type,
125 "", nullptr, 0, nullptr, nullptr, 32,
126 32, 0, DINode::FlagZero, nullptr, 0,
127 std::nullopt, nullptr, nullptr, "");
128 }
129 Function *getFunction(StringRef Name) {
130 return Function::Create(
131 FunctionType::get(Type::getVoidTy(Context), {}, false),
132 Function::ExternalLinkage, Name, M);
133 }
134 };
135 typedef MetadataTest MDStringTest;
136
137 // Test that construction of MDString with different value produces different
138 // MDString objects, even with the same string pointer and nulls in the string.
139 TEST_F(MDStringTest, CreateDifferent) {
140 char x[3] = { 'f', 0, 'A' };
141 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
142 x[2] = 'B';
143 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
144 EXPECT_NE(s1, s2);
145 }
146
147 // Test that creation of MDStrings with the same string contents produces the
148 // same MDString object, even with different pointers.
149 TEST_F(MDStringTest, CreateSame) {
150 char x[4] = { 'a', 'b', 'c', 'X' };
151 char y[4] = { 'a', 'b', 'c', 'Y' };
152
153 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
154 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
155 EXPECT_EQ(s1, s2);
156 }
157
158 // Test that MDString prints out the string we fed it.
159 TEST_F(MDStringTest, PrintingSimple) {
160 char str[14] = "testing 1 2 3";
161 MDString *s = MDString::get(Context, StringRef(&str[0], 13));
162 strncpy(str, "aaaaaaaaaaaaa", 14);
163
164 std::string Str;
165 raw_string_ostream oss(Str);
166 s->print(oss);
167 EXPECT_STREQ("!\"testing 1 2 3\"", Str.c_str());
168 }
169
170 // Test printing of MDString with non-printable characters.
171 TEST_F(MDStringTest, PrintingComplex) {
172 char str[5] = {0, '\n', '"', '\\', (char)-1};
173 MDString *s = MDString::get(Context, StringRef(str+0, 5));
174 std::string Str;
175 raw_string_ostream oss(Str);
176 s->print(oss);
177 EXPECT_STREQ("!\"\\00\\0A\\22\\\\\\FF\"", Str.c_str());
178 }
179
180 typedef MetadataTest MDNodeTest;
181
182 // Test the two constructors, and containing other Constants.
183 TEST_F(MDNodeTest, Simple) {
184 char x[3] = { 'a', 'b', 'c' };
185 char y[3] = { '1', '2', '3' };
186
187 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
188 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
189 ConstantAsMetadata *CI =
190 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
191
192 std::vector<Metadata *> V;
193 V.push_back(s1);
194 V.push_back(CI);
195 V.push_back(s2);
196
197 MDNode *n1 = MDNode::get(Context, V);
198 Metadata *const c1 = n1;
199 MDNode *n2 = MDNode::get(Context, c1);
200 Metadata *const c2 = n2;
201 MDNode *n3 = MDNode::get(Context, V);
202 MDNode *n4 = MDNode::getIfExists(Context, V);
203 MDNode *n5 = MDNode::getIfExists(Context, c1);
204 MDNode *n6 = MDNode::getIfExists(Context, c2);
205 EXPECT_NE(n1, n2);
206 EXPECT_EQ(n1, n3);
207 EXPECT_EQ(n4, n1);
208 EXPECT_EQ(n5, n2);
209 EXPECT_EQ(n6, (Metadata *)nullptr);
210
211 EXPECT_EQ(3u, n1->getNumOperands());
212 EXPECT_EQ(s1, n1->getOperand(0));
213 EXPECT_EQ(CI, n1->getOperand(1));
214 EXPECT_EQ(s2, n1->getOperand(2));
215
216 EXPECT_EQ(1u, n2->getNumOperands());
217 EXPECT_EQ(n1, n2->getOperand(0));
218 }
219
220 TEST_F(MDNodeTest, Delete) {
221 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
222 Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
223
224 Metadata *const V = LocalAsMetadata::get(I);
225 MDNode *n = MDNode::get(Context, V);
226 TrackingMDRef wvh(n);
227
228 EXPECT_EQ(n, wvh);
229
230 I->deleteValue();
231 }
232
233 TEST_F(MDNodeTest, SelfReference) {
234 // !0 = !{!0}
235 // !1 = !{!0}
236 {
237 auto Temp = MDNode::getTemporary(Context, {});
238 Metadata *Args[] = {Temp.get()};
239 MDNode *Self = MDNode::get(Context, Args);
240 Self->replaceOperandWith(0, Self);
241 ASSERT_EQ(Self, Self->getOperand(0));
242
243 // Self-references should be distinct, so MDNode::get() should grab a
244 // uniqued node that references Self, not Self.
245 Args[0] = Self;
246 MDNode *Ref1 = MDNode::get(Context, Args);
247 MDNode *Ref2 = MDNode::get(Context, Args);
248 EXPECT_NE(Self, Ref1);
249 EXPECT_EQ(Ref1, Ref2);
250 }
251
252 // !0 = !{!0, !{}}
253 // !1 = !{!0, !{}}
254 {
255 auto Temp = MDNode::getTemporary(Context, {});
256 Metadata *Args[] = {Temp.get(), MDNode::get(Context, {})};
257 MDNode *Self = MDNode::get(Context, Args);
258 Self->replaceOperandWith(0, Self);
259 ASSERT_EQ(Self, Self->getOperand(0));
260
261 // Self-references should be distinct, so MDNode::get() should grab a
262 // uniqued node that references Self, not Self itself.
263 Args[0] = Self;
264 MDNode *Ref1 = MDNode::get(Context, Args);
265 MDNode *Ref2 = MDNode::get(Context, Args);
266 EXPECT_NE(Self, Ref1);
267 EXPECT_EQ(Ref1, Ref2);
268 }
269 }
270
271 TEST_F(MDNodeTest, Print) {
272 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
273 MDString *S = MDString::get(Context, "foo");
274 MDNode *N0 = getNode();
275 MDNode *N1 = getNode(N0);
276 MDNode *N2 = getNode(N0, N1);
277
278 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
279 MDNode *N = MDNode::get(Context, Args);
280
281 std::string Expected;
282 {
283 raw_string_ostream OS(Expected);
284 OS << "<" << (void *)N << "> = !{";
285 C->printAsOperand(OS);
286 OS << ", ";
287 S->printAsOperand(OS);
288 OS << ", null";
289 MDNode *Nodes[] = {N0, N1, N2};
290 for (auto *Node : Nodes)
291 OS << ", <" << (void *)Node << ">";
292 OS << "}";
293 }
294
295 std::string Actual;
296 {
297 raw_string_ostream OS(Actual);
298 N->print(OS);
299 }
300
301 EXPECT_EQ(Expected, Actual);
302 }
303
304 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
305 do { \
306 std::string Actual_; \
307 raw_string_ostream OS(Actual_); \
308 PRINT; \
309 std::string Expected_(EXPECTED); \
310 EXPECT_EQ(Expected_, Actual_); \
311 } while (false)
312
313 TEST_F(MDNodeTest, PrintTemporary) {
314 MDNode *Arg = getNode();
315 TempMDNode Temp = MDNode::getTemporary(Context, Arg);
316 MDNode *N = getNode(Temp.get());
317 Module M("test", Context);
318 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
319 NMD->addOperand(N);
320
321 EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
322 EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
323 EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
324
325 // Cleanup.
326 Temp->replaceAllUsesWith(Arg);
327 }
328
329 TEST_F(MDNodeTest, PrintFromModule) {
330 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
331 MDString *S = MDString::get(Context, "foo");
332 MDNode *N0 = getNode();
333 MDNode *N1 = getNode(N0);
334 MDNode *N2 = getNode(N0, N1);
335
336 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
337 MDNode *N = MDNode::get(Context, Args);
338 Module M("test", Context);
339 NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
340 NMD->addOperand(N);
341
342 std::string Expected;
343 {
344 raw_string_ostream OS(Expected);
345 OS << "!0 = !{";
346 C->printAsOperand(OS);
347 OS << ", ";
348 S->printAsOperand(OS);
349 OS << ", null, !1, !2, !3}";
350 }
351
352 EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
353 }
354
355 TEST_F(MDNodeTest, PrintFromFunction) {
356 Module M("test", Context);
357 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
358 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
359 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
360 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
361 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
362 auto *R0 = ReturnInst::Create(Context, BB0);
363 auto *R1 = ReturnInst::Create(Context, BB1);
364 auto *N0 = MDNode::getDistinct(Context, {});
365 auto *N1 = MDNode::getDistinct(Context, {});
366 R0->setMetadata("md", N0);
367 R1->setMetadata("md", N1);
368
369 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
370 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
371
372 ModuleSlotTracker MST(&M);
373 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
374 EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
375 }
376
377 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
378 Module M("test", Context);
379
380 auto *Intrinsic =
381 Function::Create(FunctionType::get(Type::getVoidTy(Context),
382 Type::getMetadataTy(Context), false),
383 GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
384
385 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
386 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
387 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
388 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
389 auto *BB1 = BasicBlock::Create(Context, "entry", F1);
390 auto *N0 = MDNode::getDistinct(Context, {});
391 auto *N1 = MDNode::getDistinct(Context, {});
392 auto *MAV0 = MetadataAsValue::get(Context, N0);
393 auto *MAV1 = MetadataAsValue::get(Context, N1);
394 CallInst::Create(Intrinsic, MAV0, "", BB0);
395 CallInst::Create(Intrinsic, MAV1, "", BB1);
396
397 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
398 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
399 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
400 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
401 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
402 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
403
404 ModuleSlotTracker MST(&M);
405 EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
406 EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
407 EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
408 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
409 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
410 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
411 }
412
413 TEST_F(MDNodeTest, PrintWithDroppedCallOperand) {
414 Module M("test", Context);
415
416 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
417 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
418 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
419 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
420
421 CallInst *CI0 = CallInst::Create(F1, "", BB0);
422 CI0->dropAllReferences();
423
424 auto *R0 = ReturnInst::Create(Context, BB0);
425 auto *N0 = MDNode::getDistinct(Context, {});
426 R0->setMetadata("md", N0);
427
428 // Printing the metadata node would previously result in a failed assertion
429 // due to the call instruction's dropped function operand.
430 ModuleSlotTracker MST(&M);
431 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
432 }
433
434 TEST_F(MDNodeTest, PrintTree) {
435 DILocalScope *Scope = getSubprogram();
436 DIFile *File = getFile();
437 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
438 {
439 DIType *Type = getDerivedType();
440 auto *Var = DILocalVariable::get(Context, Scope, "foo", File,
441 /*LineNo=*/8, Type, /*ArgNo=*/2, Flags,
442 /*Align=*/8, nullptr);
443 std::string Expected;
444 {
445 raw_string_ostream SS(Expected);
446 Var->print(SS);
447 // indent level 1
448 Scope->print((SS << "\n").indent(2));
449 File->print((SS << "\n").indent(2));
450 Type->print((SS << "\n").indent(2));
451 // indent level 2
452 auto *BaseType = cast<DIDerivedType>(Type)->getBaseType();
453 BaseType->print((SS << "\n").indent(4));
454 }
455
456 EXPECT_PRINTER_EQ(Expected, Var->printTree(OS));
457 }
458
459 {
460 // Test if printTree works correctly when there is
461 // a cycle in the MDNode and its dependencies.
462 //
463 // We're trying to create type like this:
464 // struct LinkedList {
465 // LinkedList *Head;
466 // };
467 auto *StructTy = cast<DICompositeType>(getCompositeType());
468 DIType *PointerTy = DIDerivedType::getDistinct(
469 Context, dwarf::DW_TAG_pointer_type, "", nullptr, 0, nullptr, StructTy,
470 1, 2, 0, std::nullopt, {}, DINode::FlagZero);
471 StructTy->replaceElements(MDTuple::get(Context, PointerTy));
472
473 auto *Var = DILocalVariable::get(Context, Scope, "foo", File,
474 /*LineNo=*/8, StructTy, /*ArgNo=*/2, Flags,
475 /*Align=*/8, nullptr);
476 std::string Expected;
477 {
478 raw_string_ostream SS(Expected);
479 Var->print(SS);
480 // indent level 1
481 Scope->print((SS << "\n").indent(2));
482 File->print((SS << "\n").indent(2));
483 StructTy->print((SS << "\n").indent(2));
484 // indent level 2
485 StructTy->getRawElements()->print((SS << "\n").indent(4));
486 // indent level 3
487 auto Elements = StructTy->getElements();
488 Elements[0]->print((SS << "\n").indent(6));
489 }
490
491 EXPECT_PRINTER_EQ(Expected, Var->printTree(OS));
492 }
493 }
494 #undef EXPECT_PRINTER_EQ
495
496 TEST_F(MDNodeTest, NullOperand) {
497 // metadata !{}
498 MDNode *Empty = MDNode::get(Context, {});
499
500 // metadata !{metadata !{}}
501 Metadata *Ops[] = {Empty};
502 MDNode *N = MDNode::get(Context, Ops);
503 ASSERT_EQ(Empty, N->getOperand(0));
504
505 // metadata !{metadata !{}} => metadata !{null}
506 N->replaceOperandWith(0, nullptr);
507 ASSERT_EQ(nullptr, N->getOperand(0));
508
509 // metadata !{null}
510 Ops[0] = nullptr;
511 MDNode *NullOp = MDNode::get(Context, Ops);
512 ASSERT_EQ(nullptr, NullOp->getOperand(0));
513 EXPECT_EQ(N, NullOp);
514 }
515
516 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
517 // !{}
518 MDNode *Empty = MDNode::get(Context, {});
519 ASSERT_TRUE(Empty->isResolved());
520 EXPECT_FALSE(Empty->isDistinct());
521
522 // !{!{}}
523 Metadata *Wrapped1Ops[] = {Empty};
524 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
525 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
526 ASSERT_TRUE(Wrapped1->isResolved());
527 EXPECT_FALSE(Wrapped1->isDistinct());
528
529 // !{!{!{}}}
530 Metadata *Wrapped2Ops[] = {Wrapped1};
531 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
532 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
533 ASSERT_TRUE(Wrapped2->isResolved());
534 EXPECT_FALSE(Wrapped2->isDistinct());
535
536 // !{!{!{}}} => !{!{}}
537 Wrapped2->replaceOperandWith(0, Empty);
538 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
539 EXPECT_TRUE(Wrapped2->isDistinct());
540 EXPECT_FALSE(Wrapped1->isDistinct());
541 }
542
543 TEST_F(MDNodeTest, UniquedOnDeletedOperand) {
544 // temp !{}
545 TempMDTuple T = MDTuple::getTemporary(Context, {});
546
547 // !{temp !{}}
548 Metadata *Ops[] = {T.get()};
549 MDTuple *N = MDTuple::get(Context, Ops);
550
551 // !{temp !{}} => !{null}
552 T.reset();
553 ASSERT_TRUE(N->isUniqued());
554 Metadata *NullOps[] = {nullptr};
555 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
556 }
557
558 TEST_F(MDNodeTest, DistinctOnDeletedValueOperand) {
559 // i1* @GV
560 Type *Ty = PointerType::getUnqual(Context);
561 std::unique_ptr<GlobalVariable> GV(
562 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
563 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
564
565 // !{i1* @GV}
566 Metadata *Ops[] = {Op};
567 MDTuple *N = MDTuple::get(Context, Ops);
568
569 // !{i1* @GV} => !{null}
570 GV.reset();
571 ASSERT_TRUE(N->isDistinct());
572 ASSERT_EQ(nullptr, N->getOperand(0));
573 Metadata *NullOps[] = {nullptr};
574 ASSERT_NE(N, MDTuple::get(Context, NullOps));
575 }
576
577 TEST_F(MDNodeTest, getDistinct) {
578 // !{}
579 MDNode *Empty = MDNode::get(Context, {});
580 ASSERT_TRUE(Empty->isResolved());
581 ASSERT_FALSE(Empty->isDistinct());
582 ASSERT_EQ(Empty, MDNode::get(Context, {}));
583
584 // distinct !{}
585 MDNode *Distinct1 = MDNode::getDistinct(Context, {});
586 MDNode *Distinct2 = MDNode::getDistinct(Context, {});
587 EXPECT_TRUE(Distinct1->isResolved());
588 EXPECT_TRUE(Distinct2->isDistinct());
589 EXPECT_NE(Empty, Distinct1);
590 EXPECT_NE(Empty, Distinct2);
591 EXPECT_NE(Distinct1, Distinct2);
592
593 // !{}
594 ASSERT_EQ(Empty, MDNode::get(Context, {}));
595 }
596
597 TEST_F(MDNodeTest, isUniqued) {
598 MDNode *U = MDTuple::get(Context, {});
599 MDNode *D = MDTuple::getDistinct(Context, {});
600 auto T = MDTuple::getTemporary(Context, {});
601 EXPECT_TRUE(U->isUniqued());
602 EXPECT_FALSE(D->isUniqued());
603 EXPECT_FALSE(T->isUniqued());
604 }
605
606 TEST_F(MDNodeTest, isDistinct) {
607 MDNode *U = MDTuple::get(Context, {});
608 MDNode *D = MDTuple::getDistinct(Context, {});
609 auto T = MDTuple::getTemporary(Context, {});
610 EXPECT_FALSE(U->isDistinct());
611 EXPECT_TRUE(D->isDistinct());
612 EXPECT_FALSE(T->isDistinct());
613 }
614
615 TEST_F(MDNodeTest, isTemporary) {
616 MDNode *U = MDTuple::get(Context, {});
617 MDNode *D = MDTuple::getDistinct(Context, {});
618 auto T = MDTuple::getTemporary(Context, {});
619 EXPECT_FALSE(U->isTemporary());
620 EXPECT_FALSE(D->isTemporary());
621 EXPECT_TRUE(T->isTemporary());
622 }
623
624 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
625 // temporary !{}
626 auto Temp = MDTuple::getTemporary(Context, {});
627 ASSERT_FALSE(Temp->isResolved());
628
629 // distinct !{temporary !{}}
630 Metadata *Ops[] = {Temp.get()};
631 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
632 EXPECT_TRUE(Distinct->isResolved());
633 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
634
635 // temporary !{} => !{}
636 MDNode *Empty = MDNode::get(Context, {});
637 Temp->replaceAllUsesWith(Empty);
638 EXPECT_EQ(Empty, Distinct->getOperand(0));
639 }
640
641 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
642 // !0 = !{}
643 MDNode *N0 = MDNode::get(Context, {});
644
645 // !1 = !{!3, null}
646 auto Temp3 = MDTuple::getTemporary(Context, {});
647 Metadata *Ops1[] = {Temp3.get(), nullptr};
648 MDNode *N1 = MDNode::get(Context, Ops1);
649
650 // !2 = !{!3, !0}
651 Metadata *Ops2[] = {Temp3.get(), N0};
652 MDNode *N2 = MDNode::get(Context, Ops2);
653
654 // !3 = !{!2}
655 Metadata *Ops3[] = {N2};
656 MDNode *N3 = MDNode::get(Context, Ops3);
657 Temp3->replaceAllUsesWith(N3);
658
659 // !4 = !{!1}
660 Metadata *Ops4[] = {N1};
661 MDNode *N4 = MDNode::get(Context, Ops4);
662
663 // Confirm that the cycle prevented RAUW from getting dropped.
664 EXPECT_TRUE(N0->isResolved());
665 EXPECT_FALSE(N1->isResolved());
666 EXPECT_FALSE(N2->isResolved());
667 EXPECT_FALSE(N3->isResolved());
668 EXPECT_FALSE(N4->isResolved());
669
670 // Create a couple of distinct nodes to observe what's going on.
671 //
672 // !5 = distinct !{!2}
673 // !6 = distinct !{!3}
674 Metadata *Ops5[] = {N2};
675 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
676 Metadata *Ops6[] = {N3};
677 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
678
679 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
680 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
681 // references !3, this can cause a re-entry of handleChangedOperand() when !3
682 // is not ready for it.
683 //
684 // !2->replaceOperandWith(1, nullptr)
685 // !2: !{!3, !0} => !{!3, null}
686 // !2->replaceAllUsesWith(!1)
687 // !3: !{!2] => !{!1}
688 // !3->replaceAllUsesWith(!4)
689 N2->replaceOperandWith(1, nullptr);
690
691 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
692 // under us. Just check that the other nodes are sane.
693 //
694 // !1 = !{!4, null}
695 // !4 = !{!1}
696 // !5 = distinct !{!1}
697 // !6 = distinct !{!4}
698 EXPECT_EQ(N4, N1->getOperand(0));
699 EXPECT_EQ(N1, N4->getOperand(0));
700 EXPECT_EQ(N1, N5->getOperand(0));
701 EXPECT_EQ(N4, N6->getOperand(0));
702 }
703
704 TEST_F(MDNodeTest, replaceResolvedOperand) {
705 // Check code for replacing one resolved operand with another. If doing this
706 // directly (via replaceOperandWith()) becomes illegal, change the operand to
707 // a global value that gets RAUW'ed.
708 //
709 // Use a temporary node to keep N from being resolved.
710 auto Temp = MDTuple::getTemporary(Context, {});
711 Metadata *Ops[] = {nullptr, Temp.get()};
712
713 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
714 MDNode *N = MDTuple::get(Context, Ops);
715 EXPECT_EQ(nullptr, N->getOperand(0));
716 ASSERT_FALSE(N->isResolved());
717
718 // Check code for replacing resolved nodes.
719 N->replaceOperandWith(0, Empty);
720 EXPECT_EQ(Empty, N->getOperand(0));
721
722 // Check code for adding another unresolved operand.
723 N->replaceOperandWith(0, Temp.get());
724 EXPECT_EQ(Temp.get(), N->getOperand(0));
725
726 // Remove the references to Temp; required for teardown.
727 Temp->replaceAllUsesWith(nullptr);
728 }
729
730 TEST_F(MDNodeTest, replaceWithUniqued) {
731 auto *Empty = MDTuple::get(Context, {});
732 MDTuple *FirstUniqued;
733 {
734 Metadata *Ops[] = {Empty};
735 auto Temp = MDTuple::getTemporary(Context, Ops);
736 EXPECT_TRUE(Temp->isTemporary());
737
738 // Don't expect a collision.
739 auto *Current = Temp.get();
740 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
741 EXPECT_TRUE(FirstUniqued->isUniqued());
742 EXPECT_TRUE(FirstUniqued->isResolved());
743 EXPECT_EQ(Current, FirstUniqued);
744 }
745 {
746 Metadata *Ops[] = {Empty};
747 auto Temp = MDTuple::getTemporary(Context, Ops);
748 EXPECT_TRUE(Temp->isTemporary());
749
750 // Should collide with Uniqued above this time.
751 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
752 EXPECT_TRUE(Uniqued->isUniqued());
753 EXPECT_TRUE(Uniqued->isResolved());
754 EXPECT_EQ(FirstUniqued, Uniqued);
755 }
756 {
757 auto Unresolved = MDTuple::getTemporary(Context, {});
758 Metadata *Ops[] = {Unresolved.get()};
759 auto Temp = MDTuple::getTemporary(Context, Ops);
760 EXPECT_TRUE(Temp->isTemporary());
761
762 // Shouldn't be resolved.
763 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
764 EXPECT_TRUE(Uniqued->isUniqued());
765 EXPECT_FALSE(Uniqued->isResolved());
766
767 // Should be a different node.
768 EXPECT_NE(FirstUniqued, Uniqued);
769
770 // Should resolve when we update its node (note: be careful to avoid a
771 // collision with any other nodes above).
772 Uniqued->replaceOperandWith(0, nullptr);
773 EXPECT_TRUE(Uniqued->isResolved());
774 }
775 }
776
777 TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
778 // temp !{}
779 MDTuple *Op = MDTuple::getTemporary(Context, {}).release();
780 EXPECT_FALSE(Op->isResolved());
781
782 // temp !{temp !{}}
783 Metadata *Ops[] = {Op};
784 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
785 EXPECT_FALSE(N->isResolved());
786
787 // temp !{temp !{}} => !{temp !{}}
788 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
789 EXPECT_FALSE(N->isResolved());
790
791 // !{temp !{}} => !{!{}}
792 ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
793 EXPECT_TRUE(Op->isResolved());
794 EXPECT_TRUE(N->isResolved());
795 }
796
797 TEST_F(MDNodeTest, replaceWithUniquedDeletedOperand) {
798 // i1* @GV
799 Type *Ty = PointerType::getUnqual(Context);
800 std::unique_ptr<GlobalVariable> GV(
801 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
802 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
803
804 // temp !{i1* @GV}
805 Metadata *Ops[] = {Op};
806 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
807
808 // temp !{i1* @GV} => !{i1* @GV}
809 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
810 ASSERT_TRUE(N->isUniqued());
811
812 // !{i1* @GV} => !{null}
813 GV.reset();
814 ASSERT_TRUE(N->isDistinct());
815 ASSERT_EQ(nullptr, N->getOperand(0));
816 Metadata *NullOps[] = {nullptr};
817 ASSERT_NE(N, MDTuple::get(Context, NullOps));
818 }
819
820 TEST_F(MDNodeTest, replaceWithUniquedChangedOperand) {
821 // i1* @GV
822 Type *Ty = PointerType::getUnqual(Context);
823 std::unique_ptr<GlobalVariable> GV(
824 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
825 ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
826
827 // temp !{i1* @GV}
828 Metadata *Ops[] = {Op};
829 MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
830
831 // temp !{i1* @GV} => !{i1* @GV}
832 ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
833 ASSERT_TRUE(N->isUniqued());
834
835 // !{i1* @GV} => !{i1* @GV2}
836 std::unique_ptr<GlobalVariable> GV2(
837 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
838 GV->replaceAllUsesWith(GV2.get());
839 ASSERT_TRUE(N->isUniqued());
840 Metadata *NullOps[] = {ConstantAsMetadata::get(GV2.get())};
841 ASSERT_EQ(N, MDTuple::get(Context, NullOps));
842 }
843
844 TEST_F(MDNodeTest, replaceWithDistinct) {
845 {
846 auto *Empty = MDTuple::get(Context, {});
847 Metadata *Ops[] = {Empty};
848 auto Temp = MDTuple::getTemporary(Context, Ops);
849 EXPECT_TRUE(Temp->isTemporary());
850
851 // Don't expect a collision.
852 auto *Current = Temp.get();
853 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
854 EXPECT_TRUE(Distinct->isDistinct());
855 EXPECT_TRUE(Distinct->isResolved());
856 EXPECT_EQ(Current, Distinct);
857 }
858 {
859 auto Unresolved = MDTuple::getTemporary(Context, {});
860 Metadata *Ops[] = {Unresolved.get()};
861 auto Temp = MDTuple::getTemporary(Context, Ops);
862 EXPECT_TRUE(Temp->isTemporary());
863
864 // Don't expect a collision.
865 auto *Current = Temp.get();
866 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
867 EXPECT_TRUE(Distinct->isDistinct());
868 EXPECT_TRUE(Distinct->isResolved());
869 EXPECT_EQ(Current, Distinct);
870
871 // Cleanup; required for teardown.
872 Unresolved->replaceAllUsesWith(nullptr);
873 }
874 }
875
876 TEST_F(MDNodeTest, replaceWithPermanent) {
877 Metadata *Ops[] = {nullptr};
878 auto Temp = MDTuple::getTemporary(Context, Ops);
879 auto *T = Temp.get();
880
881 // U is a normal, uniqued node that references T.
882 auto *U = MDTuple::get(Context, T);
883 EXPECT_TRUE(U->isUniqued());
884
885 // Make Temp self-referencing.
886 Temp->replaceOperandWith(0, T);
887
888 // Try to uniquify Temp. This should, despite the name in the API, give a
889 // 'distinct' node, since self-references aren't allowed to be uniqued.
890 //
891 // Since it's distinct, N should have the same address as when it was a
892 // temporary (i.e., be equal to T not U).
893 auto *N = MDNode::replaceWithPermanent(std::move(Temp));
894 EXPECT_EQ(N, T);
895 EXPECT_TRUE(N->isDistinct());
896
897 // U should be the canonical unique node with N as the argument.
898 EXPECT_EQ(U, MDTuple::get(Context, N));
899 EXPECT_TRUE(U->isUniqued());
900
901 // This temporary should collide with U when replaced, but it should still be
902 // uniqued.
903 EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
904 EXPECT_TRUE(U->isUniqued());
905
906 // This temporary should become a new uniqued node.
907 auto Temp2 = MDTuple::getTemporary(Context, U);
908 auto *V = Temp2.get();
909 EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
910 EXPECT_TRUE(V->isUniqued());
911 EXPECT_EQ(U, V->getOperand(0));
912 }
913
914 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
915 TrackingMDRef Ref;
916 EXPECT_EQ(nullptr, Ref.get());
917 {
918 auto Temp = MDTuple::getTemporary(Context, {});
919 Ref.reset(Temp.get());
920 EXPECT_EQ(Temp.get(), Ref.get());
921 }
922 EXPECT_EQ(nullptr, Ref.get());
923 }
924
925 typedef MetadataTest DILocationTest;
926
927 TEST_F(DILocationTest, Merge) {
928 DIFile *F = getFile();
929 DISubprogram *N = getSubprogram(F);
930 DIScope *S = DILexicalBlock::get(Context, N, F, 3, 4);
931
932 {
933 // Identical.
934 auto *A = DILocation::get(Context, 2, 7, N);
935 auto *B = DILocation::get(Context, 2, 7, N);
936 auto *M = DILocation::getMergedLocation(A, B);
937 EXPECT_EQ(2u, M->getLine());
938 EXPECT_EQ(7u, M->getColumn());
939 EXPECT_EQ(N, M->getScope());
940 }
941
942 {
943 // Identical, inside DILexicalBlockFile.
944 auto *OtherF = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
945 auto *LBF = DILexicalBlockFile::get(Context, S, OtherF, 0);
946 auto *A = DILocation::get(Context, 2, 7, LBF);
947 auto *B = DILocation::get(Context, 2, 7, LBF);
948 auto *M = DILocation::getMergedLocation(A, B);
949 EXPECT_EQ(2u, M->getLine());
950 EXPECT_EQ(7u, M->getColumn());
951 EXPECT_EQ(LBF, M->getScope());
952 }
953
954 {
955 // Identical, different scopes.
956 auto *A = DILocation::get(Context, 2, 7, N);
957 auto *B = DILocation::get(Context, 2, 7, S);
958 auto *M = DILocation::getMergedLocation(A, B);
959 EXPECT_EQ(2u, M->getLine());
960 EXPECT_EQ(7u, M->getColumn());
961 EXPECT_EQ(N, M->getScope());
962 }
963
964 {
965 // Same line, different column.
966 auto *A = DILocation::get(Context, 2, 7, N);
967 auto *B = DILocation::get(Context, 2, 10, S);
968 auto *M0 = DILocation::getMergedLocation(A, B);
969 auto *M1 = DILocation::getMergedLocation(B, A);
970 for (auto *M : {M0, M1}) {
971 EXPECT_EQ(2u, M->getLine());
972 EXPECT_EQ(0u, M->getColumn());
973 EXPECT_EQ(N, M->getScope());
974 }
975 }
976
977 {
978 // Same line, different column, same DILexicalBlockFile scope.
979 auto *OtherF = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
980 auto *LBF = DILexicalBlockFile::get(Context, S, OtherF, 0);
981 auto *A = DILocation::get(Context, 2, 7, LBF);
982 auto *B = DILocation::get(Context, 2, 10, LBF);
983 auto *M0 = DILocation::getMergedLocation(A, B);
984 auto *M1 = DILocation::getMergedLocation(B, A);
985 for (auto *M : {M0, M1}) {
986 EXPECT_EQ(2u, M->getLine());
987 EXPECT_EQ(0u, M->getColumn());
988 EXPECT_EQ(LBF, M->getScope());
989 }
990 }
991
992 {
993 // Different lines, same DISubprogram scopes.
994 auto *A = DILocation::get(Context, 1, 6, N);
995 auto *B = DILocation::get(Context, 2, 7, N);
996 auto *M = DILocation::getMergedLocation(A, B);
997 EXPECT_EQ(0u, M->getLine());
998 EXPECT_EQ(0u, M->getColumn());
999 EXPECT_EQ(N, M->getScope());
1000 }
1001
1002 {
1003 // Different lines, same DILexicalBlockFile scopes.
1004 auto *OtherF = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
1005 auto *LBF = DILexicalBlockFile::get(Context, S, OtherF, 0);
1006 auto *A = DILocation::get(Context, 1, 6, LBF);
1007 auto *B = DILocation::get(Context, 2, 7, LBF);
1008 auto *M = DILocation::getMergedLocation(A, B);
1009 EXPECT_EQ(0u, M->getLine());
1010 EXPECT_EQ(0u, M->getColumn());
1011 EXPECT_EQ(LBF, M->getScope());
1012 }
1013
1014 {
1015 // Different lines, same DILexicalBlock scopes.
1016 auto *A = DILocation::get(Context, 1, 6, S);
1017 auto *B = DILocation::get(Context, 2, 7, S);
1018 auto *M = DILocation::getMergedLocation(A, B);
1019 EXPECT_EQ(0u, M->getLine());
1020 EXPECT_EQ(0u, M->getColumn());
1021 EXPECT_EQ(S, M->getScope());
1022 }
1023
1024 {
1025 // Twisty locations, all different, same function.
1026 auto *A = DILocation::get(Context, 1, 6, N);
1027 auto *B = DILocation::get(Context, 2, 7, S);
1028 auto *M = DILocation::getMergedLocation(A, B);
1029 EXPECT_EQ(0u, M->getLine());
1030 EXPECT_EQ(0u, M->getColumn());
1031 EXPECT_EQ(N, M->getScope());
1032 }
1033
1034 {
1035 // Different files, same line numbers, same subprogram.
1036 auto *F1 = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
1037 auto *F2 = DIFile::getDistinct(Context, "file2.c", "/path/to/dir");
1038 DISubprogram *N = getSubprogram(F1);
1039 auto *LBF = DILexicalBlockFile::get(Context, N, F2, 0);
1040 auto *A = DILocation::get(Context, 1, 6, N);
1041 auto *B = DILocation::get(Context, 1, 6, LBF);
1042 auto *M = DILocation::getMergedLocation(A, B);
1043 EXPECT_EQ(0u, M->getLine());
1044 EXPECT_EQ(0u, M->getColumn());
1045 EXPECT_EQ(N, M->getScope());
1046 }
1047
1048 {
1049 // Different files, same line numbers.
1050 auto *F1 = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
1051 auto *F2 = DIFile::getDistinct(Context, "file2.c", "/path/to/dir");
1052 DISubprogram *N = getSubprogram(F1);
1053 auto *LB = DILexicalBlock::getDistinct(Context, N, F1, 4, 9);
1054 auto *LBF = DILexicalBlockFile::get(Context, LB, F2, 0);
1055 auto *A = DILocation::get(Context, 1, 6, LB);
1056 auto *B = DILocation::get(Context, 1, 6, LBF);
1057 auto *M = DILocation::getMergedLocation(A, B);
1058 EXPECT_EQ(4u, M->getLine());
1059 EXPECT_EQ(9u, M->getColumn());
1060 EXPECT_EQ(LB, M->getScope());
1061 }
1062
1063 {
1064 // Different files, same line numbers,
1065 // both locations have DILexicalBlockFile scopes.
1066 auto *F1 = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
1067 auto *F2 = DIFile::getDistinct(Context, "file2.c", "/path/to/dir");
1068 auto *F3 = DIFile::getDistinct(Context, "file3.c", "/path/to/dir");
1069 DISubprogram *N = getSubprogram(F1);
1070 auto *LB = DILexicalBlock::getDistinct(Context, N, F1, 4, 9);
1071 auto *LBF1 = DILexicalBlockFile::get(Context, LB, F2, 0);
1072 auto *LBF2 = DILexicalBlockFile::get(Context, LB, F3, 0);
1073 auto *A = DILocation::get(Context, 1, 6, LBF1);
1074 auto *B = DILocation::get(Context, 1, 6, LBF2);
1075 auto *M = DILocation::getMergedLocation(A, B);
1076 EXPECT_EQ(4u, M->getLine());
1077 EXPECT_EQ(9u, M->getColumn());
1078 EXPECT_EQ(LB, M->getScope());
1079 }
1080
1081 {
1082 // Same file, same line numbers, but different LBF objects.
1083 // both locations have DILexicalBlockFile scope.
1084 auto *F1 = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
1085 DISubprogram *N = getSubprogram(F1);
1086 auto *LB1 = DILexicalBlock::getDistinct(Context, N, F1, 4, 9);
1087 auto *LB2 = DILexicalBlock::getDistinct(Context, N, F1, 5, 9);
1088 auto *F2 = DIFile::getDistinct(Context, "file2.c", "/path/to/dir");
1089 auto *LBF1 = DILexicalBlockFile::get(Context, LB1, F2, 0);
1090 auto *LBF2 = DILexicalBlockFile::get(Context, LB2, F2, 0);
1091 auto *A = DILocation::get(Context, 1, 6, LBF1);
1092 auto *B = DILocation::get(Context, 1, 6, LBF2);
1093 auto *M = DILocation::getMergedLocation(A, B);
1094 EXPECT_EQ(1u, M->getLine());
1095 EXPECT_EQ(6u, M->getColumn());
1096 EXPECT_EQ(LBF1->getFile(), M->getScope()->getFile());
1097 EXPECT_EQ(N, M->getScope()->getScope());
1098 }
1099
1100 {
1101 // Merge locations A and B, where B is included in A's file
1102 // at the same position as A's position.
1103 auto *F1 = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
1104 DISubprogram *N = getSubprogram(F1);
1105 auto *LB = DILexicalBlock::getDistinct(Context, N, F1, 4, 9);
1106 auto *F2 = DIFile::getDistinct(Context, "file2.c", "/path/to/dir");
1107 auto *LBF = DILexicalBlockFile::get(Context, LB, F2, 0);
1108 auto *A = DILocation::get(Context, 4, 9, LB);
1109 auto *B = DILocation::get(Context, 1, 6, LBF);
1110 auto *M = DILocation::getMergedLocation(A, B);
1111 EXPECT_EQ(4u, M->getLine());
1112 EXPECT_EQ(9u, M->getColumn());
1113 EXPECT_EQ(LB, M->getScope());
1114 }
1115
1116 {
1117 // Different locations from different files included from the same block.
1118 auto *F1 = DIFile::getDistinct(Context, "file1.c", "/path/to/dir");
1119 DISubprogram *N = getSubprogram(F1);
1120
1121 auto *LBCommon = DILexicalBlock::getDistinct(Context, N, F1, 4, 9);
1122
1123 auto *F2 = DIFile::getDistinct(Context, "file2.c", "/path/to/dir");
1124 LBCommon = DILexicalBlock::getDistinct(Context, LBCommon, F2, 5, 9);
1125
1126 auto *F3 = DIFile::getDistinct(Context, "file3.c", "/path/to/dir");
1127 auto *LB1 = DILexicalBlock::getDistinct(Context, LBCommon, F3, 6, 9);
1128
1129 auto *F4 = DIFile::getDistinct(Context, "file4.c", "/path/to/dir");
1130 auto *LB2 = DILexicalBlock::getDistinct(Context, LBCommon, F4, 7, 9);
1131
1132 auto *F5 = DIFile::getDistinct(Context, "file5.c", "/path/to/dir");
1133 auto *LBF1 = DILexicalBlockFile::get(Context, LB1, F5, 0);
1134
1135 auto *A = DILocation::get(Context, 8, 9, LB2);
1136 auto *B = DILocation::get(Context, 9, 6, LBF1);
1137 auto *M = DILocation::getMergedLocation(A, B);
1138 EXPECT_EQ(5u, M->getLine());
1139 EXPECT_EQ(9u, M->getColumn());
1140 EXPECT_EQ(LBCommon, M->getScope());
1141 }
1142
1143 {
1144 // Different locations from different files having common include parent.
1145 auto *LBCommon = DILexicalBlock::getDistinct(Context, N, F, 4, 1);
1146
1147 // Different scopes.
1148 DILexicalBlock *Block[2] = {};
1149 Block[0] = DILexicalBlock::get(Context, LBCommon, F, 10, 2);
1150 Block[1] = DILexicalBlock::get(Context, LBCommon, F, 20, 3);
1151
1152 // Includes of the same file.
1153 auto *F1 = DIFile::getDistinct(Context, "file1.inc", "/path/to/dir");
1154 DILexicalBlock *Block1[2] = {};
1155 Block1[0] = DILexicalBlock::get(
1156 Context, DILexicalBlockFile::get(Context, Block[0], F1, 0), F1, 30, 4);
1157 Block1[1] = DILexicalBlock::get(
1158 Context, DILexicalBlockFile::get(Context, Block[1], F1, 0), F1, 30, 4);
1159
1160 // Different sub-includes.
1161 DIFile *F2[2] = {};
1162 DILexicalBlock *Block2[2] = {};
1163
1164 F2[0] = DIFile::getDistinct(Context, "file2_a.inc", "/path/to/dir");
1165 Block2[0] = DILexicalBlock::get(
1166 Context, DILexicalBlockFile::get(Context, Block1[0], F2[0], 0), F2[0],
1167 40, 5);
1168
1169 F2[1] = DIFile::getDistinct(Context, "file2_b.inc", "/path/to/dir");
1170 Block2[1] = DILexicalBlock::get(
1171 Context, DILexicalBlockFile::get(Context, Block1[1], F2[1], 0), F2[1],
1172 50, 6);
1173
1174 auto *A = DILocation::get(Context, 41, 7, Block2[0]);
1175 auto *B = DILocation::get(Context, 51, 8, Block2[1]);
1176 auto *M = DILocation::getMergedLocation(A, B);
1177 auto *MScope = dyn_cast<DILexicalBlock>(M->getScope());
1178 EXPECT_EQ(30u, M->getLine());
1179 EXPECT_EQ(4u, M->getColumn());
1180 EXPECT_EQ(Block1[0]->getFile(), MScope->getFile());
1181 EXPECT_EQ(Block1[0]->getLine(), MScope->getLine());
1182 EXPECT_EQ(Block1[0]->getColumn(), MScope->getColumn());
1183 EXPECT_EQ(LBCommon, MScope->getScope());
1184 }
1185
1186 {
1187 // Different function, same inlined-at.
1188 auto *F = getFile();
1189 auto *SP1 = DISubprogram::getDistinct(Context, F, "a", "a", F, 0, nullptr,
1190 0, nullptr, 0, 0, DINode::FlagZero,
1191 DISubprogram::SPFlagZero, nullptr);
1192 auto *SP2 = DISubprogram::getDistinct(Context, F, "b", "b", F, 0, nullptr,
1193 0, nullptr, 0, 0, DINode::FlagZero,
1194 DISubprogram::SPFlagZero, nullptr);
1195
1196 auto *I = DILocation::get(Context, 2, 7, N);
1197 auto *A = DILocation::get(Context, 1, 6, SP1, I);
1198 auto *B = DILocation::get(Context, 3, 8, SP2, I);
1199 auto *M = DILocation::getMergedLocation(A, B);
1200 EXPECT_EQ(2u, M->getLine());
1201 EXPECT_EQ(7u, M->getColumn());
1202 EXPECT_EQ(N, M->getScope());
1203 EXPECT_EQ(nullptr, M->getInlinedAt());
1204 }
1205
1206 {
1207 // Different function, inlined-at same line, but different column.
1208 auto *F = getFile();
1209 auto *SP1 = DISubprogram::getDistinct(Context, F, "a", "a", F, 0, nullptr,
1210 0, nullptr, 0, 0, DINode::FlagZero,
1211 DISubprogram::SPFlagZero, nullptr);
1212 auto *SP2 = DISubprogram::getDistinct(Context, F, "b", "b", F, 0, nullptr,
1213 0, nullptr, 0, 0, DINode::FlagZero,
1214 DISubprogram::SPFlagZero, nullptr);
1215
1216 auto *IA = DILocation::get(Context, 2, 7, N);
1217 auto *IB = DILocation::get(Context, 2, 8, N);
1218 auto *A = DILocation::get(Context, 1, 6, SP1, IA);
1219 auto *B = DILocation::get(Context, 3, 8, SP2, IB);
1220 auto *M = DILocation::getMergedLocation(A, B);
1221 EXPECT_EQ(2u, M->getLine());
1222 EXPECT_EQ(0u, M->getColumn());
1223 EXPECT_EQ(N, M->getScope());
1224 EXPECT_EQ(nullptr, M->getInlinedAt());
1225 }
1226
1227 {
1228 // Completely different.
1229 auto *I = DILocation::get(Context, 2, 7, N);
1230 auto *A = DILocation::get(Context, 1, 6, S, I);
1231 auto *B = DILocation::get(Context, 2, 7, getSubprogram());
1232 auto *M = DILocation::getMergedLocation(A, B);
1233 EXPECT_EQ(0u, M->getLine());
1234 EXPECT_EQ(0u, M->getColumn());
1235 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1236 EXPECT_EQ(S, M->getScope());
1237 EXPECT_EQ(nullptr, M->getInlinedAt());
1238 }
1239
1240 // Two locations, same line/column different file, inlined at the same place.
1241 {
1242 auto *FA = getFile();
1243 auto *FB = getFile();
1244 auto *FI = getFile();
1245
1246 auto *SPA = DISubprogram::getDistinct(Context, FA, "a", "a", FA, 0, nullptr,
1247 0, nullptr, 0, 0, DINode::FlagZero,
1248 DISubprogram::SPFlagZero, nullptr);
1249
1250 auto *SPB = DISubprogram::getDistinct(Context, FB, "b", "b", FB, 0, nullptr,
1251 0, nullptr, 0, 0, DINode::FlagZero,
1252 DISubprogram::SPFlagZero, nullptr);
1253
1254 auto *SPI = DISubprogram::getDistinct(Context, FI, "i", "i", FI, 0, nullptr,
1255 0, nullptr, 0, 0, DINode::FlagZero,
1256 DISubprogram::SPFlagZero, nullptr);
1257
1258 auto *I = DILocation::get(Context, 3, 8, SPI);
1259 auto *A = DILocation::get(Context, 2, 7, SPA, I);
1260 auto *B = DILocation::get(Context, 2, 7, SPB, I);
1261 auto *M = DILocation::getMergedLocation(A, B);
1262 EXPECT_EQ(3u, M->getLine());
1263 EXPECT_EQ(8u, M->getColumn());
1264 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1265 EXPECT_EQ(SPI, M->getScope());
1266 EXPECT_EQ(nullptr, M->getInlinedAt());
1267 }
1268
1269 // Two locations, same line/column different file, one location with 2 scopes,
1270 // inlined at the same place.
1271 {
1272 auto *FA = getFile();
1273 auto *FB = getFile();
1274 auto *FI = getFile();
1275
1276 auto *SPA = DISubprogram::getDistinct(Context, FA, "a", "a", FA, 0, nullptr,
1277 0, nullptr, 0, 0, DINode::FlagZero,
1278 DISubprogram::SPFlagZero, nullptr);
1279
1280 auto *SPB = DISubprogram::getDistinct(Context, FB, "b", "b", FB, 0, nullptr,
1281 0, nullptr, 0, 0, DINode::FlagZero,
1282 DISubprogram::SPFlagZero, nullptr);
1283
1284 auto *SPI = DISubprogram::getDistinct(Context, FI, "i", "i", FI, 0, nullptr,
1285 0, nullptr, 0, 0, DINode::FlagZero,
1286 DISubprogram::SPFlagZero, nullptr);
1287
1288 auto *SPAScope = DILexicalBlock::getDistinct(Context, SPA, FA, 4, 9);
1289
1290 auto *I = DILocation::get(Context, 3, 8, SPI);
1291 auto *A = DILocation::get(Context, 2, 7, SPAScope, I);
1292 auto *B = DILocation::get(Context, 2, 7, SPB, I);
1293 auto *M = DILocation::getMergedLocation(A, B);
1294 EXPECT_EQ(3u, M->getLine());
1295 EXPECT_EQ(8u, M->getColumn());
1296 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1297 EXPECT_EQ(SPI, M->getScope());
1298 EXPECT_EQ(nullptr, M->getInlinedAt());
1299 }
1300
1301 // Merge a location in C, which is inlined-at in B that is inlined in A,
1302 // with a location in A that has the same scope, line and column as B's
1303 // inlined-at location.
1304 {
1305 auto *FA = getFile();
1306 auto *FB = getFile();
1307 auto *FC = getFile();
1308
1309 auto *SPA = DISubprogram::getDistinct(Context, FA, "a", "a", FA, 0, nullptr,
1310 0, nullptr, 0, 0, DINode::FlagZero,
1311 DISubprogram::SPFlagZero, nullptr);
1312
1313 auto *SPB = DISubprogram::getDistinct(Context, FB, "b", "b", FB, 0, nullptr,
1314 0, nullptr, 0, 0, DINode::FlagZero,
1315 DISubprogram::SPFlagZero, nullptr);
1316
1317 auto *SPC = DISubprogram::getDistinct(Context, FC, "c", "c", FC, 0, nullptr,
1318 0, nullptr, 0, 0, DINode::FlagZero,
1319 DISubprogram::SPFlagZero, nullptr);
1320
1321 auto *A = DILocation::get(Context, 3, 2, SPA);
1322 auto *B = DILocation::get(Context, 2, 4, SPB, A);
1323 auto *C = DILocation::get(Context, 13, 2, SPC, B);
1324 auto *M = DILocation::getMergedLocation(A, C);
1325 EXPECT_EQ(3u, M->getLine());
1326 EXPECT_EQ(2u, M->getColumn());
1327 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1328 EXPECT_EQ(SPA, M->getScope());
1329 EXPECT_EQ(nullptr, M->getInlinedAt());
1330 }
1331
1332 // Two inlined locations with the same scope, line and column
1333 // in the same inlined-at function at different line and column.
1334 {
1335 auto *FA = getFile();
1336 auto *FB = getFile();
1337 auto *FC = getFile();
1338
1339 auto *SPA = DISubprogram::getDistinct(Context, FA, "a", "a", FA, 0, nullptr,
1340 0, nullptr, 0, 0, DINode::FlagZero,
1341 DISubprogram::SPFlagZero, nullptr);
1342
1343 auto *SPB = DISubprogram::getDistinct(Context, FB, "b", "b", FB, 0, nullptr,
1344 0, nullptr, 0, 0, DINode::FlagZero,
1345 DISubprogram::SPFlagZero, nullptr);
1346
1347 auto *SPC = DISubprogram::getDistinct(Context, FC, "c", "c", FC, 0, nullptr,
1348 0, nullptr, 0, 0, DINode::FlagZero,
1349 DISubprogram::SPFlagZero, nullptr);
1350
1351 auto *A = DILocation::get(Context, 10, 20, SPA);
1352 auto *B1 = DILocation::get(Context, 3, 2, SPB, A);
1353 auto *B2 = DILocation::get(Context, 4, 5, SPB, A);
1354 auto *C1 = DILocation::get(Context, 2, 4, SPC, B1);
1355 auto *C2 = DILocation::get(Context, 2, 4, SPC, B2);
1356
1357 auto *M = DILocation::getMergedLocation(C1, C2);
1358 EXPECT_EQ(2u, M->getLine());
1359 EXPECT_EQ(4u, M->getColumn());
1360 EXPECT_EQ(SPC, M->getScope());
1361 ASSERT_NE(nullptr, M->getInlinedAt());
1362
1363 auto *I1 = M->getInlinedAt();
1364 EXPECT_EQ(0u, I1->getLine());
1365 EXPECT_EQ(0u, I1->getColumn());
1366 EXPECT_EQ(SPB, I1->getScope());
1367 EXPECT_EQ(A, I1->getInlinedAt());
1368 }
1369
1370 // Two locations, different line/column and scope in the same subprogram,
1371 // inlined at the same place. This should result in a 0:0 location with
1372 // the nearest common scope in the inlined function.
1373 {
1374 auto *FA = getFile();
1375 auto *FI = getFile();
1376
1377 auto *SPA = DISubprogram::getDistinct(Context, FA, "a", "a", FA, 0, nullptr,
1378 0, nullptr, 0, 0, DINode::FlagZero,
1379 DISubprogram::SPFlagZero, nullptr);
1380
1381 auto *SPI = DISubprogram::getDistinct(Context, FI, "i", "i", FI, 0, nullptr,
1382 0, nullptr, 0, 0, DINode::FlagZero,
1383 DISubprogram::SPFlagZero, nullptr);
1384
1385 // Nearest common scope for the two locations in a.
1386 auto *SPAScope1 = DILexicalBlock::getDistinct(Context, SPA, FA, 4, 9);
1387
1388 // Scope for the first location in a.
1389 auto *SPAScope2 =
1390 DILexicalBlock::getDistinct(Context, SPAScope1, FA, 10, 12);
1391
1392 // Scope for the second location in a.
1393 auto *SPAScope3 =
1394 DILexicalBlock::getDistinct(Context, SPAScope1, FA, 20, 8);
1395 auto *SPAScope4 =
1396 DILexicalBlock::getDistinct(Context, SPAScope3, FA, 21, 12);
1397
1398 auto *I = DILocation::get(Context, 3, 8, SPI);
1399 auto *A1 = DILocation::get(Context, 12, 7, SPAScope2, I);
1400 auto *A2 = DILocation::get(Context, 21, 15, SPAScope4, I);
1401 auto *M = DILocation::getMergedLocation(A1, A2);
1402 EXPECT_EQ(0u, M->getLine());
1403 EXPECT_EQ(0u, M->getColumn());
1404 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1405 EXPECT_EQ(SPAScope1, M->getScope());
1406 EXPECT_EQ(I, M->getInlinedAt());
1407 }
1408
1409 // Regression test to catch a case where an iterator was invalidated due to
1410 // handling the chain of inlined-at locations after the nearest common
1411 // location for the two arguments were found.
1412 {
1413 auto *FA = getFile();
1414 auto *FB = getFile();
1415 auto *FI = getFile();
1416
1417 auto *SPA = DISubprogram::getDistinct(Context, FA, "a", "a", FA, 0, nullptr,
1418 0, nullptr, 0, 0, DINode::FlagZero,
1419 DISubprogram::SPFlagZero, nullptr);
1420
1421 auto *SPB = DISubprogram::getDistinct(Context, FB, "b", "b", FB, 0, nullptr,
1422 0, nullptr, 0, 0, DINode::FlagZero,
1423 DISubprogram::SPFlagZero, nullptr);
1424
1425 auto *SPI = DISubprogram::getDistinct(Context, FI, "i", "i", FI, 0, nullptr,
1426 0, nullptr, 0, 0, DINode::FlagZero,
1427 DISubprogram::SPFlagZero, nullptr);
1428
1429 auto *SPAScope1 = DILexicalBlock::getDistinct(Context, SPA, FA, 4, 9);
1430 auto *SPAScope2 = DILexicalBlock::getDistinct(Context, SPA, FA, 8, 3);
1431
1432 DILocation *InlinedAt = nullptr;
1433
1434 // Create a chain of inlined-at locations.
1435 for (int i = 0; i < 256; i++) {
1436 InlinedAt = DILocation::get(Context, 3 + i, 8 + i, SPI, InlinedAt);
1437 }
1438
1439 auto *A1 = DILocation::get(Context, 5, 9, SPAScope1, InlinedAt);
1440 auto *A2 = DILocation::get(Context, 9, 8, SPAScope2, InlinedAt);
1441 auto *B = DILocation::get(Context, 10, 3, SPB, A1);
1442 auto *M1 = DILocation::getMergedLocation(B, A2);
1443 EXPECT_EQ(0u, M1->getLine());
1444 EXPECT_EQ(0u, M1->getColumn());
1445 EXPECT_TRUE(isa<DILocalScope>(M1->getScope()));
1446 EXPECT_EQ(SPA, M1->getScope());
1447 EXPECT_EQ(InlinedAt, M1->getInlinedAt());
1448
1449 // Test the other argument order for good measure.
1450 auto *M2 = DILocation::getMergedLocation(A2, B);
1451 EXPECT_EQ(M1, M2);
1452 }
1453
1454 {
1455 // If PickMergedSourceLocation is enabled, when one source location is null
1456 // we should return the valid location.
1457 PickMergedSourceLocations = true;
1458 auto *A = DILocation::get(Context, 2, 7, N);
1459 auto *M1 = DILocation::getMergedLocation(A, nullptr);
1460 ASSERT_NE(nullptr, M1);
1461 EXPECT_EQ(2u, M1->getLine());
1462 EXPECT_EQ(7u, M1->getColumn());
1463 EXPECT_EQ(N, M1->getScope());
1464
1465 auto *M2 = DILocation::getMergedLocation(nullptr, A);
1466 ASSERT_NE(nullptr, M2);
1467 EXPECT_EQ(2u, M2->getLine());
1468 EXPECT_EQ(7u, M2->getColumn());
1469 EXPECT_EQ(N, M2->getScope());
1470 PickMergedSourceLocations = false;
1471 }
1472
1473 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
1474 #define EXPECT_ATOM(Loc, Group, Rank) \
1475 EXPECT_EQ(Group, M->getAtomGroup()); \
1476 EXPECT_EQ(Rank, M->getAtomRank());
1477 #else
1478 #define EXPECT_ATOM(Loc, Group, Rank) \
1479 EXPECT_EQ(0u, M->getAtomGroup()); \
1480 EXPECT_EQ(0u, M->getAtomRank()); \
1481 (void)Group; \
1482 (void)Rank;
1483 #endif
1484 // Identical, including source atom numbers.
1485 {
1486 auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1487 /*AtomRank*/ 1);
1488 auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1489 /*AtomRank*/ 1);
1490 auto *M = DILocation::getMergedLocation(A, B);
1491 EXPECT_ATOM(M, /*AtomGroup*/ 1u, 1u);
1492 // DILocations are uniqued, so we can check equality by ptr.
1493 EXPECT_EQ(M, DILocation::getMergedLocation(A, B));
1494 }
1495
1496 // Identical but different atom ranks (same atom) - choose the lowest nonzero
1497 // rank.
1498 {
1499 auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1500 /*AtomRank*/ 1);
1501 auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1502 /*AtomRank*/ 2);
1503 auto *M = DILocation::getMergedLocation(A, B);
1504 EXPECT_ATOM(M, /*AtomGroup*/ 1u, /*AtomRank*/ 1u);
1505 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1506
1507 A = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1508 /*AtomRank*/ 0);
1509 B = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1510 /*AtomRank*/ 2);
1511 M = DILocation::getMergedLocation(A, B);
1512 EXPECT_ATOM(M, /*AtomGroup*/ 1u, /*AtomRank*/ 2u);
1513 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1514 }
1515
1516 // Identical but different atom ranks (different atom) - choose the lowest
1517 // nonzero rank.
1518 {
1519 auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1520 /*AtomRank*/ 1);
1521 auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 2,
1522 /*AtomRank*/ 2);
1523 auto *M = DILocation::getMergedLocation(A, B);
1524 EXPECT_ATOM(M, 1u, 1u);
1525 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1526
1527 A = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1528 /*AtomRank*/ 0);
1529 B = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 2,
1530 /*AtomRank*/ 2);
1531 M = DILocation::getMergedLocation(A, B);
1532 EXPECT_ATOM(M, /*AtomGroup*/ 2u, /*AtomRank*/ 2u);
1533 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1534 }
1535
1536 // Identical but equal atom rank (different atom) - choose the lowest non-zero
1537 // group (arbitrary choice for deterministic behaviour).
1538 {
1539 auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 1,
1540 /*AtomRank*/ 1);
1541 auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 2,
1542 /*AtomRank*/ 1);
1543 auto *M = DILocation::getMergedLocation(A, B);
1544 EXPECT_ATOM(M, 1u, 1u);
1545 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1546
1547 A = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 0,
1548 /*AtomRank*/ 1);
1549 B = DILocation::get(Context, 2, 7, N, nullptr, false, /*AtomGroup*/ 2,
1550 /*AtomRank*/ 1);
1551 M = DILocation::getMergedLocation(A, B);
1552 EXPECT_ATOM(M, /*AtomGroup*/ 2u, /*AtomRank*/ 1u);
1553 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1554 }
1555
1556 // Completely different except same atom numbers. Zero out the atoms.
1557 {
1558 auto *I = DILocation::get(Context, 2, 7, N);
1559 auto *A = DILocation::get(Context, 1, 6, S, I, false, /*AtomGroup*/ 1,
1560 /*AtomRank*/ 1);
1561 auto *B = DILocation::get(Context, 2, 7, getSubprogram(), nullptr, false,
1562 /*AtomGroup*/ 1, /*AtomRank*/ 1);
1563 auto *M = DILocation::getMergedLocation(A, B);
1564 EXPECT_EQ(0u, M->getLine());
1565 EXPECT_EQ(0u, M->getColumn());
1566 EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1567 EXPECT_EQ(S, M->getScope());
1568 EXPECT_EQ(nullptr, M->getInlinedAt());
1569 }
1570
1571 // Same inlined-at chain but different atoms. Choose the lowest
1572 // non-zero group (arbitrary choice for deterministic behaviour).
1573 {
1574 auto *I = DILocation::get(Context, 1, 7, N);
1575 auto *F = getSubprogram();
1576 auto *A = DILocation::get(Context, 1, 1, F, I, false, /*AtomGroup*/ 1,
1577 /*AtomRank*/ 2);
1578 auto *B = DILocation::get(Context, 1, 1, F, I, false, /*AtomGroup*/ 2,
1579 /*AtomRank*/ 2);
1580 auto *M = DILocation::getMergedLocation(A, B);
1581 EXPECT_ATOM(M, /*AtomGroup*/ 1u, /*AtomRank*/ 2u);
1582 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1583
1584 A = DILocation::get(Context, 1, 1, F, I, false, /*AtomGroup*/ 1,
1585 /*AtomRank*/ 2);
1586 B = DILocation::get(Context, 1, 1, F, I, false, /*AtomGroup*/ 2,
1587 /*AtomRank*/ 0);
1588 M = DILocation::getMergedLocation(A, B);
1589 EXPECT_ATOM(M, /*AtomGroup*/ 1u, /*AtomRank*/ 2u);
1590 EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1591 }
1592
1593 // Partially equal inlined-at chain but different atoms. Generate a new atom
1594 // group (if either have a group number). This configuration seems unlikely
1595 // to occur as line numbers must match, but isn't impossible.
1596 {
1597 // Reset global counter to ensure EXPECT numbers line up.
1598 Context.pImpl->NextAtomGroup = 1;
1599 // x1 -> y2 -> z4
1600 // y3 -> z4
1601 auto *FX = getSubprogram();
1602 auto *FY = getSubprogram();
1603 auto *FZ = getSubprogram();
1604 auto *Z4 = DILocation::get(Context, 1, 4, FZ);
1605 auto *Y3IntoZ4 = DILocation::get(Context, 1, 3, FY, Z4, false,
1606 /*AtomGroup*/ 1, /*AtomRank*/ 1);
1607 auto *Y2IntoZ4 = DILocation::get(Context, 1, 2, FY, Z4);
1608 auto *X1IntoY2 = DILocation::get(Context, 1, 1, FX, Y2IntoZ4);
1609 auto *M = DILocation::getMergedLocation(X1IntoY2, Y3IntoZ4);
1610 EXPECT_EQ(M->getScope(), FY);
1611 EXPECT_EQ(M->getInlinedAt()->getScope(), FZ);
1612 EXPECT_ATOM(M, /*AtomGroup*/ 2u, /*AtomRank*/ 1u);
1613
1614 // This swapped merge will produce a new atom group too.
1615 M = DILocation::getMergedLocation(Y3IntoZ4, X1IntoY2);
1616
1617 // Same again, even if the atom numbers match.
1618 auto *X1IntoY2SameAtom = DILocation::get(Context, 1, 1, FX, Y2IntoZ4, false,
1619 /*AtomGroup*/ 1, /*AtomRank*/ 1);
1620 M = DILocation::getMergedLocation(X1IntoY2SameAtom, Y3IntoZ4);
1621 EXPECT_ATOM(M, /*AtomGroup*/ 4u, /*AtomRank*/ 1u);
1622 M = DILocation::getMergedLocation(Y3IntoZ4, X1IntoY2SameAtom);
1623 EXPECT_ATOM(M, /*AtomGroup*/ 5u, /*AtomRank*/ 1u);
1624 }
1625 #undef EXPECT_ATOM
1626 }
1627
1628 TEST_F(DILocationTest, getDistinct) {
1629 MDNode *N = getSubprogram();
1630 DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
1631 EXPECT_TRUE(L0->isDistinct());
1632 DILocation *L1 = DILocation::get(Context, 2, 7, N);
1633 EXPECT_FALSE(L1->isDistinct());
1634 EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
1635 }
1636
1637 TEST_F(DILocationTest, getTemporary) {
1638 MDNode *N = MDNode::get(Context, {});
1639 auto L = DILocation::getTemporary(Context, 2, 7, N);
1640 EXPECT_TRUE(L->isTemporary());
1641 EXPECT_FALSE(L->isResolved());
1642 }
1643
1644 TEST_F(DILocationTest, cloneTemporary) {
1645 MDNode *N = MDNode::get(Context, {});
1646 auto L = DILocation::getTemporary(Context, 2, 7, N);
1647 EXPECT_TRUE(L->isTemporary());
1648 auto L2 = L->clone();
1649 EXPECT_TRUE(L2->isTemporary());
1650 }
1651
1652 TEST_F(DILocationTest, discriminatorEncoding) {
1653 EXPECT_EQ(0U, *DILocation::encodeDiscriminator(0, 0, 0));
1654
1655 // Encode base discriminator as a component: lsb is 0, then the value.
1656 // The other components are all absent, so we leave all the other bits 0.
1657 EXPECT_EQ(2U, *DILocation::encodeDiscriminator(1, 0, 0));
1658
1659 // Base discriminator component is empty, so lsb is 1. Next component is not
1660 // empty, so its lsb is 0, then its value (1). Next component is empty.
1661 // So the bit pattern is 101.
1662 EXPECT_EQ(5U, *DILocation::encodeDiscriminator(0, 1, 0));
1663
1664 // First 2 components are empty, so the bit pattern is 11. Then the
1665 // next component - ending up with 1011.
1666 EXPECT_EQ(0xbU, *DILocation::encodeDiscriminator(0, 0, 1));
1667
1668 // The bit pattern for the first 2 components is 11. The next bit is 0,
1669 // because the last component is not empty. We have 29 bits usable for
1670 // encoding, but we cap it at 12 bits uniformously for all components. We
1671 // encode the last component over 14 bits.
1672 EXPECT_EQ(0xfffbU, *DILocation::encodeDiscriminator(0, 0, 0xfff));
1673
1674 EXPECT_EQ(0x102U, *DILocation::encodeDiscriminator(1, 1, 0));
1675
1676 EXPECT_EQ(0x13eU, *DILocation::encodeDiscriminator(0x1f, 1, 0));
1677
1678 EXPECT_EQ(0x87feU, *DILocation::encodeDiscriminator(0x1ff, 1, 0));
1679
1680 EXPECT_EQ(0x1f3eU, *DILocation::encodeDiscriminator(0x1f, 0x1f, 0));
1681
1682 EXPECT_EQ(0x3ff3eU, *DILocation::encodeDiscriminator(0x1f, 0x1ff, 0));
1683
1684 EXPECT_EQ(0x1ff87feU, *DILocation::encodeDiscriminator(0x1ff, 0x1ff, 0));
1685
1686 EXPECT_EQ(0xfff9f3eU, *DILocation::encodeDiscriminator(0x1f, 0x1f, 0xfff));
1687
1688 EXPECT_EQ(0xffc3ff3eU, *DILocation::encodeDiscriminator(0x1f, 0x1ff, 0x1ff));
1689
1690 EXPECT_EQ(0xffcf87feU, *DILocation::encodeDiscriminator(0x1ff, 0x1f, 0x1ff));
1691
1692 EXPECT_EQ(0xe1ff87feU, *DILocation::encodeDiscriminator(0x1ff, 0x1ff, 7));
1693 }
1694
1695 TEST_F(DILocationTest, discriminatorEncodingNegativeTests) {
1696 EXPECT_EQ(std::nullopt, DILocation::encodeDiscriminator(0, 0, 0x1000));
1697 EXPECT_EQ(std::nullopt, DILocation::encodeDiscriminator(0x1000, 0, 0));
1698 EXPECT_EQ(std::nullopt, DILocation::encodeDiscriminator(0, 0x1000, 0));
1699 EXPECT_EQ(std::nullopt, DILocation::encodeDiscriminator(0, 0, 0x1000));
1700 EXPECT_EQ(std::nullopt, DILocation::encodeDiscriminator(0x1ff, 0x1ff, 8));
1701 EXPECT_EQ(std::nullopt, DILocation::encodeDiscriminator(
1702 std::numeric_limits<uint32_t>::max(),
1703 std::numeric_limits<uint32_t>::max(), 0));
1704 }
1705
1706 TEST_F(DILocationTest, discriminatorSpecialCases) {
1707 // We don't test getCopyIdentifier here because the only way
1708 // to set it is by constructing an encoded discriminator using
1709 // encodeDiscriminator, which is already tested.
1710 auto L1 = DILocation::get(Context, 1, 2, getSubprogram());
1711 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1712 EXPECT_EQ(1U, L1->getDuplicationFactor());
1713
1714 EXPECT_EQ(L1, *L1->cloneWithBaseDiscriminator(0));
1715 EXPECT_EQ(L1, *L1->cloneByMultiplyingDuplicationFactor(0));
1716 EXPECT_EQ(L1, *L1->cloneByMultiplyingDuplicationFactor(1));
1717
1718 auto L2 = *L1->cloneWithBaseDiscriminator(1);
1719 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1720 EXPECT_EQ(1U, L1->getDuplicationFactor());
1721
1722 EXPECT_EQ(1U, L2->getBaseDiscriminator());
1723 EXPECT_EQ(1U, L2->getDuplicationFactor());
1724
1725 auto L3 = *L2->cloneByMultiplyingDuplicationFactor(2);
1726 EXPECT_EQ(1U, L3->getBaseDiscriminator());
1727 EXPECT_EQ(2U, L3->getDuplicationFactor());
1728
1729 EXPECT_EQ(L2, *L2->cloneByMultiplyingDuplicationFactor(1));
1730
1731 auto L4 = *L3->cloneByMultiplyingDuplicationFactor(4);
1732 EXPECT_EQ(1U, L4->getBaseDiscriminator());
1733 EXPECT_EQ(8U, L4->getDuplicationFactor());
1734
1735 auto L5 = *L4->cloneWithBaseDiscriminator(2);
1736 EXPECT_EQ(2U, L5->getBaseDiscriminator());
1737 EXPECT_EQ(8U, L5->getDuplicationFactor());
1738
1739 // Check extreme cases
1740 auto L6 = *L1->cloneWithBaseDiscriminator(0xfff);
1741 EXPECT_EQ(0xfffU, L6->getBaseDiscriminator());
1742 EXPECT_EQ(0xfffU, (*L6->cloneByMultiplyingDuplicationFactor(0xfff))
1743 ->getDuplicationFactor());
1744
1745 // Check we return std::nullopt for unencodable cases.
1746 EXPECT_EQ(std::nullopt, L4->cloneWithBaseDiscriminator(0x1000));
1747 EXPECT_EQ(std::nullopt, L4->cloneByMultiplyingDuplicationFactor(0x1000));
1748 }
1749
1750 TEST_F(DILocationTest, KeyInstructions) {
1751 Context.pImpl->NextAtomGroup = 1;
1752
1753 EXPECT_EQ(Context.pImpl->NextAtomGroup, 1u);
1754 DILocation *A1 =
1755 DILocation::get(Context, 1, 0, getSubprogram(), nullptr, false, 1, 2);
1756 // The group is only applied to the DILocation if we've built LLVM with
1757 // EXPERIMENTAL_KEY_INSTRUCTIONS.
1758 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
1759 EXPECT_EQ(A1->getAtomGroup(), 1u);
1760 EXPECT_EQ(A1->getAtomRank(), 2u);
1761 #else
1762 EXPECT_EQ(A1->getAtomGroup(), 0u);
1763 EXPECT_EQ(A1->getAtomRank(), 0u);
1764 #endif
1765
1766 // Group number 1 has been "used" so next available is 2.
1767 EXPECT_EQ(Context.pImpl->NextAtomGroup, 2u);
1768
1769 // Set a group number higher than current + 1, then check the waterline.
1770 DILocation::get(Context, 2, 0, getSubprogram(), nullptr, false, 5, 1);
1771 EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
1772
1773 // The waterline should be unchanged (group <= next).
1774 DILocation::get(Context, 3, 0, getSubprogram(), nullptr, false, 4, 1);
1775 EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
1776 DILocation::get(Context, 3, 0, getSubprogram(), nullptr, false, 5, 1);
1777 EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
1778
1779 // Check the waterline gets incremented by 1.
1780 EXPECT_EQ(Context.incNextDILocationAtomGroup(), 6u);
1781 EXPECT_EQ(Context.pImpl->NextAtomGroup, 7u);
1782
1783 Context.updateDILocationAtomGroupWaterline(8);
1784 EXPECT_EQ(Context.pImpl->NextAtomGroup, 8u);
1785 Context.updateDILocationAtomGroupWaterline(7);
1786 EXPECT_EQ(Context.pImpl->NextAtomGroup, 8u);
1787 }
1788
1789 typedef MetadataTest GenericDINodeTest;
1790
1791 TEST_F(GenericDINodeTest, get) {
1792 StringRef Header = "header";
1793 auto *Empty = MDNode::get(Context, {});
1794 Metadata *Ops1[] = {Empty};
1795 auto *N = GenericDINode::get(Context, 15, Header, Ops1);
1796 EXPECT_EQ(15u, N->getTag());
1797 EXPECT_EQ(2u, N->getNumOperands());
1798 EXPECT_EQ(Header, N->getHeader());
1799 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
1800 EXPECT_EQ(1u, N->getNumDwarfOperands());
1801 EXPECT_EQ(Empty, N->getDwarfOperand(0));
1802 EXPECT_EQ(Empty, N->getOperand(1));
1803 ASSERT_TRUE(N->isUniqued());
1804
1805 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
1806
1807 N->replaceOperandWith(1, nullptr);
1808 EXPECT_EQ(15u, N->getTag());
1809 EXPECT_EQ(Header, N->getHeader());
1810 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
1811 ASSERT_TRUE(N->isUniqued());
1812
1813 Metadata *Ops2[] = {nullptr};
1814 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
1815
1816 N->replaceDwarfOperandWith(0, Empty);
1817 EXPECT_EQ(15u, N->getTag());
1818 EXPECT_EQ(Header, N->getHeader());
1819 EXPECT_EQ(Empty, N->getDwarfOperand(0));
1820 ASSERT_TRUE(N->isUniqued());
1821 EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
1822
1823 TempGenericDINode Temp = N->clone();
1824 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1825 }
1826
1827 TEST_F(GenericDINodeTest, getEmptyHeader) {
1828 // Canonicalize !"" to null.
1829 auto *N = GenericDINode::get(Context, 15, StringRef(), {});
1830 EXPECT_EQ(StringRef(), N->getHeader());
1831 EXPECT_EQ(nullptr, N->getOperand(0));
1832 }
1833
1834 typedef MetadataTest DISubrangeTest;
1835
1836 TEST_F(DISubrangeTest, get) {
1837 auto *N = DISubrange::get(Context, 5, 7);
1838 auto Count = N->getCount();
1839 auto Lower = N->getLowerBound();
1840 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
1841 ASSERT_TRUE(Count);
1842 ASSERT_TRUE(isa<ConstantInt *>(Count));
1843 EXPECT_EQ(5, cast<ConstantInt *>(Count)->getSExtValue());
1844 EXPECT_EQ(7, cast<ConstantInt *>(Lower)->getSExtValue());
1845 EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
1846 EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
1847
1848 TempDISubrange Temp = N->clone();
1849 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1850 }
1851
1852 TEST_F(DISubrangeTest, getEmptyArray) {
1853 auto *N = DISubrange::get(Context, -1, 0);
1854 auto Count = N->getCount();
1855 auto Lower = N->getLowerBound();
1856 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
1857 ASSERT_TRUE(Count);
1858 ASSERT_TRUE(isa<ConstantInt *>(Count));
1859 EXPECT_EQ(-1, cast<ConstantInt *>(Count)->getSExtValue());
1860 EXPECT_EQ(0, cast<ConstantInt *>(Lower)->getSExtValue());
1861 EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
1862 }
1863
1864 TEST_F(DISubrangeTest, getVariableCount) {
1865 DILocalScope *Scope = getSubprogram();
1866 DIFile *File = getFile();
1867 DIType *Type = getDerivedType();
1868 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
1869 auto *VlaExpr = DILocalVariable::get(Context, Scope, "vla_expr", File, 8,
1870 Type, 2, Flags, 8, nullptr);
1871
1872 auto *N = DISubrange::get(Context, VlaExpr, 0);
1873 auto Count = N->getCount();
1874 auto Lower = N->getLowerBound();
1875 ASSERT_TRUE(Count);
1876 ASSERT_TRUE(isa<DIVariable *>(Count));
1877 EXPECT_EQ(VlaExpr, cast<DIVariable *>(Count));
1878 ASSERT_TRUE(isa<DIVariable>(N->getRawCountNode()));
1879 EXPECT_EQ(0, cast<ConstantInt *>(Lower)->getSExtValue());
1880 EXPECT_EQ("vla_expr", cast<DIVariable *>(Count)->getName());
1881 EXPECT_EQ(N, DISubrange::get(Context, VlaExpr, 0));
1882 }
1883
1884 TEST_F(DISubrangeTest, fortranAllocatableInt) {
1885 DILocalScope *Scope = getSubprogram();
1886 DIFile *File = getFile();
1887 DIType *Type = getDerivedType();
1888 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
1889 auto *LI = ConstantAsMetadata::get(
1890 ConstantInt::getSigned(Type::getInt64Ty(Context), -10));
1891 auto *UI = ConstantAsMetadata::get(
1892 ConstantInt::getSigned(Type::getInt64Ty(Context), 10));
1893 auto *SI = ConstantAsMetadata::get(
1894 ConstantInt::getSigned(Type::getInt64Ty(Context), 4));
1895 auto *UIother = ConstantAsMetadata::get(
1896 ConstantInt::getSigned(Type::getInt64Ty(Context), 20));
1897 auto *UVother = DILocalVariable::get(Context, Scope, "ubother", File, 8, Type,
1898 2, Flags, 8, nullptr);
1899 auto *UEother = DIExpression::get(Context, {5, 6});
1900 auto *LIZero = ConstantAsMetadata::get(
1901 ConstantInt::getSigned(Type::getInt64Ty(Context), 0));
1902 auto *UIZero = ConstantAsMetadata::get(
1903 ConstantInt::getSigned(Type::getInt64Ty(Context), 0));
1904
1905 auto *N = DISubrange::get(Context, nullptr, LI, UI, SI);
1906
1907 auto Lower = N->getLowerBound();
1908 ASSERT_TRUE(Lower);
1909 ASSERT_TRUE(isa<ConstantInt *>(Lower));
1910 EXPECT_EQ(cast<ConstantInt>(LI->getValue()), cast<ConstantInt *>(Lower));
1911
1912 auto Upper = N->getUpperBound();
1913 ASSERT_TRUE(Upper);
1914 ASSERT_TRUE(isa<ConstantInt *>(Upper));
1915 EXPECT_EQ(cast<ConstantInt>(UI->getValue()), cast<ConstantInt *>(Upper));
1916
1917 auto Stride = N->getStride();
1918 ASSERT_TRUE(Stride);
1919 ASSERT_TRUE(isa<ConstantInt *>(Stride));
1920 EXPECT_EQ(cast<ConstantInt>(SI->getValue()), cast<ConstantInt *>(Stride));
1921
1922 EXPECT_EQ(N, DISubrange::get(Context, nullptr, LI, UI, SI));
1923
1924 EXPECT_NE(N, DISubrange::get(Context, nullptr, LI, UIother, SI));
1925 EXPECT_NE(N, DISubrange::get(Context, nullptr, LI, UEother, SI));
1926 EXPECT_NE(N, DISubrange::get(Context, nullptr, LI, UVother, SI));
1927
1928 auto *NZeroLower = DISubrange::get(Context, nullptr, LIZero, UI, SI);
1929 EXPECT_NE(NZeroLower, DISubrange::get(Context, nullptr, nullptr, UI, SI));
1930
1931 auto *NZeroUpper = DISubrange::get(Context, nullptr, LI, UIZero, SI);
1932 EXPECT_NE(NZeroUpper, DISubrange::get(Context, nullptr, LI, nullptr, SI));
1933 }
1934
1935 TEST_F(DISubrangeTest, fortranAllocatableVar) {
1936 DILocalScope *Scope = getSubprogram();
1937 DIFile *File = getFile();
1938 DIType *Type = getDerivedType();
1939 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
1940 auto *LV =
1941 DILocalVariable::get(Context, Scope, "lb", File, 8, Type, 2, Flags, 8,
1942 nullptr);
1943 auto *UV =
1944 DILocalVariable::get(Context, Scope, "ub", File, 8, Type, 2, Flags, 8,
1945 nullptr);
1946 auto *SV =
1947 DILocalVariable::get(Context, Scope, "st", File, 8, Type, 2, Flags, 8,
1948 nullptr);
1949 auto *SVother = DILocalVariable::get(Context, Scope, "stother", File, 8, Type,
1950 2, Flags, 8, nullptr);
1951 auto *SIother = ConstantAsMetadata::get(
1952 ConstantInt::getSigned(Type::getInt64Ty(Context), 20));
1953 auto *SEother = DIExpression::get(Context, {5, 6});
1954
1955 auto *N = DISubrange::get(Context, nullptr, LV, UV, SV);
1956
1957 auto Lower = N->getLowerBound();
1958 ASSERT_TRUE(Lower);
1959 ASSERT_TRUE(isa<DIVariable *>(Lower));
1960 EXPECT_EQ(LV, cast<DIVariable *>(Lower));
1961
1962 auto Upper = N->getUpperBound();
1963 ASSERT_TRUE(Upper);
1964 ASSERT_TRUE(isa<DIVariable *>(Upper));
1965 EXPECT_EQ(UV, cast<DIVariable *>(Upper));
1966
1967 auto Stride = N->getStride();
1968 ASSERT_TRUE(Stride);
1969 ASSERT_TRUE(isa<DIVariable *>(Stride));
1970 EXPECT_EQ(SV, cast<DIVariable *>(Stride));
1971
1972 EXPECT_EQ(N, DISubrange::get(Context, nullptr, LV, UV, SV));
1973
1974 EXPECT_NE(N, DISubrange::get(Context, nullptr, LV, UV, SVother));
1975 EXPECT_NE(N, DISubrange::get(Context, nullptr, LV, UV, SEother));
1976 EXPECT_NE(N, DISubrange::get(Context, nullptr, LV, UV, SIother));
1977 }
1978
1979 TEST_F(DISubrangeTest, fortranAllocatableExpr) {
1980 DILocalScope *Scope = getSubprogram();
1981 DIFile *File = getFile();
1982 DIType *Type = getDerivedType();
1983 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
1984 auto *LE = DIExpression::get(Context, {1, 2});
1985 auto *UE = DIExpression::get(Context, {2, 3});
1986 auto *SE = DIExpression::get(Context, {3, 4});
1987 auto *LEother = DIExpression::get(Context, {5, 6});
1988 auto *LIother = ConstantAsMetadata::get(
1989 ConstantInt::getSigned(Type::getInt64Ty(Context), 20));
1990 auto *LVother = DILocalVariable::get(Context, Scope, "lbother", File, 8, Type,
1991 2, Flags, 8, nullptr);
1992
1993 auto *N = DISubrange::get(Context, nullptr, LE, UE, SE);
1994
1995 auto Lower = N->getLowerBound();
1996 ASSERT_TRUE(Lower);
1997 ASSERT_TRUE(isa<DIExpression *>(Lower));
1998 EXPECT_EQ(LE, cast<DIExpression *>(Lower));
1999
2000 auto Upper = N->getUpperBound();
2001 ASSERT_TRUE(Upper);
2002 ASSERT_TRUE(isa<DIExpression *>(Upper));
2003 EXPECT_EQ(UE, cast<DIExpression *>(Upper));
2004
2005 auto Stride = N->getStride();
2006 ASSERT_TRUE(Stride);
2007 ASSERT_TRUE(isa<DIExpression *>(Stride));
2008 EXPECT_EQ(SE, cast<DIExpression *>(Stride));
2009
2010 EXPECT_EQ(N, DISubrange::get(Context, nullptr, LE, UE, SE));
2011
2012 EXPECT_NE(N, DISubrange::get(Context, nullptr, LEother, UE, SE));
2013 EXPECT_NE(N, DISubrange::get(Context, nullptr, LIother, UE, SE));
2014 EXPECT_NE(N, DISubrange::get(Context, nullptr, LVother, UE, SE));
2015 }
2016
2017 typedef MetadataTest DISubrangeTypeTest;
2018
2019 TEST_F(DISubrangeTypeTest, get) {
2020 auto *Base =
2021 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "test_integer", 32, 0,
2022 dwarf::DW_ATE_signed, 100, DINode::FlagZero);
2023
2024 DILocalScope *Scope = getSubprogram();
2025 DIFile *File = getFile();
2026
2027 ConstantInt *Lower = ConstantInt::get(Context, APInt(32, -7, true));
2028 ConstantAsMetadata *LowerConst = ConstantAsMetadata::get(Lower);
2029 ConstantInt *Upper = ConstantInt::get(Context, APInt(32, 23, true));
2030 ConstantAsMetadata *UpperConst = ConstantAsMetadata::get(Upper);
2031
2032 auto *N = DISubrangeType::get(Context, StringRef(), File, 101, Scope, 32, 0,
2033 DINode::FlagZero, Base, LowerConst, UpperConst,
2034 nullptr, LowerConst);
2035 EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
2036
2037 auto L = N->getLowerBound();
2038 EXPECT_EQ(-7, cast<ConstantInt *>(L)->getSExtValue());
2039
2040 auto U = N->getUpperBound();
2041 EXPECT_EQ(23, cast<ConstantInt *>(U)->getSExtValue());
2042
2043 EXPECT_EQ(101u, N->getLine());
2044 EXPECT_EQ(32u, N->getSizeInBits());
2045
2046 TempDISubrangeType Temp = N->clone();
2047 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2048 }
2049
2050 typedef MetadataTest DIGenericSubrangeTest;
2051
2052 TEST_F(DIGenericSubrangeTest, fortranAssumedRankInt) {
2053 DILocalScope *Scope = getSubprogram();
2054 DIFile *File = getFile();
2055 DIType *Type = getDerivedType();
2056 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
2057 auto *LI = DIExpression::get(
2058 Context, {dwarf::DW_OP_consts, static_cast<uint64_t>(-10)});
2059 auto *UI = DIExpression::get(Context, {dwarf::DW_OP_consts, 10});
2060 auto *SI = DIExpression::get(Context, {dwarf::DW_OP_consts, 4});
2061 auto *UIother = DIExpression::get(Context, {dwarf::DW_OP_consts, 20});
2062 auto *UVother = DILocalVariable::get(Context, Scope, "ubother", File, 8, Type,
2063 2, Flags, 8, nullptr);
2064 auto *UEother = DIExpression::get(Context, {5, 6});
2065 auto *LIZero = DIExpression::get(Context, {dwarf::DW_OP_consts, 0});
2066 auto *UIZero = DIExpression::get(Context, {dwarf::DW_OP_consts, 0});
2067
2068 auto *N = DIGenericSubrange::get(Context, nullptr, LI, UI, SI);
2069
2070 auto Lower = N->getLowerBound();
2071 ASSERT_TRUE(Lower);
2072 ASSERT_TRUE(isa<DIExpression *>(Lower));
2073 EXPECT_EQ(dyn_cast_or_null<DIExpression>(LI), cast<DIExpression *>(Lower));
2074
2075 auto Upper = N->getUpperBound();
2076 ASSERT_TRUE(Upper);
2077 ASSERT_TRUE(isa<DIExpression *>(Upper));
2078 EXPECT_EQ(dyn_cast_or_null<DIExpression>(UI), cast<DIExpression *>(Upper));
2079
2080 auto Stride = N->getStride();
2081 ASSERT_TRUE(Stride);
2082 ASSERT_TRUE(isa<DIExpression *>(Stride));
2083 EXPECT_EQ(dyn_cast_or_null<DIExpression>(SI), cast<DIExpression *>(Stride));
2084
2085 EXPECT_EQ(N, DIGenericSubrange::get(Context, nullptr, LI, UI, SI));
2086
2087 EXPECT_NE(N, DIGenericSubrange::get(Context, nullptr, LI, UIother, SI));
2088 EXPECT_NE(N, DIGenericSubrange::get(Context, nullptr, LI, UEother, SI));
2089 EXPECT_NE(N, DIGenericSubrange::get(Context, nullptr, LI, UVother, SI));
2090
2091 auto *NZeroLower = DIGenericSubrange::get(Context, nullptr, LIZero, UI, SI);
2092 EXPECT_NE(NZeroLower,
2093 DIGenericSubrange::get(Context, nullptr, nullptr, UI, SI));
2094
2095 auto *NZeroUpper = DIGenericSubrange::get(Context, nullptr, LI, UIZero, SI);
2096 EXPECT_NE(NZeroUpper,
2097 DIGenericSubrange::get(Context, nullptr, LI, nullptr, SI));
2098 }
2099
2100 TEST_F(DIGenericSubrangeTest, fortranAssumedRankVar) {
2101 DILocalScope *Scope = getSubprogram();
2102 DIFile *File = getFile();
2103 DIType *Type = getDerivedType();
2104 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
2105 auto *LV =
2106 DILocalVariable::get(Context, Scope, "lb", File, 8, Type, 2, Flags, 8,
2107 nullptr);
2108 auto *UV =
2109 DILocalVariable::get(Context, Scope, "ub", File, 8, Type, 2, Flags, 8,
2110 nullptr);
2111 auto *SV =
2112 DILocalVariable::get(Context, Scope, "st", File, 8, Type, 2, Flags, 8,
2113 nullptr);
2114 auto *SVother = DILocalVariable::get(Context, Scope, "stother", File, 8, Type,
2115 2, Flags, 8, nullptr);
2116 auto *SIother = DIExpression::get(
2117 Context, {dwarf::DW_OP_consts, static_cast<uint64_t>(-1)});
2118 auto *SEother = DIExpression::get(Context, {5, 6});
2119
2120 auto *N = DIGenericSubrange::get(Context, nullptr, LV, UV, SV);
2121
2122 auto Lower = N->getLowerBound();
2123 ASSERT_TRUE(Lower);
2124 ASSERT_TRUE(isa<DIVariable *>(Lower));
2125 EXPECT_EQ(LV, cast<DIVariable *>(Lower));
2126
2127 auto Upper = N->getUpperBound();
2128 ASSERT_TRUE(Upper);
2129 ASSERT_TRUE(isa<DIVariable *>(Upper));
2130 EXPECT_EQ(UV, cast<DIVariable *>(Upper));
2131
2132 auto Stride = N->getStride();
2133 ASSERT_TRUE(Stride);
2134 ASSERT_TRUE(isa<DIVariable *>(Stride));
2135 EXPECT_EQ(SV, cast<DIVariable *>(Stride));
2136
2137 EXPECT_EQ(N, DIGenericSubrange::get(Context, nullptr, LV, UV, SV));
2138
2139 EXPECT_NE(N, DIGenericSubrange::get(Context, nullptr, LV, UV, SVother));
2140 EXPECT_NE(N, DIGenericSubrange::get(Context, nullptr, LV, UV, SEother));
2141 EXPECT_NE(N, DIGenericSubrange::get(Context, nullptr, LV, UV, SIother));
2142 }
2143
2144 TEST_F(DIGenericSubrangeTest, useDIBuilder) {
2145 DILocalScope *Scope = getSubprogram();
2146 DIFile *File = getFile();
2147 DIType *Type = getDerivedType();
2148 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
2149 auto *LV =
2150 DILocalVariable::get(Context, Scope, "lb", File, 8, Type, 2, Flags, 8, nullptr);
2151 auto *UE = DIExpression::get(Context, {2, 3});
2152 auto *SE = DIExpression::get(Context, {3, 4});
2153
2154 auto *LVother = DILocalVariable::get(Context, Scope, "lbother", File, 8, Type,
2155 2, Flags, 8, nullptr);
2156 auto *LIother = DIExpression::get(
2157 Context, {dwarf::DW_OP_consts, static_cast<uint64_t>(-1)});
2158
2159 Module M("M", Context);
2160 DIBuilder DIB(M);
2161
2162 auto *N = DIB.getOrCreateGenericSubrange(
2163 DIGenericSubrange::BoundType(nullptr), DIGenericSubrange::BoundType(LV),
2164 DIGenericSubrange::BoundType(UE), DIGenericSubrange::BoundType(SE));
2165
2166 auto Lower = N->getLowerBound();
2167 ASSERT_TRUE(Lower);
2168 ASSERT_TRUE(isa<DIVariable *>(Lower));
2169 EXPECT_EQ(LV, cast<DIVariable *>(Lower));
2170
2171 auto Upper = N->getUpperBound();
2172 ASSERT_TRUE(Upper);
2173 ASSERT_TRUE(isa<DIExpression *>(Upper));
2174 EXPECT_EQ(UE, cast<DIExpression *>(Upper));
2175
2176 auto Stride = N->getStride();
2177 ASSERT_TRUE(Stride);
2178 ASSERT_TRUE(isa<DIExpression *>(Stride));
2179 EXPECT_EQ(SE, cast<DIExpression *>(Stride));
2180
2181 EXPECT_EQ(
2182 N, DIB.getOrCreateGenericSubrange(DIGenericSubrange::BoundType(nullptr),
2183 DIGenericSubrange::BoundType(LV),
2184 DIGenericSubrange::BoundType(UE),
2185 DIGenericSubrange::BoundType(SE)));
2186
2187 EXPECT_NE(
2188 N, DIB.getOrCreateGenericSubrange(DIGenericSubrange::BoundType(nullptr),
2189 DIGenericSubrange::BoundType(LVother),
2190 DIGenericSubrange::BoundType(UE),
2191 DIGenericSubrange::BoundType(SE)));
2192 EXPECT_NE(
2193 N, DIB.getOrCreateGenericSubrange(DIGenericSubrange::BoundType(nullptr),
2194 DIGenericSubrange::BoundType(LIother),
2195 DIGenericSubrange::BoundType(UE),
2196 DIGenericSubrange::BoundType(SE)));
2197 }
2198 typedef MetadataTest DIEnumeratorTest;
2199
2200 TEST_F(DIEnumeratorTest, get) {
2201 auto *N = DIEnumerator::get(Context, 7, false, "name");
2202 EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
2203 EXPECT_EQ(7, N->getValue().getSExtValue());
2204 EXPECT_FALSE(N->isUnsigned());
2205 EXPECT_EQ("name", N->getName());
2206 EXPECT_EQ(N, DIEnumerator::get(Context, 7, false, "name"));
2207
2208 EXPECT_NE(N, DIEnumerator::get(Context, 7, true, "name"));
2209 EXPECT_NE(N, DIEnumerator::get(Context, 8, false, "name"));
2210 EXPECT_NE(N, DIEnumerator::get(Context, 7, false, "nam"));
2211
2212 TempDIEnumerator Temp = N->clone();
2213 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2214 }
2215
2216 TEST_F(DIEnumeratorTest, getWithLargeValues) {
2217 auto *N = DIEnumerator::get(Context, APInt::getMaxValue(128), false, "val");
2218 EXPECT_EQ(128U, N->getValue().popcount());
2219 EXPECT_EQ(N,
2220 DIEnumerator::get(Context, APInt::getMaxValue(128), false, "val"));
2221 EXPECT_NE(N,
2222 DIEnumerator::get(Context, APInt::getMinValue(128), false, "val"));
2223 }
2224
2225 typedef MetadataTest DIBasicTypeTest;
2226
2227 TEST_F(DIBasicTypeTest, get) {
2228 auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
2229 26, 7, 100, DINode::FlagZero);
2230 EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
2231 EXPECT_EQ("special", N->getName());
2232 EXPECT_EQ(33u, N->getSizeInBits());
2233 EXPECT_EQ(26u, N->getAlignInBits());
2234 EXPECT_EQ(7u, N->getEncoding());
2235 EXPECT_EQ(0u, N->getLine());
2236 EXPECT_EQ(100u, N->getNumExtraInhabitants());
2237 EXPECT_EQ(DINode::FlagZero, N->getFlags());
2238 EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
2239 26, 7, 100, DINode::FlagZero));
2240
2241 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
2242 "special", 33, 26, 7, 100, DINode::FlagZero));
2243 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26,
2244 7, 100, DINode::FlagZero));
2245 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
2246 26, 7, 100, DINode::FlagZero));
2247 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
2248 25, 7, 100, DINode::FlagZero));
2249
2250 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
2251 26, 7, 99, DINode::FlagZero));
2252 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
2253 26, 6, 100, DINode::FlagZero));
2254 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
2255 26, 7, 100, DINode::FlagBigEndian));
2256 EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
2257 26, 7, 100, DINode::FlagLittleEndian));
2258
2259 TempDIBasicType Temp = N->clone();
2260 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2261 }
2262
2263 TEST_F(DIBasicTypeTest, getWithLargeValues) {
2264 auto *N =
2265 DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", UINT64_MAX,
2266 UINT32_MAX - 1, 7, UINT32_MAX, DINode::FlagZero);
2267 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
2268 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
2269 EXPECT_EQ(UINT32_MAX, N->getNumExtraInhabitants());
2270 }
2271
2272 TEST_F(DIBasicTypeTest, getUnspecified) {
2273 auto *N =
2274 DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
2275 EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
2276 EXPECT_EQ("unspecified", N->getName());
2277 EXPECT_EQ(0u, N->getSizeInBits());
2278 EXPECT_EQ(0u, N->getAlignInBits());
2279 EXPECT_EQ(0u, N->getEncoding());
2280 EXPECT_EQ(0u, N->getLine());
2281 EXPECT_EQ(DINode::FlagZero, N->getFlags());
2282 }
2283
2284 typedef MetadataTest DITypeTest;
2285
2286 TEST_F(DITypeTest, clone) {
2287 // Check that DIType has a specialized clone that returns TempDIType.
2288 DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
2289 0, dwarf::DW_ATE_signed, DINode::FlagZero);
2290
2291 TempDIType Temp = N->clone();
2292 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2293 }
2294
2295 TEST_F(DITypeTest, cloneWithFlags) {
2296 // void (void)
2297 Metadata *TypesOps[] = {nullptr};
2298 Metadata *Types = MDTuple::get(Context, TypesOps);
2299
2300 DIType *D =
2301 DISubroutineType::getDistinct(Context, DINode::FlagZero, 0, Types);
2302 EXPECT_EQ(DINode::FlagZero, D->getFlags());
2303 TempDIType D2 = D->cloneWithFlags(DINode::FlagRValueReference);
2304 EXPECT_EQ(DINode::FlagRValueReference, D2->getFlags());
2305 EXPECT_EQ(DINode::FlagZero, D->getFlags());
2306
2307 TempDIType T =
2308 DISubroutineType::getTemporary(Context, DINode::FlagZero, 0, Types);
2309 EXPECT_EQ(DINode::FlagZero, T->getFlags());
2310 TempDIType T2 = T->cloneWithFlags(DINode::FlagRValueReference);
2311 EXPECT_EQ(DINode::FlagRValueReference, T2->getFlags());
2312 EXPECT_EQ(DINode::FlagZero, T->getFlags());
2313 }
2314
2315 typedef MetadataTest DIDerivedTypeTest;
2316
2317 TEST_F(DIDerivedTypeTest, get) {
2318 DIFile *File = getFile();
2319 DIScope *Scope = getSubprogram();
2320 DIType *BaseType = getBasicType("basic");
2321 MDTuple *ExtraData = getTuple();
2322 unsigned DWARFAddressSpace = 8;
2323 DIDerivedType::PtrAuthData PtrAuthData(1, false, 1234, true, true);
2324 DIDerivedType::PtrAuthData PtrAuthData2(1, false, 1234, true, false);
2325 DINode::DIFlags Flags5 = static_cast<DINode::DIFlags>(5);
2326 DINode::DIFlags Flags4 = static_cast<DINode::DIFlags>(4);
2327
2328 auto *N = DIDerivedType::get(
2329 Context, dwarf::DW_TAG_pointer_type, "something", File, 1, Scope,
2330 BaseType, 2, 3, 4, DWARFAddressSpace, std::nullopt, Flags5, ExtraData);
2331 auto *N1 = DIDerivedType::get(Context, dwarf::DW_TAG_LLVM_ptrauth_type, "",
2332 File, 1, Scope, N, 2, 3, 4, DWARFAddressSpace,
2333 PtrAuthData, Flags5, ExtraData);
2334 EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
2335 EXPECT_EQ("something", N->getName());
2336 EXPECT_EQ(File, N->getFile());
2337 EXPECT_EQ(1u, N->getLine());
2338 EXPECT_EQ(Scope, N->getScope());
2339 EXPECT_EQ(BaseType, N->getBaseType());
2340 EXPECT_EQ(2u, N->getSizeInBits());
2341 EXPECT_EQ(3u, N->getAlignInBits());
2342 EXPECT_EQ(4u, N->getOffsetInBits());
2343 EXPECT_EQ(DWARFAddressSpace, *N->getDWARFAddressSpace());
2344 EXPECT_EQ(std::nullopt, N->getPtrAuthData());
2345 EXPECT_EQ(PtrAuthData, N1->getPtrAuthData());
2346 EXPECT_NE(PtrAuthData2, N1->getPtrAuthData());
2347 EXPECT_EQ(5u, N->getFlags());
2348 EXPECT_EQ(ExtraData, N->getExtraData());
2349 EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2350 "something", File, 1, Scope, BaseType, 2, 3,
2351 4, DWARFAddressSpace, std::nullopt, Flags5,
2352 ExtraData));
2353
2354 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
2355 "something", File, 1, Scope, BaseType, 2, 3,
2356 4, DWARFAddressSpace, std::nullopt, Flags5,
2357 ExtraData));
2358 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
2359 File, 1, Scope, BaseType, 2, 3, 4,
2360 DWARFAddressSpace, std::nullopt, Flags5,
2361 ExtraData));
2362 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2363 "something", getFile(), 1, Scope, BaseType, 2,
2364 3, 4, DWARFAddressSpace, std::nullopt, Flags5,
2365 ExtraData));
2366 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2367 "something", File, 2, Scope, BaseType, 2, 3,
2368 4, DWARFAddressSpace, std::nullopt, Flags5,
2369 ExtraData));
2370 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2371 "something", File, 1, getSubprogram(),
2372 BaseType, 2, 3, 4, DWARFAddressSpace,
2373 std::nullopt, Flags5, ExtraData));
2374 EXPECT_NE(N, DIDerivedType::get(
2375 Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
2376 Scope, getBasicType("basic2"), 2, 3, 4, DWARFAddressSpace,
2377 std::nullopt, Flags5, ExtraData));
2378 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2379 "something", File, 1, Scope, BaseType, 3, 3,
2380 4, DWARFAddressSpace, std::nullopt, Flags5,
2381 ExtraData));
2382 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2383 "something", File, 1, Scope, BaseType, 2, 2,
2384 4, DWARFAddressSpace, std::nullopt, Flags5,
2385 ExtraData));
2386 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2387 "something", File, 1, Scope, BaseType, 2, 3,
2388 5, DWARFAddressSpace, std::nullopt, Flags5,
2389 ExtraData));
2390 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2391 "something", File, 1, Scope, BaseType, 2, 3,
2392 4, DWARFAddressSpace + 1, std::nullopt,
2393 Flags5, ExtraData));
2394 EXPECT_NE(N1,
2395 DIDerivedType::get(Context, dwarf::DW_TAG_LLVM_ptrauth_type, "",
2396 File, 1, Scope, N, 2, 3, 4, DWARFAddressSpace,
2397 std::nullopt, Flags5, ExtraData));
2398 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2399 "something", File, 1, Scope, BaseType, 2, 3,
2400 4, DWARFAddressSpace, std::nullopt, Flags4,
2401 ExtraData));
2402 EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
2403 "something", File, 1, Scope, BaseType, 2, 3,
2404 4, DWARFAddressSpace, std::nullopt, Flags5,
2405 getTuple()));
2406
2407 TempDIDerivedType Temp = N->clone();
2408 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2409 TempDIDerivedType Temp1 = N1->clone();
2410 EXPECT_EQ(N1, MDNode::replaceWithUniqued(std::move(Temp1)));
2411 }
2412
2413 TEST_F(DIDerivedTypeTest, getWithLargeValues) {
2414 DIFile *File = getFile();
2415 DIScope *Scope = getSubprogram();
2416 DIType *BaseType = getBasicType("basic");
2417 MDTuple *ExtraData = getTuple();
2418 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
2419
2420 auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
2421 File, 1, Scope, BaseType, UINT64_MAX,
2422 UINT32_MAX - 1, UINT64_MAX - 2, UINT32_MAX - 3,
2423 std::nullopt, Flags, ExtraData);
2424 EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
2425 EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
2426 EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
2427 EXPECT_EQ(UINT32_MAX - 3, *N->getDWARFAddressSpace());
2428
2429 auto *N1 = DIDerivedType::get(
2430 Context, dwarf::DW_TAG_LLVM_ptrauth_type, "", File, 1, Scope, N,
2431 UINT64_MAX, UINT32_MAX - 1, UINT64_MAX - 2, UINT32_MAX - 3,
2432 DIDerivedType::PtrAuthData(7, true, 0xffff, true, false), Flags,
2433 ExtraData);
2434 EXPECT_EQ(7U, N1->getPtrAuthData()->key());
2435 EXPECT_EQ(true, N1->getPtrAuthData()->isAddressDiscriminated());
2436 EXPECT_EQ(0xffffU, N1->getPtrAuthData()->extraDiscriminator());
2437 }
2438
2439 typedef MetadataTest DICompositeTypeTest;
2440
2441 TEST_F(DICompositeTypeTest, get) {
2442 unsigned Tag = dwarf::DW_TAG_structure_type;
2443 StringRef Name = "some name";
2444 DIFile *File = getFile();
2445 unsigned Line = 1;
2446 DIScope *Scope = getSubprogram();
2447 DIType *BaseType = getCompositeType();
2448 uint64_t SizeInBits = 2;
2449 uint32_t AlignInBits = 3;
2450 uint64_t OffsetInBits = 4;
2451 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
2452 MDTuple *Elements = getTuple();
2453 unsigned RuntimeLang = 6;
2454 DIType *VTableHolder = getCompositeType();
2455 MDTuple *TemplateParams = getTuple();
2456 StringRef Identifier = "some id";
2457 std::optional<uint32_t> EnumKind = 1;
2458
2459 auto *N = DICompositeType::get(
2460 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2461 OffsetInBits, Flags, Elements, RuntimeLang, EnumKind, VTableHolder,
2462 TemplateParams, Identifier);
2463 EXPECT_EQ(Tag, N->getTag());
2464 EXPECT_EQ(Name, N->getName());
2465 EXPECT_EQ(File, N->getFile());
2466 EXPECT_EQ(Line, N->getLine());
2467 EXPECT_EQ(Scope, N->getScope());
2468 EXPECT_EQ(BaseType, N->getBaseType());
2469 EXPECT_EQ(SizeInBits, N->getSizeInBits());
2470 EXPECT_EQ(AlignInBits, N->getAlignInBits());
2471 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
2472 EXPECT_EQ(Flags, N->getFlags());
2473 EXPECT_EQ(Elements, N->getElements().get());
2474 EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
2475 EXPECT_EQ(VTableHolder, N->getVTableHolder());
2476 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
2477 EXPECT_EQ(Identifier, N->getIdentifier());
2478 EXPECT_EQ(EnumKind, N->getEnumKind());
2479
2480 EXPECT_EQ(N, DICompositeType::get(
2481 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2482 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
2483 EnumKind, VTableHolder, TemplateParams, Identifier));
2484
2485 EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
2486 BaseType, SizeInBits, AlignInBits,
2487 OffsetInBits, Flags, Elements, RuntimeLang,
2488 EnumKind, VTableHolder, TemplateParams,
2489 Identifier));
2490 EXPECT_NE(N, DICompositeType::get(
2491 Context, Tag, "abc", File, Line, Scope, BaseType, SizeInBits,
2492 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
2493 EnumKind, VTableHolder, TemplateParams, Identifier));
2494 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
2495 BaseType, SizeInBits, AlignInBits,
2496 OffsetInBits, Flags, Elements, RuntimeLang,
2497 EnumKind, VTableHolder, TemplateParams,
2498 Identifier));
2499 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
2500 BaseType, SizeInBits, AlignInBits,
2501 OffsetInBits, Flags, Elements, RuntimeLang,
2502 EnumKind, VTableHolder, TemplateParams,
2503 Identifier));
2504 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line,
2505 getSubprogram(), BaseType, SizeInBits,
2506 AlignInBits, OffsetInBits, Flags, Elements,
2507 RuntimeLang, EnumKind, VTableHolder,
2508 TemplateParams, Identifier));
2509 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
2510 getBasicType("other"), SizeInBits,
2511 AlignInBits, OffsetInBits, Flags, Elements,
2512 RuntimeLang, EnumKind, VTableHolder,
2513 TemplateParams, Identifier));
2514 EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
2515 BaseType, SizeInBits + 1, AlignInBits,
2516 OffsetInBits, Flags, Elements, RuntimeLang,
2517 EnumKind, VTableHolder, TemplateParams,
2518 Identifier));
2519 EXPECT_NE(N, DICompositeType::get(
2520 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2521 AlignInBits + 1, OffsetInBits, Flags, Elements, RuntimeLang,
2522 EnumKind, VTableHolder, TemplateParams, Identifier));
2523 EXPECT_NE(N, DICompositeType::get(
2524 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2525 AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
2526 EnumKind, VTableHolder, TemplateParams, Identifier));
2527 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
2528 EXPECT_NE(N, DICompositeType::get(
2529 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2530 AlignInBits, OffsetInBits, FlagsPOne, Elements, RuntimeLang,
2531 EnumKind, VTableHolder, TemplateParams, Identifier));
2532 EXPECT_NE(N, DICompositeType::get(
2533 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2534 AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
2535 EnumKind, VTableHolder, TemplateParams, Identifier));
2536 EXPECT_NE(N, DICompositeType::get(
2537 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2538 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
2539 EnumKind, VTableHolder, TemplateParams, Identifier));
2540 EXPECT_NE(N, DICompositeType::get(
2541 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2542 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
2543 EnumKind, getCompositeType(), TemplateParams, Identifier));
2544 EXPECT_NE(N, DICompositeType::get(
2545 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2546 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
2547 EnumKind, VTableHolder, getTuple(), Identifier));
2548 EXPECT_NE(N, DICompositeType::get(
2549 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
2550 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
2551 EnumKind, VTableHolder, TemplateParams, "other"));
2552
2553 // Be sure that missing identifiers get null pointers.
2554 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
2555 BaseType, SizeInBits, AlignInBits,
2556 OffsetInBits, Flags, Elements, RuntimeLang,
2557 EnumKind, VTableHolder, TemplateParams, "")
2558 ->getRawIdentifier());
2559 EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
2560 BaseType, SizeInBits, AlignInBits,
2561 OffsetInBits, Flags, Elements, RuntimeLang,
2562 EnumKind, VTableHolder, TemplateParams)
2563 ->getRawIdentifier());
2564
2565 TempDICompositeType Temp = N->clone();
2566 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2567 }
2568
2569 TEST_F(DICompositeTypeTest, getWithLargeValues) {
2570 unsigned Tag = dwarf::DW_TAG_structure_type;
2571 StringRef Name = "some name";
2572 DIFile *File = getFile();
2573 unsigned Line = 1;
2574 DIScope *Scope = getSubprogram();
2575 DIType *BaseType = getCompositeType();
2576 uint64_t SizeInBits = UINT64_MAX;
2577 uint32_t AlignInBits = UINT32_MAX - 1;
2578 uint64_t OffsetInBits = UINT64_MAX - 2;
2579 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
2580 MDTuple *Elements = getTuple();
2581 unsigned RuntimeLang = 6;
2582 std::optional<uint32_t> EnumKind = 1;
2583 DIType *VTableHolder = getCompositeType();
2584 MDTuple *TemplateParams = getTuple();
2585 StringRef Identifier = "some id";
2586
2587 auto *N = DICompositeType::get(
2588 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2589 OffsetInBits, Flags, Elements, RuntimeLang, EnumKind, VTableHolder,
2590 TemplateParams, Identifier);
2591 EXPECT_EQ(SizeInBits, N->getSizeInBits());
2592 EXPECT_EQ(AlignInBits, N->getAlignInBits());
2593 EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
2594 }
2595
2596 TEST_F(DICompositeTypeTest, replaceOperands) {
2597 unsigned Tag = dwarf::DW_TAG_structure_type;
2598 StringRef Name = "some name";
2599 DIFile *File = getFile();
2600 unsigned Line = 1;
2601 DIScope *Scope = getSubprogram();
2602 DIType *BaseType = getCompositeType();
2603 uint64_t SizeInBits = 2;
2604 uint32_t AlignInBits = 3;
2605 uint64_t OffsetInBits = 4;
2606 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
2607 unsigned RuntimeLang = 6;
2608 std::optional<uint32_t> EnumKind = 1;
2609 StringRef Identifier = "some id";
2610
2611 auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
2612 BaseType, SizeInBits, AlignInBits,
2613 OffsetInBits, Flags, nullptr, RuntimeLang,
2614 EnumKind, nullptr, nullptr, Identifier);
2615
2616 auto *Elements = MDTuple::getDistinct(Context, {});
2617 EXPECT_EQ(nullptr, N->getElements().get());
2618 N->replaceElements(Elements);
2619 EXPECT_EQ(Elements, N->getElements().get());
2620 N->replaceElements(nullptr);
2621 EXPECT_EQ(nullptr, N->getElements().get());
2622
2623 DIType *VTableHolder = getCompositeType();
2624 EXPECT_EQ(nullptr, N->getVTableHolder());
2625 N->replaceVTableHolder(VTableHolder);
2626 EXPECT_EQ(VTableHolder, N->getVTableHolder());
2627 // As an extension, the containing type can be anything. This is
2628 // used by Rust to associate vtables with their concrete type.
2629 DIType *BasicType = getBasicType("basic");
2630 N->replaceVTableHolder(BasicType);
2631 EXPECT_EQ(BasicType, N->getVTableHolder());
2632 N->replaceVTableHolder(nullptr);
2633 EXPECT_EQ(nullptr, N->getVTableHolder());
2634
2635 auto *TemplateParams = MDTuple::getDistinct(Context, {});
2636 EXPECT_EQ(nullptr, N->getTemplateParams().get());
2637 N->replaceTemplateParams(TemplateParams);
2638 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
2639 N->replaceTemplateParams(nullptr);
2640 EXPECT_EQ(nullptr, N->getTemplateParams().get());
2641 }
2642
2643 TEST_F(DICompositeTypeTest, variant_part) {
2644 unsigned Tag = dwarf::DW_TAG_variant_part;
2645 StringRef Name = "some name";
2646 DIFile *File = getFile();
2647 unsigned Line = 1;
2648 DIScope *Scope = getSubprogram();
2649 DIType *BaseType = getCompositeType();
2650 uint64_t SizeInBits = 2;
2651 uint32_t AlignInBits = 3;
2652 uint64_t OffsetInBits = 4;
2653 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
2654 unsigned RuntimeLang = 6;
2655 std::optional<uint32_t> EnumKind = 1;
2656 StringRef Identifier = "some id";
2657 DIDerivedType *Discriminator = cast<DIDerivedType>(getDerivedType());
2658 DIDerivedType *Discriminator2 = cast<DIDerivedType>(getDerivedType());
2659
2660 EXPECT_NE(Discriminator, Discriminator2);
2661
2662 auto *N = DICompositeType::get(
2663 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2664 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2665 Identifier, Discriminator);
2666
2667 // Test the hashing.
2668 auto *Same = DICompositeType::get(
2669 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2670 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2671 Identifier, Discriminator);
2672 auto *Other = DICompositeType::get(
2673 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2674 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2675 Identifier, Discriminator2);
2676 auto *NoDisc = DICompositeType::get(
2677 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2678 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2679 Identifier, nullptr);
2680
2681 EXPECT_EQ(N, Same);
2682 EXPECT_NE(Same, Other);
2683 EXPECT_NE(Same, NoDisc);
2684 EXPECT_NE(Other, NoDisc);
2685
2686 EXPECT_EQ(N->getDiscriminator(), Discriminator);
2687 }
2688
2689 TEST_F(DICompositeTypeTest, dynamicArray) {
2690 unsigned Tag = dwarf::DW_TAG_array_type;
2691 StringRef Name = "some name";
2692 DIFile *File = getFile();
2693 unsigned Line = 1;
2694 DILocalScope *Scope = getSubprogram();
2695 DIType *BaseType = getCompositeType();
2696 uint64_t SizeInBits = 32;
2697 uint32_t AlignInBits = 32;
2698 uint64_t OffsetInBits = 4;
2699 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(3);
2700 unsigned RuntimeLang = 6;
2701 std::optional<uint32_t> EnumKind = 1;
2702 StringRef Identifier = "some id";
2703 DIType *Type = getDerivedType();
2704 Metadata *DlVar1 = DILocalVariable::get(Context, Scope, "dl_var1", File, 8,
2705 Type, 2, Flags, 8, nullptr);
2706 Metadata *DlVar2 = DILocalVariable::get(Context, Scope, "dl_var2", File, 8,
2707 Type, 2, Flags, 8, nullptr);
2708 uint64_t Elements1[] = {dwarf::DW_OP_push_object_address, dwarf::DW_OP_deref};
2709 Metadata *DataLocation1 = DIExpression::get(Context, Elements1);
2710
2711 uint64_t Elements2[] = {dwarf::DW_OP_constu, 0};
2712 Metadata *DataLocation2 = DIExpression::get(Context, Elements2);
2713
2714 uint64_t Elements3[] = {dwarf::DW_OP_constu, 3};
2715 Metadata *Rank1 = DIExpression::get(Context, Elements3);
2716
2717 uint64_t Elements4[] = {dwarf::DW_OP_constu, 4};
2718 Metadata *Rank2 = DIExpression::get(Context, Elements4);
2719
2720 ConstantInt *RankInt1 = ConstantInt::get(Context, APInt(7, 0));
2721 ConstantAsMetadata *RankConst1 = ConstantAsMetadata::get(RankInt1);
2722 ConstantInt *RankInt2 = ConstantInt::get(Context, APInt(6, 0));
2723 ConstantAsMetadata *RankConst2 = ConstantAsMetadata::get(RankInt2);
2724 auto *N1 = DICompositeType::get(
2725 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2726 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2727 Identifier, nullptr, DlVar1);
2728
2729 auto *Same1 = DICompositeType::get(
2730 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2731 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2732 Identifier, nullptr, DlVar1);
2733
2734 auto *Other1 = DICompositeType::get(
2735 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2736 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2737 Identifier, nullptr, DlVar2);
2738
2739 EXPECT_EQ(N1, Same1);
2740 EXPECT_NE(Same1, Other1);
2741 EXPECT_EQ(N1->getDataLocation(), DlVar1);
2742
2743 auto *N2 = DICompositeType::get(
2744 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2745 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2746 Identifier, nullptr, DataLocation1);
2747
2748 auto *Same2 = DICompositeType::get(
2749 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2750 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2751 Identifier, nullptr, DataLocation1);
2752
2753 auto *Other2 = DICompositeType::get(
2754 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2755 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2756 Identifier, nullptr, DataLocation2);
2757
2758 EXPECT_EQ(N2, Same2);
2759 EXPECT_NE(Same2, Other2);
2760 EXPECT_EQ(N2->getDataLocationExp(), DataLocation1);
2761
2762 auto *N3 = DICompositeType::get(
2763 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2764 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2765 Identifier, nullptr, DataLocation1, nullptr, nullptr, Rank1);
2766
2767 auto *Same3 = DICompositeType::get(
2768 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2769 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2770 Identifier, nullptr, DataLocation1, nullptr, nullptr, Rank1);
2771
2772 auto *Other3 = DICompositeType::get(
2773 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2774 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2775 Identifier, nullptr, DataLocation1, nullptr, nullptr, Rank2);
2776
2777 EXPECT_EQ(N3, Same3);
2778 EXPECT_NE(Same3, Other3);
2779 EXPECT_EQ(N3->getRankExp(), Rank1);
2780
2781 auto *N4 = DICompositeType::get(
2782 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2783 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2784 Identifier, nullptr, DataLocation1, nullptr, nullptr, RankConst1);
2785
2786 auto *Same4 = DICompositeType::get(
2787 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2788 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2789 Identifier, nullptr, DataLocation1, nullptr, nullptr, RankConst1);
2790
2791 auto *Other4 = DICompositeType::get(
2792 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
2793 OffsetInBits, Flags, nullptr, RuntimeLang, EnumKind, nullptr, nullptr,
2794 Identifier, nullptr, DataLocation1, nullptr, nullptr, RankConst2);
2795
2796 EXPECT_EQ(N4, Same4);
2797 EXPECT_NE(Same4, Other4);
2798 EXPECT_EQ(N4->getRankConst(), RankInt1);
2799 }
2800
2801 typedef MetadataTest DISubroutineTypeTest;
2802
2803 TEST_F(DISubroutineTypeTest, get) {
2804 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(1);
2805 DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
2806 MDTuple *TypeArray = getTuple();
2807
2808 auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray);
2809 EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
2810 EXPECT_EQ(Flags, N->getFlags());
2811 EXPECT_EQ(TypeArray, N->getTypeArray().get());
2812 EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray));
2813
2814 EXPECT_NE(N, DISubroutineType::get(Context, FlagsPOne, 0, TypeArray));
2815 EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple()));
2816
2817 // Test the hashing of calling conventions.
2818 auto *Fast = DISubroutineType::get(
2819 Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray);
2820 auto *Std = DISubroutineType::get(Context, Flags,
2821 dwarf::DW_CC_BORLAND_stdcall, TypeArray);
2822 EXPECT_EQ(Fast,
2823 DISubroutineType::get(Context, Flags,
2824 dwarf::DW_CC_BORLAND_msfastcall, TypeArray));
2825 EXPECT_EQ(Std, DISubroutineType::get(
2826 Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray));
2827
2828 EXPECT_NE(N, Fast);
2829 EXPECT_NE(N, Std);
2830 EXPECT_NE(Fast, Std);
2831
2832 TempDISubroutineType Temp = N->clone();
2833 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2834
2835 // Test always-empty operands.
2836 EXPECT_EQ(nullptr, N->getScope());
2837 EXPECT_EQ(nullptr, N->getFile());
2838 EXPECT_EQ("", N->getName());
2839 }
2840
2841 typedef MetadataTest DIFileTest;
2842
2843 TEST_F(DIFileTest, get) {
2844 StringRef Filename = "file";
2845 StringRef Directory = "dir";
2846 DIFile::ChecksumKind CSKind = DIFile::ChecksumKind::CSK_MD5;
2847 StringRef ChecksumString = "000102030405060708090a0b0c0d0e0f";
2848 DIFile::ChecksumInfo<StringRef> Checksum(CSKind, ChecksumString);
2849 StringRef Source = "source";
2850 auto *N = DIFile::get(Context, Filename, Directory, Checksum, Source);
2851
2852 EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
2853 EXPECT_EQ(Filename, N->getFilename());
2854 EXPECT_EQ(Directory, N->getDirectory());
2855 EXPECT_EQ(Checksum, N->getChecksum());
2856 EXPECT_EQ(Source, N->getSource());
2857 EXPECT_EQ(N, DIFile::get(Context, Filename, Directory, Checksum, Source));
2858
2859 EXPECT_NE(N, DIFile::get(Context, "other", Directory, Checksum, Source));
2860 EXPECT_NE(N, DIFile::get(Context, Filename, "other", Checksum, Source));
2861 DIFile::ChecksumInfo<StringRef> OtherChecksum(DIFile::ChecksumKind::CSK_SHA1, ChecksumString);
2862 EXPECT_NE(
2863 N, DIFile::get(Context, Filename, Directory, OtherChecksum));
2864 StringRef OtherSource = "other";
2865 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum, OtherSource));
2866 EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum));
2867 EXPECT_NE(N, DIFile::get(Context, Filename, Directory));
2868
2869 TempDIFile Temp = N->clone();
2870 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2871 }
2872
2873 TEST_F(DIFileTest, EmptySource) {
2874 DIFile *N = DIFile::get(Context, "file", "dir");
2875 EXPECT_EQ(std::nullopt, N->getSource());
2876
2877 std::optional<DIFile::ChecksumInfo<StringRef>> Checksum;
2878 std::optional<StringRef> Source;
2879 N = DIFile::get(Context, "file", "dir", Checksum, Source);
2880 EXPECT_EQ(Source, N->getSource());
2881
2882 Source = "";
2883 N = DIFile::get(Context, "file", "dir", Checksum, Source);
2884 EXPECT_EQ(Source, N->getSource());
2885 }
2886
2887 TEST_F(DIFileTest, ScopeGetFile) {
2888 // Ensure that DIScope::getFile() returns itself.
2889 DIScope *N = DIFile::get(Context, "file", "dir");
2890 EXPECT_EQ(N, N->getFile());
2891 }
2892
2893 typedef MetadataTest DICompileUnitTest;
2894
2895 TEST_F(DICompileUnitTest, get) {
2896 unsigned SourceLanguage = 1;
2897 DIFile *File = getFile();
2898 StringRef Producer = "some producer";
2899 bool IsOptimized = false;
2900 StringRef Flags = "flag after flag";
2901 unsigned RuntimeVersion = 2;
2902 StringRef SplitDebugFilename = "another/file";
2903 auto EmissionKind = DICompileUnit::FullDebug;
2904 MDTuple *EnumTypes = getTuple();
2905 MDTuple *RetainedTypes = getTuple();
2906 MDTuple *GlobalVariables = getTuple();
2907 MDTuple *ImportedEntities = getTuple();
2908 uint64_t DWOId = 0x10000000c0ffee;
2909 MDTuple *Macros = getTuple();
2910 StringRef SysRoot = "/";
2911 StringRef SDK = "MacOSX.sdk";
2912 auto *N = DICompileUnit::getDistinct(
2913 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
2914 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
2915 RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true,
2916 false, DICompileUnit::DebugNameTableKind::Default, false, SysRoot, SDK);
2917
2918 EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
2919 EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
2920 EXPECT_EQ(File, N->getFile());
2921 EXPECT_EQ(Producer, N->getProducer());
2922 EXPECT_EQ(IsOptimized, N->isOptimized());
2923 EXPECT_EQ(Flags, N->getFlags());
2924 EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
2925 EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
2926 EXPECT_EQ(EmissionKind, N->getEmissionKind());
2927 EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
2928 EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
2929 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
2930 EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
2931 EXPECT_EQ(Macros, N->getMacros().get());
2932 EXPECT_EQ(DWOId, N->getDWOId());
2933 EXPECT_EQ(SysRoot, N->getSysRoot());
2934 EXPECT_EQ(SDK, N->getSDK());
2935
2936 TempDICompileUnit Temp = N->clone();
2937 EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
2938 EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
2939 EXPECT_EQ(File, Temp->getFile());
2940 EXPECT_EQ(Producer, Temp->getProducer());
2941 EXPECT_EQ(IsOptimized, Temp->isOptimized());
2942 EXPECT_EQ(Flags, Temp->getFlags());
2943 EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
2944 EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
2945 EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
2946 EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
2947 EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
2948 EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
2949 EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
2950 EXPECT_EQ(Macros, Temp->getMacros().get());
2951 EXPECT_EQ(SysRoot, Temp->getSysRoot());
2952 EXPECT_EQ(SDK, Temp->getSDK());
2953
2954 auto *TempAddress = Temp.get();
2955 auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
2956 EXPECT_TRUE(Clone->isDistinct());
2957 EXPECT_EQ(TempAddress, Clone);
2958 }
2959
2960 TEST_F(DICompileUnitTest, replaceArrays) {
2961 unsigned SourceLanguage = 1;
2962 DIFile *File = getFile();
2963 StringRef Producer = "some producer";
2964 bool IsOptimized = false;
2965 StringRef Flags = "flag after flag";
2966 unsigned RuntimeVersion = 2;
2967 StringRef SplitDebugFilename = "another/file";
2968 auto EmissionKind = DICompileUnit::FullDebug;
2969 MDTuple *EnumTypes = MDTuple::getDistinct(Context, {});
2970 MDTuple *RetainedTypes = MDTuple::getDistinct(Context, {});
2971 MDTuple *ImportedEntities = MDTuple::getDistinct(Context, {});
2972 uint64_t DWOId = 0xc0ffee;
2973 StringRef SysRoot = "/";
2974 StringRef SDK = "MacOSX.sdk";
2975 auto *N = DICompileUnit::getDistinct(
2976 Context, SourceLanguage, File, Producer, IsOptimized, Flags,
2977 RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
2978 RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true, false,
2979 DICompileUnit::DebugNameTableKind::Default, false, SysRoot, SDK);
2980
2981 auto *GlobalVariables = MDTuple::getDistinct(Context, {});
2982 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
2983 N->replaceGlobalVariables(GlobalVariables);
2984 EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
2985 N->replaceGlobalVariables(nullptr);
2986 EXPECT_EQ(nullptr, N->getGlobalVariables().get());
2987
2988 auto *Macros = MDTuple::getDistinct(Context, {});
2989 EXPECT_EQ(nullptr, N->getMacros().get());
2990 N->replaceMacros(Macros);
2991 EXPECT_EQ(Macros, N->getMacros().get());
2992 N->replaceMacros(nullptr);
2993 EXPECT_EQ(nullptr, N->getMacros().get());
2994 }
2995
2996 typedef MetadataTest DISubprogramTest;
2997
2998 TEST_F(DISubprogramTest, get) {
2999 DIScope *Scope = getCompositeType();
3000 StringRef Name = "name";
3001 StringRef LinkageName = "linkage";
3002 DIFile *File = getFile();
3003 unsigned Line = 2;
3004 DISubroutineType *Type = getSubroutineType();
3005 bool IsLocalToUnit = false;
3006 bool IsDefinition = true;
3007 unsigned ScopeLine = 3;
3008 DIType *ContainingType = getCompositeType();
3009 unsigned Virtuality = 2;
3010 unsigned VirtualIndex = 5;
3011 int ThisAdjustment = -3;
3012 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(6);
3013 bool IsOptimized = false;
3014 MDTuple *TemplateParams = getTuple();
3015 DISubprogram *Declaration = getSubprogram();
3016 MDTuple *RetainedNodes = getTuple();
3017 MDTuple *ThrownTypes = getTuple();
3018 MDTuple *Annotations = getTuple();
3019 StringRef TargetFuncName = "target";
3020 DICompileUnit *Unit = getUnit();
3021 DISubprogram::DISPFlags SPFlags =
3022 static_cast<DISubprogram::DISPFlags>(Virtuality);
3023 assert(!IsLocalToUnit && IsDefinition && !IsOptimized &&
3024 "bools and SPFlags have to match");
3025 SPFlags |= DISubprogram::SPFlagDefinition;
3026 bool KeyInstructions = false;
3027
3028 auto *N = DISubprogram::get(
3029 Context, Scope, Name, LinkageName, File, Line, Type, ScopeLine,
3030 ContainingType, VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit,
3031 TemplateParams, Declaration, RetainedNodes, ThrownTypes, Annotations,
3032 TargetFuncName, KeyInstructions);
3033
3034 EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
3035 EXPECT_EQ(Scope, N->getScope());
3036 EXPECT_EQ(Name, N->getName());
3037 EXPECT_EQ(LinkageName, N->getLinkageName());
3038 EXPECT_EQ(File, N->getFile());
3039 EXPECT_EQ(Line, N->getLine());
3040 EXPECT_EQ(Type, N->getType());
3041 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
3042 EXPECT_EQ(IsDefinition, N->isDefinition());
3043 EXPECT_EQ(ScopeLine, N->getScopeLine());
3044 EXPECT_EQ(ContainingType, N->getContainingType());
3045 EXPECT_EQ(Virtuality, N->getVirtuality());
3046 EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
3047 EXPECT_EQ(ThisAdjustment, N->getThisAdjustment());
3048 EXPECT_EQ(Flags, N->getFlags());
3049 EXPECT_EQ(IsOptimized, N->isOptimized());
3050 EXPECT_EQ(Unit, N->getUnit());
3051 EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
3052 EXPECT_EQ(Declaration, N->getDeclaration());
3053 EXPECT_EQ(RetainedNodes, N->getRetainedNodes().get());
3054 EXPECT_EQ(ThrownTypes, N->getThrownTypes().get());
3055 EXPECT_EQ(Annotations, N->getAnnotations().get());
3056 EXPECT_EQ(TargetFuncName, N->getTargetFuncName());
3057 EXPECT_EQ(KeyInstructions, N->getKeyInstructionsEnabled());
3058 EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3059 Type, ScopeLine, ContainingType, VirtualIndex,
3060 ThisAdjustment, Flags, SPFlags, Unit,
3061 TemplateParams, Declaration, RetainedNodes,
3062 ThrownTypes, Annotations, TargetFuncName,
3063 KeyInstructions));
3064
3065 EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
3066 File, Line, Type, ScopeLine, ContainingType,
3067 VirtualIndex, ThisAdjustment, Flags, SPFlags,
3068 Unit, TemplateParams, Declaration,
3069 RetainedNodes, ThrownTypes, Annotations,
3070 TargetFuncName, KeyInstructions));
3071 EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
3072 Line, Type, ScopeLine, ContainingType,
3073 VirtualIndex, ThisAdjustment, Flags, SPFlags,
3074 Unit, TemplateParams, Declaration,
3075 RetainedNodes, ThrownTypes, Annotations,
3076 TargetFuncName, KeyInstructions));
3077 EXPECT_NE(N, DISubprogram::get(
3078 Context, Scope, Name, "other", File, Line, Type, ScopeLine,
3079 ContainingType, VirtualIndex, ThisAdjustment, Flags, SPFlags,
3080 Unit, TemplateParams, Declaration, RetainedNodes,
3081 ThrownTypes, Annotations, TargetFuncName, KeyInstructions));
3082 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
3083 Line, Type, ScopeLine, ContainingType,
3084 VirtualIndex, ThisAdjustment, Flags, SPFlags,
3085 Unit, TemplateParams, Declaration,
3086 RetainedNodes, ThrownTypes, Annotations,
3087 TargetFuncName, KeyInstructions));
3088 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
3089 Line + 1, Type, ScopeLine, ContainingType,
3090 VirtualIndex, ThisAdjustment, Flags, SPFlags,
3091 Unit, TemplateParams, Declaration,
3092 RetainedNodes, ThrownTypes, Annotations,
3093 TargetFuncName, KeyInstructions));
3094 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3095 getSubroutineType(), ScopeLine, ContainingType,
3096 VirtualIndex, ThisAdjustment, Flags, SPFlags,
3097 Unit, TemplateParams, Declaration,
3098 RetainedNodes, ThrownTypes, Annotations,
3099 TargetFuncName, KeyInstructions));
3100 EXPECT_NE(N, DISubprogram::get(
3101 Context, Scope, Name, LinkageName, File, Line, Type,
3102 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
3103 Flags, SPFlags ^ DISubprogram::SPFlagLocalToUnit, Unit,
3104 TemplateParams, Declaration, RetainedNodes, ThrownTypes,
3105 Annotations, TargetFuncName, KeyInstructions));
3106 EXPECT_NE(N, DISubprogram::get(
3107 Context, Scope, Name, LinkageName, File, Line, Type,
3108 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
3109 Flags, SPFlags ^ DISubprogram::SPFlagDefinition, Unit,
3110 TemplateParams, Declaration, RetainedNodes, ThrownTypes,
3111 Annotations, TargetFuncName, KeyInstructions));
3112 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3113 Type, ScopeLine + 1, ContainingType,
3114 VirtualIndex, ThisAdjustment, Flags, SPFlags,
3115 Unit, TemplateParams, Declaration,
3116 RetainedNodes, ThrownTypes, Annotations,
3117 TargetFuncName, KeyInstructions));
3118 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3119 Type, ScopeLine, getCompositeType(),
3120 VirtualIndex, ThisAdjustment, Flags, SPFlags,
3121 Unit, TemplateParams, Declaration,
3122 RetainedNodes, ThrownTypes, Annotations,
3123 TargetFuncName, KeyInstructions));
3124 EXPECT_NE(N, DISubprogram::get(
3125 Context, Scope, Name, LinkageName, File, Line, Type,
3126 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
3127 Flags, SPFlags ^ DISubprogram::SPFlagVirtual, Unit,
3128 TemplateParams, Declaration, RetainedNodes, ThrownTypes,
3129 Annotations, TargetFuncName, KeyInstructions));
3130 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3131 Type, ScopeLine, ContainingType,
3132 VirtualIndex + 1, ThisAdjustment, Flags,
3133 SPFlags, Unit, TemplateParams, Declaration,
3134 RetainedNodes, ThrownTypes, Annotations,
3135 TargetFuncName, KeyInstructions));
3136 EXPECT_NE(N, DISubprogram::get(
3137 Context, Scope, Name, LinkageName, File, Line, Type,
3138 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
3139 Flags, SPFlags ^ DISubprogram::SPFlagOptimized, Unit,
3140 TemplateParams, Declaration, RetainedNodes, ThrownTypes,
3141 Annotations, TargetFuncName, KeyInstructions));
3142 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3143 Type, ScopeLine, ContainingType, VirtualIndex,
3144 ThisAdjustment, Flags, SPFlags, nullptr,
3145 TemplateParams, Declaration, RetainedNodes,
3146 ThrownTypes, Annotations, TargetFuncName,
3147 KeyInstructions));
3148 EXPECT_NE(N, DISubprogram::get(
3149 Context, Scope, Name, LinkageName, File, Line, Type,
3150 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
3151 Flags, SPFlags, Unit, getTuple(), Declaration, RetainedNodes,
3152 ThrownTypes, Annotations, TargetFuncName, KeyInstructions));
3153 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3154 Type, ScopeLine, ContainingType, VirtualIndex,
3155 ThisAdjustment, Flags, SPFlags, Unit,
3156 TemplateParams, getSubprogram(), RetainedNodes,
3157 ThrownTypes, Annotations, TargetFuncName,
3158 KeyInstructions));
3159 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3160 Type, ScopeLine, ContainingType, VirtualIndex,
3161 ThisAdjustment, Flags, SPFlags, Unit,
3162 TemplateParams, Declaration, getTuple(),
3163 ThrownTypes, Annotations, TargetFuncName,
3164 KeyInstructions));
3165 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3166 Type, ScopeLine, ContainingType, VirtualIndex,
3167 ThisAdjustment, Flags, SPFlags, Unit,
3168 TemplateParams, Declaration, RetainedNodes,
3169 getTuple(), Annotations, TargetFuncName,
3170 KeyInstructions));
3171 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3172 Type, ScopeLine, ContainingType, VirtualIndex,
3173 ThisAdjustment, Flags, SPFlags, Unit,
3174 TemplateParams, Declaration, RetainedNodes,
3175 ThrownTypes, getTuple(), TargetFuncName,
3176 KeyInstructions));
3177 EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3178 Type, ScopeLine, ContainingType, VirtualIndex,
3179 ThisAdjustment, Flags, SPFlags, Unit,
3180 TemplateParams, Declaration, RetainedNodes,
3181 ThrownTypes, Annotations, "other",
3182 KeyInstructions));
3183 EXPECT_NE(N,
3184 DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
3185 Type, ScopeLine, ContainingType, VirtualIndex,
3186 ThisAdjustment, Flags, SPFlags, Unit,
3187 TemplateParams, Declaration, RetainedNodes,
3188 ThrownTypes, Annotations, TargetFuncName, true));
3189
3190 TempDISubprogram Temp = N->clone();
3191 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3192 }
3193
3194 typedef MetadataTest DILexicalBlockTest;
3195
3196 TEST_F(DILexicalBlockTest, get) {
3197 DILocalScope *Scope = getSubprogram();
3198 DIFile *File = getFile();
3199 unsigned Line = 5;
3200 unsigned Column = 8;
3201
3202 auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
3203
3204 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
3205 EXPECT_EQ(Scope, N->getScope());
3206 EXPECT_EQ(File, N->getFile());
3207 EXPECT_EQ(Line, N->getLine());
3208 EXPECT_EQ(Column, N->getColumn());
3209 EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
3210
3211 EXPECT_NE(N,
3212 DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
3213 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
3214 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
3215 EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
3216
3217 TempDILexicalBlock Temp = N->clone();
3218 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3219 }
3220
3221 TEST_F(DILexicalBlockTest, Overflow) {
3222 DISubprogram *SP = getSubprogram();
3223 DIFile *F = getFile();
3224 {
3225 auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
3226 EXPECT_EQ(2u, LB->getLine());
3227 EXPECT_EQ(7u, LB->getColumn());
3228 }
3229 unsigned U16 = 1u << 16;
3230 {
3231 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
3232 EXPECT_EQ(UINT32_MAX, LB->getLine());
3233 EXPECT_EQ(U16 - 1, LB->getColumn());
3234 }
3235 {
3236 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
3237 EXPECT_EQ(UINT32_MAX, LB->getLine());
3238 EXPECT_EQ(0u, LB->getColumn());
3239 }
3240 {
3241 auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
3242 EXPECT_EQ(UINT32_MAX, LB->getLine());
3243 EXPECT_EQ(0u, LB->getColumn());
3244 }
3245 }
3246
3247 typedef MetadataTest DILexicalBlockFileTest;
3248
3249 TEST_F(DILexicalBlockFileTest, get) {
3250 DILocalScope *Scope = getSubprogram();
3251 DIFile *File = getFile();
3252 unsigned Discriminator = 5;
3253
3254 auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
3255
3256 EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
3257 EXPECT_EQ(Scope, N->getScope());
3258 EXPECT_EQ(File, N->getFile());
3259 EXPECT_EQ(Discriminator, N->getDiscriminator());
3260 EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
3261
3262 EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
3263 Discriminator));
3264 EXPECT_NE(N,
3265 DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
3266 EXPECT_NE(N,
3267 DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
3268
3269 TempDILexicalBlockFile Temp = N->clone();
3270 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3271 }
3272
3273 typedef MetadataTest DINamespaceTest;
3274
3275 TEST_F(DINamespaceTest, get) {
3276 DIScope *Scope = getFile();
3277 StringRef Name = "namespace";
3278 bool ExportSymbols = true;
3279
3280 auto *N = DINamespace::get(Context, Scope, Name, ExportSymbols);
3281
3282 EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
3283 EXPECT_EQ(Scope, N->getScope());
3284 EXPECT_EQ(Name, N->getName());
3285 EXPECT_EQ(N, DINamespace::get(Context, Scope, Name, ExportSymbols));
3286 EXPECT_NE(N, DINamespace::get(Context, getFile(), Name, ExportSymbols));
3287 EXPECT_NE(N, DINamespace::get(Context, Scope, "other", ExportSymbols));
3288 EXPECT_NE(N, DINamespace::get(Context, Scope, Name, !ExportSymbols));
3289
3290 TempDINamespace Temp = N->clone();
3291 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3292 }
3293
3294 typedef MetadataTest DIModuleTest;
3295
3296 TEST_F(DIModuleTest, get) {
3297 DIFile *File = getFile();
3298 DIScope *Scope = getFile();
3299 StringRef Name = "module";
3300 StringRef ConfigMacro = "-DNDEBUG";
3301 StringRef Includes = "-I.";
3302 StringRef APINotes = "/tmp/m.apinotes";
3303 unsigned LineNo = 4;
3304 bool IsDecl = true;
3305
3306 auto *N = DIModule::get(Context, File, Scope, Name, ConfigMacro, Includes,
3307 APINotes, LineNo, IsDecl);
3308
3309 EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
3310 EXPECT_EQ(File, N->getFile());
3311 EXPECT_EQ(Scope, N->getScope());
3312 EXPECT_EQ(Name, N->getName());
3313 EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
3314 EXPECT_EQ(Includes, N->getIncludePath());
3315 EXPECT_EQ(APINotes, N->getAPINotesFile());
3316 EXPECT_EQ(LineNo, N->getLineNo());
3317 EXPECT_EQ(IsDecl, N->getIsDecl());
3318 EXPECT_EQ(N, DIModule::get(Context, File, Scope, Name, ConfigMacro, Includes,
3319 APINotes, LineNo, IsDecl));
3320 EXPECT_NE(N, DIModule::get(Context, getFile(), getFile(), Name, ConfigMacro,
3321 Includes, APINotes, LineNo, IsDecl));
3322 EXPECT_NE(N, DIModule::get(Context, File, Scope, "other", ConfigMacro,
3323 Includes, APINotes, LineNo, IsDecl));
3324 EXPECT_NE(N, DIModule::get(Context, File, Scope, Name, "other", Includes,
3325 APINotes, LineNo, IsDecl));
3326 EXPECT_NE(N, DIModule::get(Context, File, Scope, Name, ConfigMacro, "other",
3327 APINotes, LineNo, IsDecl));
3328 EXPECT_NE(N, DIModule::get(Context, File, Scope, Name, ConfigMacro, Includes,
3329 "other", LineNo, IsDecl));
3330 EXPECT_NE(N, DIModule::get(Context, getFile(), Scope, Name, ConfigMacro,
3331 Includes, APINotes, LineNo, IsDecl));
3332 EXPECT_NE(N, DIModule::get(Context, File, Scope, Name, ConfigMacro, Includes,
3333 APINotes, 5, IsDecl));
3334 EXPECT_NE(N, DIModule::get(Context, File, Scope, Name, ConfigMacro, Includes,
3335 APINotes, LineNo, false));
3336
3337 TempDIModule Temp = N->clone();
3338 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3339 }
3340
3341 typedef MetadataTest DITemplateTypeParameterTest;
3342
3343 TEST_F(DITemplateTypeParameterTest, get) {
3344 StringRef Name = "template";
3345 DIType *Type = getBasicType("basic");
3346 bool defaulted = false;
3347
3348 auto *N = DITemplateTypeParameter::get(Context, Name, Type, defaulted);
3349
3350 EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
3351 EXPECT_EQ(Name, N->getName());
3352 EXPECT_EQ(Type, N->getType());
3353 EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type, defaulted));
3354
3355 EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type, defaulted));
3356 EXPECT_NE(N, DITemplateTypeParameter::get(Context, Name,
3357 getBasicType("other"), defaulted));
3358 EXPECT_NE(N, DITemplateTypeParameter::get(Context, Name, Type, true));
3359
3360 TempDITemplateTypeParameter Temp = N->clone();
3361 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3362 }
3363
3364 typedef MetadataTest DITemplateValueParameterTest;
3365
3366 TEST_F(DITemplateValueParameterTest, get) {
3367 unsigned Tag = dwarf::DW_TAG_template_value_parameter;
3368 StringRef Name = "template";
3369 DIType *Type = getBasicType("basic");
3370 bool defaulted = false;
3371 Metadata *Value = getConstantAsMetadata();
3372
3373 auto *N =
3374 DITemplateValueParameter::get(Context, Tag, Name, Type, defaulted, Value);
3375 EXPECT_EQ(Tag, N->getTag());
3376 EXPECT_EQ(Name, N->getName());
3377 EXPECT_EQ(Type, N->getType());
3378 EXPECT_EQ(Value, N->getValue());
3379 EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
3380 defaulted, Value));
3381
3382 EXPECT_NE(N, DITemplateValueParameter::get(
3383 Context, dwarf::DW_TAG_GNU_template_template_param, Name,
3384 Type, defaulted, Value));
3385 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, "other", Type,
3386 defaulted, Value));
3387 EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
3388 getBasicType("other"), defaulted,
3389 Value));
3390 EXPECT_NE(N,
3391 DITemplateValueParameter::get(Context, Tag, Name, Type, defaulted,
3392 getConstantAsMetadata()));
3393 EXPECT_NE(
3394 N, DITemplateValueParameter::get(Context, Tag, Name, Type, true, Value));
3395
3396 TempDITemplateValueParameter Temp = N->clone();
3397 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3398 }
3399
3400 typedef MetadataTest DIGlobalVariableTest;
3401
3402 TEST_F(DIGlobalVariableTest, get) {
3403 DIScope *Scope = getSubprogram();
3404 StringRef Name = "name";
3405 StringRef LinkageName = "linkage";
3406 DIFile *File = getFile();
3407 unsigned Line = 5;
3408 DIType *Type = getDerivedType();
3409 bool IsLocalToUnit = false;
3410 bool IsDefinition = true;
3411 MDTuple *templateParams = getTuple();
3412 DIDerivedType *StaticDataMemberDeclaration =
3413 cast<DIDerivedType>(getDerivedType());
3414
3415 uint32_t AlignInBits = 8;
3416
3417 auto *N = DIGlobalVariable::get(
3418 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
3419 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits,
3420 nullptr);
3421
3422 EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
3423 EXPECT_EQ(Scope, N->getScope());
3424 EXPECT_EQ(Name, N->getName());
3425 EXPECT_EQ(LinkageName, N->getLinkageName());
3426 EXPECT_EQ(File, N->getFile());
3427 EXPECT_EQ(Line, N->getLine());
3428 EXPECT_EQ(Type, N->getType());
3429 EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
3430 EXPECT_EQ(IsDefinition, N->isDefinition());
3431 EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
3432 EXPECT_EQ(templateParams, N->getTemplateParams());
3433 EXPECT_EQ(AlignInBits, N->getAlignInBits());
3434 EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3435 Line, Type, IsLocalToUnit, IsDefinition,
3436 StaticDataMemberDeclaration,
3437 templateParams, AlignInBits, nullptr));
3438
3439 EXPECT_NE(N, DIGlobalVariable::get(
3440 Context, getSubprogram(), Name, LinkageName, File, Line,
3441 Type, IsLocalToUnit, IsDefinition,
3442 StaticDataMemberDeclaration, templateParams, AlignInBits,
3443 nullptr));
3444 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
3445 Line, Type, IsLocalToUnit, IsDefinition,
3446 StaticDataMemberDeclaration,
3447 templateParams, AlignInBits, nullptr));
3448 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
3449 Type, IsLocalToUnit, IsDefinition,
3450 StaticDataMemberDeclaration,
3451 templateParams, AlignInBits, nullptr));
3452 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName,
3453 getFile(), Line, Type, IsLocalToUnit,
3454 IsDefinition, StaticDataMemberDeclaration,
3455 templateParams, AlignInBits, nullptr));
3456 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3457 Line + 1, Type, IsLocalToUnit,
3458 IsDefinition, StaticDataMemberDeclaration,
3459 templateParams, AlignInBits, nullptr));
3460 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3461 Line, getDerivedType(), IsLocalToUnit,
3462 IsDefinition, StaticDataMemberDeclaration,
3463 templateParams, AlignInBits, nullptr));
3464 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3465 Line, Type, !IsLocalToUnit, IsDefinition,
3466 StaticDataMemberDeclaration,
3467 templateParams, AlignInBits, nullptr));
3468 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3469 Line, Type, IsLocalToUnit, !IsDefinition,
3470 StaticDataMemberDeclaration,
3471 templateParams, AlignInBits, nullptr));
3472 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3473 Line, Type, IsLocalToUnit, IsDefinition,
3474 cast<DIDerivedType>(getDerivedType()),
3475 templateParams, AlignInBits, nullptr));
3476 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3477 Line, Type, IsLocalToUnit, IsDefinition,
3478 StaticDataMemberDeclaration, nullptr,
3479 AlignInBits, nullptr));
3480 EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
3481 Line, Type, IsLocalToUnit, IsDefinition,
3482 StaticDataMemberDeclaration,
3483 templateParams, (AlignInBits << 1),
3484 nullptr));
3485
3486 TempDIGlobalVariable Temp = N->clone();
3487 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3488 }
3489
3490 typedef MetadataTest DIGlobalVariableExpressionTest;
3491
3492 TEST_F(DIGlobalVariableExpressionTest, get) {
3493 DIScope *Scope = getSubprogram();
3494 StringRef Name = "name";
3495 StringRef LinkageName = "linkage";
3496 DIFile *File = getFile();
3497 unsigned Line = 5;
3498 DIType *Type = getDerivedType();
3499 bool IsLocalToUnit = false;
3500 bool IsDefinition = true;
3501 MDTuple *templateParams = getTuple();
3502 auto *Expr = DIExpression::get(Context, {1, 2});
3503 auto *Expr2 = DIExpression::get(Context, {1, 2, 3});
3504 DIDerivedType *StaticDataMemberDeclaration =
3505 cast<DIDerivedType>(getDerivedType());
3506 uint32_t AlignInBits = 8;
3507
3508 auto *Var = DIGlobalVariable::get(
3509 Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
3510 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits,
3511 nullptr);
3512 auto *Var2 = DIGlobalVariable::get(
3513 Context, Scope, "other", LinkageName, File, Line, Type, IsLocalToUnit,
3514 IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits,
3515 nullptr);
3516 auto *N = DIGlobalVariableExpression::get(Context, Var, Expr);
3517
3518 EXPECT_EQ(Var, N->getVariable());
3519 EXPECT_EQ(Expr, N->getExpression());
3520 EXPECT_EQ(N, DIGlobalVariableExpression::get(Context, Var, Expr));
3521 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var2, Expr));
3522 EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var, Expr2));
3523
3524 TempDIGlobalVariableExpression Temp = N->clone();
3525 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3526 }
3527
3528 typedef MetadataTest DILocalVariableTest;
3529
3530 TEST_F(DILocalVariableTest, get) {
3531 DILocalScope *Scope = getSubprogram();
3532 StringRef Name = "name";
3533 DIFile *File = getFile();
3534 unsigned Line = 5;
3535 DIType *Type = getDerivedType();
3536 unsigned Arg = 6;
3537 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
3538 uint32_t AlignInBits = 8;
3539
3540 auto *N =
3541 DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags,
3542 AlignInBits, nullptr);
3543 EXPECT_TRUE(N->isParameter());
3544 EXPECT_EQ(Scope, N->getScope());
3545 EXPECT_EQ(Name, N->getName());
3546 EXPECT_EQ(File, N->getFile());
3547 EXPECT_EQ(Line, N->getLine());
3548 EXPECT_EQ(Type, N->getType());
3549 EXPECT_EQ(Arg, N->getArg());
3550 EXPECT_EQ(Flags, N->getFlags());
3551 EXPECT_EQ(AlignInBits, N->getAlignInBits());
3552 EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
3553 Flags, AlignInBits, nullptr));
3554
3555 EXPECT_FALSE(
3556 DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags,
3557 AlignInBits, nullptr)->isParameter());
3558 EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
3559 Type, Arg, Flags, AlignInBits, nullptr));
3560 EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
3561 Arg, Flags, AlignInBits, nullptr));
3562 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
3563 Arg, Flags, AlignInBits, nullptr));
3564 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
3565 Arg, Flags, AlignInBits, nullptr));
3566 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
3567 getDerivedType(), Arg, Flags, AlignInBits,
3568 nullptr));
3569 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
3570 Arg + 1, Flags, AlignInBits, nullptr));
3571 EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
3572 Arg, Flags, (AlignInBits << 1), nullptr));
3573
3574 TempDILocalVariable Temp = N->clone();
3575 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3576 }
3577
3578 TEST_F(DILocalVariableTest, getArg256) {
3579 EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
3580 0, nullptr, 255, DINode::FlagZero, 0,
3581 nullptr)
3582 ->getArg());
3583 EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
3584 0, nullptr, 256, DINode::FlagZero, 0,
3585 nullptr)
3586 ->getArg());
3587 EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
3588 0, nullptr, 257, DINode::FlagZero, 0,
3589 nullptr)
3590 ->getArg());
3591 unsigned Max = UINT16_MAX;
3592 EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
3593 0, nullptr, Max, DINode::FlagZero, 0,
3594 nullptr)
3595 ->getArg());
3596 }
3597
3598 typedef MetadataTest DIExpressionTest;
3599
3600 TEST_F(DIExpressionTest, get) {
3601 uint64_t Elements[] = {2, 6, 9, 78, 0};
3602 auto *N = DIExpression::get(Context, Elements);
3603 EXPECT_EQ(ArrayRef(Elements), N->getElements());
3604 EXPECT_EQ(N, DIExpression::get(Context, Elements));
3605
3606 EXPECT_EQ(5u, N->getNumElements());
3607 EXPECT_EQ(2u, N->getElement(0));
3608 EXPECT_EQ(6u, N->getElement(1));
3609 EXPECT_EQ(9u, N->getElement(2));
3610 EXPECT_EQ(78u, N->getElement(3));
3611 EXPECT_EQ(0u, N->getElement(4));
3612
3613 TempDIExpression Temp = N->clone();
3614 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
3615
3616 // Test DIExpression::prepend().
3617 uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
3618 auto *N0 = DIExpression::get(Context, Elts0);
3619 uint8_t DIExprFlags = DIExpression::ApplyOffset;
3620 DIExprFlags |= DIExpression::DerefBefore;
3621 DIExprFlags |= DIExpression::DerefAfter;
3622 DIExprFlags |= DIExpression::StackValue;
3623 auto *N0WithPrependedOps = DIExpression::prepend(N0, DIExprFlags, 64);
3624 uint64_t Elts1[] = {dwarf::DW_OP_deref,
3625 dwarf::DW_OP_plus_uconst, 64,
3626 dwarf::DW_OP_deref,
3627 dwarf::DW_OP_stack_value,
3628 dwarf::DW_OP_LLVM_fragment, 0, 32};
3629 auto *N1 = DIExpression::get(Context, Elts1);
3630 EXPECT_EQ(N0WithPrependedOps, N1);
3631
3632 // Test DIExpression::append().
3633 uint64_t Elts2[] = {dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 64,
3634 dwarf::DW_OP_deref, dwarf::DW_OP_stack_value};
3635 auto *N2 = DIExpression::append(N0, Elts2);
3636 EXPECT_EQ(N0WithPrependedOps, N2);
3637 }
3638
3639 TEST_F(DIExpressionTest, Fold) {
3640
3641 // Remove a No-op DW_OP_plus_uconst from an expression.
3642 SmallVector<uint64_t, 8> Ops = {dwarf::DW_OP_plus_uconst, 0};
3643 auto *Expr = DIExpression::get(Context, Ops);
3644 auto *E = Expr->foldConstantMath();
3645 SmallVector<uint64_t, 8> ResOps;
3646 auto *EmptyExpr = DIExpression::get(Context, ResOps);
3647 EXPECT_EQ(E, EmptyExpr);
3648
3649 // Remove a No-op add from an expression.
3650 Ops.clear();
3651 Ops.push_back(dwarf::DW_OP_constu);
3652 Ops.push_back(0);
3653 Ops.push_back(dwarf::DW_OP_plus);
3654 Expr = DIExpression::get(Context, Ops);
3655 E = Expr->foldConstantMath();
3656 EXPECT_EQ(E, EmptyExpr);
3657
3658 // Remove a No-op subtract from an expression.
3659 Ops.clear();
3660 Ops.push_back(dwarf::DW_OP_constu);
3661 Ops.push_back(0);
3662 Ops.push_back(dwarf::DW_OP_minus);
3663 Expr = DIExpression::get(Context, Ops);
3664 E = Expr->foldConstantMath();
3665 EXPECT_EQ(E, EmptyExpr);
3666
3667 // Remove a No-op shift left from an expression.
3668 Ops.clear();
3669 Ops.push_back(dwarf::DW_OP_constu);
3670 Ops.push_back(0);
3671 Ops.push_back(dwarf::DW_OP_shl);
3672 Expr = DIExpression::get(Context, Ops);
3673 E = Expr->foldConstantMath();
3674 EXPECT_EQ(E, EmptyExpr);
3675
3676 // Remove a No-op shift right from an expression.
3677 Ops.clear();
3678 Ops.push_back(dwarf::DW_OP_constu);
3679 Ops.push_back(0);
3680 Ops.push_back(dwarf::DW_OP_shr);
3681 Expr = DIExpression::get(Context, Ops);
3682 E = Expr->foldConstantMath();
3683 EXPECT_EQ(E, EmptyExpr);
3684
3685 // Remove a No-op multiply from an expression.
3686 Ops.clear();
3687 Ops.push_back(dwarf::DW_OP_constu);
3688 Ops.push_back(1);
3689 Ops.push_back(dwarf::DW_OP_mul);
3690 Expr = DIExpression::get(Context, Ops);
3691 E = Expr->foldConstantMath();
3692 EXPECT_EQ(E, EmptyExpr);
3693
3694 // Remove a No-op divide from an expression.
3695 Ops.clear();
3696 Ops.push_back(dwarf::DW_OP_constu);
3697 Ops.push_back(1);
3698 Ops.push_back(dwarf::DW_OP_div);
3699 Expr = DIExpression::get(Context, Ops);
3700 E = Expr->foldConstantMath();
3701 EXPECT_EQ(E, EmptyExpr);
3702
3703 // Test fold {DW_OP_plus_uconst, Const1, DW_OP_plus_uconst, Const2} ->
3704 // {DW_OP_plus_uconst, Const1 + Const2}
3705 Ops.clear();
3706 Ops.push_back(dwarf::DW_OP_plus_uconst);
3707 Ops.push_back(2);
3708 Ops.push_back(dwarf::DW_OP_plus_uconst);
3709 Ops.push_back(3);
3710 Expr = DIExpression::get(Context, Ops);
3711 E = Expr->foldConstantMath();
3712 ResOps.push_back(dwarf::DW_OP_plus_uconst);
3713 ResOps.push_back(5);
3714 auto *ResExpr = DIExpression::get(Context, ResOps);
3715 EXPECT_EQ(E, ResExpr);
3716
3717 // Test {DW_OP_constu, Const1, DW_OP_plus_uconst, Const2} -> {DW_OP_constu,
3718 // Const1 + Const2}
3719 Ops.clear();
3720 Ops.push_back(dwarf::DW_OP_constu);
3721 Ops.push_back(2);
3722 Ops.push_back(dwarf::DW_OP_plus_uconst);
3723 Ops.push_back(3);
3724 Expr = DIExpression::get(Context, Ops);
3725 E = Expr->foldConstantMath();
3726 ResOps.clear();
3727 ResOps.push_back(dwarf::DW_OP_constu);
3728 ResOps.push_back(5);
3729 ResExpr = DIExpression::get(Context, ResOps);
3730 EXPECT_EQ(E, ResExpr);
3731
3732 // Test {DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_plus} ->
3733 // {DW_OP_constu, Const1 + Const2}
3734 Ops.clear();
3735 Ops.push_back(dwarf::DW_OP_constu);
3736 Ops.push_back(8);
3737 Ops.push_back(dwarf::DW_OP_constu);
3738 Ops.push_back(2);
3739 Ops.push_back(dwarf::DW_OP_plus);
3740 Expr = DIExpression::get(Context, Ops);
3741 E = Expr->foldConstantMath();
3742 ResOps.clear();
3743 ResOps.push_back(dwarf::DW_OP_constu);
3744 ResOps.push_back(10);
3745 ResExpr = DIExpression::get(Context, ResOps);
3746 EXPECT_EQ(E, ResExpr);
3747
3748 // Test {DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_minus} ->
3749 // {DW_OP_constu, Const1 - Const2}
3750 Ops.clear();
3751 Ops.push_back(dwarf::DW_OP_constu);
3752 Ops.push_back(8);
3753 Ops.push_back(dwarf::DW_OP_constu);
3754 Ops.push_back(2);
3755 Ops.push_back(dwarf::DW_OP_minus);
3756 Expr = DIExpression::get(Context, Ops);
3757 E = Expr->foldConstantMath();
3758 ResOps.clear();
3759 ResOps.push_back(dwarf::DW_OP_constu);
3760 ResOps.push_back(6);
3761 ResExpr = DIExpression::get(Context, ResOps);
3762 EXPECT_EQ(E, ResExpr);
3763
3764 // Test {DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_mul} ->
3765 // {DW_OP_constu, Const1 * Const2}
3766 Ops.clear();
3767 Ops.push_back(dwarf::DW_OP_constu);
3768 Ops.push_back(8);
3769 Ops.push_back(dwarf::DW_OP_constu);
3770 Ops.push_back(2);
3771 Ops.push_back(dwarf::DW_OP_mul);
3772 Expr = DIExpression::get(Context, Ops);
3773 E = Expr->foldConstantMath();
3774 ResOps.clear();
3775 ResOps.push_back(dwarf::DW_OP_constu);
3776 ResOps.push_back(16);
3777 ResExpr = DIExpression::get(Context, ResOps);
3778 EXPECT_EQ(E, ResExpr);
3779
3780 // Test {DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_div} ->
3781 // {DW_OP_constu, Const1 / Const2}
3782 Ops.clear();
3783 Ops.push_back(dwarf::DW_OP_constu);
3784 Ops.push_back(8);
3785 Ops.push_back(dwarf::DW_OP_constu);
3786 Ops.push_back(2);
3787 Ops.push_back(dwarf::DW_OP_div);
3788 Expr = DIExpression::get(Context, Ops);
3789 E = Expr->foldConstantMath();
3790 ResOps.clear();
3791 ResOps.push_back(dwarf::DW_OP_constu);
3792 ResOps.push_back(4);
3793 ResExpr = DIExpression::get(Context, ResOps);
3794 EXPECT_EQ(E, ResExpr);
3795
3796 // Test {DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_shl} ->
3797 // {DW_OP_constu, Const1 << Const2}
3798 Ops.clear();
3799 Ops.push_back(dwarf::DW_OP_constu);
3800 Ops.push_back(8);
3801 Ops.push_back(dwarf::DW_OP_constu);
3802 Ops.push_back(2);
3803 Ops.push_back(dwarf::DW_OP_shl);
3804 Expr = DIExpression::get(Context, Ops);
3805 E = Expr->foldConstantMath();
3806 ResOps.clear();
3807 ResOps.push_back(dwarf::DW_OP_constu);
3808 ResOps.push_back(32);
3809 ResExpr = DIExpression::get(Context, ResOps);
3810 EXPECT_EQ(E, ResExpr);
3811
3812 // Test {DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_shr} ->
3813 // {DW_OP_constu, Const1 >> Const2}
3814 Ops.clear();
3815 Ops.push_back(dwarf::DW_OP_constu);
3816 Ops.push_back(8);
3817 Ops.push_back(dwarf::DW_OP_constu);
3818 Ops.push_back(2);
3819 Ops.push_back(dwarf::DW_OP_shr);
3820 Expr = DIExpression::get(Context, Ops);
3821 E = Expr->foldConstantMath();
3822 ResOps.clear();
3823 ResOps.push_back(dwarf::DW_OP_constu);
3824 ResOps.push_back(2);
3825 ResExpr = DIExpression::get(Context, ResOps);
3826 EXPECT_EQ(E, ResExpr);
3827
3828 // Test {DW_OP_plus_uconst, Const1, DW_OP_constu, Const2, DW_OP_plus} ->
3829 // {DW_OP_plus_uconst, Const1 + Const2}
3830 Ops.clear();
3831 Ops.push_back(dwarf::DW_OP_plus_uconst);
3832 Ops.push_back(8);
3833 Ops.push_back(dwarf::DW_OP_constu);
3834 Ops.push_back(2);
3835 Ops.push_back(dwarf::DW_OP_plus);
3836 Expr = DIExpression::get(Context, Ops);
3837 E = Expr->foldConstantMath();
3838 ResOps.clear();
3839 ResOps.push_back(dwarf::DW_OP_plus_uconst);
3840 ResOps.push_back(10);
3841 ResExpr = DIExpression::get(Context, ResOps);
3842 EXPECT_EQ(E, ResExpr);
3843
3844 // Test {DW_OP_constu, Const1, DW_OP_plus, DW_OP_plus_uconst, Const2} ->
3845 // {DW_OP_plus_uconst, Const1 + Const2}
3846 Ops.clear();
3847 Ops.push_back(dwarf::DW_OP_constu);
3848 Ops.push_back(8);
3849 Ops.push_back(dwarf::DW_OP_plus);
3850 Ops.push_back(dwarf::DW_OP_plus_uconst);
3851 Ops.push_back(2);
3852 Expr = DIExpression::get(Context, Ops);
3853 E = Expr->foldConstantMath();
3854 ResOps.clear();
3855 ResOps.push_back(dwarf::DW_OP_plus_uconst);
3856 ResOps.push_back(10);
3857 ResExpr = DIExpression::get(Context, ResOps);
3858 EXPECT_EQ(E, ResExpr);
3859
3860 // Test {DW_OP_constu, Const1, DW_OP_plus, DW_OP_constu, Const2, DW_OP_plus}
3861 // -> {DW_OP_plus_uconst, Const1 + Const2}
3862 Ops.clear();
3863 Ops.push_back(dwarf::DW_OP_constu);
3864 Ops.push_back(8);
3865 Ops.push_back(dwarf::DW_OP_plus);
3866 Ops.push_back(dwarf::DW_OP_constu);
3867 Ops.push_back(2);
3868 Ops.push_back(dwarf::DW_OP_plus);
3869 Expr = DIExpression::get(Context, Ops);
3870 E = Expr->foldConstantMath();
3871 ResOps.clear();
3872 ResOps.push_back(dwarf::DW_OP_plus_uconst);
3873 ResOps.push_back(10);
3874 ResExpr = DIExpression::get(Context, ResOps);
3875 EXPECT_EQ(E, ResExpr);
3876
3877 // Test {DW_OP_constu, Const1, DW_OP_mul, DW_OP_constu, Const2, DW_OP_mul} ->
3878 // {DW_OP_constu, Const1 * Const2, DW_OP_mul}
3879 Ops.clear();
3880 Ops.push_back(dwarf::DW_OP_constu);
3881 Ops.push_back(8);
3882 Ops.push_back(dwarf::DW_OP_mul);
3883 Ops.push_back(dwarf::DW_OP_constu);
3884 Ops.push_back(2);
3885 Ops.push_back(dwarf::DW_OP_mul);
3886 Expr = DIExpression::get(Context, Ops);
3887 E = Expr->foldConstantMath();
3888 ResOps.clear();
3889 ResOps.push_back(dwarf::DW_OP_constu);
3890 ResOps.push_back(16);
3891 ResOps.push_back(dwarf::DW_OP_mul);
3892 ResExpr = DIExpression::get(Context, ResOps);
3893 EXPECT_EQ(E, ResExpr);
3894
3895 // Test {DW_OP_plus_uconst, Const1, DW_OP_plus, DW_OP_LLVM_arg, Arg,
3896 // DW_OP_plus, DW_OP_constu, Const2, DW_OP_plus} -> {DW_OP_plus_uconst, Const1
3897 // + Const2, DW_OP_LLVM_arg, Arg, DW_OP_plus}
3898 Ops.clear();
3899 Ops.push_back(dwarf::DW_OP_plus_uconst);
3900 Ops.push_back(8);
3901 Ops.push_back(dwarf::DW_OP_LLVM_arg);
3902 Ops.push_back(0);
3903 Ops.push_back(dwarf::DW_OP_plus);
3904 Ops.push_back(dwarf::DW_OP_constu);
3905 Ops.push_back(2);
3906 Ops.push_back(dwarf::DW_OP_plus);
3907 Expr = DIExpression::get(Context, Ops);
3908 E = Expr->foldConstantMath();
3909 ResOps.clear();
3910 ResOps.push_back(dwarf::DW_OP_plus_uconst);
3911 ResOps.push_back(10);
3912 ResOps.push_back(dwarf::DW_OP_LLVM_arg);
3913 ResOps.push_back(0);
3914 ResOps.push_back(dwarf::DW_OP_plus);
3915 ResExpr = DIExpression::get(Context, ResOps);
3916 EXPECT_EQ(E, ResExpr);
3917
3918 // Test {DW_OP_constu, Const1, DW_OP_plus, DW_OP_LLVM_arg, Arg, DW_OP_plus,
3919 // DW_OP_plus_uconst, Const2} -> {DW_OP_constu, Const1 + Const2, DW_OP_plus,
3920 // DW_OP_LLVM_arg, Arg, DW_OP_plus}
3921 Ops.clear();
3922 Ops.push_back(dwarf::DW_OP_constu);
3923 Ops.push_back(8);
3924 Ops.push_back(dwarf::DW_OP_plus);
3925 Ops.push_back(dwarf::DW_OP_LLVM_arg);
3926 Ops.push_back(0);
3927 Ops.push_back(dwarf::DW_OP_plus);
3928 Ops.push_back(dwarf::DW_OP_plus_uconst);
3929 Ops.push_back(2);
3930 Expr = DIExpression::get(Context, Ops);
3931 E = Expr->foldConstantMath();
3932 ResOps.clear();
3933 ResOps.push_back(dwarf::DW_OP_plus_uconst);
3934 ResOps.push_back(10);
3935 ResOps.push_back(dwarf::DW_OP_LLVM_arg);
3936 ResOps.push_back(0);
3937 ResOps.push_back(dwarf::DW_OP_plus);
3938 ResExpr = DIExpression::get(Context, ResOps);
3939 EXPECT_EQ(E, ResExpr);
3940
3941 // Test {DW_OP_constu, Const1, DW_OP_plus, DW_OP_LLVM_arg, Arg, DW_OP_plus,
3942 // DW_OP_constu, Const2, DW_OP_plus} -> {DW_OP_constu, Const1 + Const2,
3943 // DW_OP_plus, DW_OP_LLVM_arg, Arg, DW_OP_plus}
3944 Ops.clear();
3945 Ops.push_back(dwarf::DW_OP_constu);
3946 Ops.push_back(8);
3947 Ops.push_back(dwarf::DW_OP_plus);
3948 Ops.push_back(dwarf::DW_OP_LLVM_arg);
3949 Ops.push_back(0);
3950 Ops.push_back(dwarf::DW_OP_plus);
3951 Ops.push_back(dwarf::DW_OP_constu);
3952 Ops.push_back(2);
3953 Ops.push_back(dwarf::DW_OP_plus);
3954 Expr = DIExpression::get(Context, Ops);
3955 E = Expr->foldConstantMath();
3956 ResOps.clear();
3957 ResOps.push_back(dwarf::DW_OP_plus_uconst);
3958 ResOps.push_back(10);
3959 ResOps.push_back(dwarf::DW_OP_LLVM_arg);
3960 ResOps.push_back(0);
3961 ResOps.push_back(dwarf::DW_OP_plus);
3962 ResExpr = DIExpression::get(Context, ResOps);
3963 EXPECT_EQ(E, ResExpr);
3964
3965 // Test {DW_OP_constu, Const1, DW_OP_mul, DW_OP_LLVM_arg, Arg, DW_OP_mul,
3966 // DW_OP_constu, Const2, DW_OP_mul} -> {DW_OP_constu, Const1 * Const2,
3967 // DW_OP_mul, DW_OP_LLVM_arg, Arg, DW_OP_mul}
3968 Ops.clear();
3969 Ops.push_back(dwarf::DW_OP_constu);
3970 Ops.push_back(8);
3971 Ops.push_back(dwarf::DW_OP_mul);
3972 Ops.push_back(dwarf::DW_OP_LLVM_arg);
3973 Ops.push_back(0);
3974 Ops.push_back(dwarf::DW_OP_mul);
3975 Ops.push_back(dwarf::DW_OP_constu);
3976 Ops.push_back(2);
3977 Ops.push_back(dwarf::DW_OP_mul);
3978 Expr = DIExpression::get(Context, Ops);
3979 E = Expr->foldConstantMath();
3980 ResOps.clear();
3981 ResOps.push_back(dwarf::DW_OP_constu);
3982 ResOps.push_back(16);
3983 ResOps.push_back(dwarf::DW_OP_mul);
3984 ResOps.push_back(dwarf::DW_OP_LLVM_arg);
3985 ResOps.push_back(0);
3986 ResOps.push_back(dwarf::DW_OP_mul);
3987 ResExpr = DIExpression::get(Context, ResOps);
3988 EXPECT_EQ(E, ResExpr);
3989
3990 // Test an overflow addition.
3991 Ops.clear();
3992 Ops.push_back(dwarf::DW_OP_plus_uconst);
3993 Ops.push_back(UINT64_MAX);
3994 Ops.push_back(dwarf::DW_OP_plus_uconst);
3995 Ops.push_back(2);
3996 Expr = DIExpression::get(Context, Ops);
3997 E = Expr->foldConstantMath();
3998 ResOps.clear();
3999 ResOps.push_back(dwarf::DW_OP_plus_uconst);
4000 ResOps.push_back(UINT64_MAX);
4001 ResOps.push_back(dwarf::DW_OP_plus_uconst);
4002 ResOps.push_back(2);
4003 ResExpr = DIExpression::get(Context, ResOps);
4004 EXPECT_EQ(E, ResExpr);
4005
4006 // Test an underflow subtraction.
4007 Ops.clear();
4008 Ops.push_back(dwarf::DW_OP_constu);
4009 Ops.push_back(1);
4010 Ops.push_back(dwarf::DW_OP_constu);
4011 Ops.push_back(2);
4012 Ops.push_back(dwarf::DW_OP_minus);
4013 Expr = DIExpression::get(Context, Ops);
4014 E = Expr->foldConstantMath();
4015 ResOps.clear();
4016 ResOps.push_back(dwarf::DW_OP_constu);
4017 ResOps.push_back(1);
4018 ResOps.push_back(dwarf::DW_OP_constu);
4019 ResOps.push_back(2);
4020 ResOps.push_back(dwarf::DW_OP_minus);
4021 ResExpr = DIExpression::get(Context, ResOps);
4022 EXPECT_EQ(E, ResExpr);
4023
4024 // Test a left shift greater than 63.
4025 Ops.clear();
4026 Ops.push_back(dwarf::DW_OP_constu);
4027 Ops.push_back(1);
4028 Ops.push_back(dwarf::DW_OP_constu);
4029 Ops.push_back(64);
4030 Ops.push_back(dwarf::DW_OP_shl);
4031 Expr = DIExpression::get(Context, Ops);
4032 E = Expr->foldConstantMath();
4033 ResOps.clear();
4034 ResOps.push_back(dwarf::DW_OP_constu);
4035 ResOps.push_back(1);
4036 ResOps.push_back(dwarf::DW_OP_constu);
4037 ResOps.push_back(64);
4038 ResOps.push_back(dwarf::DW_OP_shl);
4039 ResExpr = DIExpression::get(Context, ResOps);
4040 EXPECT_EQ(E, ResExpr);
4041
4042 // Test a right shift greater than 63.
4043 Ops.clear();
4044 Ops.push_back(dwarf::DW_OP_constu);
4045 Ops.push_back(1);
4046 Ops.push_back(dwarf::DW_OP_constu);
4047 Ops.push_back(64);
4048 Ops.push_back(dwarf::DW_OP_shr);
4049 Expr = DIExpression::get(Context, Ops);
4050 E = Expr->foldConstantMath();
4051 ResOps.clear();
4052 ResOps.push_back(dwarf::DW_OP_constu);
4053 ResOps.push_back(1);
4054 ResOps.push_back(dwarf::DW_OP_constu);
4055 ResOps.push_back(64);
4056 ResOps.push_back(dwarf::DW_OP_shr);
4057 ResExpr = DIExpression::get(Context, ResOps);
4058 EXPECT_EQ(E, ResExpr);
4059
4060 // Test an overflow multiplication.
4061 Ops.clear();
4062 Ops.push_back(dwarf::DW_OP_constu);
4063 Ops.push_back(UINT64_MAX);
4064 Ops.push_back(dwarf::DW_OP_constu);
4065 Ops.push_back(2);
4066 Ops.push_back(dwarf::DW_OP_mul);
4067 Expr = DIExpression::get(Context, Ops);
4068 E = Expr->foldConstantMath();
4069 ResOps.clear();
4070 ResOps.push_back(dwarf::DW_OP_constu);
4071 ResOps.push_back(UINT64_MAX);
4072 ResOps.push_back(dwarf::DW_OP_constu);
4073 ResOps.push_back(2);
4074 ResOps.push_back(dwarf::DW_OP_mul);
4075 ResExpr = DIExpression::get(Context, ResOps);
4076 EXPECT_EQ(E, ResExpr);
4077
4078 // Test a divide by 0.
4079 Ops.clear();
4080 Ops.push_back(dwarf::DW_OP_constu);
4081 Ops.push_back(2);
4082 Ops.push_back(dwarf::DW_OP_constu);
4083 Ops.push_back(0);
4084 Ops.push_back(dwarf::DW_OP_div);
4085 Expr = DIExpression::get(Context, Ops);
4086 E = Expr->foldConstantMath();
4087 ResOps.clear();
4088 ResOps.push_back(dwarf::DW_OP_constu);
4089 ResOps.push_back(2);
4090 ResOps.push_back(dwarf::DW_OP_lit0);
4091 ResOps.push_back(dwarf::DW_OP_div);
4092 ResExpr = DIExpression::get(Context, ResOps);
4093 EXPECT_EQ(E, ResExpr);
4094 }
4095
4096 TEST_F(DIExpressionTest, Append) {
4097 // Test appending a {dwarf::DW_OP_constu, <const>, DW_OP_plus} to a DW_OP_plus
4098 // expression
4099 SmallVector<uint64_t, 8> Ops = {dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_constu,
4100 2, dwarf::DW_OP_plus};
4101 auto *Expr = DIExpression::get(Context, Ops);
4102 SmallVector<uint64_t, 8> AppendOps = {dwarf::DW_OP_constu, 3,
4103 dwarf::DW_OP_plus};
4104 auto *AppendExpr = DIExpression::append(Expr, AppendOps);
4105 SmallVector<uint64_t, 8> OpsRes = {dwarf::DW_OP_LLVM_arg, 0,
4106 dwarf::DW_OP_plus_uconst, 5};
4107 auto *ResExpr = DIExpression::get(Context, OpsRes);
4108 EXPECT_EQ(ResExpr, AppendExpr);
4109
4110 // Test appending a {dwarf::DW_OP_plus_uconst, <const>} to a DW_OP_plus
4111 // expression uint64_t PlusUConstOps[] = {dwarf::DW_OP_plus_uconst, 3};
4112 AppendOps.clear();
4113 AppendOps.push_back(dwarf::DW_OP_plus_uconst);
4114 AppendOps.push_back(3);
4115 AppendExpr = DIExpression::append(Expr, AppendOps);
4116 OpsRes.clear();
4117 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4118 OpsRes.push_back(0);
4119 OpsRes.push_back(dwarf::DW_OP_plus_uconst);
4120 OpsRes.push_back(5);
4121 ResExpr = DIExpression::get(Context, OpsRes);
4122 EXPECT_EQ(ResExpr, AppendExpr);
4123
4124 // Test appending a {dwarf::DW_OP_constu, 0, DW_OP_plus} to an expression
4125 AppendOps.clear();
4126 AppendOps.push_back(dwarf::DW_OP_constu);
4127 AppendOps.push_back(0);
4128 AppendOps.push_back(dwarf::DW_OP_plus);
4129 AppendExpr = DIExpression::append(Expr, AppendOps);
4130 OpsRes.clear();
4131 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4132 OpsRes.push_back(0);
4133 OpsRes.push_back(dwarf::DW_OP_plus_uconst);
4134 OpsRes.push_back(2);
4135 ResExpr = DIExpression::get(Context, OpsRes);
4136 EXPECT_EQ(ResExpr, AppendExpr);
4137
4138 // Test appending a {dwarf::DW_OP_constu, 0, DW_OP_minus} to an expression
4139 AppendOps.clear();
4140 AppendOps.push_back(dwarf::DW_OP_constu);
4141 AppendOps.push_back(0);
4142 AppendOps.push_back(dwarf::DW_OP_minus);
4143 AppendExpr = DIExpression::append(Expr, AppendOps);
4144 OpsRes.clear();
4145 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4146 OpsRes.push_back(0);
4147 OpsRes.push_back(dwarf::DW_OP_plus_uconst);
4148 OpsRes.push_back(2);
4149 ResExpr = DIExpression::get(Context, OpsRes);
4150 EXPECT_EQ(ResExpr, AppendExpr);
4151
4152 // Test appending a {dwarf::DW_OP_constu, 0, DW_OP_shl} to an expression
4153 AppendOps.clear();
4154 AppendOps.push_back(dwarf::DW_OP_constu);
4155 AppendOps.push_back(0);
4156 AppendOps.push_back(dwarf::DW_OP_shl);
4157 AppendExpr = DIExpression::append(Expr, AppendOps);
4158 OpsRes.clear();
4159 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4160 OpsRes.push_back(0);
4161 OpsRes.push_back(dwarf::DW_OP_plus_uconst);
4162 OpsRes.push_back(2);
4163 ResExpr = DIExpression::get(Context, OpsRes);
4164 EXPECT_EQ(ResExpr, AppendExpr);
4165
4166 // Test appending a {dwarf::DW_OP_constu, 0, DW_OP_shr} to an expression
4167 AppendOps.clear();
4168 AppendOps.push_back(dwarf::DW_OP_constu);
4169 AppendOps.push_back(0);
4170 AppendOps.push_back(dwarf::DW_OP_shr);
4171 AppendExpr = DIExpression::append(Expr, AppendOps);
4172 OpsRes.clear();
4173 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4174 OpsRes.push_back(0);
4175 OpsRes.push_back(dwarf::DW_OP_plus_uconst);
4176 OpsRes.push_back(2);
4177 ResExpr = DIExpression::get(Context, OpsRes);
4178 EXPECT_EQ(ResExpr, AppendExpr);
4179
4180 // Test appending a {dwarf::DW_OP_constu, <const>, DW_OP_mul} to a DW_OP_mul
4181 // expression
4182 Ops.clear();
4183 Ops.push_back(dwarf::DW_OP_LLVM_arg);
4184 Ops.push_back(0);
4185 Ops.push_back(dwarf::DW_OP_constu);
4186 Ops.push_back(2);
4187 Ops.push_back(dwarf::DW_OP_mul);
4188 Expr = DIExpression::get(Context, Ops);
4189 AppendOps.clear();
4190 AppendOps.push_back(dwarf::DW_OP_constu);
4191 AppendOps.push_back(3);
4192 AppendOps.push_back(dwarf::DW_OP_mul);
4193 AppendExpr = DIExpression::append(Expr, AppendOps);
4194 OpsRes.clear();
4195 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4196 OpsRes.push_back(0);
4197 OpsRes.push_back(dwarf::DW_OP_constu);
4198 OpsRes.push_back(6);
4199 OpsRes.push_back(dwarf::DW_OP_mul);
4200 ResExpr = DIExpression::get(Context, OpsRes);
4201 EXPECT_EQ(ResExpr, AppendExpr);
4202
4203 // Test appending a {dwarf::DW_OP_constu, 1, DW_OP_mul} to an expression
4204 AppendOps.clear();
4205 AppendOps.push_back(dwarf::DW_OP_constu);
4206 AppendOps.push_back(1);
4207 AppendOps.push_back(dwarf::DW_OP_mul);
4208 AppendExpr = DIExpression::append(Expr, AppendOps);
4209 OpsRes.clear();
4210 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4211 OpsRes.push_back(0);
4212 OpsRes.push_back(dwarf::DW_OP_constu);
4213 OpsRes.push_back(2);
4214 OpsRes.push_back(dwarf::DW_OP_mul);
4215 ResExpr = DIExpression::get(Context, OpsRes);
4216 EXPECT_EQ(ResExpr, AppendExpr);
4217
4218 // Test appending a {dwarf::DW_OP_constu, 1, DW_OP_div} to an expression
4219 AppendOps.clear();
4220 AppendOps.push_back(dwarf::DW_OP_constu);
4221 AppendOps.push_back(1);
4222 AppendOps.push_back(dwarf::DW_OP_div);
4223 AppendExpr = DIExpression::append(Expr, AppendOps);
4224 OpsRes.clear();
4225 OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
4226 OpsRes.push_back(0);
4227 OpsRes.push_back(dwarf::DW_OP_constu);
4228 OpsRes.push_back(2);
4229 OpsRes.push_back(dwarf::DW_OP_mul);
4230 ResExpr = DIExpression::get(Context, OpsRes);
4231 EXPECT_EQ(ResExpr, AppendExpr);
4232 }
4233
4234 TEST_F(DIExpressionTest, isValid) {
4235 #define EXPECT_VALID(...) \
4236 do { \
4237 uint64_t Elements[] = {__VA_ARGS__}; \
4238 EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
4239 } while (false)
4240 #define EXPECT_INVALID(...) \
4241 do { \
4242 uint64_t Elements[] = {__VA_ARGS__}; \
4243 EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
4244 } while (false)
4245
4246 // Empty expression should be valid.
4247 EXPECT_TRUE(DIExpression::get(Context, {})->isValid());
4248
4249 // Valid constructions.
4250 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6);
4251 EXPECT_VALID(dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus);
4252 EXPECT_VALID(dwarf::DW_OP_deref);
4253 EXPECT_VALID(dwarf::DW_OP_LLVM_fragment, 3, 7);
4254 EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref);
4255 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6);
4256 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_LLVM_fragment, 3, 7);
4257 EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6,
4258 dwarf::DW_OP_LLVM_fragment, 3, 7);
4259 EXPECT_VALID(dwarf::DW_OP_LLVM_entry_value, 1);
4260 EXPECT_VALID(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_LLVM_entry_value, 1);
4261
4262 // Invalid constructions.
4263 EXPECT_INVALID(~0u);
4264 EXPECT_INVALID(dwarf::DW_OP_plus, 0);
4265 EXPECT_INVALID(dwarf::DW_OP_plus_uconst);
4266 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment);
4267 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3);
4268 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_plus_uconst, 3);
4269 EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_deref);
4270 EXPECT_INVALID(dwarf::DW_OP_LLVM_entry_value, 2);
4271 EXPECT_INVALID(dwarf::DW_OP_plus_uconst, 5, dwarf::DW_OP_LLVM_entry_value, 1);
4272 EXPECT_INVALID(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 5,
4273 dwarf::DW_OP_LLVM_entry_value, 1);
4274 EXPECT_INVALID(dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_LLVM_entry_value, 1);
4275
4276 #undef EXPECT_VALID
4277 #undef EXPECT_INVALID
4278 }
4279
4280 TEST_F(DIExpressionTest, createFragmentExpression) {
4281 #define EXPECT_VALID_FRAGMENT(Offset, Size, ...) \
4282 do { \
4283 uint64_t Elements[] = {__VA_ARGS__}; \
4284 DIExpression *Expression = DIExpression::get(Context, Elements); \
4285 EXPECT_TRUE( \
4286 DIExpression::createFragmentExpression(Expression, Offset, Size) \
4287 .has_value()); \
4288 } while (false)
4289 #define EXPECT_INVALID_FRAGMENT(Offset, Size, ...) \
4290 do { \
4291 uint64_t Elements[] = {__VA_ARGS__}; \
4292 DIExpression *Expression = DIExpression::get(Context, Elements); \
4293 EXPECT_FALSE( \
4294 DIExpression::createFragmentExpression(Expression, Offset, Size) \
4295 .has_value()); \
4296 } while (false)
4297
4298 // createFragmentExpression adds correct ops.
4299 std::optional<DIExpression*> R = DIExpression::createFragmentExpression(
4300 DIExpression::get(Context, {}), 0, 32);
4301 EXPECT_EQ(R.has_value(), true);
4302 EXPECT_EQ(3u, (*R)->getNumElements());
4303 EXPECT_EQ(dwarf::DW_OP_LLVM_fragment, (*R)->getElement(0));
4304 EXPECT_EQ(0u, (*R)->getElement(1));
4305 EXPECT_EQ(32u, (*R)->getElement(2));
4306
4307 // Valid fragment expressions.
4308 EXPECT_VALID_FRAGMENT(0, 32, {});
4309 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_deref);
4310 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_LLVM_fragment, 0, 32);
4311 EXPECT_VALID_FRAGMENT(16, 16, dwarf::DW_OP_LLVM_fragment, 0, 32);
4312
4313 // Invalid fragment expressions (incompatible ops).
4314 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus,
4315 dwarf::DW_OP_stack_value);
4316 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 14, dwarf::DW_OP_minus,
4317 dwarf::DW_OP_stack_value);
4318 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 16, dwarf::DW_OP_shr,
4319 dwarf::DW_OP_stack_value);
4320 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 16, dwarf::DW_OP_shl,
4321 dwarf::DW_OP_stack_value);
4322 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 16, dwarf::DW_OP_shra,
4323 dwarf::DW_OP_stack_value);
4324 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6,
4325 dwarf::DW_OP_stack_value);
4326
4327 // Fragments can be created for expressions using DW_OP_plus to compute an
4328 // address.
4329 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus);
4330 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref);
4331 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref,
4332 dwarf::DW_OP_stack_value);
4333
4334 // Check the other deref operations work in the same way.
4335 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6,
4336 dwarf::DW_OP_deref_size, 1);
4337 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6,
4338 dwarf::DW_OP_deref_type, 1, 1);
4339 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6,
4340 dwarf::DW_OP_xderef);
4341 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6,
4342 dwarf::DW_OP_xderef_size, 1);
4343 EXPECT_VALID_FRAGMENT(0, 32, dwarf::DW_OP_plus_uconst, 6,
4344 dwarf::DW_OP_xderef_type, 1, 1);
4345
4346 // Fragments cannot be created for expressions using DW_OP_plus to compute an
4347 // implicit value (check that this correctly fails even though there is a
4348 // deref in the expression).
4349 EXPECT_INVALID_FRAGMENT(0, 32, dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst,
4350 2, dwarf::DW_OP_stack_value);
4351
4352 #undef EXPECT_VALID_FRAGMENT
4353 #undef EXPECT_INVALID_FRAGMENT
4354 }
4355
4356 TEST_F(DIExpressionTest, extractLeadingOffset) {
4357 int64_t Offset;
4358 SmallVector<uint64_t> Remaining;
4359 using namespace dwarf;
4360 #define OPS(...) SmallVector<uint64_t>(ArrayRef<uint64_t>{__VA_ARGS__})
4361 #define EXTRACT_FROM(...) \
4362 DIExpression::get(Context, {__VA_ARGS__}) \
4363 ->extractLeadingOffset(Offset, Remaining)
4364 // Test the number of expression inputs
4365 // ------------------------------------
4366 //
4367 // Single location expressions are permitted.
4368 EXPECT_TRUE(EXTRACT_FROM(DW_OP_plus_uconst, 2));
4369 EXPECT_EQ(Offset, 2);
4370 EXPECT_EQ(Remaining.size(), 0u);
4371 // This is also a single-location.
4372 EXPECT_TRUE(EXTRACT_FROM(DW_OP_LLVM_arg, 0, DW_OP_plus_uconst, 2));
4373 EXPECT_EQ(Offset, 2);
4374 EXPECT_EQ(Remaining.size(), 0u);
4375 // Variadic locations are not permitted. A non-zero arg is assumed to
4376 // indicate multiple inputs.
4377 EXPECT_FALSE(EXTRACT_FROM(DW_OP_LLVM_arg, 1));
4378 EXPECT_FALSE(EXTRACT_FROM(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus));
4379
4380 // Test offsets expressions
4381 // ------------------------
4382 EXPECT_TRUE(EXTRACT_FROM());
4383 EXPECT_EQ(Offset, 0);
4384 EXPECT_EQ(Remaining.size(), 0u);
4385
4386 EXPECT_TRUE(EXTRACT_FROM(DW_OP_constu, 4, DW_OP_plus));
4387 EXPECT_EQ(Offset, 4);
4388 EXPECT_EQ(Remaining.size(), 0u);
4389
4390 EXPECT_TRUE(EXTRACT_FROM(DW_OP_constu, 2, DW_OP_minus));
4391 EXPECT_EQ(Offset, -2);
4392 EXPECT_EQ(Remaining.size(), 0u);
4393
4394 EXPECT_TRUE(EXTRACT_FROM(DW_OP_plus_uconst, 8));
4395 EXPECT_EQ(Offset, 8);
4396 EXPECT_EQ(Remaining.size(), 0u);
4397
4398 EXPECT_TRUE(EXTRACT_FROM(DW_OP_plus_uconst, 4, DW_OP_constu, 2, DW_OP_minus));
4399 EXPECT_EQ(Offset, 2);
4400 EXPECT_EQ(Remaining.size(), 0u);
4401
4402 // Not all operations are permitted for simplicity. Can be added
4403 // if needed in future.
4404 EXPECT_FALSE(EXTRACT_FROM(DW_OP_constu, 2, DW_OP_mul));
4405
4406 // Test "remaining ops"
4407 // --------------------
4408 EXPECT_TRUE(EXTRACT_FROM(DW_OP_plus_uconst, 4, DW_OP_constu, 8, DW_OP_minus,
4409 DW_OP_LLVM_fragment, 0, 32));
4410 EXPECT_EQ(Remaining, OPS(DW_OP_LLVM_fragment, 0, 32));
4411 EXPECT_EQ(Offset, -4);
4412
4413 EXPECT_TRUE(EXTRACT_FROM(DW_OP_deref));
4414 EXPECT_EQ(Remaining, OPS(DW_OP_deref));
4415 EXPECT_EQ(Offset, 0);
4416
4417 // Check things after the non-offset ops are added too.
4418 EXPECT_TRUE(EXTRACT_FROM(DW_OP_plus_uconst, 2, DW_OP_deref_size, 4,
4419 DW_OP_stack_value));
4420 EXPECT_EQ(Remaining, OPS(DW_OP_deref_size, 4, DW_OP_stack_value));
4421 EXPECT_EQ(Offset, 2);
4422
4423 // DW_OP_deref_type isn't supported in LLVM so this currently fails.
4424 EXPECT_FALSE(EXTRACT_FROM(DW_OP_deref_type, 0));
4425
4426 EXPECT_TRUE(EXTRACT_FROM(DW_OP_LLVM_extract_bits_zext, 0, 8));
4427 EXPECT_EQ(Remaining, OPS(DW_OP_LLVM_extract_bits_zext, 0, 8));
4428
4429 EXPECT_TRUE(EXTRACT_FROM(DW_OP_LLVM_extract_bits_sext, 4, 4));
4430 EXPECT_EQ(Remaining, OPS(DW_OP_LLVM_extract_bits_sext, 4, 4));
4431 #undef EXTRACT_FROM
4432 #undef OPS
4433 }
4434
4435 TEST_F(DIExpressionTest, convertToUndefExpression) {
4436 #define EXPECT_UNDEF_OPS_EQUAL(TestExpr, Expected) \
4437 do { \
4438 const DIExpression *Undef = \
4439 DIExpression::convertToUndefExpression(TestExpr); \
4440 EXPECT_EQ(Undef, Expected); \
4441 } while (false)
4442 #define GET_EXPR(...) DIExpression::get(Context, {__VA_ARGS__})
4443
4444 // Expressions which are single-location and non-complex should be unchanged.
4445 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(), GET_EXPR());
4446 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_LLVM_fragment, 0, 32),
4447 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 0, 32));
4448
4449 // Variadic expressions should become single-location.
4450 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0), GET_EXPR());
4451 EXPECT_UNDEF_OPS_EQUAL(
4452 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_LLVM_fragment, 32, 32),
4453 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 32, 32));
4454 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0,
4455 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_mul),
4456 GET_EXPR());
4457 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0,
4458 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_mul,
4459 dwarf::DW_OP_LLVM_fragment, 64, 32),
4460 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 64, 32));
4461
4462 // Any stack-computing ops should be removed.
4463 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_plus_uconst, 8), GET_EXPR());
4464 EXPECT_UNDEF_OPS_EQUAL(
4465 GET_EXPR(dwarf::DW_OP_plus_uconst, 8, dwarf::DW_OP_LLVM_fragment, 0, 16),
4466 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 0, 16));
4467 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_constu, 24, dwarf::DW_OP_shra),
4468 GET_EXPR());
4469 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_constu, 24, dwarf::DW_OP_shra,
4470 dwarf::DW_OP_LLVM_fragment, 8, 16),
4471 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 8, 16));
4472 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_deref), GET_EXPR());
4473 EXPECT_UNDEF_OPS_EQUAL(
4474 GET_EXPR(dwarf::DW_OP_deref, dwarf::DW_OP_LLVM_fragment, 16, 16),
4475 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 16, 16));
4476 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_constu, 4, dwarf::DW_OP_minus),
4477 GET_EXPR());
4478 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_constu, 4, dwarf::DW_OP_minus,
4479 dwarf::DW_OP_LLVM_fragment, 24, 16),
4480 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 24, 16));
4481
4482 // Stack-value operators are also not preserved.
4483 EXPECT_UNDEF_OPS_EQUAL(
4484 GET_EXPR(dwarf::DW_OP_plus_uconst, 8, dwarf::DW_OP_stack_value),
4485 GET_EXPR());
4486 EXPECT_UNDEF_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_plus_uconst, 8,
4487 dwarf::DW_OP_stack_value,
4488 dwarf::DW_OP_LLVM_fragment, 32, 16),
4489 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 32, 16));
4490
4491 #undef EXPECT_UNDEF_OPS_EQUAL
4492 #undef GET_EXPR
4493 }
4494
4495 TEST_F(DIExpressionTest, convertToVariadicExpression) {
4496 #define EXPECT_CONVERT_IS_NOOP(TestExpr) \
4497 do { \
4498 const DIExpression *Variadic = \
4499 DIExpression::convertToVariadicExpression(TestExpr); \
4500 EXPECT_EQ(Variadic, TestExpr); \
4501 } while (false)
4502 #define EXPECT_VARIADIC_OPS_EQUAL(TestExpr, Expected) \
4503 do { \
4504 const DIExpression *Variadic = \
4505 DIExpression::convertToVariadicExpression(TestExpr); \
4506 EXPECT_EQ(Variadic, Expected); \
4507 } while (false)
4508 #define GET_EXPR(...) DIExpression::get(Context, {__VA_ARGS__})
4509
4510 // Expressions which are already variadic should be unaffected.
4511 EXPECT_CONVERT_IS_NOOP(
4512 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_stack_value));
4513 EXPECT_CONVERT_IS_NOOP(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0,
4514 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_plus,
4515 dwarf::DW_OP_stack_value));
4516 EXPECT_CONVERT_IS_NOOP(GET_EXPR(dwarf::DW_OP_constu, 5, dwarf::DW_OP_LLVM_arg,
4517 0, dwarf::DW_OP_plus,
4518 dwarf::DW_OP_stack_value));
4519 EXPECT_CONVERT_IS_NOOP(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0,
4520 dwarf::DW_OP_stack_value,
4521 dwarf::DW_OP_LLVM_fragment, 0, 32));
4522
4523 // Other expressions should receive a leading `LLVM_arg 0`.
4524 EXPECT_VARIADIC_OPS_EQUAL(GET_EXPR(), GET_EXPR(dwarf::DW_OP_LLVM_arg, 0));
4525 EXPECT_VARIADIC_OPS_EQUAL(
4526 GET_EXPR(dwarf::DW_OP_plus_uconst, 4),
4527 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 4));
4528 EXPECT_VARIADIC_OPS_EQUAL(
4529 GET_EXPR(dwarf::DW_OP_plus_uconst, 4, dwarf::DW_OP_stack_value),
4530 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 4,
4531 dwarf::DW_OP_stack_value));
4532 EXPECT_VARIADIC_OPS_EQUAL(
4533 GET_EXPR(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_stack_value,
4534 dwarf::DW_OP_LLVM_fragment, 32, 32),
4535 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 6,
4536 dwarf::DW_OP_stack_value, dwarf::DW_OP_LLVM_fragment, 32, 32));
4537 EXPECT_VARIADIC_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_plus_uconst, 14,
4538 dwarf::DW_OP_LLVM_fragment, 32, 32),
4539 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0,
4540 dwarf::DW_OP_plus_uconst, 14,
4541 dwarf::DW_OP_LLVM_fragment, 32, 32));
4542
4543 #undef EXPECT_CONVERT_IS_NOOP
4544 #undef EXPECT_VARIADIC_OPS_EQUAL
4545 #undef GET_EXPR
4546 }
4547
4548 TEST_F(DIExpressionTest, convertToNonVariadicExpression) {
4549 #define EXPECT_CONVERT_IS_NOOP(TestExpr) \
4550 do { \
4551 std::optional<const DIExpression *> NonVariadic = \
4552 DIExpression::convertToNonVariadicExpression(TestExpr); \
4553 EXPECT_TRUE(NonVariadic.has_value()); \
4554 EXPECT_EQ(*NonVariadic, TestExpr); \
4555 } while (false)
4556 #define EXPECT_NON_VARIADIC_OPS_EQUAL(TestExpr, Expected) \
4557 do { \
4558 std::optional<const DIExpression *> NonVariadic = \
4559 DIExpression::convertToNonVariadicExpression(TestExpr); \
4560 EXPECT_TRUE(NonVariadic.has_value()); \
4561 EXPECT_EQ(*NonVariadic, Expected); \
4562 } while (false)
4563 #define EXPECT_INVALID_CONVERSION(TestExpr) \
4564 do { \
4565 std::optional<const DIExpression *> NonVariadic = \
4566 DIExpression::convertToNonVariadicExpression(TestExpr); \
4567 EXPECT_FALSE(NonVariadic.has_value()); \
4568 } while (false)
4569 #define GET_EXPR(...) DIExpression::get(Context, {__VA_ARGS__})
4570
4571 // Expressions which are already non-variadic should be unaffected.
4572 EXPECT_CONVERT_IS_NOOP(GET_EXPR());
4573 EXPECT_CONVERT_IS_NOOP(GET_EXPR(dwarf::DW_OP_plus_uconst, 4));
4574 EXPECT_CONVERT_IS_NOOP(
4575 GET_EXPR(dwarf::DW_OP_plus_uconst, 4, dwarf::DW_OP_stack_value));
4576 EXPECT_CONVERT_IS_NOOP(GET_EXPR(dwarf::DW_OP_plus_uconst, 6,
4577 dwarf::DW_OP_stack_value,
4578 dwarf::DW_OP_LLVM_fragment, 32, 32));
4579 EXPECT_CONVERT_IS_NOOP(GET_EXPR(dwarf::DW_OP_plus_uconst, 14,
4580 dwarf::DW_OP_LLVM_fragment, 32, 32));
4581
4582 // Variadic expressions with a single leading `LLVM_arg 0` and no other
4583 // LLVM_args should have the leading arg removed.
4584 EXPECT_NON_VARIADIC_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0), GET_EXPR());
4585 EXPECT_NON_VARIADIC_OPS_EQUAL(
4586 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_stack_value),
4587 GET_EXPR(dwarf::DW_OP_stack_value));
4588 EXPECT_NON_VARIADIC_OPS_EQUAL(
4589 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_LLVM_fragment, 16, 32),
4590 GET_EXPR(dwarf::DW_OP_LLVM_fragment, 16, 32));
4591 EXPECT_NON_VARIADIC_OPS_EQUAL(
4592 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_stack_value,
4593 dwarf::DW_OP_LLVM_fragment, 24, 32),
4594 GET_EXPR(dwarf::DW_OP_stack_value, dwarf::DW_OP_LLVM_fragment, 24, 32));
4595 EXPECT_NON_VARIADIC_OPS_EQUAL(
4596 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 4),
4597 GET_EXPR(dwarf::DW_OP_plus_uconst, 4));
4598 EXPECT_NON_VARIADIC_OPS_EQUAL(
4599 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 4,
4600 dwarf::DW_OP_stack_value),
4601 GET_EXPR(dwarf::DW_OP_plus_uconst, 4, dwarf::DW_OP_stack_value));
4602 EXPECT_NON_VARIADIC_OPS_EQUAL(
4603 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 6,
4604 dwarf::DW_OP_stack_value, dwarf::DW_OP_LLVM_fragment, 32, 32),
4605 GET_EXPR(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_stack_value,
4606 dwarf::DW_OP_LLVM_fragment, 32, 32));
4607 EXPECT_NON_VARIADIC_OPS_EQUAL(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0,
4608 dwarf::DW_OP_plus_uconst, 14,
4609 dwarf::DW_OP_LLVM_fragment, 32, 32),
4610 GET_EXPR(dwarf::DW_OP_plus_uconst, 14,
4611 dwarf::DW_OP_LLVM_fragment, 32, 32));
4612
4613 // Variadic expressions that have any LLVM_args other than a leading
4614 // `LLVM_arg 0` cannot be converted and so should return std::nullopt.
4615 EXPECT_INVALID_CONVERSION(GET_EXPR(
4616 dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_mul));
4617 EXPECT_INVALID_CONVERSION(
4618 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_LLVM_arg, 1,
4619 dwarf::DW_OP_plus, dwarf::DW_OP_stack_value));
4620 EXPECT_INVALID_CONVERSION(
4621 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_LLVM_arg, 0,
4622 dwarf::DW_OP_minus, dwarf::DW_OP_stack_value));
4623 EXPECT_INVALID_CONVERSION(GET_EXPR(dwarf::DW_OP_constu, 5,
4624 dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_div,
4625 dwarf::DW_OP_stack_value));
4626
4627 #undef EXPECT_CONVERT_IS_NOOP
4628 #undef EXPECT_NON_VARIADIC_OPS_EQUAL
4629 #undef EXPECT_INVALID_CONVERSION
4630 #undef GET_EXPR
4631 }
4632
4633 TEST_F(DIExpressionTest, replaceArg) {
4634 #define EXPECT_REPLACE_ARG_EQ(Expr, OldArg, NewArg, ...) \
4635 do { \
4636 uint64_t Elements[] = {__VA_ARGS__}; \
4637 ArrayRef<uint64_t> Expected = Elements; \
4638 DIExpression *Expression = DIExpression::replaceArg(Expr, OldArg, NewArg); \
4639 EXPECT_EQ(Expression->getElements(), Expected); \
4640 } while (false)
4641
4642 auto N = DIExpression::get(
4643 Context, {dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_LLVM_arg, 1,
4644 dwarf::DW_OP_plus, dwarf::DW_OP_LLVM_arg, 2, dwarf::DW_OP_mul});
4645 EXPECT_REPLACE_ARG_EQ(N, 0, 1, dwarf::DW_OP_LLVM_arg, 0,
4646 dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus,
4647 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_mul);
4648 EXPECT_REPLACE_ARG_EQ(N, 0, 2, dwarf::DW_OP_LLVM_arg, 1,
4649 dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus,
4650 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_mul);
4651 EXPECT_REPLACE_ARG_EQ(N, 2, 0, dwarf::DW_OP_LLVM_arg, 0,
4652 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_plus,
4653 dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_mul);
4654 EXPECT_REPLACE_ARG_EQ(N, 2, 1, dwarf::DW_OP_LLVM_arg, 0,
4655 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_plus,
4656 dwarf::DW_OP_LLVM_arg, 1, dwarf::DW_OP_mul);
4657
4658 #undef EXPECT_REPLACE_ARG_EQ
4659 }
4660
4661 TEST_F(DIExpressionTest, isEqualExpression) {
4662 #define EXPECT_EQ_DEBUG_VALUE(ExprA, DirectA, ExprB, DirectB) \
4663 EXPECT_TRUE(DIExpression::isEqualExpression(ExprA, DirectA, ExprB, DirectB))
4664 #define EXPECT_NE_DEBUG_VALUE(ExprA, DirectA, ExprB, DirectB) \
4665 EXPECT_FALSE(DIExpression::isEqualExpression(ExprA, DirectA, ExprB, DirectB))
4666 #define GET_EXPR(...) DIExpression::get(Context, {__VA_ARGS__})
4667
4668 EXPECT_EQ_DEBUG_VALUE(GET_EXPR(), false, GET_EXPR(), false);
4669 EXPECT_NE_DEBUG_VALUE(GET_EXPR(), false, GET_EXPR(), true);
4670 EXPECT_EQ_DEBUG_VALUE(
4671 GET_EXPR(dwarf::DW_OP_plus_uconst, 32), true,
4672 GET_EXPR(dwarf::DW_OP_plus_uconst, 32, dwarf::DW_OP_deref), false);
4673 EXPECT_NE_DEBUG_VALUE(
4674 GET_EXPR(dwarf::DW_OP_plus_uconst, 16, dwarf::DW_OP_deref), true,
4675 GET_EXPR(dwarf::DW_OP_plus_uconst, 16, dwarf::DW_OP_deref), false);
4676 EXPECT_EQ_DEBUG_VALUE(
4677 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus_uconst, 5), false,
4678 GET_EXPR(dwarf::DW_OP_plus_uconst, 5), false);
4679 EXPECT_NE_DEBUG_VALUE(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus),
4680 false,
4681 GET_EXPR(dwarf::DW_OP_LLVM_arg, 0,
4682 dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_plus),
4683 false);
4684 EXPECT_NE_DEBUG_VALUE(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_constu,
4685 8, dwarf::DW_OP_minus),
4686 false,
4687 GET_EXPR(dwarf::DW_OP_constu, 8, dwarf::DW_OP_LLVM_arg,
4688 0, dwarf::DW_OP_minus),
4689 false);
4690 // These expressions are actually equivalent, but we do not currently identify
4691 // commutative operations with different operand orders as being equivalent.
4692 EXPECT_NE_DEBUG_VALUE(GET_EXPR(dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_constu,
4693 8, dwarf::DW_OP_plus),
4694 false,
4695 GET_EXPR(dwarf::DW_OP_constu, 8, dwarf::DW_OP_LLVM_arg,
4696 0, dwarf::DW_OP_plus),
4697 false);
4698
4699 #undef EXPECT_EQ_DEBUG_VALUE
4700 #undef EXPECT_NE_DEBUG_VALUE
4701 #undef GET_EXPR
4702 }
4703
4704 TEST_F(DIExpressionTest, foldConstant) {
4705 const ConstantInt *Int;
4706 const ConstantInt *NewInt;
4707 DIExpression *Expr;
4708 DIExpression *NewExpr;
4709
4710 #define EXPECT_FOLD_CONST(StartWidth, StartValue, StartIsSigned, EndWidth, \
4711 EndValue, EndIsSigned, NumElts) \
4712 Int = \
4713 ConstantInt::get(Context, APInt(StartWidth, StartValue, StartIsSigned)); \
4714 std::tie(NewExpr, NewInt) = Expr->constantFold(Int); \
4715 ASSERT_EQ(NewInt->getBitWidth(), EndWidth##u); \
4716 EXPECT_EQ(NewInt->getValue(), APInt(EndWidth, EndValue, EndIsSigned)); \
4717 EXPECT_EQ(NewExpr->getNumElements(), NumElts##u)
4718
4719 // Unfoldable expression should return the original unmodified Int/Expr.
4720 Expr = DIExpression::get(Context, {dwarf::DW_OP_deref});
4721 EXPECT_FOLD_CONST(32, 117, false, 32, 117, false, 1);
4722 EXPECT_EQ(NewExpr, Expr);
4723 EXPECT_EQ(NewInt, Int);
4724 EXPECT_TRUE(NewExpr->startsWithDeref());
4725
4726 // One unsigned bit-width conversion.
4727 Expr = DIExpression::get(
4728 Context, {dwarf::DW_OP_LLVM_convert, 72, dwarf::DW_ATE_unsigned});
4729 EXPECT_FOLD_CONST(8, 12, false, 72, 12, false, 0);
4730
4731 // Two unsigned bit-width conversions (mask truncation).
4732 Expr = DIExpression::get(
4733 Context, {dwarf::DW_OP_LLVM_convert, 8, dwarf::DW_ATE_unsigned,
4734 dwarf::DW_OP_LLVM_convert, 16, dwarf::DW_ATE_unsigned});
4735 EXPECT_FOLD_CONST(32, -1, true, 16, 0xff, false, 0);
4736
4737 // Sign extension.
4738 Expr = DIExpression::get(
4739 Context, {dwarf::DW_OP_LLVM_convert, 32, dwarf::DW_ATE_signed});
4740 EXPECT_FOLD_CONST(16, -1, true, 32, -1, true, 0);
4741
4742 // Get non-foldable operations back in the new Expr.
4743 uint64_t Elements[] = {dwarf::DW_OP_deref, dwarf::DW_OP_stack_value};
4744 ArrayRef<uint64_t> Expected = Elements;
4745 Expr = DIExpression::get(
4746 Context, {dwarf::DW_OP_LLVM_convert, 32, dwarf::DW_ATE_signed});
4747 Expr = DIExpression::append(Expr, Expected);
4748 ASSERT_EQ(Expr->getNumElements(), 5u);
4749 EXPECT_FOLD_CONST(16, -1, true, 32, -1, true, 2);
4750 EXPECT_EQ(NewExpr->getElements(), Expected);
4751
4752 #undef EXPECT_FOLD_CONST
4753 }
4754
4755 TEST_F(DIExpressionTest, appendToStackAssert) {
4756 DIExpression *Expr = DIExpression::get(Context, {});
4757
4758 // Verify that the DW_OP_LLVM_convert operands, which have the same values as
4759 // DW_OP_stack_value and DW_OP_LLVM_fragment, do not get interpreted as such
4760 // operations. This previously triggered an assert.
4761 uint64_t FromSize = dwarf::DW_OP_stack_value;
4762 uint64_t ToSize = dwarf::DW_OP_LLVM_fragment;
4763 uint64_t Ops[] = {
4764 dwarf::DW_OP_LLVM_convert, FromSize, dwarf::DW_ATE_signed,
4765 dwarf::DW_OP_LLVM_convert, ToSize, dwarf::DW_ATE_signed,
4766 };
4767 Expr = DIExpression::appendToStack(Expr, Ops);
4768
4769 uint64_t Expected[] = {
4770 dwarf::DW_OP_LLVM_convert, FromSize, dwarf::DW_ATE_signed,
4771 dwarf::DW_OP_LLVM_convert, ToSize, dwarf::DW_ATE_signed,
4772 dwarf::DW_OP_stack_value};
4773 EXPECT_EQ(Expr->getElements(), ArrayRef<uint64_t>(Expected));
4774 }
4775
4776 typedef MetadataTest DIObjCPropertyTest;
4777
4778 TEST_F(DIObjCPropertyTest, get) {
4779 StringRef Name = "name";
4780 DIFile *File = getFile();
4781 unsigned Line = 5;
4782 StringRef GetterName = "getter";
4783 StringRef SetterName = "setter";
4784 unsigned Attributes = 7;
4785 DIType *Type = getBasicType("basic");
4786
4787 auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
4788 SetterName, Attributes, Type);
4789
4790 EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
4791 EXPECT_EQ(Name, N->getName());
4792 EXPECT_EQ(File, N->getFile());
4793 EXPECT_EQ(Line, N->getLine());
4794 EXPECT_EQ(GetterName, N->getGetterName());
4795 EXPECT_EQ(SetterName, N->getSetterName());
4796 EXPECT_EQ(Attributes, N->getAttributes());
4797 EXPECT_EQ(Type, N->getType());
4798 EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
4799 SetterName, Attributes, Type));
4800
4801 EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
4802 SetterName, Attributes, Type));
4803 EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
4804 SetterName, Attributes, Type));
4805 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
4806 SetterName, Attributes, Type));
4807 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
4808 SetterName, Attributes, Type));
4809 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
4810 "other", Attributes, Type));
4811 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
4812 SetterName, Attributes + 1, Type));
4813 EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
4814 SetterName, Attributes,
4815 getBasicType("other")));
4816
4817 TempDIObjCProperty Temp = N->clone();
4818 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
4819 }
4820
4821 typedef MetadataTest DIImportedEntityTest;
4822
4823 TEST_F(DIImportedEntityTest, get) {
4824 unsigned Tag = dwarf::DW_TAG_imported_module;
4825 DIScope *Scope = getSubprogram();
4826 DINode *Entity = getCompositeType();
4827 DIFile *File = getFile();
4828 unsigned Line = 5;
4829 StringRef Name = "name";
4830
4831 auto *N =
4832 DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name);
4833
4834 EXPECT_EQ(Tag, N->getTag());
4835 EXPECT_EQ(Scope, N->getScope());
4836 EXPECT_EQ(Entity, N->getEntity());
4837 EXPECT_EQ(File, N->getFile());
4838 EXPECT_EQ(Line, N->getLine());
4839 EXPECT_EQ(Name, N->getName());
4840 EXPECT_EQ(
4841 N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name));
4842
4843 EXPECT_NE(N,
4844 DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
4845 Scope, Entity, File, Line, Name));
4846 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
4847 File, Line, Name));
4848 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
4849 File, Line, Name));
4850 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, nullptr, Line,
4851 Name));
4852 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File,
4853 Line + 1, Name));
4854 EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
4855 "other"));
4856
4857 TempDIImportedEntity Temp = N->clone();
4858 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
4859
4860 MDTuple *Elements1 = getTuple();
4861 MDTuple *Elements2 = getTuple();
4862 auto *Ne = DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
4863 Name, Elements1);
4864
4865 EXPECT_EQ(Elements1, Ne->getElements().get());
4866
4867 EXPECT_EQ(Ne, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
4868 Name, Elements1));
4869 EXPECT_NE(Ne, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
4870 "ModOther", Elements1));
4871 EXPECT_NE(Ne, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
4872 Name, Elements2));
4873 EXPECT_NE(
4874 Ne, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name));
4875
4876 TempDIImportedEntity Tempe = Ne->clone();
4877 EXPECT_EQ(Ne, MDNode::replaceWithUniqued(std::move(Tempe)));
4878 }
4879
4880 typedef MetadataTest MetadataAsValueTest;
4881
4882 TEST_F(MetadataAsValueTest, MDNode) {
4883 MDNode *N = MDNode::get(Context, {});
4884 auto *V = MetadataAsValue::get(Context, N);
4885 EXPECT_TRUE(V->getType()->isMetadataTy());
4886 EXPECT_EQ(N, V->getMetadata());
4887
4888 auto *V2 = MetadataAsValue::get(Context, N);
4889 EXPECT_EQ(V, V2);
4890 }
4891
4892 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
4893 MDNode *N = MDNode::get(Context, {});
4894 Metadata *Ops[] = {N};
4895 MDNode *N2 = MDNode::get(Context, Ops);
4896 auto *V = MetadataAsValue::get(Context, N2);
4897 EXPECT_TRUE(V->getType()->isMetadataTy());
4898 EXPECT_EQ(N2, V->getMetadata());
4899
4900 auto *V2 = MetadataAsValue::get(Context, N2);
4901 EXPECT_EQ(V, V2);
4902
4903 auto *V3 = MetadataAsValue::get(Context, N);
4904 EXPECT_TRUE(V3->getType()->isMetadataTy());
4905 EXPECT_NE(V, V3);
4906 EXPECT_EQ(N, V3->getMetadata());
4907 }
4908
4909 TEST_F(MetadataAsValueTest, MDNodeConstant) {
4910 auto *C = ConstantInt::getTrue(Context);
4911 auto *MD = ConstantAsMetadata::get(C);
4912 Metadata *Ops[] = {MD};
4913 auto *N = MDNode::get(Context, Ops);
4914
4915 auto *V = MetadataAsValue::get(Context, MD);
4916 EXPECT_TRUE(V->getType()->isMetadataTy());
4917 EXPECT_EQ(MD, V->getMetadata());
4918
4919 auto *V2 = MetadataAsValue::get(Context, N);
4920 EXPECT_EQ(MD, V2->getMetadata());
4921 EXPECT_EQ(V, V2);
4922 }
4923
4924 typedef MetadataTest ValueAsMetadataTest;
4925
4926 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
4927 Type *Ty = PointerType::getUnqual(Context);
4928 std::unique_ptr<GlobalVariable> GV0(
4929 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
4930 auto *MD = ValueAsMetadata::get(GV0.get());
4931 EXPECT_TRUE(MD->getValue() == GV0.get());
4932 ASSERT_TRUE(GV0->use_empty());
4933
4934 std::unique_ptr<GlobalVariable> GV1(
4935 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
4936 GV0->replaceAllUsesWith(GV1.get());
4937 EXPECT_TRUE(MD->getValue() == GV1.get());
4938 }
4939
4940 TEST_F(ValueAsMetadataTest, handleRAUWWithTypeChange) {
4941 // Test that handleRAUW supports type changes.
4942 // This is helpful in cases where poison values are used to encode
4943 // types in metadata, e.g. in type annotations.
4944 // Changing the type stored in metadata requires to change the type of
4945 // the stored poison value.
4946 auto *I32Poison = PoisonValue::get(Type::getInt32Ty(Context));
4947 auto *I64Poison = PoisonValue::get(Type::getInt64Ty(Context));
4948 auto *MD = ConstantAsMetadata::get(I32Poison);
4949
4950 EXPECT_EQ(MD->getValue(), I32Poison);
4951 EXPECT_NE(MD->getValue(), I64Poison);
4952
4953 ValueAsMetadata::handleRAUW(I32Poison, I64Poison);
4954
4955 EXPECT_NE(MD->getValue(), I32Poison);
4956 EXPECT_EQ(MD->getValue(), I64Poison);
4957 }
4958
4959 TEST_F(ValueAsMetadataTest, TempTempReplacement) {
4960 // Create a constant.
4961 ConstantAsMetadata *CI =
4962 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
4963
4964 auto Temp1 = MDTuple::getTemporary(Context, {});
4965 auto Temp2 = MDTuple::getTemporary(Context, {CI});
4966 auto *N = MDTuple::get(Context, {Temp1.get()});
4967
4968 // Test replacing a temporary node with another temporary node.
4969 Temp1->replaceAllUsesWith(Temp2.get());
4970 EXPECT_EQ(N->getOperand(0), Temp2.get());
4971
4972 // Clean up Temp2 for teardown.
4973 Temp2->replaceAllUsesWith(nullptr);
4974 }
4975
4976 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
4977 // Create a constant.
4978 ConstantAsMetadata *CI =
4979 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
4980
4981 // Create a temporary to prevent nodes from resolving.
4982 auto Temp = MDTuple::getTemporary(Context, {});
4983
4984 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
4985 Metadata *Ops1[] = {CI, CI, Temp.get()};
4986 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
4987
4988 auto *N1 = MDTuple::get(Context, Ops1);
4989 auto *N2 = MDTuple::get(Context, Ops2);
4990 ASSERT_NE(N1, N2);
4991
4992 // Tell metadata that the constant is getting deleted.
4993 //
4994 // After this, N1 will be invalid, so don't touch it.
4995 ValueAsMetadata::handleDeletion(CI->getValue());
4996 EXPECT_EQ(nullptr, N2->getOperand(0));
4997 EXPECT_EQ(nullptr, N2->getOperand(1));
4998 EXPECT_EQ(Temp.get(), N2->getOperand(2));
4999
5000 // Clean up Temp for teardown.
5001 Temp->replaceAllUsesWith(nullptr);
5002 }
5003
5004 typedef MetadataTest DIArgListTest;
5005
5006 TEST_F(DIArgListTest, get) {
5007 SmallVector<ValueAsMetadata *, 2> VMs;
5008 VMs.push_back(
5009 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0))));
5010 VMs.push_back(
5011 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(2, 0))));
5012 DIArgList *DV0 = DIArgList::get(Context, VMs);
5013 DIArgList *DV1 = DIArgList::get(Context, VMs);
5014 EXPECT_EQ(DV0, DV1);
5015 }
5016
5017 TEST_F(DIArgListTest, UpdatesOnRAUW) {
5018 Type *Ty = PointerType::getUnqual(Context);
5019 ConstantAsMetadata *CI =
5020 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
5021 std::unique_ptr<GlobalVariable> GV0(
5022 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
5023 auto *MD0 = ValueAsMetadata::get(GV0.get());
5024
5025 SmallVector<ValueAsMetadata *, 2> VMs;
5026 VMs.push_back(CI);
5027 VMs.push_back(MD0);
5028 auto *AL = DIArgList::get(Context, VMs);
5029 EXPECT_EQ(AL->getArgs()[0], CI);
5030 EXPECT_EQ(AL->getArgs()[1], MD0);
5031
5032 std::unique_ptr<GlobalVariable> GV1(
5033 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
5034 auto *MD1 = ValueAsMetadata::get(GV1.get());
5035 GV0->replaceAllUsesWith(GV1.get());
5036 EXPECT_EQ(AL->getArgs()[0], CI);
5037 EXPECT_EQ(AL->getArgs()[1], MD1);
5038 }
5039
5040 typedef MetadataTest TrackingMDRefTest;
5041
5042 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
5043 Type *Ty = PointerType::getUnqual(Context);
5044 std::unique_ptr<GlobalVariable> GV0(
5045 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
5046 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
5047 EXPECT_TRUE(MD->getValue() == GV0.get());
5048 ASSERT_TRUE(GV0->use_empty());
5049
5050 std::unique_ptr<GlobalVariable> GV1(
5051 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
5052 GV0->replaceAllUsesWith(GV1.get());
5053 EXPECT_TRUE(MD->getValue() == GV1.get());
5054
5055 // Reset it, so we don't inadvertently test deletion.
5056 MD.reset();
5057 }
5058
5059 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
5060 Type *Ty = PointerType::getUnqual(Context);
5061 std::unique_ptr<GlobalVariable> GV(
5062 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
5063 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
5064 EXPECT_TRUE(MD->getValue() == GV.get());
5065 ASSERT_TRUE(GV->use_empty());
5066
5067 GV.reset();
5068 EXPECT_TRUE(!MD);
5069 }
5070
5071 TEST(NamedMDNodeTest, Search) {
5072 LLVMContext Context;
5073 ConstantAsMetadata *C =
5074 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
5075 ConstantAsMetadata *C2 =
5076 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
5077
5078 Metadata *const V = C;
5079 Metadata *const V2 = C2;
5080 MDNode *n = MDNode::get(Context, V);
5081 MDNode *n2 = MDNode::get(Context, V2);
5082
5083 Module M("MyModule", Context);
5084 const char *Name = "llvm.NMD1";
5085 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
5086 NMD->addOperand(n);
5087 NMD->addOperand(n2);
5088
5089 std::string Str;
5090 raw_string_ostream oss(Str);
5091 NMD->print(oss);
5092 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", Str.c_str());
5093 }
5094
5095 typedef MetadataTest FunctionAttachmentTest;
5096 TEST_F(FunctionAttachmentTest, setMetadata) {
5097 Function *F = getFunction("foo");
5098 ASSERT_FALSE(F->hasMetadata());
5099 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
5100 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
5101 EXPECT_EQ(nullptr, F->getMetadata("other"));
5102
5103 DISubprogram *SP1 = getSubprogram();
5104 DISubprogram *SP2 = getSubprogram();
5105 ASSERT_NE(SP1, SP2);
5106
5107 F->setMetadata("dbg", SP1);
5108 EXPECT_TRUE(F->hasMetadata());
5109 EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
5110 EXPECT_EQ(SP1, F->getMetadata("dbg"));
5111 EXPECT_EQ(nullptr, F->getMetadata("other"));
5112
5113 F->setMetadata(LLVMContext::MD_dbg, SP2);
5114 EXPECT_TRUE(F->hasMetadata());
5115 EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
5116 EXPECT_EQ(SP2, F->getMetadata("dbg"));
5117 EXPECT_EQ(nullptr, F->getMetadata("other"));
5118
5119 F->setMetadata("dbg", nullptr);
5120 EXPECT_FALSE(F->hasMetadata());
5121 EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
5122 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
5123 EXPECT_EQ(nullptr, F->getMetadata("other"));
5124
5125 MDTuple *T1 = getTuple();
5126 MDTuple *T2 = getTuple();
5127 ASSERT_NE(T1, T2);
5128
5129 F->setMetadata("other1", T1);
5130 F->setMetadata("other2", T2);
5131 EXPECT_TRUE(F->hasMetadata());
5132 EXPECT_EQ(T1, F->getMetadata("other1"));
5133 EXPECT_EQ(T2, F->getMetadata("other2"));
5134 EXPECT_EQ(nullptr, F->getMetadata("dbg"));
5135
5136 F->setMetadata("other1", T2);
5137 F->setMetadata("other2", T1);
5138 EXPECT_EQ(T2, F->getMetadata("other1"));
5139 EXPECT_EQ(T1, F->getMetadata("other2"));
5140
5141 F->setMetadata("other1", nullptr);
5142 F->setMetadata("other2", nullptr);
5143 EXPECT_FALSE(F->hasMetadata());
5144 EXPECT_EQ(nullptr, F->getMetadata("other1"));
5145 EXPECT_EQ(nullptr, F->getMetadata("other2"));
5146 }
5147
5148 TEST_F(FunctionAttachmentTest, getAll) {
5149 Function *F = getFunction("foo");
5150
5151 MDTuple *T1 = getTuple();
5152 MDTuple *T2 = getTuple();
5153 MDTuple *P = getTuple();
5154 DISubprogram *SP = getSubprogram();
5155
5156 F->setMetadata("other1", T2);
5157 F->setMetadata(LLVMContext::MD_dbg, SP);
5158 F->setMetadata("other2", T1);
5159 F->setMetadata(LLVMContext::MD_prof, P);
5160 F->setMetadata("other2", T2);
5161 F->setMetadata("other1", T1);
5162
5163 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
5164 F->getAllMetadata(MDs);
5165 ASSERT_EQ(4u, MDs.size());
5166 EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
5167 EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
5168 EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
5169 EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
5170 EXPECT_EQ(SP, MDs[0].second);
5171 EXPECT_EQ(P, MDs[1].second);
5172 EXPECT_EQ(T1, MDs[2].second);
5173 EXPECT_EQ(T2, MDs[3].second);
5174 }
5175
5176 TEST_F(FunctionAttachmentTest, Verifier) {
5177 Function *F = getFunction("foo");
5178 F->setMetadata("attach", getTuple());
5179 F->setIsMaterializable(true);
5180
5181 // Confirm this is materializable.
5182 ASSERT_TRUE(F->isMaterializable());
5183
5184 // Materializable functions cannot have metadata attachments.
5185 EXPECT_TRUE(verifyFunction(*F));
5186
5187 // Function declarations can.
5188 F->setIsMaterializable(false);
5189 EXPECT_FALSE(verifyModule(*F->getParent()));
5190 EXPECT_FALSE(verifyFunction(*F));
5191
5192 // So can definitions.
5193 (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
5194 EXPECT_FALSE(verifyModule(*F->getParent()));
5195 EXPECT_FALSE(verifyFunction(*F));
5196 }
5197
5198 TEST_F(FunctionAttachmentTest, RealEntryCount) {
5199 Function *F = getFunction("foo");
5200 EXPECT_FALSE(F->getEntryCount().has_value());
5201 F->setEntryCount(12304, Function::PCT_Real);
5202 auto Count = F->getEntryCount();
5203 EXPECT_TRUE(Count.has_value());
5204 EXPECT_EQ(12304u, Count->getCount());
5205 EXPECT_EQ(Function::PCT_Real, Count->getType());
5206 }
5207
5208 TEST_F(FunctionAttachmentTest, SyntheticEntryCount) {
5209 Function *F = getFunction("bar");
5210 EXPECT_FALSE(F->getEntryCount().has_value());
5211 F->setEntryCount(123, Function::PCT_Synthetic);
5212 auto Count = F->getEntryCount(true /*allow synthetic*/);
5213 EXPECT_TRUE(Count.has_value());
5214 EXPECT_EQ(123u, Count->getCount());
5215 EXPECT_EQ(Function::PCT_Synthetic, Count->getType());
5216 }
5217
5218 TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
5219 Function *F = getFunction("foo");
5220 DISubprogram *SP = getSubprogram();
5221 F->setSubprogram(SP);
5222
5223 // Note that the static_cast confirms that F->getSubprogram() actually
5224 // returns an DISubprogram.
5225 EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
5226 EXPECT_EQ(SP, F->getMetadata("dbg"));
5227 EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
5228 }
5229
5230 typedef MetadataTest DistinctMDOperandPlaceholderTest;
5231 TEST_F(DistinctMDOperandPlaceholderTest, getID) {
5232 EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
5233 }
5234
5235 TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
5236 // Set up some placeholders.
5237 DistinctMDOperandPlaceholder PH0(7);
5238 DistinctMDOperandPlaceholder PH1(3);
5239 DistinctMDOperandPlaceholder PH2(0);
5240 Metadata *Ops[] = {&PH0, &PH1, &PH2};
5241 auto *D = MDTuple::getDistinct(Context, Ops);
5242 ASSERT_EQ(&PH0, D->getOperand(0));
5243 ASSERT_EQ(&PH1, D->getOperand(1));
5244 ASSERT_EQ(&PH2, D->getOperand(2));
5245
5246 // Replace them.
5247 auto *N0 = MDTuple::get(Context, {});
5248 auto *N1 = MDTuple::get(Context, N0);
5249 PH0.replaceUseWith(N0);
5250 PH1.replaceUseWith(N1);
5251 PH2.replaceUseWith(nullptr);
5252 EXPECT_EQ(N0, D->getOperand(0));
5253 EXPECT_EQ(N1, D->getOperand(1));
5254 EXPECT_EQ(nullptr, D->getOperand(2));
5255 }
5256
5257 TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
5258 // There is no user, but we can still call replace.
5259 DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, {}));
5260 }
5261
5262 // Test various assertions in metadata tracking. Don't run these tests if gtest
5263 // will use SEH to recover from them. Two of these tests get halfway through
5264 // inserting metadata into DenseMaps for tracking purposes, and then they
5265 // assert, and we attempt to destroy an LLVMContext with broken invariants,
5266 // leading to infinite loops.
5267 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) && !defined(GTEST_HAS_SEH)
5268 TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
5269 // This shouldn't crash.
5270 DistinctMDOperandPlaceholder PH(7);
5271 EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
5272 "Unexpected callback to owner");
5273 }
5274
5275 TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
5276 // This shouldn't crash.
5277 DistinctMDOperandPlaceholder PH(7);
5278 EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
5279 }
5280
5281 TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
5282 // This shouldn't crash.
5283 DistinctMDOperandPlaceholder PH(7);
5284 MDTuple::getDistinct(Context, &PH);
5285 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
5286 "Placeholders can only be used once");
5287 }
5288
5289 TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
5290 // TrackingMDRef doesn't install an owner callback, so it can't be detected
5291 // as an invalid use. However, using a placeholder in a TrackingMDRef *and*
5292 // a distinct node isn't possible and we should assert.
5293 //
5294 // (There's no positive test for using TrackingMDRef because it's not a
5295 // useful thing to do.)
5296 {
5297 DistinctMDOperandPlaceholder PH(7);
5298 MDTuple::getDistinct(Context, &PH);
5299 EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
5300 }
5301 {
5302 DistinctMDOperandPlaceholder PH(7);
5303 TrackingMDRef Ref(&PH);
5304 EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
5305 "Placeholders can only be used once");
5306 }
5307 }
5308 #endif
5309
5310 typedef MetadataTest DebugVariableTest;
5311 TEST_F(DebugVariableTest, DenseMap) {
5312 DenseMap<DebugVariable, uint64_t> DebugVariableMap;
5313
5314 DILocalScope *Scope = getSubprogram();
5315 DIFile *File = getFile();
5316 DIType *Type = getDerivedType();
5317 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
5318
5319 DILocation *InlinedLoc = DILocation::get(Context, 2, 7, Scope);
5320
5321 DILocalVariable *VarA =
5322 DILocalVariable::get(Context, Scope, "A", File, 5, Type, 2, Flags, 8, nullptr);
5323 DILocalVariable *VarB =
5324 DILocalVariable::get(Context, Scope, "B", File, 7, Type, 3, Flags, 8, nullptr);
5325
5326 DebugVariable DebugVariableA(VarA, std::nullopt, nullptr);
5327 DebugVariable DebugVariableInlineA(VarA, std::nullopt, InlinedLoc);
5328 DebugVariable DebugVariableB(VarB, std::nullopt, nullptr);
5329 DebugVariable DebugVariableFragB(VarB, {{16, 16}}, nullptr);
5330
5331 DebugVariableMap.insert({DebugVariableA, 2});
5332 DebugVariableMap.insert({DebugVariableInlineA, 3});
5333 DebugVariableMap.insert({DebugVariableB, 6});
5334 DebugVariableMap.insert({DebugVariableFragB, 12});
5335
5336 EXPECT_EQ(DebugVariableMap.count(DebugVariableA), 1u);
5337 EXPECT_EQ(DebugVariableMap.count(DebugVariableInlineA), 1u);
5338 EXPECT_EQ(DebugVariableMap.count(DebugVariableB), 1u);
5339 EXPECT_EQ(DebugVariableMap.count(DebugVariableFragB), 1u);
5340
5341 EXPECT_EQ(DebugVariableMap.find(DebugVariableA)->second, 2u);
5342 EXPECT_EQ(DebugVariableMap.find(DebugVariableInlineA)->second, 3u);
5343 EXPECT_EQ(DebugVariableMap.find(DebugVariableB)->second, 6u);
5344 EXPECT_EQ(DebugVariableMap.find(DebugVariableFragB)->second, 12u);
5345 }
5346
5347 typedef MetadataTest MDTupleAllocationTest;
5348 TEST_F(MDTupleAllocationTest, Tracking) {
5349 // Make sure that the move constructor and move assignment op
5350 // for MDOperand correctly adjust tracking information.
5351 auto *Value1 = getConstantAsMetadata();
5352 MDTuple *A = MDTuple::getDistinct(Context, {Value1, Value1});
5353 EXPECT_EQ(A->getOperand(0), Value1);
5354 EXPECT_EQ(A->getOperand(1), Value1);
5355
5356 MDNode::op_range Ops = A->operands();
5357
5358 MDOperand NewOps1;
5359 // Move assignment operator.
5360 NewOps1 = std::move(*const_cast<MDOperand *>(Ops.begin()));
5361 // Move constructor.
5362 MDOperand NewOps2(std::move(*const_cast<MDOperand *>(Ops.begin() + 1)));
5363
5364 EXPECT_EQ(NewOps1.get(), static_cast<Metadata *>(Value1));
5365 EXPECT_EQ(NewOps2.get(), static_cast<Metadata *>(Value1));
5366
5367 auto *Value2 = getConstantAsMetadata();
5368 Value *V1 = Value1->getValue();
5369 Value *V2 = Value2->getValue();
5370 ValueAsMetadata::handleRAUW(V1, V2);
5371
5372 EXPECT_EQ(NewOps1.get(), static_cast<Metadata *>(Value2));
5373 EXPECT_EQ(NewOps2.get(), static_cast<Metadata *>(Value2));
5374 }
5375
5376 TEST_F(MDTupleAllocationTest, Resize) {
5377 MDTuple *A = getTuple();
5378 Metadata *Value1 = getConstantAsMetadata();
5379 Metadata *Value2 = getConstantAsMetadata();
5380 Metadata *Value3 = getConstantAsMetadata();
5381
5382 EXPECT_EQ(A->getNumOperands(), 0u);
5383
5384 // Add a couple of elements to it, which resizes the node.
5385 A->push_back(Value1);
5386 EXPECT_EQ(A->getNumOperands(), 1u);
5387 EXPECT_EQ(A->getOperand(0), Value1);
5388
5389 A->push_back(Value2);
5390 EXPECT_EQ(A->getNumOperands(), 2u);
5391 EXPECT_EQ(A->getOperand(0), Value1);
5392 EXPECT_EQ(A->getOperand(1), Value2);
5393
5394 // Append another element, which should resize the node
5395 // to a "large" node, though not detectable by the user.
5396 A->push_back(Value3);
5397 EXPECT_EQ(A->getNumOperands(), 3u);
5398 EXPECT_EQ(A->getOperand(0), Value1);
5399 EXPECT_EQ(A->getOperand(1), Value2);
5400 EXPECT_EQ(A->getOperand(2), Value3);
5401
5402 // Remove the last element
5403 A->pop_back();
5404 EXPECT_EQ(A->getNumOperands(), 2u);
5405 EXPECT_EQ(A->getOperand(1), Value2);
5406
5407 // Allocate a node with 4 operands.
5408 Metadata *Value4 = getConstantAsMetadata();
5409 Metadata *Value5 = getConstantAsMetadata();
5410
5411 Metadata *Ops[] = {Value1, Value2, Value3, Value4};
5412 MDTuple *B = MDTuple::getDistinct(Context, Ops);
5413
5414 EXPECT_EQ(B->getNumOperands(), 4u);
5415 B->pop_back();
5416 EXPECT_EQ(B->getNumOperands(), 3u);
5417 B->push_back(Value5);
5418 EXPECT_EQ(B->getNumOperands(), 4u);
5419 EXPECT_EQ(B->getOperand(0), Value1);
5420 EXPECT_EQ(B->getOperand(1), Value2);
5421 EXPECT_EQ(B->getOperand(2), Value3);
5422 EXPECT_EQ(B->getOperand(3), Value5);
5423
5424 // Check that we can resize temporary nodes as well.
5425 auto Temp1 = MDTuple::getTemporary(Context, {});
5426 EXPECT_EQ(Temp1->getNumOperands(), 0u);
5427
5428 Temp1->push_back(Value1);
5429 EXPECT_EQ(Temp1->getNumOperands(), 1u);
5430 EXPECT_EQ(Temp1->getOperand(0), Value1);
5431
5432 for (int i = 0; i < 11; i++)
5433 Temp1->push_back(Value2);
5434 EXPECT_EQ(Temp1->getNumOperands(), 12u);
5435 EXPECT_EQ(Temp1->getOperand(2), Value2);
5436 EXPECT_EQ(Temp1->getOperand(11), Value2);
5437
5438 // Allocate a node that starts off as a large one.
5439 Metadata *OpsLarge[] = {Value1, Value2, Value3, Value4,
5440 Value1, Value2, Value3, Value4,
5441 Value1, Value2, Value3, Value4,
5442 Value1, Value2, Value3, Value4,
5443 Value1, Value2, Value3, Value4};
5444 MDTuple *C = MDTuple::getDistinct(Context, OpsLarge);
5445 EXPECT_EQ(C->getNumOperands(), 20u);
5446 EXPECT_EQ(C->getOperand(7), Value4);
5447 EXPECT_EQ(C->getOperand(13), Value2);
5448
5449 C->push_back(Value1);
5450 C->push_back(Value2);
5451 EXPECT_EQ(C->getNumOperands(), 22u);
5452 EXPECT_EQ(C->getOperand(21), Value2);
5453 C->pop_back();
5454 EXPECT_EQ(C->getNumOperands(), 21u);
5455 EXPECT_EQ(C->getOperand(20), Value1);
5456 }
5457
5458 TEST_F(MDTupleAllocationTest, Tracking2) {
5459 // Resize a tuple and check that we can still RAUW one of its operands.
5460 auto *Value1 = getConstantAsMetadata();
5461 MDTuple *A = getTuple();
5462 A->push_back(Value1);
5463 A->push_back(Value1);
5464 A->push_back(Value1); // Causes a resize to large.
5465 EXPECT_EQ(A->getOperand(0), Value1);
5466 EXPECT_EQ(A->getOperand(1), Value1);
5467 EXPECT_EQ(A->getOperand(2), Value1);
5468
5469 auto *Value2 = getConstantAsMetadata();
5470 Value *V1 = Value1->getValue();
5471 Value *V2 = Value2->getValue();
5472 ValueAsMetadata::handleRAUW(V1, V2);
5473
5474 EXPECT_EQ(A->getOperand(0), Value2);
5475 EXPECT_EQ(A->getOperand(1), Value2);
5476 EXPECT_EQ(A->getOperand(2), Value2);
5477 }
5478
5479 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) && !defined(GTEST_HAS_SEH)
5480 typedef MetadataTest MDTupleAllocationDeathTest;
5481 TEST_F(MDTupleAllocationDeathTest, ResizeRejected) {
5482 MDTuple *A = MDTuple::get(Context, std::nullopt);
5483 auto *Value1 = getConstantAsMetadata();
5484 EXPECT_DEATH(A->push_back(Value1),
5485 "Resizing is not supported for uniqued nodes");
5486
5487 // Check that a node, which has been allocated as a temporary,
5488 // cannot be resized after it has been uniqued.
5489 auto *Value2 = getConstantAsMetadata();
5490 auto B = MDTuple::getTemporary(Context, {Value2});
5491 B->push_back(Value2);
5492 MDTuple *BUniqued = MDNode::replaceWithUniqued(std::move(B));
5493 EXPECT_EQ(BUniqued->getNumOperands(), 2u);
5494 EXPECT_EQ(BUniqued->getOperand(1), Value2);
5495 EXPECT_DEATH(BUniqued->push_back(Value2),
5496 "Resizing is not supported for uniqued nodes");
5497 }
5498 #endif
5499
5500 } // end namespace