Debian Codegraph @

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

raw
1 //===- llvm/unittest/DebugInfo/LogicalView/WarningInternalTest.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 MyLocation;
26
27 // This code emulates the work done by the Readers when processing the
28 // binary files and the creation of the AddressToLine mapping is done
29 // automatically, using the text sections.
30 class MyAddressToLine {
31 using LVAddressToLine = std::map<LVAddress, LVLine *>;
32 LVAddressToLine AddressToLineData;
33
34 public:
35 MyAddressToLine() = default;
36
37 void insert(LVLine *Line) {
38 AddressToLineData.emplace(Line->getOffset(), Line);
39 }
40
41 LVLine *lineLowerBound(LVAddress Address);
42 LVLine *lineUpperBound(LVAddress Address);
43 };
44
45 LVLine *MyAddressToLine::lineLowerBound(LVAddress Address) {
46 LVAddressToLine::const_iterator Iter = AddressToLineData.lower_bound(Address);
47 return (Iter != AddressToLineData.end()) ? Iter->second : nullptr;
48 }
49
50 LVLine *MyAddressToLine::lineUpperBound(LVAddress Address) {
51 if (AddressToLineData.empty())
52 return nullptr;
53 LVAddressToLine::const_iterator Iter = AddressToLineData.upper_bound(Address);
54 if (Iter != AddressToLineData.begin())
55 Iter = std::prev(Iter);
56 return Iter->second;
57 }
58
59 MyAddressToLine AddressToLine;
60
61 class ReaderTestWarningInternal : public LVReader {
62 // Types.
63 LVType *IntegerType = nullptr;
64
65 // Scopes.
66 LVScope *NestedScope = nullptr;
67 LVScopeFunction *Function = nullptr;
68
69 // Symbols.
70 LVSymbol *LocalVariable = nullptr;
71 LVSymbol *NestedVariable = nullptr;
72 LVSymbol *Parameter = nullptr;
73
74 // Lines.
75 LVLine *LineOne = nullptr;
76 LVLine *LineTwo = nullptr;
77 LVLine *LineThree = nullptr;
78 LVLine *LineFour = nullptr;
79 LVLine *LineFive = nullptr;
80 LVLine *LineSix = nullptr;
81
82 // Locations.
83 MyLocation *LocationOne = nullptr;
84 MyLocation *LocationTwo = nullptr;
85 MyLocation *LocationThree = nullptr;
86 MyLocation *LocationFour = nullptr;
87 MyLocation *LocationFive = nullptr;
88 MyLocation *LocationSix = nullptr;
89
90 protected:
91 void add(LVScope *Parent, LVElement *Element);
92 template <typename T> T *create() {
93 T *Element = new (std::nothrow) T();
94 EXPECT_NE(Element, nullptr);
95 return Element;
96 }
97 void set(LVElement *Element, StringRef Name, LVOffset Offset,
98 uint32_t LineNumber = 0, LVElement *Type = nullptr);
99 void set(MyLocation *Location, LVLine *LowerLine, LVLine *UpperLine,
100 LVAddress LowerAddress, LVAddress UpperAddress);
101 void add(LVSymbol *Symbol, LVLine *LowerLine, LVLine *UpperLine);
102
103 public:
104 ReaderTestWarningInternal(ScopedPrinter &W) : LVReader("", "", W) {
105 setInstance(this);
106 }
107
108 Error createScopes() { return LVReader::createScopes(); }
109
110 void setMapping();
111 void createElements();
112 void addElements();
113 void initElements();
114 void resolveElements();
115 void checkWarnings();
116 };
117
118 class MyLocation : public LVLocation {
119 public:
120 bool validateRanges();
121 };
122
123 bool MyLocation::validateRanges() {
124 // Traverse the locations and validate them against the address to line
125 // mapping in the current compile unit. Record those invalid ranges.
126 // A valid range must meet the following conditions:
127 // a) line(lopc) <= line(hipc)
128 // b) line(lopc) and line(hipc) are valid.
129
130 LVLine *LowLine = AddressToLine.lineLowerBound(getLowerAddress());
131 LVLine *HighLine = AddressToLine.lineUpperBound(getUpperAddress());
132 if (LowLine)
133 setLowerLine(LowLine);
134 else {
135 setIsInvalidLower();
136 return false;
137 }
138 if (HighLine)
139 setUpperLine(HighLine);
140 else {
141 setIsInvalidUpper();
142 return false;
143 }
144 // Check for a valid interval.
145 if (LowLine->getLineNumber() > HighLine->getLineNumber()) {
146 setIsInvalidRange();
147 return false;
148 }
149
150 return true;
151 }
152
153 // Map all logical lines with their addresses.
154 void ReaderTestWarningInternal::setMapping() {
155 AddressToLine.insert(LineOne);
156 AddressToLine.insert(LineTwo);
157 AddressToLine.insert(LineThree);
158 AddressToLine.insert(LineFour);
159 AddressToLine.insert(LineFive);
160 AddressToLine.insert(LineSix);
161 }
162
163 // Helper function to add a logical element to a given scope.
164 void ReaderTestWarningInternal::add(LVScope *Parent, LVElement *Child) {
165 Parent->addElement(Child);
166 EXPECT_EQ(Child->getParent(), Parent);
167 EXPECT_EQ(Child->getLevel(), Parent->getLevel() + 1);
168 }
169
170 // Helper function to set the initial values for a given logical element.
171 void ReaderTestWarningInternal::set(LVElement *Element, StringRef Name,
172 LVOffset Offset, uint32_t LineNumber,
173 LVElement *Type) {
174 Element->setName(Name);
175 Element->setOffset(Offset);
176 Element->setLineNumber(LineNumber);
177 Element->setType(Type);
178 EXPECT_EQ(Element->getName(), Name);
179 EXPECT_EQ(Element->getOffset(), Offset);
180 EXPECT_EQ(Element->getLineNumber(), LineNumber);
181 EXPECT_EQ(Element->getType(), Type);
182 }
183
184 // Helper function to set the initial values for a given logical location.
185 void ReaderTestWarningInternal::set(MyLocation *Location, LVLine *LowerLine,
186 LVLine *UpperLine, LVAddress LowerAddress,
187 LVAddress UpperAddress) {
188 Location->setLowerLine(LowerLine);
189 Location->setUpperLine(UpperLine);
190 Location->setLowerAddress(LowerAddress);
191 Location->setUpperAddress(UpperAddress);
192 EXPECT_EQ(Location->getLowerLine(), LowerLine);
193 EXPECT_EQ(Location->getUpperLine(), UpperLine);
194 EXPECT_EQ(Location->getLowerAddress(), LowerAddress);
195 EXPECT_EQ(Location->getUpperAddress(), UpperAddress);
196 }
197
198 // Helper function to add a logical location to a logical symbol.
199 void ReaderTestWarningInternal::add(LVSymbol *Symbol, LVLine *LowerLine,
200 LVLine *UpperLine) {
201 dwarf::Attribute Attr = dwarf::DW_AT_location;
202
203 Symbol->addLocation(Attr, LowerLine->getAddress(), UpperLine->getAddress(),
204 /*SectionOffset=*/0, /*LocDesOffset=*/0);
205 }
206
207 // Create the logical elements.
208 void ReaderTestWarningInternal::createElements() {
209 // Create scope root.
210 Error Err = createScopes();
211 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
212 Root = getScopesRoot();
213 EXPECT_NE(Root, nullptr);
214
215 // Create the logical types.
216 IntegerType = create<LVType>();
217
218 // Create the logical scopes.
219 NestedScope = create<LVScope>();
220 CompileUnit = create<LVScopeCompileUnit>();
221 Function = create<LVScopeFunction>();
222
223 // Create the logical symbols.
224 LocalVariable = create<LVSymbol>();
225 NestedVariable = create<LVSymbol>();
226 Parameter = create<LVSymbol>();
227
228 // Create the logical lines.
229 LineOne = create<LVLine>();
230 LineTwo = create<LVLine>();
231 LineThree = create<LVLine>();
232 LineFour = create<LVLine>();
233 LineFive = create<LVLine>();
234 LineSix = create<LVLine>();
235
236 // Create the logical locations.
237 LocationOne = create<MyLocation>();
238 LocationTwo = create<MyLocation>();
239 LocationThree = create<MyLocation>();
240 LocationFour = create<MyLocation>();
241 LocationFive = create<MyLocation>();
242 LocationSix = create<MyLocation>();
243 }
244
245 // Create the logical view adding the created logical elements.
246 void ReaderTestWarningInternal::addElements() {
247 setCompileUnit(CompileUnit);
248
249 // Root
250 // CompileUnit
251 // IntegerType
252 // Function
253 // LocationOne
254 // LocationTwo
255 // LocationFive
256 // LocationSix
257 // Parameter
258 // LocalVariable
259 // LineOne
260 // LineTwo
261 // NestedScope
262 // LocationThree
263 // LocationFour
264 // NestedVariable
265 // LineThree
266 // LineFour
267 // LineFive
268 // LineSix
269
270 // Add elements to Root.
271 add(Root, CompileUnit);
272
273 // Add elements to CompileUnit.
274 add(CompileUnit, IntegerType);
275 add(CompileUnit, Function);
276
277 // Add elements to Function.
278 add(Function, Parameter);
279 add(Function, LocalVariable);
280 add(Function, LineOne);
281 add(Function, LineTwo);
282 add(Function, LineFive);
283 add(Function, LineSix);
284 add(Function, NestedScope);
285
286 // Add elements to NestedScope.
287 add(NestedScope, NestedVariable);
288 add(NestedScope, LineThree);
289 add(NestedScope, LineFour);
290 }
291
292 void ReaderTestWarningInternal::resolveElements() {
293 // Traverse the given scope and its children checking for any warnings.
294 std::function<void(LVScope * Parent)> TraverseScope = [&](LVScope *Parent) {
295 auto Warnings = [&](auto *Entry) {
296 if (Entry->getIsLine()) {
297 LVLine *Line = (LVLine *)Entry;
298 if (options().getWarningLines() && Line->getIsLineDebug() &&
299 !Line->getLineNumber())
300 CompileUnit->addLineZero(Line);
301 }
302 };
303 auto Traverse = [&](const auto *Set) {
304 if (Set)
305 for (const auto &Entry : *Set) {
306 Warnings(Entry);
307 }
308 };
309
310 Warnings(Parent);
311
312 Traverse(Parent->getSymbols());
313 Traverse(Parent->getTypes());
314 Traverse(Parent->getLines());
315
316 if (const LVScopes *Scopes = Parent->getScopes())
317 for (LVScope *Scope : *Scopes) {
318 Warnings(Scope);
319 TraverseScope(Scope);
320 }
321 };
322
323 // Start traversing the scopes root and resolve the elements.
324 TraverseScope(Root);
325 }
326
327 // Set initial values to logical elements.
328 void ReaderTestWarningInternal::initElements() {
329 // Types.
330 set(IntegerType, "int", 0x1000);
331
332 // Scopes.
333 set(CompileUnit, "foo.cpp", 0x2000);
334 set(Function, "foo", 0x2010, 100, IntegerType);
335 set(NestedScope, "", 0x2020, 300);
336
337 // Symbols.
338 set(Parameter, "Param", 0x3000, 110, IntegerType);
339 set(LocalVariable, "LocalVariable", 0x3020, 120, IntegerType);
340 set(NestedVariable, "NestedVariable", 0x3010, 310, IntegerType);
341
342 // Lines.
343 set(LineOne, "", 0x5000, 100);
344 LineOne->setIsLineDebug();
345 set(LineTwo, "", 0x5200, 000);
346 LineTwo->setIsLineDebug();
347 set(LineThree, "", 0x5400, 300);
348 LineThree->setIsLineDebug();
349 set(LineFour, "", 0x5600, 000);
350 LineFour->setIsLineDebug();
351 set(LineFive, "", 0x5800, 500);
352 LineOne->setIsLineDebug();
353 set(LineSix, "", 0x6000, 600);
354 LineSix->setIsLineDebug();
355
356 // Locations.
357 set(LocationOne, LineOne, LineOne, 0x5000, 0x5100);
358 EXPECT_STREQ(LocationOne->getIntervalInfo().c_str(),
359 " Lines 100:100 [0x0000005000:0x0000005100]");
360
361 // Uses a Line zero.
362 set(LocationTwo, LineTwo, LineTwo, 0x5200, 0x5300);
363 EXPECT_STREQ(LocationTwo->getIntervalInfo().c_str(),
364 " Lines -:- [0x0000005200:0x0000005300]");
365
366 set(LocationThree, LineThree, LineThree, 0x5400, 0x5500);
367 EXPECT_STREQ(LocationThree->getIntervalInfo().c_str(),
368 " Lines 300:300 [0x0000005400:0x0000005500]");
369
370 // Uses a Line zero.
371 set(LocationFour, LineFour, LineFour, 0x5600, 0x5700);
372 LocationFour->setIsAddressRange();
373 EXPECT_STREQ(LocationFour->getIntervalInfo().c_str(),
374 "{Range} Lines -:- [0x0000005600:0x0000005700]");
375
376 // Invalid range.
377 set(LocationFive, LineFive, LineFive, 0x7800, 0x5900);
378 LocationFive->setIsAddressRange();
379 EXPECT_STREQ(LocationFive->getIntervalInfo().c_str(),
380 "{Range} Lines 500:500 [0x0000007800:0x0000005900]");
381
382 set(LocationSix, LineSix, LineSix, 0x6000, 0x6100);
383 LocationSix->setIsAddressRange();
384 EXPECT_STREQ(LocationSix->getIntervalInfo().c_str(),
385 "{Range} Lines 600:600 [0x0000006000:0x0000006100]");
386
387 // Add ranges to Function.
388 // Function: LocationOne, LocationTwo, LocationFive, LocationSix
389 Function->addObject(LocationOne);
390 Function->addObject(LocationTwo);
391 Function->addObject(LocationFive);
392 Function->addObject(LocationSix);
393 EXPECT_EQ(Function->rangeCount(), 4u);
394
395 // Add ranges to NestedScope.
396 // NestedScope: LocationThree, LocationFour
397 NestedScope->addObject(LocationThree);
398 NestedScope->addObject(LocationFour);
399 EXPECT_EQ(NestedScope->rangeCount(), 2u);
400
401 // Get all ranges.
402 LVRange Ranges;
403 CompileUnit->getRanges(Ranges);
404 Ranges.startSearch();
405 EXPECT_EQ(Ranges.getEntry(0x4000), nullptr);
406
407 EXPECT_EQ(Ranges.getEntry(0x5060), Function);
408 EXPECT_EQ(Ranges.getEntry(0x5850), nullptr);
409 EXPECT_EQ(Ranges.getEntry(0x5010, 0x5090), Function);
410 EXPECT_EQ(Ranges.getEntry(0x5210, 0x5290), Function);
411 EXPECT_EQ(Ranges.getEntry(0x5810, 0x5890), nullptr);
412 EXPECT_EQ(Ranges.getEntry(0x6010, 0x6090), Function);
413
414 EXPECT_EQ(Ranges.getEntry(0x5400), NestedScope);
415 EXPECT_EQ(Ranges.getEntry(0x5650), NestedScope);
416 EXPECT_EQ(Ranges.getEntry(0x5410, 0x5490), NestedScope);
417 EXPECT_EQ(Ranges.getEntry(0x5610, 0x5690), NestedScope);
418
419 EXPECT_EQ(Ranges.getEntry(0x8000), nullptr);
420 Ranges.endSearch();
421
422 // Add locations to symbols.
423 // Parameter: [LineOne, LineSix]
424 // LocalVariable: [LineTwo, LineSix], [LineFour, LineFive]
425 // NestedVariable: [LineThree, LineFour]
426 add(Parameter, LineOne, LineSix);
427 add(LocalVariable, LineTwo, LineSix);
428 add(LocalVariable, LineFour, LineFive);
429 add(NestedVariable, LineThree, LineFour);
430 add(NestedVariable, LineOne, LineSix);
431 }
432
433 // Check logical elements warnigs.
434 void ReaderTestWarningInternal::checkWarnings() {
435 // Map all lines with their addresses.
436 setMapping();
437
438 // Check for lines with line zero.
439 resolveElements();
440
441 // Check invalid locations and ranges using a customized validation.
442 CompileUnit->processRangeLocationCoverage(
443 (LVValidLocation)(&MyLocation::validateRanges));
444
445 // Get lines with line zero. [Parent, Line]
446 // Function, LineTwo
447 // NestedScope, LineFour
448 LVOffsetLinesMap LinesZero = CompileUnit->getLinesZero();
449 ASSERT_EQ(LinesZero.size(), 2u);
450
451 LVOffsetLinesMap::iterator IterZero = LinesZero.begin();
452 EXPECT_EQ(IterZero->first, Function->getOffset());
453 LVLines *Lines = IterZero->second;
454 EXPECT_NE(Lines, nullptr);
455 ASSERT_EQ(Lines->size(), 1u);
456 LVLine *Line = *(Lines->begin());
457 EXPECT_NE(Line, nullptr);
458 EXPECT_EQ(Line, LineTwo);
459
460 ++IterZero;
461 EXPECT_EQ(IterZero->first, NestedScope->getOffset());
462 Lines = IterZero->second;
463 EXPECT_NE(Lines, nullptr);
464 ASSERT_EQ(Lines->size(), 1u);
465 Line = *(Lines->begin());
466 EXPECT_NE(Line, nullptr);
467 EXPECT_EQ(Line, LineFour);
468
469 // Elements with invalid offsets.
470 // Function (line zero)
471 // NestedScope (line zero)
472 // NestedVariable (invalid location)
473 LVOffsetElementMap InvalidOffsets = CompileUnit->getWarningOffsets();
474 ASSERT_EQ(InvalidOffsets.size(), 3u);
475
476 LVOffsetElementMap::iterator IterOffset = InvalidOffsets.begin();
477 EXPECT_EQ(IterOffset->second, Function);
478 ++IterOffset;
479 EXPECT_EQ(IterOffset->second, NestedScope);
480 ++IterOffset;
481 EXPECT_EQ(IterOffset->second, NestedVariable);
482
483 // Invalid ranges.
484 // Function
485 LVOffsetLocationsMap InvalidRanges = CompileUnit->getInvalidRanges();
486 ASSERT_EQ(InvalidRanges.size(), 1u);
487
488 LVOffsetLocationsMap::iterator IterRange = InvalidRanges.begin();
489 EXPECT_EQ(IterRange->first, Function->getOffset());
490 LVLocations *Locations = IterRange->second;
491 EXPECT_NE(Locations, nullptr);
492 ASSERT_EQ(Locations->size(), 1u);
493 LVLocation *Location = *(Locations->begin());
494 EXPECT_NE(Location, nullptr);
495 EXPECT_EQ(Location, LocationFive);
496
497 // Invalid location.
498 // NestedVariable
499 LVOffsetLocationsMap InvalidLocations = CompileUnit->getInvalidLocations();
500 ASSERT_EQ(InvalidLocations.size(), 1u);
501
502 LVOffsetLocationsMap::iterator IterLocations = InvalidLocations.begin();
503 EXPECT_EQ(IterLocations->first, NestedVariable->getOffset());
504 Locations = IterLocations->second;
505 EXPECT_NE(Locations, nullptr);
506 ASSERT_EQ(Locations->size(), 1u);
507 Location = *(Locations->begin());
508 EXPECT_NE(Location, nullptr);
509 EXPECT_EQ(Location->getLowerAddress(), LocationThree->getLowerAddress());
510 EXPECT_EQ(Location->getUpperAddress(), LocationFour->getLowerAddress());
511 EXPECT_EQ(Location->getLowerLine()->getLineNumber(),
512 LineThree->getLineNumber());
513 EXPECT_EQ(Location->getUpperLine()->getLineNumber(), 0u);
514
515 // Invalid coverages.
516 // NestedVariable
517 LVOffsetSymbolMap InvalidCoverages = CompileUnit->getInvalidCoverages();
518 ASSERT_EQ(InvalidCoverages.size(), 1u);
519
520 LVOffsetSymbolMap::iterator IterCoverages = InvalidCoverages.begin();
521 EXPECT_EQ(IterCoverages->first, NestedVariable->getOffset());
522 EXPECT_EQ(IterCoverages->second, NestedVariable);
523 EXPECT_GE((int)NestedVariable->getCoveragePercentage(), 100);
524 EXPECT_EQ((int)NestedVariable->getCoveragePercentage(), 900);
525 EXPECT_EQ(NestedVariable->getCoverageFactor(), 0x1200u);
526
527 EXPECT_EQ((unsigned)Parameter->getCoveragePercentage(), 100u);
528 EXPECT_EQ(Parameter->getCoverageFactor(), 100u);
529
530 EXPECT_EQ((unsigned)LocalVariable->getCoveragePercentage(), 47u);
531 EXPECT_EQ(LocalVariable->getCoverageFactor(),
532 LineSix->getAddress() - LineOne->getAddress());
533 }
534
535 TEST(LogicalViewTest, WarningInternal) {
536 ScopedPrinter W(outs());
537 ReaderTestWarningInternal Reader(W);
538
539 // Reader options.
540 LVOptions ReaderOptions;
541 ReaderOptions.setAttributeOffset();
542 ReaderOptions.setAttributeRange();
543 ReaderOptions.setAttributeLocation();
544 ReaderOptions.setPrintAll();
545 ReaderOptions.setWarningCoverages();
546 ReaderOptions.setWarningLines();
547 ReaderOptions.setWarningLocations();
548 ReaderOptions.setWarningRanges();
549 ReaderOptions.resolveDependencies();
550 options().setOptions(&ReaderOptions);
551
552 Reader.createElements();
553 Reader.addElements();
554 Reader.initElements();
555 Reader.checkWarnings();
556 }
557
558 } // namespace