Debian Codegraph @

llvm-project/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp

raw
1 //===-- DWARFASTParserClangTests.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 "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
10 #include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
11 #include "Plugins/SymbolFile/DWARF/DWARFDIE.h"
12 #include "TestingSupport/Symbol/ClangTestUtils.h"
13 #include "TestingSupport/Symbol/YAMLModuleTester.h"
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16
17 using namespace lldb;
18 using namespace lldb_private;
19 using namespace lldb_private::dwarf;
20
21 namespace {
22 class DWARFASTParserClangTests : public testing::Test {};
23
24 class DWARFASTParserClangStub : public DWARFASTParserClang {
25 public:
26 using DWARFASTParserClang::DWARFASTParserClang;
27 using DWARFASTParserClang::LinkDeclContextToDIE;
28
29 std::vector<const clang::DeclContext *> GetDeclContextToDIEMapKeys() {
30 std::vector<const clang::DeclContext *> keys;
31 for (const auto &it : m_decl_ctx_to_die)
32 keys.push_back(it.first);
33 return keys;
34 }
35 };
36 } // namespace
37
38 // If your implementation needs to dereference the dummy pointers we are
39 // defining here, causing this test to fail, feel free to delete it.
40 TEST_F(DWARFASTParserClangTests,
41 EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) {
42
43 /// Auxiliary debug info.
44 const char *yamldata = R"(
45 --- !ELF
46 FileHeader:
47 Class: ELFCLASS64
48 Data: ELFDATA2LSB
49 Type: ET_EXEC
50 Machine: EM_386
51 DWARF:
52 debug_abbrev:
53 - Table:
54 - Code: 0x00000001
55 Tag: DW_TAG_compile_unit
56 Children: DW_CHILDREN_yes
57 Attributes:
58 - Attribute: DW_AT_language
59 Form: DW_FORM_data2
60 - Code: 0x00000002
61 Tag: DW_TAG_base_type
62 Children: DW_CHILDREN_no
63 Attributes:
64 - Attribute: DW_AT_encoding
65 Form: DW_FORM_data1
66 - Attribute: DW_AT_byte_size
67 Form: DW_FORM_data1
68 debug_info:
69 - Version: 4
70 AddrSize: 8
71 Entries:
72 - AbbrCode: 0x00000001
73 Values:
74 - Value: 0x000000000000000C
75 - AbbrCode: 0x00000002
76 Values:
77 - Value: 0x0000000000000007 # DW_ATE_unsigned
78 - Value: 0x0000000000000004
79 - AbbrCode: 0x00000002
80 Values:
81 - Value: 0x0000000000000007 # DW_ATE_unsigned
82 - Value: 0x0000000000000008
83 - AbbrCode: 0x00000002
84 Values:
85 - Value: 0x0000000000000005 # DW_ATE_signed
86 - Value: 0x0000000000000008
87 - AbbrCode: 0x00000002
88 Values:
89 - Value: 0x0000000000000008 # DW_ATE_unsigned_char
90 - Value: 0x0000000000000001
91 - AbbrCode: 0x00000000
92 )";
93
94 YAMLModuleTester t(yamldata);
95 ASSERT_TRUE((bool)t.GetDwarfUnit());
96
97 auto holder = std::make_unique<clang_utils::TypeSystemClangHolder>("ast");
98 auto &ast_ctx = *holder->GetAST();
99
100 DWARFASTParserClangStub ast_parser(ast_ctx);
101
102 DWARFUnit *unit = t.GetDwarfUnit();
103 const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
104 const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild();
105 const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling();
106 const DWARFDebugInfoEntry *die_child2 = die_child1->GetSibling();
107 const DWARFDebugInfoEntry *die_child3 = die_child2->GetSibling();
108 std::vector<DWARFDIE> dies = {
109 DWARFDIE(unit, die_child0), DWARFDIE(unit, die_child1),
110 DWARFDIE(unit, die_child2), DWARFDIE(unit, die_child3)};
111 std::vector<clang::DeclContext *> decl_ctxs = {
112 (clang::DeclContext *)1LL, (clang::DeclContext *)2LL,
113 (clang::DeclContext *)2LL, (clang::DeclContext *)3LL};
114 for (int i = 0; i < 4; ++i)
115 ast_parser.LinkDeclContextToDIE(decl_ctxs[i], dies[i]);
116 ast_parser.EnsureAllDIEsInDeclContextHaveBeenParsed(
117 CompilerDeclContext(nullptr, decl_ctxs[1]));
118
119 EXPECT_THAT(ast_parser.GetDeclContextToDIEMapKeys(),
120 testing::UnorderedElementsAre(decl_ctxs[0], decl_ctxs[3]));
121 }
122
123 TEST_F(DWARFASTParserClangTests, TestCallingConventionParsing) {
124 // Tests parsing DW_AT_calling_convention values.
125
126 // The DWARF below just declares a list of function types with
127 // DW_AT_calling_convention on them.
128 const char *yamldata = R"(
129 --- !ELF
130 FileHeader:
131 Class: ELFCLASS32
132 Data: ELFDATA2LSB
133 Type: ET_EXEC
134 Machine: EM_386
135 DWARF:
136 debug_str:
137 - func1
138 - func2
139 - func3
140 - func4
141 - func5
142 - func6
143 - func7
144 - func8
145 - func9
146 debug_abbrev:
147 - ID: 0
148 Table:
149 - Code: 0x1
150 Tag: DW_TAG_compile_unit
151 Children: DW_CHILDREN_yes
152 Attributes:
153 - Attribute: DW_AT_language
154 Form: DW_FORM_data2
155 - Code: 0x2
156 Tag: DW_TAG_subprogram
157 Children: DW_CHILDREN_no
158 Attributes:
159 - Attribute: DW_AT_low_pc
160 Form: DW_FORM_addr
161 - Attribute: DW_AT_high_pc
162 Form: DW_FORM_data4
163 - Attribute: DW_AT_name
164 Form: DW_FORM_strp
165 - Attribute: DW_AT_calling_convention
166 Form: DW_FORM_data1
167 - Attribute: DW_AT_external
168 Form: DW_FORM_flag_present
169 debug_info:
170 - Version: 4
171 AddrSize: 4
172 Entries:
173 - AbbrCode: 0x1
174 Values:
175 - Value: 0xC
176 - AbbrCode: 0x2
177 Values:
178 - Value: 0x0
179 - Value: 0x5
180 - Value: 0x00
181 - Value: 0xCB
182 - Value: 0x1
183 - AbbrCode: 0x2
184 Values:
185 - Value: 0x10
186 - Value: 0x5
187 - Value: 0x06
188 - Value: 0xB3
189 - Value: 0x1
190 - AbbrCode: 0x2
191 Values:
192 - Value: 0x20
193 - Value: 0x5
194 - Value: 0x0C
195 - Value: 0xB1
196 - Value: 0x1
197 - AbbrCode: 0x2
198 Values:
199 - Value: 0x30
200 - Value: 0x5
201 - Value: 0x12
202 - Value: 0xC0
203 - Value: 0x1
204 - AbbrCode: 0x2
205 Values:
206 - Value: 0x40
207 - Value: 0x5
208 - Value: 0x18
209 - Value: 0xB2
210 - Value: 0x1
211 - AbbrCode: 0x2
212 Values:
213 - Value: 0x50
214 - Value: 0x5
215 - Value: 0x1E
216 - Value: 0xC1
217 - Value: 0x1
218 - AbbrCode: 0x2
219 Values:
220 - Value: 0x60
221 - Value: 0x5
222 - Value: 0x24
223 - Value: 0xC2
224 - Value: 0x1
225 - AbbrCode: 0x2
226 Values:
227 - Value: 0x70
228 - Value: 0x5
229 - Value: 0x2a
230 - Value: 0xEE
231 - Value: 0x1
232 - AbbrCode: 0x2
233 Values:
234 - Value: 0x80
235 - Value: 0x5
236 - Value: 0x30
237 - Value: 0x01
238 - Value: 0x1
239 - AbbrCode: 0x0
240 ...
241 )";
242 YAMLModuleTester t(yamldata);
243
244 DWARFUnit *unit = t.GetDwarfUnit();
245 ASSERT_NE(unit, nullptr);
246 const DWARFDebugInfoEntry *cu_entry = unit->DIE().GetDIE();
247 ASSERT_EQ(cu_entry->Tag(), DW_TAG_compile_unit);
248 DWARFDIE cu_die(unit, cu_entry);
249
250 auto holder = std::make_unique<clang_utils::TypeSystemClangHolder>("ast");
251 auto &ast_ctx = *holder->GetAST();
252 DWARFASTParserClangStub ast_parser(ast_ctx);
253
254 std::vector<std::string> found_function_types;
255 // The DWARF above is just a list of functions. Parse all of them to
256 // extract the function types and their calling convention values.
257 for (DWARFDIE func : cu_die.children()) {
258 ASSERT_EQ(func.Tag(), DW_TAG_subprogram);
259 SymbolContext sc;
260 bool new_type = false;
261 lldb::TypeSP type = ast_parser.ParseTypeFromDWARF(sc, func, &new_type);
262 found_function_types.push_back(
263 type->GetForwardCompilerType().GetTypeName().AsCString());
264 }
265
266 // Compare the parsed function types against the expected list of types.
267 const std::vector<std::string> expected_function_types = {
268 "void () __attribute__((regcall))",
269 "void () __attribute__((fastcall))",
270 "void () __attribute__((stdcall))",
271 "void () __attribute__((vectorcall))",
272 "void () __attribute__((pascal))",
273 "void () __attribute__((ms_abi))",
274 "void () __attribute__((sysv_abi))",
275 "void ()", // invalid calling convention.
276 "void ()", // DW_CC_normal -> no attribute
277 };
278 ASSERT_EQ(found_function_types, expected_function_types);
279 }
280
281 struct ExtractIntFromFormValueTest : public testing::Test {
282 SubsystemRAII<FileSystem, HostInfo> subsystems;
283 clang_utils::TypeSystemClangHolder holder;
284 TypeSystemClang &ts;
285
286 DWARFASTParserClang parser;
287 ExtractIntFromFormValueTest()
288 : holder("dummy ASTContext"), ts(*holder.GetAST()), parser(ts) {}
289
290 /// Takes the given integer value, stores it in a DWARFFormValue and then
291 /// tries to extract the value back via
292 /// DWARFASTParserClang::ExtractIntFromFormValue.
293 /// Returns the string representation of the extracted value or the error
294 /// that was returned from ExtractIntFromFormValue.
295 llvm::Expected<std::string> Extract(clang::QualType qt, uint64_t value) {
296 DWARFFormValue form_value;
297 form_value.SetUnsigned(value);
298 llvm::Expected<llvm::APInt> result =
299 parser.ExtractIntFromFormValue(ts.GetType(qt), form_value);
300 if (!result)
301 return result.takeError();
302 llvm::SmallString<16> result_str;
303 result->toStringUnsigned(result_str);
304 return std::string(result_str.str());
305 }
306
307 /// Same as ExtractIntFromFormValueTest::Extract but takes a signed integer
308 /// and treats the result as a signed integer.
309 llvm::Expected<std::string> ExtractS(clang::QualType qt, int64_t value) {
310 DWARFFormValue form_value;
311 form_value.SetSigned(value);
312 llvm::Expected<llvm::APInt> result =
313 parser.ExtractIntFromFormValue(ts.GetType(qt), form_value);
314 if (!result)
315 return result.takeError();
316 llvm::SmallString<16> result_str;
317 result->toStringSigned(result_str);
318 return std::string(result_str.str());
319 }
320 };
321
322 TEST_F(ExtractIntFromFormValueTest, TestBool) {
323 using namespace llvm;
324 clang::ASTContext &ast = ts.getASTContext();
325
326 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 0), HasValue("0"));
327 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 1), HasValue("1"));
328 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 2), Failed());
329 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 3), Failed());
330 }
331
332 TEST_F(ExtractIntFromFormValueTest, TestInt) {
333 using namespace llvm;
334
335 clang::ASTContext &ast = ts.getASTContext();
336
337 // Find the min/max values for 'int' on the current host target.
338 constexpr int64_t int_max = std::numeric_limits<int>::max();
339 constexpr int64_t int_min = std::numeric_limits<int>::min();
340
341 // Check that the bit width of int matches the int width in our type system.
342 ASSERT_EQ(sizeof(int) * 8, ast.getIntWidth(ast.IntTy));
343
344 // Check values around int_min.
345 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min - 2), llvm::Failed());
346 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min - 1), llvm::Failed());
347 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min),
348 HasValue(std::to_string(int_min)));
349 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min + 1),
350 HasValue(std::to_string(int_min + 1)));
351 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min + 2),
352 HasValue(std::to_string(int_min + 2)));
353
354 // Check values around 0.
355 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, -128), HasValue("-128"));
356 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, -10), HasValue("-10"));
357 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, -1), HasValue("-1"));
358 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 0), HasValue("0"));
359 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 1), HasValue("1"));
360 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 10), HasValue("10"));
361 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 128), HasValue("128"));
362
363 // Check values around int_max.
364 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max - 2),
365 HasValue(std::to_string(int_max - 2)));
366 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max - 1),
367 HasValue(std::to_string(int_max - 1)));
368 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max),
369 HasValue(std::to_string(int_max)));
370 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max + 1), llvm::Failed());
371 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max + 5), llvm::Failed());
372
373 // Check some values not near an edge case.
374 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max / 2),
375 HasValue(std::to_string(int_max / 2)));
376 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min / 2),
377 HasValue(std::to_string(int_min / 2)));
378 }
379
380 TEST_F(ExtractIntFromFormValueTest, TestUnsignedInt) {
381 using namespace llvm;
382
383 clang::ASTContext &ast = ts.getASTContext();
384 constexpr uint64_t uint_max = std::numeric_limits<uint32_t>::max();
385
386 // Check values around 0.
387 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, 0), HasValue("0"));
388 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, 1), HasValue("1"));
389 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, 1234), HasValue("1234"));
390
391 // Check some values not near an edge case.
392 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max / 2),
393 HasValue(std::to_string(uint_max / 2)));
394
395 // Check values around uint_max.
396 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max - 2),
397 HasValue(std::to_string(uint_max - 2)));
398 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max - 1),
399 HasValue(std::to_string(uint_max - 1)));
400 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max),
401 HasValue(std::to_string(uint_max)));
402 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max + 1),
403 llvm::Failed());
404 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max + 2),
405 llvm::Failed());
406 }