Hi echristo, dblaikie, ABataev,
A Fix for Bug 19238.
Function static variable defined inside a lexical scope, same as typedef and
record (class, struct or union), which are declared inside a lexical scope,
were all associated to the function as its parent scope, rather than to the
lexical scope they are defined or declared inside.
This patch provides a fix for all these three cases in the Clang debug info
generation.
This patch is related to "D9758"
http://reviews.llvm.org/D9760
Files:
lib/CodeGen/CGDebugInfo.cpp
lib/CodeGen/CGDebugInfo.h
lib/CodeGen/CGDecl.cpp
test/CodeGenCXX/debug-info-lb-class.cpp
test/CodeGenCXX/debug-info-lb-static.cpp
test/CodeGenCXX/debug-info-lb-typedef.cpp
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
Index: test/CodeGenCXX/debug-info-lb-typedef.cpp
===================================================================
--- test/CodeGenCXX/debug-info-lb-typedef.cpp
+++ test/CodeGenCXX/debug-info-lb-typedef.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -g %s -o - | FileCheck %s
+
+int foo(int x) {
+ if(x)
+ {
+ typedef char X;
+ {
+ X a = x;
+ return a;
+ }
+ }
+ return 0;
+}
+
+// CHECK: [[LBScope:![0-9]+]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5)
+// CHECK: !{{[0-9]+}} = !DIDerivedType(tag: DW_TAG_typedef, name: "X", scope: [[LBScope]],
+
Index: test/CodeGenCXX/debug-info-lb-static.cpp
===================================================================
--- test/CodeGenCXX/debug-info-lb-static.cpp
+++ test/CodeGenCXX/debug-info-lb-static.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -g %s -o - | FileCheck %s
+
+int foo(int x) {
+ if(x)
+ {
+ static int bar = 0;
+ {
+ int a = bar++;
+ return a;
+ }
+ }
+ return 0;
+}
+
+// CHECK: !{{[0-9]+}} = !DIGlobalVariable(name: "bar", scope: [[LBScope:![0-9]+]],
+// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5)
+
Index: test/CodeGenCXX/debug-info-lb-class.cpp
===================================================================
--- test/CodeGenCXX/debug-info-lb-class.cpp
+++ test/CodeGenCXX/debug-info-lb-class.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -g %s -o - | FileCheck %s
+
+int foo(int x) {
+ if(x)
+ {
+ class X {
+ public:
+ char z;
+ X(int y) : z(y) {}
+ };
+ {
+ X a(x);
+ return a.z;
+ }
+ }
+ return 0;
+}
+
+// CHECK: !{{[0-9]+}} = !DICompositeType(tag: DW_TAG_class_type, name: "X", scope: [[LBScope:![0-9]+]],
+// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5)
Index: lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- lib/CodeGen/CGDebugInfo.cpp
+++ lib/CodeGen/CGDebugInfo.cpp
@@ -772,15 +772,20 @@
llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
llvm::DIFile *Unit) {
+ TypedefNameDecl *TD = Ty->getDecl();
// We don't set size information, but do specify where the typedef was
// declared.
- SourceLocation Loc = Ty->getDecl()->getLocation();
+ SourceLocation Loc = TD->getLocation();
+
+ auto I = LexicalBlockMap.find(TD);
+ llvm::DIScope *TDContext = I != LexicalBlockMap.end() ? I->second
+ : getContextDescriptor(cast<Decl>(TD->getDeclContext()));
// Typedefs are derived from some other type.
return DBuilder.createTypedef(
getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
- getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext())));
+ TDContext);
}
llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
@@ -1427,6 +1432,13 @@
return T;
}
+void CGDebugInfo::recordDeclarationLexicalScope(const Decl &Decl) {
+ assert(LexicalBlockMap.find(&Decl) == LexicalBlockMap.end() &&
+ "Decl is already mapped to lexical block scope");
+ if (!LexicalBlockStack.empty())
+ LexicalBlockMap[&Decl] = LexicalBlockStack.back();
+}
+
void CGDebugInfo::completeType(const EnumDecl *ED) {
if (DebugKind <= CodeGenOptions::DebugLineTablesOnly)
return;
@@ -2211,8 +2223,9 @@
unsigned Line = getLineNumber(RD->getLocation());
StringRef RDName = getClassName(RD);
- llvm::DIScope *RDContext =
- getContextDescriptor(cast<Decl>(RD->getDeclContext()));
+ auto I = LexicalBlockMap.find(RD);
+ llvm::DIScope *RDContext = I != LexicalBlockMap.end() ? I->second
+ : getContextDescriptor(cast<Decl>(RD->getDeclContext()));
// If we ended up creating the type during the context chain construction,
// just return that.
@@ -2358,7 +2371,14 @@
// outside the class by putting it in the global scope.
if (DC->isRecord())
DC = CGM.getContext().getTranslationUnitDecl();
- VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
+
+ if (VD->isStaticLocal()) {
+ // Get context for static locals (that are technically globals) the same way
+ // we do for "local" locals -- by using current lexical block.
+ assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
+ VDContext = LexicalBlockStack.back();
+ } else
+ VDContext = getContextDescriptor(dyn_cast<Decl>(DC));
}
llvm::DISubprogram *
Index: lib/CodeGen/CGDebugInfo.h
===================================================================
--- lib/CodeGen/CGDebugInfo.h
+++ lib/CodeGen/CGDebugInfo.h
@@ -97,6 +97,11 @@
// LexicalBlockStack - Keep track of our current nested lexical block.
std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
+
+ /// \brief Map of AST declaration to its lexical block scope.
+ llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIScope>>
+ LexicalBlockMap;
+
llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
// FnBeginRegionCount - Keep track of LexicalBlockStack counter at the
// beginning of a function. This is used to pop unbalanced regions at
@@ -299,6 +304,9 @@
/// debug info.
llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
+ /// \brief Map AST declaration to its lexical block scope if available.
+ void recordDeclarationLexicalScope(const Decl &Decl);
+
void completeType(const EnumDecl *ED);
void completeType(const RecordDecl *RD);
void completeRequiredType(const RecordDecl *RD);
Index: lib/CodeGen/CGDecl.cpp
===================================================================
--- lib/CodeGen/CGDecl.cpp
+++ lib/CodeGen/CGDecl.cpp
@@ -81,18 +81,22 @@
case Decl::UsingShadow:
llvm_unreachable("Declaration should not be in declstmts!");
case Decl::Function: // void X();
- case Decl::Record: // struct/union/class X;
case Decl::Enum: // enum X;
case Decl::EnumConstant: // enum ? { X = ? }
- case Decl::CXXRecord: // struct/union/class X; [C++]
case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
case Decl::Label: // __label__ x;
case Decl::Import:
case Decl::OMPThreadPrivate:
case Decl::Empty:
// None of these decls require codegen support.
return;
+ case Decl::CXXRecord: // struct/union/class X; [C++]
+ case Decl::Record: // struct/union/class X;
+ if (CGDebugInfo *DI = getDebugInfo())
+ DI->recordDeclarationLexicalScope(D);
+ return;
+
case Decl::NamespaceAlias:
if (CGDebugInfo *DI = getDebugInfo())
DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
@@ -117,6 +121,9 @@
const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
QualType Ty = TD.getUnderlyingType();
+ if (CGDebugInfo *DI = getDebugInfo())
+ DI->recordDeclarationLexicalScope(D);
+
if (Ty->isVariablyModifiedType())
EmitVariablyModifiedType(Ty);
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits