Debian Codegraph @

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

raw
1 //===- llvm/unittest/DebugInfo/LogicalView/CompareElementsTest.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/LVCompare.h"
10 #include "llvm/DebugInfo/LogicalView/Core/LVLine.h"
11 #include "llvm/DebugInfo/LogicalView/Core/LVReader.h"
12 #include "llvm/DebugInfo/LogicalView/Core/LVScope.h"
13 #include "llvm/DebugInfo/LogicalView/Core/LVSymbol.h"
14 #include "llvm/DebugInfo/LogicalView/Core/LVType.h"
15 #include "llvm/Support/ScopedPrinter.h"
16 #include "llvm/Support/ToolOutputFile.h"
17 #include "llvm/Testing/Support/Error.h"
18
19 #include "gtest/gtest.h"
20
21 using namespace llvm;
22 using namespace llvm::logicalview;
23
24 namespace {
25
26 //===----------------------------------------------------------------------===//
27 // Basic Reader functionality.
28 class ReaderTestCompare : public LVReader {
29 // Elements created but not added to any logical scope. They are
30 // deleted when the logical Reader is destroyed.
31 LVAutoSmallVector<LVElement *> OrphanElements;
32
33 public:
34 // Types.
35 LVType *IntegerType = nullptr;
36 LVType *UnsignedType = nullptr;
37 LVType *GlobalType = nullptr;
38 LVType *LocalType = nullptr;
39 LVType *NestedType = nullptr;
40 LVTypeDefinition *TypeDefinitionOne = nullptr;
41 LVTypeDefinition *TypeDefinitionTwo = nullptr;
42 LVTypeEnumerator *EnumeratorOne = nullptr;
43 LVTypeEnumerator *EnumeratorTwo = nullptr;
44
45 // Scopes.
46 LVScope *NestedScope = nullptr;
47 LVScope *InnerScope = nullptr;
48 LVScopeAggregate *Aggregate = nullptr;
49 LVScopeEnumeration *Enumeration = nullptr;
50 LVScopeFunction *FunctionOne = nullptr;
51 LVScopeFunction *FunctionTwo = nullptr;
52 LVScopeNamespace *Namespace = nullptr;
53
54 // Symbols.
55 LVSymbol *GlobalVariable = nullptr;
56 LVSymbol *LocalVariable = nullptr;
57 LVSymbol *ClassMember = nullptr;
58 LVSymbol *NestedVariable = nullptr;
59 LVSymbol *ParameterOne = nullptr;
60 LVSymbol *ParameterTwo = nullptr;
61
62 // Lines.
63 LVLine *LineOne = nullptr;
64 LVLine *LineTwo = nullptr;
65 LVLine *LineThree = nullptr;
66
67 protected:
68 void add(LVScope *Parent, LVElement *Element);
69 template <typename T, typename F> T *create(F Function) {
70 // 'Function' will update a specific kind of the logical element to
71 // have the ability of kind selection.
72 T *Element = new (std::nothrow) T();
73 EXPECT_NE(Element, nullptr);
74 (Element->*Function)();
75 return Element;
76 }
77 void set(LVElement *Element, StringRef Name, LVOffset Offset,
78 uint32_t LineNumber = 0, LVElement *Type = nullptr);
79
80 public:
81 ReaderTestCompare(ScopedPrinter &W) : LVReader("", "", W) {
82 setInstance(this);
83 }
84
85 Error createScopes() { return LVReader::createScopes(); }
86 Error printScopes() { return LVReader::printScopes(); }
87
88 void createElements();
89 void addElements(bool IsReference, bool IsTarget);
90 void initElements();
91 };
92
93 // Helper function to add a logical element to a given scope.
94 void ReaderTestCompare::add(LVScope *Parent, LVElement *Child) {
95 Parent->addElement(Child);
96 EXPECT_EQ(Child->getParent(), Parent);
97 EXPECT_EQ(Child->getLevel(), Parent->getLevel() + 1);
98 }
99
100 // Helper function to set the initial values for a given logical element.
101 void ReaderTestCompare::set(LVElement *Element, StringRef Name, LVOffset Offset,
102 uint32_t LineNumber, LVElement *Type) {
103 Element->setName(Name);
104 Element->setOffset(Offset);
105 Element->setLineNumber(LineNumber);
106 Element->setType(Type);
107 }
108
109 //===----------------------------------------------------------------------===//
110 // Create the logical elements.
111 void ReaderTestCompare::createElements() {
112 // Create scope root.
113 Error Err = createScopes();
114 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
115 Root = getScopesRoot();
116 ASSERT_NE(Root, nullptr);
117
118 // Create the logical types.
119 IntegerType = create<LVType, LVTypeSetFunction>(&LVType::setIsBase);
120 UnsignedType = create<LVType, LVTypeSetFunction>(&LVType::setIsBase);
121 GlobalType = create<LVType, LVTypeSetFunction>(&LVType::setIsBase);
122 LocalType = create<LVType, LVTypeSetFunction>(&LVType::setIsBase);
123 NestedType = create<LVType, LVTypeSetFunction>(&LVType::setIsBase);
124 EnumeratorOne =
125 create<LVTypeEnumerator, LVTypeSetFunction>(&LVType::setIsEnumerator);
126 EnumeratorTwo =
127 create<LVTypeEnumerator, LVTypeSetFunction>(&LVType::setIsEnumerator);
128 TypeDefinitionOne =
129 create<LVTypeDefinition, LVTypeSetFunction>(&LVType::setIsTypedef);
130 TypeDefinitionTwo =
131 create<LVTypeDefinition, LVTypeSetFunction>(&LVType::setIsTypedef);
132
133 // Create the logical scopes.
134 NestedScope =
135 create<LVScope, LVScopeSetFunction>(&LVScope::setIsLexicalBlock);
136 InnerScope = create<LVScope, LVScopeSetFunction>(&LVScope::setIsLexicalBlock);
137 Aggregate =
138 create<LVScopeAggregate, LVScopeSetFunction>(&LVScope::setIsAggregate);
139 CompileUnit = create<LVScopeCompileUnit, LVScopeSetFunction>(
140 &LVScope::setIsCompileUnit);
141 Enumeration = create<LVScopeEnumeration, LVScopeSetFunction>(
142 &LVScope::setIsEnumeration);
143 FunctionOne =
144 create<LVScopeFunction, LVScopeSetFunction>(&LVScope::setIsFunction);
145 FunctionTwo =
146 create<LVScopeFunction, LVScopeSetFunction>(&LVScope::setIsFunction);
147 Namespace =
148 create<LVScopeNamespace, LVScopeSetFunction>(&LVScope::setIsNamespace);
149
150 // Create the logical symbols.
151 GlobalVariable =
152 create<LVSymbol, LVSymbolSetFunction>(&LVSymbol::setIsVariable);
153 LocalVariable =
154 create<LVSymbol, LVSymbolSetFunction>(&LVSymbol::setIsVariable);
155 ClassMember = create<LVSymbol, LVSymbolSetFunction>(&LVSymbol::setIsMember);
156 NestedVariable =
157 create<LVSymbol, LVSymbolSetFunction>(&LVSymbol::setIsVariable);
158 ParameterOne =
159 create<LVSymbol, LVSymbolSetFunction>(&LVSymbol::setIsParameter);
160 ParameterTwo =
161 create<LVSymbol, LVSymbolSetFunction>(&LVSymbol::setIsParameter);
162
163 // Create the logical lines.
164 LineOne = create<LVLine, LVLineSetFunction>(&LVLine::setIsLineDebug);
165 LineTwo = create<LVLine, LVLineSetFunction>(&LVLine::setIsLineDebug);
166 LineThree = create<LVLine, LVLineSetFunction>(&LVLine::setIsLineDebug);
167 }
168
169 // Reference Reader: Target Reader:
170 // ---------------------- ----------------------
171 // Root Root
172 // CompileUnit CompileUnit
173 // IntegerType IntegerType
174 // UnsignedType UnsignedType
175 // FunctionOne FunctionOne
176 // ParameterOne ParameterOne
177 // LocalVariable ---
178 // LocalType LocalType
179 // LineOne LineOne
180 // NestedScope NestedScope
181 // NestedVariable NestedVariable
182 // NestedType NestedType
183 // LineTwo ---
184 // InnerScope InnerScope
185 // --- LineThree
186 // --- FunctionTwo
187 // --- ParameterTwo
188 // GlobalVariable GlobalVariable
189 // GlobalType GlobalType
190 // Namespace Namespace
191 // Aggregate Aggregate
192 // ClassMember ClassMember
193 // Enumeration Enumeration
194 // EnumeratorOne EnumeratorOne
195 // EnumeratorTwo EnumeratorTwo
196 // TypeDefinitionOne ---
197 // --- TypeDefinitionTwo
198
199 // Create the logical view adding the created logical elements.
200 void ReaderTestCompare::addElements(bool IsReference, bool IsTarget) {
201 Root->setName(IsReference ? "Reference-Reader" : "Target-Reader");
202
203 auto Insert = [&](bool Insert, auto *Parent, auto *Child) {
204 if (Insert)
205 add(Parent, Child);
206 else
207 OrphanElements.push_back(Child);
208 };
209
210 setCompileUnit(CompileUnit);
211 add(Root, CompileUnit);
212
213 // Add elements to CompileUnit.
214 Insert(true, CompileUnit, IntegerType);
215 Insert(true, CompileUnit, UnsignedType);
216 Insert(true, CompileUnit, FunctionOne);
217 Insert(IsTarget, CompileUnit, FunctionTwo);
218 Insert(true, CompileUnit, GlobalVariable);
219 Insert(true, CompileUnit, GlobalType);
220 Insert(true, CompileUnit, Namespace);
221
222 // Add elements to Namespace.
223 Insert(true, Namespace, Aggregate);
224 Insert(true, Namespace, Enumeration);
225 Insert(IsReference, Namespace, TypeDefinitionOne);
226 Insert(IsTarget, Namespace, TypeDefinitionTwo);
227
228 // Add elements to FunctionOne.
229 Insert(true, FunctionOne, ParameterOne);
230 Insert(IsReference, FunctionOne, LocalVariable);
231 Insert(true, FunctionOne, LocalType);
232 Insert(true, FunctionOne, LineOne);
233 Insert(true, FunctionOne, NestedScope);
234
235 // Add elements to FunctionTwo.
236 Insert(IsTarget, FunctionTwo, ParameterTwo);
237
238 // Add elements to NestedScope.
239 Insert(true, NestedScope, NestedVariable);
240 Insert(true, NestedScope, NestedType);
241 Insert(IsReference, NestedScope, LineTwo);
242 Insert(true, NestedScope, InnerScope);
243
244 // Add elements to Enumeration.
245 Insert(true, Enumeration, EnumeratorOne);
246 Insert(true, Enumeration, EnumeratorTwo);
247
248 // Add elements to Aggregate.
249 Insert(true, Aggregate, ClassMember);
250
251 Insert(IsTarget, InnerScope, LineThree);
252 }
253
254 // Set initial values to logical elements.
255 void ReaderTestCompare::initElements() {
256 setFilename("LogicalElements.obj");
257
258 Root->setFileFormatName("FileFormat");
259
260 // Types.
261 set(IntegerType, "int", 0x1000);
262 set(UnsignedType, "unsigned", 0x1010);
263 set(GlobalType, "GlobalType", 0x1020, 1020);
264 set(LocalType, "LocalType", 0x1030, 1030);
265 set(NestedType, "NestedType", 0x1040, 1040);
266
267 set(TypeDefinitionOne, "INTEGER", 0x1050, 1050, IntegerType);
268 set(TypeDefinitionTwo, "INT", 0x1060, 1060, TypeDefinitionOne);
269
270 set(EnumeratorOne, "One", 0x1070, 1070);
271 EnumeratorOne->setValue("Blue");
272
273 set(EnumeratorTwo, "Two", 0x1080, 1080);
274 EnumeratorTwo->setValue("Red");
275
276 // Scopes.
277 set(Aggregate, "Class", 0x2000, 2000);
278 set(Enumeration, "Colors", 0x2010, 2010);
279 set(FunctionOne, "FunctionOne", 0x2020, 2020, GlobalType);
280 set(FunctionTwo, "FunctionTwo", 0x2030, 2030, GlobalType);
281 set(Namespace, "Namespace", 0x2040, 2040);
282 set(NestedScope, "", 0x2050, 2050);
283 set(InnerScope, "", 0x2060, 2060);
284 set(CompileUnit, "test.cpp", 0x2070, 2070);
285
286 // Symbols.
287 set(GlobalVariable, "GlobalVariable", 0x3000, 3000);
288 set(LocalVariable, "LocalVariable", 0x3010, 3010, UnsignedType);
289 set(ClassMember, "ClassMember", 0x3020, 3020, IntegerType);
290 set(ParameterOne, "ParameterOne", 0x3030, 3030, UnsignedType);
291 set(ParameterTwo, "ParameterTwo", 0x3040, 3040, UnsignedType);
292 set(NestedVariable, "NestedVariable", 0x3050, 3050);
293
294 // Lines.
295 set(LineOne, "", 0x4000, 4000);
296 set(LineTwo, "", 0x4010, 4010);
297 set(LineThree, "", 0x4020, 4020);
298 }
299
300 // Compare the logical views.
301 void compareReadersViews(ReaderTestCompare *ReferenceReader,
302 ReaderTestCompare *TargetReader) {
303 LVCompare Compare(nulls());
304 Error Err = Compare.execute(ReferenceReader, TargetReader);
305 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
306
307 // Get comparison table.
308 LVPassTable PassTable = Compare.getPassTable();
309 ASSERT_EQ(PassTable.size(), 5u);
310
311 LVReader *Reader;
312 LVElement *Element;
313 LVComparePass Pass;
314
315 // Reference: Missing 'FunctionOne'
316 std::tie(Reader, Element, Pass) = PassTable[0];
317 EXPECT_EQ(Reader, ReferenceReader);
318 EXPECT_EQ(Element, ReferenceReader->FunctionOne);
319 EXPECT_EQ(Pass, LVComparePass::Missing);
320
321 // Reference: Missing 'TypeDefinitionOne'
322 std::tie(Reader, Element, Pass) = PassTable[1];
323 EXPECT_EQ(Reader, ReferenceReader);
324 EXPECT_EQ(Element, ReferenceReader->TypeDefinitionOne);
325 EXPECT_EQ(Pass, LVComparePass::Missing);
326
327 // Target: Added 'FunctionOne'
328 std::tie(Reader, Element, Pass) = PassTable[2];
329 EXPECT_EQ(Reader, TargetReader);
330 EXPECT_EQ(Element, TargetReader->FunctionOne);
331 EXPECT_EQ(Pass, LVComparePass::Added);
332
333 // Target: Added 'FunctionTwo'
334 std::tie(Reader, Element, Pass) = PassTable[3];
335 EXPECT_EQ(Reader, TargetReader);
336 EXPECT_EQ(Element, TargetReader->FunctionTwo);
337 EXPECT_EQ(Pass, LVComparePass::Added);
338
339 // Target: Added 'TypeDefinitionTwo'
340 std::tie(Reader, Element, Pass) = PassTable[4];
341 EXPECT_EQ(Reader, TargetReader);
342 EXPECT_EQ(Element, TargetReader->TypeDefinitionTwo);
343 EXPECT_EQ(Pass, LVComparePass::Added);
344 }
345
346 // Compare the logical elements.
347 void compareReadersElements(ReaderTestCompare *ReferenceReader,
348 ReaderTestCompare *TargetReader) {
349 LVCompare Compare(nulls());
350 Error Err = Compare.execute(ReferenceReader, TargetReader);
351 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
352
353 // Get comparison table.
354 LVPassTable PassTable = Compare.getPassTable();
355 ASSERT_EQ(PassTable.size(), 7u);
356
357 LVReader *Reader;
358 LVElement *Element;
359 LVComparePass Pass;
360
361 // Reference: Missing 'LocalVariable'
362 std::tie(Reader, Element, Pass) = PassTable[0];
363 EXPECT_EQ(Reader, ReferenceReader);
364 EXPECT_EQ(Element, ReferenceReader->LocalVariable);
365 EXPECT_EQ(Pass, LVComparePass::Missing);
366
367 // Reference: Missing 'TypeDefinitionOne'
368 std::tie(Reader, Element, Pass) = PassTable[1];
369 EXPECT_EQ(Reader, ReferenceReader);
370 EXPECT_EQ(Element, ReferenceReader->TypeDefinitionOne);
371 EXPECT_EQ(Pass, LVComparePass::Missing);
372
373 // Reference: Missing 'LineTwo'
374 std::tie(Reader, Element, Pass) = PassTable[2];
375 EXPECT_EQ(Reader, ReferenceReader);
376 EXPECT_EQ(Element, ReferenceReader->LineTwo);
377 EXPECT_EQ(Pass, LVComparePass::Missing);
378
379 // Target: Added 'FunctionTwo'
380 std::tie(Reader, Element, Pass) = PassTable[3];
381 EXPECT_EQ(Reader, TargetReader);
382 EXPECT_EQ(Element, TargetReader->FunctionTwo);
383 EXPECT_EQ(Pass, LVComparePass::Added);
384
385 // Target: Added 'ParameterTwo'
386 std::tie(Reader, Element, Pass) = PassTable[4];
387 EXPECT_EQ(Reader, TargetReader);
388 EXPECT_EQ(Element, TargetReader->ParameterTwo);
389 EXPECT_EQ(Pass, LVComparePass::Added);
390
391 // Target: Added 'TypeDefinitionTwo'
392 std::tie(Reader, Element, Pass) = PassTable[5];
393 EXPECT_EQ(Reader, TargetReader);
394 EXPECT_EQ(Element, TargetReader->TypeDefinitionTwo);
395 EXPECT_EQ(Pass, LVComparePass::Added);
396
397 // Target: Added 'LineThree'
398 std::tie(Reader, Element, Pass) = PassTable[6];
399 EXPECT_EQ(Reader, TargetReader);
400 EXPECT_EQ(Element, TargetReader->LineThree);
401 EXPECT_EQ(Pass, LVComparePass::Added);
402 }
403
404 TEST(LogicalViewTest, CompareElements) {
405 ScopedPrinter W(outs());
406
407 // Reader options.
408 LVOptions ReaderOptions;
409 ReaderOptions.setCompareLines();
410 ReaderOptions.setCompareScopes();
411 ReaderOptions.setCompareSymbols();
412 ReaderOptions.setCompareTypes();
413
414 // The next set-ups are very similar. The only difference is the
415 // comparison type, which must be set before the readers are created.
416 // Views: setCompareContext()
417 // Elements: resetCompareContext()
418 {
419 // Compare the logical views as whole unit (--compare-context).
420 ReaderOptions.setCompareContext();
421 ReaderOptions.resolveDependencies();
422 options().setOptions(&ReaderOptions);
423
424 ReaderTestCompare ReferenceReader(W);
425 ReferenceReader.createElements();
426 ReferenceReader.addElements(/*IsReference=*/true, /*IsTarget=*/false);
427 ReferenceReader.initElements();
428
429 ReaderTestCompare TargetReader(W);
430 TargetReader.createElements();
431 TargetReader.addElements(/*IsReference=*/false, /*IsTarget=*/true);
432 TargetReader.initElements();
433
434 compareReadersViews(&ReferenceReader, &TargetReader);
435 }
436 {
437 // Compare the logical elements.
438 ReaderOptions.resetCompareContext();
439 ReaderOptions.resolveDependencies();
440 options().setOptions(&ReaderOptions);
441
442 ReaderTestCompare ReferenceReader(W);
443 ReferenceReader.createElements();
444 ReferenceReader.addElements(/*IsReference=*/true, /*IsTarget=*/false);
445 ReferenceReader.initElements();
446
447 ReaderTestCompare TargetReader(W);
448 TargetReader.createElements();
449 TargetReader.addElements(/*IsReference=*/false, /*IsTarget=*/true);
450 TargetReader.initElements();
451
452 compareReadersElements(&ReferenceReader, &TargetReader);
453 }
454 }
455
456 } // namespace