Debian Codegraph @

llvm-project/llvm/unittests/DebugInfo/LogicalView/LocationRangesTest.cpp

raw
1 //===- llvm/unittest/DebugInfo/LogicalView/LocationRangesTest.cpp ---------===//
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/DebugInfo/LogicalView/Core/LVLine.h"
10 #include "llvm/DebugInfo/LogicalView/Core/LVReader.h"
11 #include "llvm/DebugInfo/LogicalView/Core/LVScope.h"
12 #include "llvm/DebugInfo/LogicalView/Core/LVSymbol.h"
13 #include "llvm/DebugInfo/LogicalView/Core/LVType.h"
14 #include "llvm/Support/ScopedPrinter.h"
15 #include "llvm/Support/ToolOutputFile.h"
16 #include "llvm/Testing/Support/Error.h"
17
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21 using namespace llvm::logicalview;
22
23 namespace {
24
25 class ReaderTest : public LVReader {
26 protected:
27 void add(LVScope *Parent, LVElement *Element);
28 template <typename T> T *create() {
29 T *Element = new (std::nothrow) T();
30 EXPECT_NE(Element, nullptr);
31 return Element;
32 }
33 void set(LVElement *Element, StringRef Name, LVOffset Offset,
34 uint32_t LineNumber = 0, LVElement *Type = nullptr);
35 void set(LVLocation *Location, LVLine *LowerLine, LVLine *UpperLine,
36 LVAddress LowerAddress, LVAddress UpperAddress);
37 void add(LVSymbol *Symbol, LVLine *LowerLine, LVLine *UpperLine);
38
39 public:
40 ReaderTest(ScopedPrinter &W) : LVReader("", "", W) { setInstance(this); }
41
42 Error createScopes() { return LVReader::createScopes(); }
43 };
44
45 // Helper function to add a logical element to a given scope.
46 void ReaderTest::add(LVScope *Parent, LVElement *Child) {
47 Parent->addElement(Child);
48 EXPECT_EQ(Child->getParent(), Parent);
49 EXPECT_EQ(Child->getLevel(), Parent->getLevel() + 1);
50 }
51
52 // Helper function to set the initial values for a given logical element.
53 void ReaderTest::set(LVElement *Element, StringRef Name, LVOffset Offset,
54 uint32_t LineNumber, LVElement *Type) {
55 Element->setName(Name);
56 Element->setOffset(Offset);
57 Element->setLineNumber(LineNumber);
58 Element->setType(Type);
59 EXPECT_EQ(Element->getName(), Name);
60 EXPECT_EQ(Element->getOffset(), Offset);
61 EXPECT_EQ(Element->getLineNumber(), LineNumber);
62 EXPECT_EQ(Element->getType(), Type);
63 }
64
65 // Helper function to set the initial values for a given logical location.
66 void ReaderTest::set(LVLocation *Location, LVLine *LowerLine, LVLine *UpperLine,
67 LVAddress LowerAddress, LVAddress UpperAddress) {
68 Location->setLowerLine(LowerLine);
69 Location->setUpperLine(UpperLine);
70 Location->setLowerAddress(LowerAddress);
71 Location->setUpperAddress(UpperAddress);
72 EXPECT_EQ(Location->getLowerLine(), LowerLine);
73 EXPECT_EQ(Location->getUpperLine(), UpperLine);
74 EXPECT_EQ(Location->getLowerAddress(), LowerAddress);
75 EXPECT_EQ(Location->getUpperAddress(), UpperAddress);
76 }
77
78 // Helper function to add a logical location to a logical symbol.
79 void ReaderTest::add(LVSymbol *Symbol, LVLine *LowerLine, LVLine *UpperLine) {
80 dwarf::Attribute Attr = dwarf::DW_AT_location;
81
82 Symbol->addLocation(Attr, LowerLine->getAddress(), UpperLine->getAddress(),
83 /*SectionOffset=*/0, /*LocDesOffset=*/0);
84 }
85
86 class ReaderTestLocations : public ReaderTest {
87 // Types.
88 LVType *IntegerType = nullptr;
89
90 // Scopes.
91 LVScope *NestedScope = nullptr;
92 LVScopeFunction *Function = nullptr;
93
94 // Symbols.
95 LVSymbol *LocalVariable = nullptr;
96 LVSymbol *NestedVariable = nullptr;
97 LVSymbol *Parameter = nullptr;
98
99 // Lines.
100 LVLine *LineOne = nullptr;
101 LVLine *LineTwo = nullptr;
102 LVLine *LineThree = nullptr;
103 LVLine *LineFour = nullptr;
104 LVLine *LineFive = nullptr;
105 LVLine *LineSix = nullptr;
106
107 // Locations.
108 LVLocation *LocationOne = nullptr;
109 LVLocation *LocationTwo = nullptr;
110 LVLocation *LocationThree = nullptr;
111 LVLocation *LocationFour = nullptr;
112 LVLocation *LocationFive = nullptr;
113 LVLocation *LocationSix = nullptr;
114
115 public:
116 ReaderTestLocations(ScopedPrinter &W) : ReaderTest(W) {}
117
118 void createElements();
119 void addElements();
120 void initElements();
121 };
122
123 // Create the logical elements.
124 void ReaderTestLocations::createElements() {
125 // Create scope root.
126 Error Err = createScopes();
127 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
128 Root = getScopesRoot();
129 EXPECT_NE(Root, nullptr);
130
131 // Create the logical types.
132 IntegerType = create<LVType>();
133
134 // Create the logical scopes.
135 NestedScope = create<LVScope>();
136 CompileUnit = create<LVScopeCompileUnit>();
137 Function = create<LVScopeFunction>();
138
139 // Create the logical symbols.
140 LocalVariable = create<LVSymbol>();
141 NestedVariable = create<LVSymbol>();
142 Parameter = create<LVSymbol>();
143
144 // Create the logical lines.
145 LineOne = create<LVLine>();
146 LineTwo = create<LVLine>();
147 LineThree = create<LVLine>();
148 LineFour = create<LVLine>();
149 LineFive = create<LVLine>();
150 LineSix = create<LVLine>();
151
152 // Create the logical locations.
153 LocationOne = create<LVLocation>();
154 LocationTwo = create<LVLocation>();
155 LocationThree = create<LVLocation>();
156 LocationFour = create<LVLocation>();
157 LocationFive = create<LVLocation>();
158 LocationSix = create<LVLocation>();
159 }
160
161 // Create the logical view adding the created logical elements.
162 void ReaderTestLocations::addElements() {
163 setCompileUnit(CompileUnit);
164
165 // Root
166 // CompileUnit
167 // IntegerType
168 // Function
169 // LocationOne
170 // LocationTwo
171 // LocationFive
172 // LocationSix
173 // Parameter
174 // LocalVariable
175 // LineOne
176 // LineTwo
177 // NestedScope
178 // LocationThree
179 // LocationFour
180 // NestedVariable
181 // LineThree
182 // LineFour
183 // LineFive
184 // LineSix
185
186 // Add elements to Root.
187 add(Root, CompileUnit);
188
189 // Add elements to CompileUnit.
190 add(CompileUnit, IntegerType);
191 add(CompileUnit, Function);
192
193 // Add elements to Function.
194 add(Function, Parameter);
195 add(Function, LocalVariable);
196 add(Function, LineOne);
197 add(Function, LineTwo);
198 add(Function, LineFive);
199 add(Function, LineSix);
200 add(Function, NestedScope);
201
202 // Add elements to NestedScope.
203 add(NestedScope, NestedVariable);
204 add(NestedScope, LineThree);
205 add(NestedScope, LineFour);
206 }
207
208 // Set initial values to logical elements.
209 void ReaderTestLocations::initElements() {
210 // Types.
211 set(IntegerType, "int", 0x1000);
212
213 // Scopes.
214 set(CompileUnit, "foo.cpp", 0x2000);
215 set(Function, "foo", 0x2010, 100, IntegerType);
216 set(NestedScope, "", 0x2020, 300);
217
218 // Symbols.
219 set(Parameter, "Param", 0x3000, 110, IntegerType);
220 set(LocalVariable, "LocalVariable", 0x3000, 120, IntegerType);
221 set(NestedVariable, "NestedVariable", 0x3010, 310, IntegerType);
222
223 // Lines.
224 set(LineOne, "", 0x5000, 100);
225 set(LineTwo, "", 0x5200, 200);
226 set(LineThree, "", 0x5400, 300);
227 set(LineFour, "", 0x5600, 400);
228 set(LineFive, "", 0x5800, 500);
229 set(LineSix, "", 0x6000, 600);
230
231 // Locations.
232 set(LocationOne, LineOne, LineOne, 0x5000, 0x5100);
233 EXPECT_STREQ(LocationOne->getIntervalInfo().c_str(),
234 " Lines 100:100 [0x0000005000:0x0000005100]");
235
236 set(LocationTwo, LineTwo, LineTwo, 0x5200, 0x5300);
237 EXPECT_STREQ(LocationTwo->getIntervalInfo().c_str(),
238 " Lines 200:200 [0x0000005200:0x0000005300]");
239
240 set(LocationThree, LineThree, LineThree, 0x5400, 0x5500);
241 EXPECT_STREQ(LocationThree->getIntervalInfo().c_str(),
242 " Lines 300:300 [0x0000005400:0x0000005500]");
243
244 set(LocationFour, LineFour, LineFour, 0x5600, 0x5700);
245 LocationFour->setIsAddressRange();
246 EXPECT_STREQ(LocationFour->getIntervalInfo().c_str(),
247 "{Range} Lines 400:400 [0x0000005600:0x0000005700]");
248
249 set(LocationFive, LineFive, LineFive, 0x5800, 0x5900);
250 LocationFive->setIsAddressRange();
251 EXPECT_STREQ(LocationFive->getIntervalInfo().c_str(),
252 "{Range} Lines 500:500 [0x0000005800:0x0000005900]");
253
254 set(LocationSix, LineSix, LineSix, 0x6000, 0x6100);
255 LocationSix->setIsAddressRange();
256 EXPECT_STREQ(LocationSix->getIntervalInfo().c_str(),
257 "{Range} Lines 600:600 [0x0000006000:0x0000006100]");
258
259 // Add ranges to Function.
260 // Function: LocationOne, LocationTwo, LocationFive, LocationSix
261 Function->addObject(LocationOne);
262 Function->addObject(LocationTwo);
263 Function->addObject(LocationFive);
264 Function->addObject(LocationSix);
265 EXPECT_EQ(Function->rangeCount(), 4u);
266
267 // Add ranges to NestedScope.
268 // NestedScope: LocationThree, LocationFour
269 NestedScope->addObject(LocationThree);
270 NestedScope->addObject(LocationFour);
271 EXPECT_EQ(NestedScope->rangeCount(), 2u);
272
273 // Get all ranges.
274 LVRange Ranges;
275 CompileUnit->getRanges(Ranges);
276 Ranges.startSearch();
277 EXPECT_EQ(Ranges.getEntry(0x4000), nullptr);
278
279 EXPECT_EQ(Ranges.getEntry(0x5060), Function);
280 EXPECT_EQ(Ranges.getEntry(0x5850), Function);
281 EXPECT_EQ(Ranges.getEntry(0x5010, 0x5090), Function);
282 EXPECT_EQ(Ranges.getEntry(0x5210, 0x5290), Function);
283 EXPECT_EQ(Ranges.getEntry(0x5810, 0x5890), Function);
284 EXPECT_EQ(Ranges.getEntry(0x6010, 0x6090), Function);
285
286 EXPECT_EQ(Ranges.getEntry(0x5400), NestedScope);
287 EXPECT_EQ(Ranges.getEntry(0x5650), NestedScope);
288 EXPECT_EQ(Ranges.getEntry(0x5410, 0x5490), NestedScope);
289 EXPECT_EQ(Ranges.getEntry(0x5610, 0x5690), NestedScope);
290
291 EXPECT_EQ(Ranges.getEntry(0x8000), nullptr);
292 Ranges.endSearch();
293
294 // Add locations to symbols.
295 // Parameter: [LineOne, LineSix]
296 // LocalVariable: [LineTwo, LineSix], [LineFour, LineFive]
297 // NestedVariable: [LineThree, LineFour]
298 add(Parameter, LineOne, LineSix);
299 add(LocalVariable, LineTwo, LineSix);
300 add(LocalVariable, LineFour, LineFive);
301 add(NestedVariable, LineThree, LineFour);
302
303 LVLocation *Location;
304 LVLocations Locations;
305 Parameter->getLocations(Locations);
306 ASSERT_EQ(Locations.size(), 1u);
307 Location = Locations[0];
308 EXPECT_EQ(Location->getLowerAddress(), LineOne->getAddress());
309 EXPECT_EQ(Location->getUpperAddress(), LineSix->getAddress());
310
311 Locations.clear();
312 LocalVariable->getLocations(Locations);
313 ASSERT_EQ(Locations.size(), 2u);
314 Location = Locations[0];
315 EXPECT_EQ(Location->getLowerAddress(), LineTwo->getAddress());
316 EXPECT_EQ(Location->getUpperAddress(), LineSix->getAddress());
317 Location = Locations[1];
318 EXPECT_EQ(Location->getLowerAddress(), LineFour->getAddress());
319 EXPECT_EQ(Location->getUpperAddress(), LineFive->getAddress());
320
321 Locations.clear();
322 NestedVariable->getLocations(Locations);
323 ASSERT_EQ(Locations.size(), 1u);
324 Location = Locations[0];
325 EXPECT_EQ(Location->getLowerAddress(), LineThree->getAddress());
326 EXPECT_EQ(Location->getUpperAddress(), LineFour->getAddress());
327 }
328
329 class ReaderTestCoverage : public ReaderTest {
330 // Types.
331 LVType *IntegerType = nullptr;
332
333 // Scopes.
334 LVScopeFunction *Function = nullptr;
335 LVScopeFunctionInlined *InlinedFunction = nullptr;
336
337 // Symbols.
338 LVSymbol *Variable = nullptr;
339 LVSymbol *Parameter = nullptr;
340
341 // Lines.
342 LVLine *LineOne = nullptr;
343 LVLine *LineTwo = nullptr;
344 LVLine *LineThree = nullptr;
345 LVLine *LineFour = nullptr;
346 LVLine *LineFive = nullptr;
347 LVLine *LineSix = nullptr;
348
349 // Locations.
350 LVLocation *LocationOne = nullptr;
351 LVLocation *LocationTwo = nullptr;
352 LVLocation *LocationFive = nullptr;
353 LVLocation *LocationSix = nullptr;
354
355 public:
356 ReaderTestCoverage(ScopedPrinter &W) : ReaderTest(W) {}
357
358 void createElements();
359 void addElements();
360 void initElements();
361 };
362
363 // Create the logical elements.
364 void ReaderTestCoverage::createElements() {
365 // Create scope root.
366 Error Err = createScopes();
367 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
368 Root = getScopesRoot();
369 EXPECT_NE(Root, nullptr);
370
371 // Create the logical types.
372 IntegerType = create<LVType>();
373
374 // Create the logical scopes.
375 CompileUnit = create<LVScopeCompileUnit>();
376 Function = create<LVScopeFunction>();
377 InlinedFunction = create<LVScopeFunctionInlined>();
378
379 // Create the logical symbols.
380 Variable = create<LVSymbol>();
381 Parameter = create<LVSymbol>();
382
383 // Create the logical lines.
384 LineOne = create<LVLine>();
385 LineTwo = create<LVLine>();
386 LineThree = create<LVLine>();
387 LineFour = create<LVLine>();
388 LineFive = create<LVLine>();
389 LineSix = create<LVLine>();
390
391 // Create the logical locations.
392 LocationOne = create<LVLocation>();
393 LocationTwo = create<LVLocation>();
394 LocationFive = create<LVLocation>();
395 LocationSix = create<LVLocation>();
396 }
397
398 // Create the logical view adding the created logical elements.
399 void ReaderTestCoverage::addElements() {
400 setCompileUnit(CompileUnit);
401
402 // Root
403 // CompileUnit
404 // IntegerType
405 // Function
406 // Ranges
407 // [LineOne, LineOne]
408 // [LineTwo, LineSix]
409 // [LineSix, LineSix]
410 // LineOne
411 // LineTwo
412 // InlinedFunction
413 // Ranges
414 // [LineFive, LineFive]
415 // Parameter
416 // Location
417 // [LineThree, LineThree]
418 // Variable
419 // Location
420 // [LineFour, LineFive]
421 // [LineFive, LineSix]
422 // LineThree
423 // LineFour
424 // LineFive
425 // LineSix
426
427 // Add elements to Root.
428 add(Root, CompileUnit);
429
430 // Add elements to CompileUnit.
431 add(CompileUnit, IntegerType);
432 add(CompileUnit, Function);
433
434 // Add elements to Function.
435 add(Function, InlinedFunction);
436 add(Function, LineOne);
437 add(Function, LineTwo);
438 add(Function, LineSix);
439
440 // Add elements to function InlinedFunction.
441 add(InlinedFunction, Parameter);
442 add(InlinedFunction, Variable);
443 add(InlinedFunction, LineThree);
444 add(InlinedFunction, LineFour);
445 add(InlinedFunction, LineFive);
446 }
447
448 // Set initial values to logical elements.
449 void ReaderTestCoverage::initElements() {
450 // Types.
451 set(IntegerType, "int", 0x1000);
452
453 // Scopes.
454 set(CompileUnit, "foo.cpp", 0x2000);
455 set(Function, "foo", 0x2500, 100, IntegerType);
456 set(InlinedFunction, "InlinedFunction", 0x3000, 300);
457
458 // Symbols.
459 set(Parameter, "Parameter", 0x3100, 310, IntegerType);
460 set(Variable, "Variable", 0x3200, 320, IntegerType);
461
462 // Lines.
463 set(LineOne, "", 0x5000, 100);
464 set(LineTwo, "", 0x5200, 200);
465 set(LineThree, "", 0x5400, 300);
466 set(LineFour, "", 0x5600, 400);
467 set(LineFive, "", 0x5800, 500);
468 set(LineSix, "", 0x6000, 600);
469
470 // Locations.
471 set(LocationOne, LineOne, LineOne, 0x5000, 0x5199);
472 EXPECT_STREQ(LocationOne->getIntervalInfo().c_str(),
473 " Lines 100:100 [0x0000005000:0x0000005199]");
474
475 set(LocationTwo, LineTwo, LineSix, 0x5200, 0x6100);
476 EXPECT_STREQ(LocationTwo->getIntervalInfo().c_str(),
477 " Lines 200:600 [0x0000005200:0x0000006100]");
478
479 set(LocationFive, LineFive, LineFive, 0x5800, 0x5900);
480 EXPECT_STREQ(LocationFive->getIntervalInfo().c_str(),
481 " Lines 500:500 [0x0000005800:0x0000005900]");
482
483 set(LocationSix, LineSix, LineSix, 0x6000, 0x6100);
484 EXPECT_STREQ(LocationSix->getIntervalInfo().c_str(),
485 " Lines 600:600 [0x0000006000:0x0000006100]");
486
487 // Add ranges to Function.
488 // Function: LocationOne, LocationTwo, LocationSix
489 Function->addObject(LocationOne);
490 Function->addObject(LocationTwo);
491 Function->addObject(LocationSix);
492 EXPECT_EQ(Function->rangeCount(), 3u);
493
494 // Add ranges to Inlined.
495 // Inlined: LocationFive
496 InlinedFunction->addObject(LocationFive);
497 EXPECT_EQ(InlinedFunction->rangeCount(), 1u);
498
499 // Add locations to symbols.
500 // Parameter: [LineThree, LineThree]
501 // Variable: [LineFour, LineFive], [LineFive, LineSix]
502 add(Parameter, LineThree, LineThree);
503 add(Variable, LineFour, LineFive);
504 add(Variable, LineFive, LineSix);
505
506 CompileUnit->processRangeLocationCoverage();
507
508 LVLocation *Location;
509 LVLocations Locations;
510 Parameter->getLocations(Locations);
511 ASSERT_EQ(Locations.size(), 1u);
512 Location = Locations[0];
513 EXPECT_EQ(Location->getLowerAddress(), LineThree->getAddress());
514 EXPECT_EQ(Location->getUpperAddress(), LineThree->getAddress());
515
516 Locations.clear();
517 Variable->getLocations(Locations);
518 ASSERT_EQ(Locations.size(), 2u);
519 Location = Locations[0];
520 EXPECT_EQ(Location->getLowerAddress(), LineFour->getAddress());
521 EXPECT_EQ(Location->getUpperAddress(), LineFive->getAddress());
522 Location = Locations[1];
523 EXPECT_EQ(Location->getLowerAddress(), LineFive->getAddress());
524 EXPECT_EQ(Location->getUpperAddress(), LineSix->getAddress());
525
526 // Test the changes done to 'LVScope::outermostParent' to use the
527 // ranges allocated int the current scope during the scopes traversal.
528 // These are the pre-conditions for the symbol:
529 // - Its parent must be an inlined function.
530 // - Have more than one location description.
531
532 // Before the changes: Parameter: CoveragePercentage = 100.00
533 // After the changes: Parameter: CoveragePercentage = 100.00
534 EXPECT_FLOAT_EQ(Parameter->getCoveragePercentage(), 100.00f);
535
536 // Before the changes: Variable: CoveragePercentage = 1000.00
537 // After the changes: Variable: CoveragePercentage = 56.83
538 EXPECT_FLOAT_EQ(Variable->getCoveragePercentage(), 56.83f);
539 }
540
541 TEST(LogicalViewTest, LocationRanges) {
542 ScopedPrinter W(outs());
543 ReaderTestLocations Reader(W);
544
545 // Reader options.
546 LVOptions ReaderOptions;
547 ReaderOptions.setAttributeOffset();
548 ReaderOptions.setPrintAll();
549 ReaderOptions.resolveDependencies();
550 options().setOptions(&ReaderOptions);
551
552 Reader.createElements();
553 Reader.addElements();
554 Reader.initElements();
555 }
556
557 TEST(LogicalViewTest, LocationCoverage) {
558 ScopedPrinter W(outs());
559 ReaderTestCoverage Reader(W);
560
561 // Reader options.
562 LVOptions ReaderOptions;
563 ReaderOptions.setAttributeOffset();
564 ReaderOptions.setAttributeRange();
565 ReaderOptions.setAttributeLocation();
566 ReaderOptions.setPrintAll();
567 ReaderOptions.resolveDependencies();
568 options().setOptions(&ReaderOptions);
569
570 Reader.createElements();
571 Reader.addElements();
572 Reader.initElements();
573 }
574
575 } // namespace