From e403f4fdc88322201040f2bee7b328e8a78e2f7f Mon Sep 17 00:00:00 2001 From: Kristina Bessonova Date: Mon, 6 Dec 2021 12:19:09 +0200 Subject: [PATCH] [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 --- clang/lib/CodeGen/CGDebugInfo.cpp | 32 ++++++++++++++++++---- clang/lib/CodeGen/CGDebugInfo.h | 11 ++++++++ clang/lib/CodeGen/CGDecl.cpp | 18 ++++++++---- clang/test/CodeGenCXX/debug-info-lexcial-block.cpp | 27 ++++++++++++++++++ 4 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 clang/test/CodeGenCXX/debug-info-lexcial-block.cpp diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index af651e6..75eec22 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -227,6 +227,20 @@ llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context, 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(); @@ -1346,13 +1360,13 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty, // 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) { @@ -3251,7 +3265,7 @@ llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) { // 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)); @@ -3294,7 +3308,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) { 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, @@ -3597,7 +3611,7 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) { 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. @@ -3790,6 +3804,14 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, 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/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h index a7b72fa..1350ee6 100644 --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -139,6 +139,11 @@ class CGDebugInfo { /// 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 @@ -543,6 +548,12 @@ public: /// 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/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 941671c..742bc8e 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -103,17 +103,21 @@ void CodeGenFunction::EmitDecl(const Decl &D) { 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 @@ void CodeGenFunction::EmitDecl(const Decl &D) { 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 @@ void CodeGenFunction::EmitDecl(const Decl &D) { 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 index 0000000..75b4b2c --- /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]] -- 2.7.4