Skip to content

Commit e403f4f

Browse files
committed
[clang][DebugInfo] Allow function-local statics and types to be scoped within a lexical block
This is almost a reincarnation of https://reviews.llvm.org/D15977 originally implemented by Amjad Aboud. It was discussed on llvm-dev [0], committed with its backend counterpart [1], but finally reverted [2]. This patch makes clang to emit debug info for function-local static variables, records (classes, structs and unions) and typdefs correctly scoped if those function-local entites defined within a lexical (bracketed) block. Before this patch, clang emits all those entities directly scoped in DISubprogram no matter where they were really defined, causing debug info loss (reported several times in [3], [4], [5]). [0] https://lists.llvm.org/pipermail/llvm-dev/2015-November/092551.html [1] https://reviews.llvm.org/rG30e7a8f694a19553f64b3a3a5de81ce317b9ec2f [2] https://reviews.llvm.org/rGdc4531e552af6c880a69d226d3666756198fbdc8 [3] https://bugs.llvm.org/show_bug.cgi?id=19238 [4] https://bugs.llvm.org/show_bug.cgi?id=23164 [5] https://bugs.llvm.org/show_bug.cgi?id=44695 Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D113743
1 parent e761c49 commit e403f4f

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
227227
return Default;
228228
}
229229

230+
void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) {
231+
assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() &&
232+
"D is already mapped to a lexical block scope");
233+
if (!LexicalBlockStack.empty())
234+
LexicalBlockMap.insert({&D, LexicalBlockStack.back()});
235+
}
236+
237+
llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl *D) {
238+
auto I = LexicalBlockMap.find(D);
239+
if (I != LexicalBlockMap.end())
240+
return I->second;
241+
return getDeclContextDescriptor(cast<Decl>(D));
242+
}
243+
230244
PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
231245
PrintingPolicy PP = CGM.getContext().getPrintingPolicy();
232246

@@ -1346,13 +1360,13 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
13461360
// declared.
13471361
SourceLocation Loc = Ty->getDecl()->getLocation();
13481362

1363+
llvm::DIScope *TDContext = getDeclarationLexicalScope(Ty->getDecl());
13491364
uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext());
13501365
// Typedefs are derived from some other type.
13511366
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(Ty->getDecl());
13521367
return DBuilder.createTypedef(Underlying, Ty->getDecl()->getName(),
13531368
getOrCreateFile(Loc), getLineNumber(Loc),
1354-
getDeclContextDescriptor(Ty->getDecl()), Align,
1355-
Annotations);
1369+
TDContext, Align, Annotations);
13561370
}
13571371

13581372
static unsigned getDwarfCC(CallingConv CC) {
@@ -3251,7 +3265,7 @@ llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
32513265
// entered into the ReplaceMap: finalize() will replace the first
32523266
// FwdDecl with the second and then replace the second with
32533267
// complete type.
3254-
llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
3268+
llvm::DIScope *EDContext = getDeclarationLexicalScope(ED);
32553269
llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
32563270
llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
32573271
llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
@@ -3294,7 +3308,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
32943308

32953309
llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
32963310
unsigned Line = getLineNumber(ED->getLocation());
3297-
llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
3311+
llvm::DIScope *EnumContext = getDeclarationLexicalScope(ED);
32983312
llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
32993313
return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
33003314
Line, Size, Align, EltArray, ClassTy,
@@ -3597,7 +3611,7 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
35973611
Line = getLineNumber(Loc);
35983612
}
35993613

3600-
llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
3614+
llvm::DIScope *RDContext = getDeclarationLexicalScope(RD);
36013615

36023616
// If we ended up creating the type during the context chain construction,
36033617
// just return that.
@@ -3790,6 +3804,14 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
37903804
TemplateParameters = nullptr;
37913805
}
37923806

