diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -138,6 +138,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 @@ -542,6 +547,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); + /// Emit standalone debug info for a type. llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -227,6 +227,20 @@ return Default; } +void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) { + assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() && + "D is already mapped to a lexical block scope"); + if (!LexicalBlockStack.empty()) + LexicalBlockMap.insert({&D, LexicalBlockStack.back()}); +} + +llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl *D) { + auto I = LexicalBlockMap.find(D); + if (I != LexicalBlockMap.end()) + return I->second; + return getDeclContextDescriptor(cast(D)); +} + PrintingPolicy CGDebugInfo::getPrintingPolicy() const { PrintingPolicy PP = CGM.getContext().getPrintingPolicy(); @@ -1344,13 +1358,13 @@ // declared. SourceLocation Loc = Ty->getDecl()->getLocation(); + llvm::DIScope *TDContext = getDeclarationLexicalScope(Ty->getDecl()); uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext()); // Typedefs are derived from some other type. llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(Ty->getDecl()); return DBuilder.createTypedef(Underlying, Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), - getDeclContextDescriptor(Ty->getDecl()), Align, - Annotations); + TDContext, Align, Annotations); } static unsigned getDwarfCC(CallingConv CC) { @@ -3249,7 +3263,7 @@ // entered into the ReplaceMap: finalize() will replace the first // FwdDecl with the second and then replace the second with // complete type. - llvm::DIScope *EDContext = getDeclContextDescriptor(ED); + llvm::DIScope *EDContext = getDeclarationLexicalScope(ED); llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType( llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0)); @@ -3292,7 +3306,7 @@ llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); unsigned Line = getLineNumber(ED->getLocation()); - llvm::DIScope *EnumContext = getDeclContextDescriptor(ED); + llvm::DIScope *EnumContext = getDeclarationLexicalScope(ED); llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit); return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line, Size, Align, EltArray, ClassTy, @@ -3599,7 +3613,7 @@ Line = getLineNumber(Loc); } - llvm::DIScope *RDContext = getDeclContextDescriptor(RD); + llvm::DIScope *RDContext = getDeclarationLexicalScope(RD); // If we ended up creating the type during the context chain construction, // just return that. @@ -3795,6 +3809,14 @@ TemplateParameters = nullptr; } + // Get context for static locals (that are technically globals) the same way + // we do for "local" locals -- by using current lexical block. + if (VD->isStaticLocal()) { + assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); + VDContext = LexicalBlockStack.back(); + return; + } + // Since we emit declarations (DW_AT_members) for static members, place the // definition of those static members in the namespace they were declared in // in the source code (the lexical decl context). diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -103,17 +103,21 @@ llvm_unreachable("Declaration should not be in declstmts!"); case Decl::Record: // struct/union/class X; case Decl::CXXRecord: // struct/union/class X; [C++] - if (CGDebugInfo *DI = getDebugInfo()) + if (CGDebugInfo *DI = getDebugInfo()) { + DI->recordDeclarationLexicalScope(D); if (cast(D).getDefinition()) DI->EmitAndRetainType(getContext().getRecordType(cast(&D))); + } return; case Decl::Enum: // enum X; - if (CGDebugInfo *DI = getDebugInfo()) + if (CGDebugInfo *DI = getDebugInfo()) { + DI->recordDeclarationLexicalScope(D); if (cast(D).getDefinition()) DI->EmitAndRetainType(getContext().getEnumType(cast(&D))); + } return; - case Decl::Function: // void X(); case Decl::EnumConstant: // enum ? { X = ? } + case Decl::Function: // void X(); case Decl::StaticAssert: // static_assert(X, ""); [C++0x] case Decl::Label: // __label__ x; case Decl::Import: @@ -132,11 +136,11 @@ 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::UsingEnum: // using enum X; [C++] if (CGDebugInfo *DI = getDebugInfo()) @@ -172,8 +176,10 @@ case Decl::Typedef: // typedef int X; case Decl::TypeAlias: { // using X = int; [C++0x] QualType Ty = cast(D).getUnderlyingType(); - if (CGDebugInfo *DI = getDebugInfo()) + if (CGDebugInfo *DI = getDebugInfo()) { + DI->recordDeclarationLexicalScope(D); DI->EmitAndRetainType(Ty); + } if (Ty->isVariablyModifiedType()) EmitVariablyModifiedType(Ty); return; diff --git a/clang/test/CodeGenCXX/debug-info-lexcial-block.cpp b/clang/test/CodeGenCXX/debug-info-lexcial-block.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenCXX/debug-info-lexcial-block.cpp @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -triple x86_64-none-linux-gnu -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s + +void foo() { + static int bar = 1; + { + struct X {}; + typedef char Y; + static int bar = 0; + // The following basic block is intended, in order to check the case where + // types "X", "Y" are defined in a different scope than where they are used. + // They should have the scope they are defined at as their parent scope. + { + X a; + Y b; + } + } +} + +// CHECK: !{{[0-9]+}} = distinct !DIGlobalVariable(name: "bar", scope: [[FSCOPE:![0-9]+]] +// CHECK: [[FSCOPE]] = distinct !DISubprogram(name: "foo" +// CHECK: !{{[0-9]+}} = distinct !DIGlobalVariable(name: "bar", scope: [[LBSCOPE:![0-9]+]] +// CHECK: [[LBSCOPE]] = distinct !DILexicalBlock(scope: [[FSCOPE]] +// CHECK: !{{[0-9]+}} = !DILocalVariable(name: "a", scope: [[LBSCOPE2:![0-9]+]], {{.*}} type: [[STRUCT:![0-9]+]] +// CHECK: [[LBSCOPE2]] = distinct !DILexicalBlock(scope: [[LBSCOPE]] +// CHECK: [[STRUCT]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "X", scope: [[LBSCOPE]] +// CHECK: !{{[0-9]+}} = !DILocalVariable(name: "b", scope: [[LBSCOPE2]], {{.*}} type: [[TYPEDEF:![0-9]+]] +// CHECK: [[TYPEDEF]] = !DIDerivedType(tag: DW_TAG_typedef, name: "Y", scope: [[LBSCOPE]]