Skip to content

Commit 2bf01d7

Browse files
authored
[lldb][DWARFASTParserClang] GetClangDeclForDIE: don't create VarDecl for static data members (#77155)
With DWARFv5, C++ static data members are represented as `DW_TAG_variable`s (see `faa3a5ea9ae481da757dab1c95c589e2d5645982`). In GetClangDeclForDIE, when trying to parse the `DW_AT_specification` that a static data member's CU-level `DW_TAG_variable` points to, we would try to `CreateVariableDeclaration`. Whereas previously it was a no-op (for `DW_TAG_member`s). However, adding `VarDecls` to RecordDecls for static data members should always be done in `CreateStaticMemberVariable`. The test-case is an exapmle where we would crash if we tried to create a `VarDecl` from within `GetClangDeclForDIE` for a static data member. This patch simply checks whether the `DW_TAG_variable` being parsed is a static data member, and if so, trivially returns from `GetClangDeclForDIE` (as we previously did for `DW_TAG_member`s).
1 parent 7ca4473 commit 2bf01d7

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ static bool ShouldIgnoreArtificialField(llvm::StringRef FieldName) {
142142
|| FieldName.starts_with("_vptr.");
143143
}
144144

145+
/// Returns true for C++ constructs represented by clang::CXXRecordDecl
146+
static bool TagIsRecordType(dw_tag_t tag) {
147+
switch (tag) {
148+
case DW_TAG_class_type:
149+
case DW_TAG_structure_type:
150+
case DW_TAG_union_type:
151+
return true;
152+
default:
153+
return false;
154+
}
155+
}
156+
145157
TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc,
146158
const DWARFDIE &die,
147159
Log *log) {
@@ -3304,12 +3316,19 @@ clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) {
33043316
return nullptr;
33053317

33063318
switch (die.Tag()) {
3307-
case DW_TAG_variable:
33083319
case DW_TAG_constant:
33093320
case DW_TAG_formal_parameter:
33103321
case DW_TAG_imported_declaration:
33113322
case DW_TAG_imported_module:
33123323
break;
3324+
case DW_TAG_variable:
3325+
// This means 'die' is a C++ static data member.
3326+
// We don't want to create decls for such members
3327+
// here.
3328+
if (auto parent = die.GetParent();
3329+
parent.IsValid() && TagIsRecordType(parent.Tag()))
3330+
return nullptr;
3331+
break;
33133332
default:
33143333
return nullptr;
33153334
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct NoCtor {
2+
NoCtor();
3+
static int i;
4+
};
5+
6+
int NoCtor::i = 15;
7+
8+
int main() { return NoCtor::i; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# In DWARFv5, C++ static data members are represented
2+
# as DW_TAG_variable. We make sure LLDB's expression
3+
# evaluator doesn't crash when trying to parse such
4+
# a DW_TAG_variable DIE, whose parent DIE is only
5+
# a forward declaration.
6+
7+
# RUN: %clangxx_host %S/Inputs/dwo-static-data-member.cpp \
8+
# RUN: -g -gdwarf-5 -gsplit-dwarf -flimit-debug-info -o %t
9+
# RUN: %lldb %t -s %s -o exit 2>&1 | FileCheck %s
10+
11+
breakpoint set -n main
12+
process launch
13+
14+
# CHECK: Process {{.*}} stopped
15+
16+
# FIXME: The expression evaluator tries to attach
17+
# the static member's VarDecl to the NoCtor RecordDecl
18+
# before passing the AST to clang; this requires the
19+
# RecordDecl to be a full definition. But the debug-info
20+
# only contains forward declaration for NoCtor. So
21+
# LLDB fails to evaluate the expression.
22+
expression NoCtor::i
23+
# CHECK-LABEL: expression NoCtor::i
24+
# CHECK: use of undeclared identifier 'NoCtor'

0 commit comments

Comments
 (0)