Index: lib/CodeGen/CGDebugInfo.h =================================================================== --- lib/CodeGen/CGDebugInfo.h +++ lib/CodeGen/CGDebugInfo.h @@ -114,6 +114,11 @@ /// Keep track of our current nested lexical block. std::vector> LexicalBlockStack; + + /// Map of AST declaration to its lexical block scope. + llvm::DenseMap> + LexicalBlockMap; + llvm::DenseMap RegionMap; /// Keep track of LexicalBlockStack counter at the beginning of a /// function. This is used to pop unbalanced regions at the end of a @@ -365,6 +370,12 @@ /// Emit an Objective-C interface type standalone debug info. llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc); + /// Map AST declaration to its lexical block scope if available. + void recordDeclarationLexicalScope(const Decl &D); + + /// Get lexical scope of AST declaration. + llvm::DIScope *getDeclarationLexicalScope(const Decl &D, QualType Ty); + /// Emit standalone debug info for a type. llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); Index: lib/CodeGen/CGDebugInfo.cpp =================================================================== --- lib/CodeGen/CGDebugInfo.cpp +++ lib/CodeGen/CGDebugInfo.cpp @@ -831,15 +831,18 @@ 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(); + + llvm::DIScope *TDContext = getDeclarationLexicalScope(*TD, QualType(Ty, 0)); // Typedefs are derived from some other type. return DBuilder.createTypedef( getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), - getDeclContextDescriptor(Ty->getDecl())); + TDContext); } llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty, @@ -1472,6 +1475,23 @@ return T; } +void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) { + assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() && + "D is already mapped to lexical block scope"); + if (!LexicalBlockStack.empty()) + LexicalBlockMap[&D] = LexicalBlockStack.back(); +} + +llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl &D, + QualType Ty) { + auto I = LexicalBlockMap.find(&D); + if (I != LexicalBlockMap.end()) { + RetainedTypes.push_back(Ty.getAsOpaquePtr()); + return I->second; + } else + return getDeclContextDescriptor(cast(&D)); +} + void CGDebugInfo::completeType(const EnumDecl *ED) { if (DebugKind <= CodeGenOptions::DebugLineTablesOnly) return; @@ -2043,7 +2063,7 @@ // If this is just a forward declaration, construct an appropriately // marked node and just return it. if (isImportedFromModule || !ED->getDefinition()) { - llvm::DIScope *EDContext = getDeclContextDescriptor(ED); + llvm::DIScope *EDContext = getDeclarationLexicalScope(*ED, QualType(Ty, 0)); llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); unsigned Line = getLineNumber(ED->getLocation()); StringRef EDName = ED->getName(); @@ -2083,7 +2103,7 @@ llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); unsigned Line = getLineNumber(ED->getLocation()); - llvm::DIScope *EnumContext = getDeclContextDescriptor(ED); + llvm::DIScope *EnumContext = getDeclarationLexicalScope(*ED, QualType(Ty, 0)); llvm::DIType *ClassTy = ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr; return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, @@ -2337,7 +2357,7 @@ unsigned Line = getLineNumber(RD->getLocation()); StringRef RDName = getClassName(RD); - llvm::DIScope *RDContext = getDeclContextDescriptor(RD); + llvm::DIScope *RDContext = getDeclarationLexicalScope(*RD, QualType(Ty, 0)); // If we ended up creating the type during the context chain construction, // just return that. @@ -2485,8 +2505,15 @@ if (DC->isRecord()) DC = CGM.getContext().getTranslationUnitDecl(); - llvm::DIScope *Mod = getParentModuleOrNull(VD); - VDContext = getContextDescriptor(cast(DC), Mod ? Mod : TheCU); + 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 { + llvm::DIScope *Mod = getParentModuleOrNull(VD); + VDContext = getContextDescriptor(cast(DC), Mod ? Mod : TheCU); + } } llvm::DISubprogram * Index: lib/CodeGen/CGDecl.cpp =================================================================== --- lib/CodeGen/CGDecl.cpp +++ lib/CodeGen/CGDecl.cpp @@ -83,11 +83,7 @@ case Decl::UsingShadow: case Decl::ObjCTypeParam: 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::Function: // void X(); case Decl::StaticAssert: // static_assert(X, ""); [C++0x] case Decl::Label: // __label__ x; case Decl::Import: @@ -96,13 +92,21 @@ // None of these decls require codegen support. return; + 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++] + if (CGDebugInfo *DI = getDebugInfo()) + DI->recordDeclarationLexicalScope(D); + return; + case Decl::NamespaceAlias: if (CGDebugInfo *DI = getDebugInfo()) - DI->EmitNamespaceAlias(cast(D)); + DI->EmitNamespaceAlias(cast(D)); return; case Decl::Using: // using X; [C++] if (CGDebugInfo *DI = getDebugInfo()) - DI->EmitUsingDecl(cast(D)); + DI->EmitUsingDecl(cast(D)); return; case Decl::UsingDirective: // using namespace X; [C++] if (CGDebugInfo *DI = getDebugInfo()) @@ -120,6 +124,9 @@ const TypedefNameDecl &TD = cast(D); QualType Ty = TD.getUnderlyingType(); + if (CGDebugInfo *DI = getDebugInfo()) + DI->recordDeclarationLexicalScope(D); + if (Ty->isVariablyModifiedType()) EmitVariablyModifiedType(Ty); } 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,21 @@ +// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -debug-info-kind=limited %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: 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 -debug-info-kind=limited %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-static2.cpp =================================================================== --- test/CodeGenCXX/debug-info-lb-static2.cpp +++ test/CodeGenCXX/debug-info-lb-static2.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s + +int main() { + static int X = 10; + { + static bool X = false; + return (int) X; + } + return X; +} + +// CHECK: [[FuncScope:![0-9]+]] = distinct !DISubprogram(name: "main", +// CHECK: !{{[0-9]+}} = !DIGlobalVariable(name: "X", scope: [[FuncScope:![0-9]+]], +// CHECK: !{{[0-9]+}} = !DIGlobalVariable(name: "X", scope: [[LBScope:![0-9]+]], +// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5) + 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 -debug-info-kind=limited %s -o - | FileCheck %s + +int foo(int x) { + if(x) + { + typedef char X; + { + X a = x; + return a; + } + } + return 0; +} + +// CHECK: !{{[0-9]+}} = !DIDerivedType(tag: DW_TAG_typedef, name: "X", scope: [[LBScope:![0-9]+]], +// CHECK: [[LBScope]] = distinct !DILexicalBlock(scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 5) +