3807+
// Get context for static locals (that are technically globals) the same way
3808+
// we do for "local" locals -- by using current lexical block.
3809+
if (VD->isStaticLocal()) {
3810+
assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
3811+
VDContext = LexicalBlockStack.back();
3812+
return;
3813+
}
3814+
37933815
// Since we emit declarations (DW_AT_members) for static members, place the
37943816
// definition of those static members in the namespace they were declared in
37953817
// in the source code (the lexical decl context).

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ class CGDebugInfo {
139139

140140
/// Keep track of our current nested lexical block.
141141
std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
142+
143+
/// Map of AST declaration to its lexical block scope.
144+
llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIScope>>
145+
LexicalBlockMap;
146+
142147
llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
143148
/// Keep track of LexicalBlockStack counter at the beginning of a
144149
/// function. This is used to pop unbalanced regions at the end of a
@@ -543,6 +548,12 @@ class CGDebugInfo {
543548
/// Emit an Objective-C interface type standalone debug info.
544549
llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
545550

551+
/// Map AST declaration to its lexical block scope if available.
552+
void recordDeclarationLexicalScope(const Decl &D);
553+
554+
/// Get lexical scope of AST declaration.
555+
llvm::DIScope *getDeclarationLexicalScope(const Decl *D);
556+
546557
/// Emit standalone debug info for a type.
547558
llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);
548559

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,21 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
103103
llvm_unreachable("Declaration should not be in declstmts!");
104104
case Decl::Record: // struct/union/class X;
105105
case Decl::CXXRecord: // struct/union/class X; [C++]
106-
if (CGDebugInfo *DI = getDebugInfo())
106+
if (CGDebugInfo *DI = getDebugInfo()) {
107+
DI->recordDeclarationLexicalScope(D);
107108
if (cast<RecordDecl>(D).getDefinition())
108109
DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(&D)));
110+
}
109111
return;
110112
case Decl::Enum: // enum X;
111-
if (CGDebugInfo *DI = getDebugInfo())
113+
if (CGDebugInfo *DI = getDebugInfo()) {
114+
DI->recordDeclarationLexicalScope(D);
112115
if (cast<EnumDecl>(D).getDefinition())
113116
DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(&D)));
117+
}
114118
return;
115-
case Decl::Function: // void X();
116119
case Decl::EnumConstant: // enum ? { X = ? }
120+
case Decl::Function: // void X();
117121
case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
118122
case Decl::Label: // __label__ x;
119123
case Decl::Import:
@@ -132,11 +136,11 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
132136

133137
case Decl::NamespaceAlias:
134138
if (CGDebugInfo *DI = getDebugInfo())
135-
DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
139+
DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
136140
return;
137141
case Decl::Using: // using X; [C++]
138142
if (CGDebugInfo *DI = getDebugInfo())
139-
DI->EmitUsingDecl(cast<UsingDecl>(D));
143+
DI->EmitUsingDecl(cast<UsingDecl>(D));
140144
return;
141145
case Decl::UsingEnum: // using enum X; [C++]
142146
if (CGDebugInfo *DI = getDebugInfo())
@@ -172,8 +176,10 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
172176
case Decl::Typedef: // typedef int X;
173177
case Decl::TypeAlias: { // using X = int; [C++0x]
174178
QualType Ty = cast<TypedefNameDecl>(D).getUnderlyingType();
175-
if (CGDebugInfo *DI = getDebugInfo())
179+
if (CGDebugInfo *DI = getDebugInfo()) {
180+
DI->recordDeclarationLexicalScope(D);
176181
DI->EmitAndRetainType(Ty);
182+
}
177183
if (Ty->isVariablyModifiedType())
178184
EmitVariablyModifiedType(Ty);
179185
return;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s
2+
3+
void foo() {
4+
static int bar = 1;
5+
{
6+
struct X {};
7+
typedef char Y;
8+
static int bar = 0;
9+
// The following basic block is intended, in order to check the case where
10+
// types "X", "Y" are defined in a different scope than where they are used.
11+
// They should have the scope they are defined at as their parent scope.
12+
{
13+
X a;
14+
Y b;
15+
}
16+
}
17+
}
18+
19+
// CHECK: !{{[0-9]+}} = distinct !DIGlobalVariable(name: "bar", scope: [[FSCOPE:![0-9]+]]
20+
// CHECK: [[FSCOPE]] = distinct !DISubprogram(name: "foo"
21+
// CHECK: !{{[0-9]+}} = distinct !DIGlobalVariable(name: "bar", scope: [[LBSCOPE:![0-9]+]]
22+
// CHECK: [[LBSCOPE]] = distinct !DILexicalBlock(scope: [[FSCOPE]]
23+
// CHECK: !{{[0-9]+}} = !DILocalVariable(name: "a", scope: [[LBSCOPE2:![0-9]+]], {{.*}} type: [[STRUCT:![0-9]+]]
24+
// CHECK: [[LBSCOPE2]] = distinct !DILexicalBlock(scope: [[LBSCOPE]]
25+
// CHECK: [[STRUCT]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "X", scope: [[LBSCOPE]]
26+
// CHECK: !{{[0-9]+}} = !DILocalVariable(name: "b", scope: [[LBSCOPE2]], {{.*}} type: [[TYPEDEF:![0-9]+]]
27+
// CHECK: [[TYPEDEF]] = !DIDerivedType(tag: DW_TAG_typedef, name: "Y", scope: [[LBSCOPE]]

0 commit comments

Comments
 (0)