Debian Codegraph @

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

raw
1 //===- llvm/unittest/DebugInfo/LogicalView/LogicalElementsTest.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 ReaderTestElements : public LVReader {
26 // Types.
27 LVType *IntegerType = nullptr;
28 LVType *UnsignedType = nullptr;
29 LVType *GlobalType = nullptr;
30 LVType *LocalType = nullptr;
31 LVType *NestedType = nullptr;
32 LVTypeDefinition *TypeDefinitionOne = nullptr;
33 LVTypeDefinition *TypeDefinitionTwo = nullptr;
34 LVTypeEnumerator *EnumeratorOne = nullptr;
35 LVTypeEnumerator *EnumeratorTwo = nullptr;
36 LVTypeImport *TypeImport = nullptr;
37 LVTypeParam *TypeParam = nullptr;
38 LVTypeSubrange *TypeSubrange = nullptr;
39
40 // Scopes.
41 LVScope *NestedScope = nullptr;
42 LVScopeAggregate *Aggregate = nullptr;
43 LVScopeArray *Array = nullptr;
44 LVScopeEnumeration *Enumeration = nullptr;
45 LVScopeFunction *Function = nullptr;
46 LVScopeFunction *ClassFunction = nullptr;
47 LVScopeFunctionInlined *InlinedFunction = nullptr;
48 LVScopeNamespace *Namespace = nullptr;
49
50 // Symbols.
51 LVSymbol *GlobalVariable = nullptr;
52 LVSymbol *LocalVariable = nullptr;
53 LVSymbol *ClassMember = nullptr;
54 LVSymbol *NestedVariable = nullptr;
55 LVSymbol *Parameter = nullptr;
56
57 // Lines.
58 LVLine *LocalLine = nullptr;
59 LVLine *NestedLine = nullptr;
60
61 protected:
62 void add(LVScope *Parent, LVElement *Element);
63 template <typename T> T *create() {
64 T *Element = new (std::nothrow) T();
65 EXPECT_NE(Element, nullptr);
66 return Element;
67 }
68 void set(LVElement *Element, StringRef Name, LVOffset Offset,
69 uint32_t LineNumber = 0, LVElement *Type = nullptr);
70
71 public:
72 ReaderTestElements(ScopedPrinter &W) : LVReader("", "", W) {
73 setInstance(this);
74 }
75
76 Error createScopes() { return LVReader::createScopes(); }
77 Error printScopes() { return LVReader::printScopes(); }
78
79 void createElements();
80 void addElements();
81 void initElements();
82 };
83
84 // Helper function to add a logical element to a given scope.
85 void ReaderTestElements::add(LVScope *Parent, LVElement *Child) {
86 Parent->addElement(Child);
87 EXPECT_EQ(Child->getParent(), Parent);
88 EXPECT_EQ(Child->getLevel(), Parent->getLevel() + 1);
89 }
90
91 // Helper function to set the initial values for a given logical element.
92 void ReaderTestElements::set(LVElement *Element, StringRef Name,
93 LVOffset Offset, uint32_t LineNumber,
94 LVElement *Type) {
95 Element->setName(Name);
96 Element->setOffset(Offset);
97 Element->setLineNumber(LineNumber);
98 Element->setType(Type);
99 EXPECT_EQ(Element->getName(), Name);
100 EXPECT_EQ(Element->getOffset(), Offset);
101 EXPECT_EQ(Element->getLineNumber(), LineNumber);
102 EXPECT_EQ(Element->getType(), Type);
103 }
104
105 // Create the logical elements.
106 void ReaderTestElements::createElements() {
107 // Create scope root.
108 Error Err = createScopes();
109 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
110 Root = getScopesRoot();
111 ASSERT_NE(Root, nullptr);
112
113 // Create the logical types.
114 IntegerType = create<LVType>();
115 UnsignedType = create<LVType>();
116 GlobalType = create<LVType>();
117 LocalType = create<LVType>();
118 NestedType = create<LVType>();
119 EnumeratorOne = create<LVTypeEnumerator>();
120 EnumeratorTwo = create<LVTypeEnumerator>();
121 TypeDefinitionOne = create<LVTypeDefinition>();
122 TypeDefinitionTwo = create<LVTypeDefinition>();
123 TypeSubrange = create<LVTypeSubrange>();
124 TypeParam = create<LVTypeParam>();
125 TypeImport = create<LVTypeImport>();
126
127 // Create the logical scopes.
128 NestedScope = create<LVScope>();
129 Aggregate = create<LVScopeAggregate>();
130 Array = create<LVScopeArray>();
131 CompileUnit = create<LVScopeCompileUnit>();
132 Enumeration = create<LVScopeEnumeration>();
133 Function = create<LVScopeFunction>();
134 ClassFunction = create<LVScopeFunction>();
135 InlinedFunction = create<LVScopeFunctionInlined>();
136 Namespace = create<LVScopeNamespace>();
137
138 // Create the logical symbols.
139 GlobalVariable = create<LVSymbol>();
140 LocalVariable = create<LVSymbol>();
141 ClassMember = create<LVSymbol>();
142 NestedVariable = create<LVSymbol>();
143 Parameter = create<LVSymbol>();
144
145 // Create the logical lines.
146 LocalLine = create<LVLine>();
147 NestedLine = create<LVLine>();
148 }
149
150 // Create the logical view adding the created logical elements.
151 void ReaderTestElements::addElements() {
152 setCompileUnit(CompileUnit);
153
154 // Root
155 // CompileUnit
156 // IntegerType
157 // UnsignedType
158 // Array
159 // TypeSubrange
160 // Function
161 // Parameter
162 // LocalVariable
163 // LocalType
164 // LocalLine
165 // InlinedFunction
166 // TypeImport
167 // TypeParam
168 // NestedScope
169 // NestedVariable
170 // NestedType
171 // NestedLine
172 // GlobalVariable
173 // GlobalType
174 // Namespace
175 // Aggregate
176 // ClassMember
177 // ClassFunction
178 // Enumeration
179 // EnumeratorOne
180 // EnumeratorTwo
181 // TypeDefinitionOne
182 // TypeDefinitionTwo
183
184 add(Root, CompileUnit);
185 EXPECT_EQ(Root->lineCount(), 0u);
186 EXPECT_EQ(Root->scopeCount(), 1u);
187 EXPECT_EQ(Root->symbolCount(), 0u);
188 EXPECT_EQ(Root->typeCount(), 0u);
189
190 // Add elements to CompileUnit.
191 add(CompileUnit, IntegerType);
192 add(CompileUnit, UnsignedType);
193 add(CompileUnit, Array);
194 add(CompileUnit, Function);
195 add(CompileUnit, GlobalVariable);
196 add(CompileUnit, GlobalType);
197 add(CompileUnit, Namespace);
198 EXPECT_EQ(CompileUnit->lineCount(), 0u);
199 EXPECT_EQ(CompileUnit->scopeCount(), 3u);
200 EXPECT_EQ(CompileUnit->symbolCount(), 1u);
201 EXPECT_EQ(CompileUnit->typeCount(), 3u);
202
203 // Add elements to Namespace.
204 add(Namespace, Aggregate);
205 add(Namespace, Enumeration);
206 add(Namespace, TypeDefinitionOne);
207 add(Namespace, TypeDefinitionTwo);
208 EXPECT_EQ(Namespace->lineCount(), 0u);
209 EXPECT_EQ(Namespace->scopeCount(), 2u);
210 EXPECT_EQ(Namespace->symbolCount(), 0u);
211 EXPECT_EQ(Namespace->typeCount(), 2u);
212
213 // Add elements to Function.
214 add(Function, Parameter);
215 add(Function, LocalVariable);
216 add(Function, LocalType);
217 add(Function, LocalLine);
218 add(Function, InlinedFunction);
219 add(Function, TypeImport);
220 add(Function, TypeParam);
221 add(Function, NestedScope);
222 EXPECT_EQ(Function->lineCount(), 1u);
223 EXPECT_EQ(Function->scopeCount(), 2u);
224 EXPECT_EQ(Function->symbolCount(), 2u);
225 EXPECT_EQ(Function->typeCount(), 3u);
226
227 // Add elements to NestedScope.
228 add(NestedScope, NestedVariable);
229 add(NestedScope, NestedType);
230 add(NestedScope, NestedLine);
231 EXPECT_EQ(NestedScope->lineCount(), 1u);
232 EXPECT_EQ(NestedScope->scopeCount(), 0u);
233 EXPECT_EQ(NestedScope->symbolCount(), 1u);
234 EXPECT_EQ(NestedScope->typeCount(), 1u);
235
236 // Add elements to Enumeration.
237 add(Enumeration, EnumeratorOne);
238 add(Enumeration, EnumeratorTwo);
239 EXPECT_EQ(Enumeration->lineCount(), 0u);
240 EXPECT_EQ(Enumeration->scopeCount(), 0u);
241 EXPECT_EQ(Enumeration->symbolCount(), 0u);
242 EXPECT_EQ(Enumeration->typeCount(), 2u);
243
244 // Add elements to Aggregate.
245 add(Aggregate, ClassMember);
246 add(Aggregate, ClassFunction);
247 EXPECT_EQ(Aggregate->lineCount(), 0u);
248 EXPECT_EQ(Aggregate->scopeCount(), 1u);
249 EXPECT_EQ(Aggregate->symbolCount(), 1u);
250 EXPECT_EQ(Aggregate->typeCount(), 0u);
251
252 // Add elements to Array.
253 add(Array, TypeSubrange);
254 EXPECT_EQ(Array->lineCount(), 0u);
255 EXPECT_EQ(Array->scopeCount(), 0u);
256 EXPECT_EQ(Array->symbolCount(), 0u);
257 EXPECT_EQ(Array->typeCount(), 1u);
258 }
259
260 // Set initial values to logical elements.
261 void ReaderTestElements::initElements() {
262 setFilename("LogicalElements.obj");
263 EXPECT_EQ(getFilename(), "LogicalElements.obj");
264
265 Root->setFileFormatName("FileFormat");
266 EXPECT_EQ(Root->getFileFormatName(), "FileFormat");
267
268 // Types.
269 set(IntegerType, "int", 0x1000);
270 set(UnsignedType, "unsigned", 0x1010);
271 set(GlobalType, "GlobalType", 0x1020, 1020);
272 set(LocalType, "LocalType", 0x1030, 1030);
273 set(NestedType, "NestedType", 0x1040, 1040);
274
275 set(TypeDefinitionOne, "INTEGER", 0x1040, 1040, IntegerType);
276 set(TypeDefinitionTwo, "INT", 0x1050, 1050, TypeDefinitionOne);
277 EXPECT_EQ(TypeDefinitionOne->getUnderlyingType(), IntegerType);
278 EXPECT_EQ(TypeDefinitionTwo->getUnderlyingType(), IntegerType);
279
280 set(EnumeratorOne, "one", 0x1060, 1060);
281 EnumeratorOne->setValue("blue");
282 EXPECT_EQ(EnumeratorOne->getValue(), "blue");
283
284 set(EnumeratorTwo, "two", 0x1070, 1070);
285 EnumeratorTwo->setValue("red");
286 EXPECT_EQ(EnumeratorTwo->getValue(), "red");
287
288 set(TypeSubrange, "", 0x1080, 1080, IntegerType);
289 TypeSubrange->setCount(5);
290 EXPECT_EQ(TypeSubrange->getCount(), 5);
291
292 TypeSubrange->setLowerBound(10);
293 TypeSubrange->setUpperBound(15);
294 EXPECT_EQ(TypeSubrange->getLowerBound(), 10);
295 EXPECT_EQ(TypeSubrange->getUpperBound(), 15);
296
297 TypeSubrange->setBounds(20, 25);
298 std::pair<unsigned, unsigned> Pair;
299 Pair = TypeSubrange->getBounds();
300 EXPECT_EQ(Pair.first, 20u);
301 EXPECT_EQ(Pair.second, 25u);
302
303 set(TypeParam, "INTEGER", 0x1090, 1090, UnsignedType);
304 TypeParam->setValue("10");
305 EXPECT_EQ(TypeParam->getValue(), "10");
306
307 set(TypeImport, "", 0x1090, 1090, Aggregate);
308 EXPECT_EQ(TypeImport->getType(), Aggregate);
309
310 // Scopes.
311 set(Aggregate, "Class", 0x2000, 2000);
312 set(Enumeration, "Colors", 0x2010, 2010);
313 set(Function, "function", 0x2020, 2020, GlobalType);
314 set(ClassFunction, "foo", 0x2030, 2030, TypeDefinitionTwo);
315 set(Namespace, "nsp", 0x2040, 2040);
316 set(NestedScope, "", 0x2050, 2050);
317 set(Array, "", 0x2060, 2060, UnsignedType);
318 set(InlinedFunction, "bar", 0x2070, 2070, TypeDefinitionOne);
319 set(CompileUnit, "test.cpp", 0x2080, 2080);
320
321 // Symbols.
322 set(GlobalVariable, "GlobalVariable", 0x3000, 3000);
323 set(LocalVariable, "LocalVariable", 0x3010, 3010, TypeDefinitionOne);
324 set(ClassMember, "Member", 0x3020, 3020, IntegerType);
325 set(Parameter, "Param", 0x3030, 3030, UnsignedType);
326 set(NestedVariable, "NestedVariable", 0x3040, 3040);
327
328 // Lines.
329 set(LocalLine, "", 0x4000, 4000);
330 set(NestedLine, "", 0x4010, 4010);
331 }
332
333 TEST(LogicalViewTest, LogicalElements) {
334 ScopedPrinter W(outs());
335 ReaderTestElements Reader(W);
336
337 Reader.createElements();
338 Reader.addElements();
339 Reader.initElements();
340 }
341
342 } // namespace