From e6b5195e156f9a8a04ee2d403ec0aab8b59a9f7f Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 19 Nov 2014 19:42:40 +0000 Subject: [PATCH] DebugInfo: Don't emit a 'global variable' when a static member declaration is referenced without a definition, just ensure the enclosing class (with the static member declaration) is emitted. Addresses PR21511 by emitting appropriate metadata rather than faux-global definitions for a variable that doesn't have a definition. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@222377 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGDebugInfo.cpp | 22 ++++++++++++++++---- test/CodeGenCXX/debug-info-class.cpp | 4 ---- test/CodeGenCXX/debug-info-static-member.cpp | 18 ++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index 71ccb0d9f9..03a0cd92c2 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -3275,15 +3275,29 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, if (isa(VD->getDeclContext())) return; VD = cast(VD->getCanonicalDecl()); + llvm::DIDescriptor DContext = + getContextDescriptor(dyn_cast(VD->getDeclContext())); + auto *VarD = cast(VD); + + // If this is only a declaration, it might be the declaration of a static + // variable with an initializer - we still want to ensure that's emitted, but + // merely calling getContextDescriptor above has already ensured that. Since + // there's no definition to emit, there's no further work to do. + if (!VarD->hasDefinition()) { + // Ensure that the type is retained even though it's otherwise unreferenced. + RetainedTypes.push_back( + CGM.getContext() + .getRecordType(cast(VD->getDeclContext())) + .getAsOpaquePtr()); + return; + } + auto pair = DeclCache.insert(std::make_pair(VD, llvm::WeakVH())); if (!pair.second) return; - llvm::DIDescriptor DContext = - getContextDescriptor(dyn_cast(VD->getDeclContext())); llvm::DIGlobalVariable GV = DBuilder.createGlobalVariable( DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty, - true, Init, - getOrCreateStaticDataMemberDeclarationOrNull(cast(VD))); + true, Init, getOrCreateStaticDataMemberDeclarationOrNull(VarD)); pair.first->second = llvm::WeakVH(GV); } diff --git a/test/CodeGenCXX/debug-info-class.cpp b/test/CodeGenCXX/debug-info-class.cpp index 783c00c0d0..5da53210c4 100644 --- a/test/CodeGenCXX/debug-info-class.cpp +++ b/test/CodeGenCXX/debug-info-class.cpp @@ -118,9 +118,5 @@ int main(int argc, char **argv) { // CHECK: metadata !"_ZTS1D", {{.*}}, metadata [[D_FUNC_DECL:![0-9]*]], metadata {{![0-9]*}}} ; [ DW_TAG_subprogram ] {{.*}} [def] [func] // CHECK: [[D_FUNC_DECL]] = metadata !{metadata !"0x2e\00func\00{{.*}}\000\00{{[0-9]+}}"{{.*}}, metadata !"_ZTS1D", {{.*}}, null} ; [ DW_TAG_subprogram ] {{.*}} [func] -// CHECK: [[F_I_DEF:![0-9]*]] = {{.*}}, metadata !"_ZTS1F", metadata {{![0-9]*}}, metadata {{![0-9]*}}, i32 2, metadata [[F_I:![0-9]*]]} ; [ DW_TAG_variable ] [i] - -// CHECK: [[F_I]] = {{.*}}, metadata !"_ZTS1F", {{.*}} ; [ DW_TAG_member ] [i] - // CHECK: ![[EXCEPTLOC]] = metadata !{i32 84, // CHECK: ![[RETLOC]] = metadata !{i32 83, diff --git a/test/CodeGenCXX/debug-info-static-member.cpp b/test/CodeGenCXX/debug-info-static-member.cpp index c65ccd64dd..803d0dc1c7 100644 --- a/test/CodeGenCXX/debug-info-static-member.cpp +++ b/test/CodeGenCXX/debug-info-static-member.cpp @@ -43,10 +43,28 @@ int main() // CHECK: metadata !"0xd\00const_c\00{{.*}}", {{.*}} [ DW_TAG_member ] [const_c] [line {{.*}}, size 0, align 0, offset 0] [public] [static] // CHECK: metadata !"0xd\00x_a\00{{.*}}", {{.*}} [ DW_TAG_member ] [x_a] {{.*}} [public] [static] // CHECK: [[NS_X:![0-9]+]] = {{.*}} ; [ DW_TAG_namespace ] [x] + +// Test this in an anonymous namespace to ensure the type is retained even when +// it doesn't get automatically retained by the string type reference machinery. +namespace { +struct anon_static_decl_struct { + static const int anon_static_decl_var = 117; +}; +} + +// CHECK: ; [ DW_TAG_structure_type ] [anon_static_decl_struct] {{.*}} [def] +// CHECK: ; [ DW_TAG_member ] [anon_static_decl_var] + +int ref() { + return anon_static_decl_struct::anon_static_decl_var; +} + // CHECK: metadata !{metadata !"0x34\00a\00{{.*}}", null, {{.*}} @_ZN1C1aE, metadata ![[DECL_A]]} ; [ DW_TAG_variable ] [a] {{.*}} [def] // CHECK: metadata !{metadata !"0x34\00b\00{{.*}}", null, {{.*}} @_ZN1C1bE, metadata ![[DECL_B]]} ; [ DW_TAG_variable ] [b] {{.*}} [def] // CHECK: metadata !{metadata !"0x34\00c\00{{.*}}", null, {{.*}} @_ZN1C1cE, metadata ![[DECL_C]]} ; [ DW_TAG_variable ] [c] {{.*}} [def] +// CHECK-NOT: ; [ DW_TAG_variable ] [anon_static_decl_var] + // Verify that even when a static member declaration is created lazily when // creating the definition, the declaration line is that of the canonical // declaration, not the definition. Also, since we look at the canonical -- 2.50.1