From 325003be02bff4291afb7c635cb0e75d7b3d1ba2 Mon Sep 17 00:00:00 2001 From: Amy Huang Date: Wed, 29 May 2019 21:45:34 +0000 Subject: [PATCH] CodeView - add static data members to global variable debug info. Summary: Add static data members to IR debug info's list of global variables so that they are emitted as S_CONSTANT records. Related to https://bugs.llvm.org/show_bug.cgi?id=41615. Reviewers: rnk Subscribers: aprantl, cfe-commits, llvm-commits, thakis Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D62167 llvm-svn: 362038 --- clang/lib/CodeGen/CGDebugInfo.cpp | 18 ++++- .../CodeGenCXX/debug-info-static-member.cpp | 13 +++- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp | 7 +- llvm/test/DebugInfo/COFF/global-constants.ll | 75 ++++++++++++------- 4 files changed, 80 insertions(+), 33 deletions(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 89c053f4b290..b79169f0a060 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -4361,9 +4361,13 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { return; } - // Do not emit separate definitions for function local const/statics. + llvm::DIScope *DContext = nullptr; + + // Do not emit separate definitions for function local consts. if (isa(VD->getDeclContext())) return; + + // Emit definition for static members in CodeView. VD = cast(VD->getCanonicalDecl()); auto *VarD = cast(VD); if (VarD->isStaticDataMember()) { @@ -4375,10 +4379,16 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { // through its scope. RetainedTypes.push_back( CGM.getContext().getRecordType(RD).getAsOpaquePtr()); - return; - } - llvm::DIScope *DContext = getDeclContextDescriptor(VD); + if (!CGM.getCodeGenOpts().EmitCodeView) + return; + + // Use the global scope for static members. + DContext = getContextDescriptor( + cast(CGM.getContext().getTranslationUnitDecl()), TheCU); + } else { + DContext = getDeclContextDescriptor(VD); + } auto &GV = DeclCache[VD]; if (GV) diff --git a/clang/test/CodeGenCXX/debug-info-static-member.cpp b/clang/test/CodeGenCXX/debug-info-static-member.cpp index 702d1f87e752..8ad86843f4e9 100644 --- a/clang/test/CodeGenCXX/debug-info-static-member.cpp +++ b/clang/test/CodeGenCXX/debug-info-static-member.cpp @@ -1,6 +1,7 @@ // RUN: %clangxx -target x86_64-unknown-unknown -g %s -emit-llvm -S -o - | FileCheck %s // RUN: %clangxx -target x86_64-unknown-unknown -g -std=c++98 %s -emit-llvm -S -o - | FileCheck %s // RUN: %clangxx -target x86_64-unknown-unknown -g -std=c++11 %s -emit-llvm -S -o - | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-windows-msvc -gcodeview -debug-info-kind=limited %s -emit-llvm -o - | FileCheck --check-prefix MSVC %s // PR14471 // CHECK: @_ZN1C1aE = dso_local global i32 4, align 4, !dbg [[A:![0-9]+]] @@ -35,6 +36,7 @@ public: // CHECK: [[A]] = !DIGlobalVariableExpression(var: [[AV:.*]], expr: !DIExpression()) // CHECK: [[AV]] = distinct !DIGlobalVariable(name: "a", // CHECK-SAME: declaration: ![[DECL_A:[0-9]+]]) +// MSVC: distinct !DIGlobalVariable(name: "a" // // CHECK: !DICompositeType(tag: DW_TAG_enumeration_type, name: "X"{{.*}}, identifier: "_ZTS1X") // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "anon_static_decl_struct" @@ -48,6 +50,7 @@ int C::a = 4; // CHECK: [[B]] = !DIGlobalVariableExpression(var: [[BV:.*]], expr: !DIExpression()) // CHECK: [[BV]] = distinct !DIGlobalVariable(name: "b", // CHECK-SAME: declaration: ![[DECL_B:[0-9]+]]) +// MSVC: distinct !DIGlobalVariable(name: "b" // CHECK: ![[DECL_B]] = !DIDerivedType(tag: DW_TAG_member, name: "b" // CHECK-NOT: size: // CHECK-NOT: align: @@ -95,6 +98,7 @@ int C::a = 4; int C::b = 2; // CHECK: [[C]] = !DIGlobalVariableExpression(var: [[CV:.*]], expr: !DIExpression()) // CHECK: [[CV]] = distinct !DIGlobalVariable(name: "c", {{.*}} declaration: ![[DECL_C]]) +// MSVC: distinct !DIGlobalVariable(name: "c" int C::c = 1; int main() @@ -114,11 +118,18 @@ struct anon_static_decl_struct { }; } - int ref() { return anon_static_decl_struct::anon_static_decl_var; } +// In MSVC, static data members should be emitted as global variables when used. +// MSVC: !DIGlobalVariableExpression(var: [[ANON_STATIC_DECL:![0-9]+]], +// MSVC-SAME: !DIExpression(DW_OP_constu, 117, DW_OP_stack_value) +// MSVC: [[ANON_STATIC_DECL]] = distinct !DIGlobalVariable(name: "anon_static_decl_var" +// MSVC: !DIGlobalVariableExpression(var: [[STATIC_DECL_TEMPL:![0-9]+]] +// MSVC-SAME: !DIExpression(DW_OP_constu, 7, DW_OP_stack_value) +// MSVC: [[STATIC_DECL_TEMPL]] = distinct !DIGlobalVariable(name: "static_decl_templ_var" + template struct static_decl_templ { static const int static_decl_templ_var = 7; diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp index 417388e86784..b87757583a46 100644 --- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -3069,7 +3069,12 @@ void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) { OS.EmitBinaryData(SRef); OS.AddComment("Name"); - emitNullTerminatedSymbolName(OS, DIGV->getDisplayName()); + // Get fully qualified name if global is a static data member. + std::string Name = DIGV->getDisplayName(); + if (const auto *MemberDecl = dyn_cast_or_null( + DIGV->getRawStaticDataMemberDeclaration())) + Name = getFullyQualifiedName(MemberDecl->getScope(), Name); + emitNullTerminatedSymbolName(OS, Name); endSymbolRecord(SConstantEnd); } } diff --git a/llvm/test/DebugInfo/COFF/global-constants.ll b/llvm/test/DebugInfo/COFF/global-constants.ll index b906886d0fd4..502870da3760 100644 --- a/llvm/test/DebugInfo/COFF/global-constants.ll +++ b/llvm/test/DebugInfo/COFF/global-constants.ll @@ -3,18 +3,25 @@ ; C++ source to regenerate: ; const int Test1 = 1; +; struct Foo { static const int Test2 = 2; }; ; int main() { -; return Test1; +; return Test1 + Foo::Test2; ; } ; $ clang t.cpp -S -emit-llvm -g -gcodeview -o t.ll -; ASM-LABEL: .long 241 # Symbol subsection for globals +; ASM-LABEL: .long 241 # Symbol subsection for globals -; ASM: .short {{.*-.*}} # Record length -; ASM: .short 4359 # Record kind: S_CONSTANT -; ASM-NEXT: .long 4099 # Type -; ASM-NEXT: .byte 0x01, 0x00 # Value -; ASM-NEXT: .asciz "Test1" # Name +; ASM: .short {{.*-.*}} # Record length +; ASM: .short 4359 # Record kind: S_CONSTANT +; ASM-NEXT: .long 4099 # Type +; ASM-NEXT: .byte 0x01, 0x00 # Value +; ASM-NEXT: .asciz "Test1" # Name + +; ASM: .short {{.*-.*}} # Record length +; ASM: .short 4359 # Record kind: S_CONSTANT +; ASM: .long 4099 # Type +; ASM: .byte 0x02, 0x00 # Value +; ASM: .asciz "Foo::Test2" # Name ; OBJ: CodeViewDebugInfo [ ; OBJ: Section: .debug$S @@ -27,6 +34,12 @@ ; OBJ-NEXT: Value: 1 ; OBJ-NEXT: Name: Test1 ; OBJ-NEXT: } +; OBJ: ConstantSym { +; OBJ-NEXT: Kind: S_CONSTANT (0x1107) +; OBJ-NEXT: Type: const int (0x1003) +; OBJ-NEXT: Value: 2 +; OBJ-NEXT: Name: Foo::Test2 +; OBJ-NEXT: } ; ModuleID = 't.cpp' source_filename = "t.cpp" @@ -34,31 +47,39 @@ target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-pc-windows-msvc" ; Function Attrs: noinline norecurse nounwind optnone -define dso_local i32 @main() #0 !dbg !13 { +define dso_local i32 @main() #0 !dbg !19 { entry: %retval = alloca i32, align 4 store i32 0, i32* %retval, align 4 - ret i32 1, !dbg !16 + ret i32 3, !dbg !22 } -!llvm.dbg.cu = !{!0} -!llvm.module.flags = !{!9, !10, !11} -!llvm.ident = !{!12} +attributes #0 = { noinline norecurse nounwind optnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } -!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4a1902b6739e3087a03c0ac7ab85b640764e9335)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3, nameTableKind: None) -!1 = !DIFile(filename: "", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "0d5ef00bdd80bdb409a3deac9938f20d") +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!15, !16, !17} +!llvm.ident = !{!18} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git 2b66a49044196d8b90d95d7d3b5246ccbe3abc05)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, globals: !10, nameTableKind: None) +!1 = !DIFile(filename: "", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "77cff5e1c7b260440ed03b23c18809c3") !2 = !{} !3 = !{!4} -!4 = !DIGlobalVariableExpression(var: !5, expr: !DIExpression(DW_OP_constu, 1, DW_OP_stack_value)) -!5 = distinct !DIGlobalVariable(name: "Test1", scope: !0, file: !6, line: 1, type: !7, isLocal: true, isDefinition: true) -!6 = !DIFile(filename: "t.cpp", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "0d5ef00bdd80bdb409a3deac9938f20d") -!7 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !8) -!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) -!9 = !{i32 2, !"CodeView", i32 1} -!10 = !{i32 2, !"Debug Info Version", i32 3} -!11 = !{i32 1, !"wchar_size", i32 2} -!12 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git 4a1902b6739e3087a03c0ac7ab85b640764e9335)"} -!13 = distinct !DISubprogram(name: "main", scope: !6, file: !6, line: 3, type: !14, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) -!14 = !DISubroutineType(types: !15) -!15 = !{!8} -!16 = !DILocation(line: 4, scope: !13) +!4 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", file: !5, line: 3, size: 8, flags: DIFlagTypePassByValue, elements: !6, identifier: ".?AUFoo@@") +!5 = !DIFile(filename: "t.cpp", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "77cff5e1c7b260440ed03b23c18809c3") +!6 = !{!7} +!7 = !DIDerivedType(tag: DW_TAG_member, name: "Test2", scope: !4, file: !5, line: 4, baseType: !8, flags: DIFlagStaticMember, extraData: i32 2) +!8 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !9) +!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!10 = !{!11, !13} +!11 = !DIGlobalVariableExpression(var: !12, expr: !DIExpression(DW_OP_constu, 1, DW_OP_stack_value)) +!12 = distinct !DIGlobalVariable(name: "Test1", scope: null, file: !5, line: 1, type: !8, isLocal: true, isDefinition: true) +!13 = !DIGlobalVariableExpression(var: !14, expr: !DIExpression(DW_OP_constu, 2, DW_OP_stack_value)) +!14 = distinct !DIGlobalVariable(name: "Test2", scope: !0, file: !5, line: 4, type: !8, isLocal: true, isDefinition: true, declaration: !7) +!15 = !{i32 2, !"CodeView", i32 1} +!16 = !{i32 2, !"Debug Info Version", i32 3} +!17 = !{i32 1, !"wchar_size", i32 2} +!18 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git 2b66a49044196d8b90d95d7d3b5246ccbe3abc05)"} +!19 = distinct !DISubprogram(name: "main", scope: !5, file: !5, line: 7, type: !20, scopeLine: 7, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!20 = !DISubroutineType(types: !21) +!21 = !{!9} +!22 = !DILocation(line: 8, scope: !19)