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 @@ -4240,7 +4240,7 @@ llvm::DIDerivedType * CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) { - if (!D->isStaticDataMember()) + if (!D || !D->isStaticDataMember()) return nullptr; auto MI = StaticDataMemberCache.find(D->getCanonicalDecl()); @@ -4353,12 +4353,14 @@ StringRef Name = VD->getName(); llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit); - // Do not use global variables for enums. + // Do not use global variables for enums, unless for CodeView. if (const auto *ECD = dyn_cast(VD)) { const auto *ED = cast(ECD->getDeclContext()); assert(isa(ED->getTypeForDecl()) && "Enum without EnumType?"); (void)ED; - return; + + if (!CGM.getCodeGenOpts().EmitCodeView) + return; } llvm::DIScope *DContext = nullptr; @@ -4369,8 +4371,8 @@ // Emit definition for static members in CodeView. VD = cast(VD->getCanonicalDecl()); - auto *VarD = cast(VD); - if (VarD->isStaticDataMember()) { + auto *VarD = dyn_cast(VD); + if (VarD && VarD->isStaticDataMember()) { auto *RD = cast(VarD->getDeclContext()); getDeclContextDescriptor(VarD); // Ensure that the type is retained even though it's otherwise unreferenced. diff --git a/clang/test/CodeGenCXX/debug-info-enum.cpp b/clang/test/CodeGenCXX/debug-info-enum.cpp --- a/clang/test/CodeGenCXX/debug-info-enum.cpp +++ b/clang/test/CodeGenCXX/debug-info-enum.cpp @@ -1,9 +1,15 @@ // RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm -gcodeview -debug-info-kind=limited %s -o - | FileCheck --check-prefix MSVC %s // CHECK: !DICompileUnit( // CHECK-SAME: enums: [[ENUMS:![0-9]*]] // CHECK: [[ENUMS]] = !{[[E1:![0-9]*]], [[E2:![0-9]*]], [[E3:![0-9]*]]} +// In MSVC check that used enum values are emitted as globals. +// MSVC: !DICompileUnit( +// MSVC-SAME: globals: [[GLOBALS:![0-9]*]] +// MSVC: [[GLOBALS]] = !{[[G1:![0-9]*]], [[G2:![0-9]*]]} + namespace test1 { // CHECK: [[E1]] = !DICompositeType(tag: DW_TAG_enumeration_type, name: "e" // CHECK-SAME: scope: [[TEST1:![0-9]*]] @@ -12,6 +18,10 @@ // CHECK: [[TEST1]] = !DINamespace(name: "test1" // CHECK: [[TEST1_ENUMS]] = !{[[TEST1_E:![0-9]*]]} // CHECK: [[TEST1_E]] = !DIEnumerator(name: "E", value: 0, isUnsigned: true) + +// MSVC: [[G1]] = !DIGlobalVariableExpression(var: [[VAR1:![0-9]*]], +// MSVC-SAME: expr: !DIExpression(DW_OP_constu, 0 +// MSVC: [[VAR1]] = distinct !DIGlobalVariable(name: "E" enum e { E }; void foo() { int v = E; @@ -25,6 +35,10 @@ // CHECK-SAME: elements: [[TEST1_ENUMS]] // CHECK-SAME: identifier: "_ZTSN5test21eE" // CHECK: [[TEST2]] = !DINamespace(name: "test2" + +// MSVC: [[G2]] = !DIGlobalVariableExpression(var: [[VAR2:![0-9]*]], +// MSVC-SAME: expr: !DIExpression(DW_OP_constu, 0 +// MSVC: [[VAR2]] = distinct !DIGlobalVariable(name: "E" enum e { E }; bool func(int i) { return i == E; diff --git a/llvm/test/DebugInfo/COFF/global-constants.ll b/llvm/test/DebugInfo/COFF/global-constants.ll --- a/llvm/test/DebugInfo/COFF/global-constants.ll +++ b/llvm/test/DebugInfo/COFF/global-constants.ll @@ -2,26 +2,41 @@ ; RUN: llc < %s -filetype=obj | llvm-readobj - --codeview | FileCheck %s --check-prefix=OBJ ; C++ source to regenerate: -; const int Test1 = 1; -; struct Foo { static const int Test2 = 2; }; -; int main() { -; return Test1 + Foo::Test2; +; const float TestConst1 = 3.14; +; struct S { +; static const int TestConst2 = -10; +; } +; enum TestEnum : int { +; ENUM_A = 2147000000, +; ENUM_B = -2147000000, +; }; +; void useConst(int); +; void foo() { +; useConst(TestConst1); +; useConst(S::TestConst2); +; useConst(ENUM_B); ; } ; $ clang t.cpp -S -emit-llvm -g -gcodeview -o t.ll -; 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: .long 4099 # Type -; ASM: .byte 0x02, 0x00 # Value -; ASM: .asciz "Foo::Test2" # Name +; 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 0x04, 0x80, 0xc3, 0xf5 # Value +; ASM-NEXT: .byte 0x48, 0x40 +; ASM-NEXT: .asciz "TestConst1" # Name +; ASM: .short {{.*-.*}} # Record length +; ASM: .short 4359 # Record kind: S_CONSTANT +; ASM-NEXT: .long 4100 # Type +; ASM-NEXT: .byte 0x61, 0x00 # Value +; ASM-NEXT: .asciz "S::TestConst2" # Name +; ASM: .short {{.*-.*}} # Record length +; ASM: .short 4359 # Record kind: S_CONSTANT +; ASM-NEXT: .long 4102 # Type +; ASM-NEXT: .byte 0x0a, 0x80, 0x40, 0x61 # Value +; ASM-NEXT: .byte 0x07, 0x80, 0xff, 0xff +; ASM-NEXT: .byte 0xff, 0xff +; ASM-NEXT: .asciz "ENUM_B" # Name ; OBJ: CodeViewDebugInfo [ ; OBJ: Section: .debug$S @@ -30,56 +45,77 @@ ; OBJ: SubSectionType: Symbols (0xF1) ; OBJ: ConstantSym { ; OBJ-NEXT: Kind: S_CONSTANT (0x1107) -; OBJ-NEXT: Type: const int (0x1003) -; OBJ-NEXT: Value: 1 -; OBJ-NEXT: Name: Test1 +; OBJ-NEXT: Type: const float (0x1003) +; OBJ-NEXT: Value: 1078523331 +; OBJ-NEXT: Name: TestConst1 ; OBJ-NEXT: } -; OBJ: ConstantSym { +; OBJ-NEXT: ConstantSym { ; OBJ-NEXT: Kind: S_CONSTANT (0x1107) -; OBJ-NEXT: Type: const int (0x1003) -; OBJ-NEXT: Value: 2 -; OBJ-NEXT: Name: Foo::Test2 +; OBJ-NEXT: Type: const char (0x1004) +; OBJ-NEXT: Value: 97 +; OBJ-NEXT: Name: S::TestConst2 ; OBJ-NEXT: } +; OBJ-NEXT: ConstantSym { +; OBJ-NEXT: Kind: S_CONSTANT (0x1107) +; OBJ-NEXT: Type: TestEnum (0x1006) +; OBJ-NEXT: Value: 18446744071562551616 +; OBJ-NEXT: Name: ENUM_B +; OBJ-NEXT: } + ; ModuleID = 't.cpp' source_filename = "t.cpp" target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-pc-windows-msvc" +target triple = "x86_64-w64-windows-gnu" -; Function Attrs: noinline norecurse nounwind optnone -define dso_local i32 @main() #0 !dbg !19 { +; Function Attrs: noinline nounwind optnone +define dso_local void @_Z3foov() #0 !dbg !28 { entry: - %retval = alloca i32, align 4 - store i32 0, i32* %retval, align 4 - ret i32 3, !dbg !22 + call void @_Z8useConsti(i32 3), !dbg !32 + call void @_Z8useConsti(i32 97), !dbg !33 + call void @_Z8useConsti(i32 -2147000000), !dbg !34 + ret void, !dbg !35 } -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" } +declare dso_local void @_Z8useConsti(i32) #1 !llvm.dbg.cu = !{!0} -!llvm.module.flags = !{!15, !16, !17} -!llvm.ident = !{!18} +!llvm.module.flags = !{!24, !25, !26} +!llvm.ident = !{!27} -!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 = 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) +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 9.0.0 (https://github.com/llvm/llvm-project.git dee1891507401f396290b5d9cb5717d6b0755337)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !9, globals: !15, nameTableKind: None) +!1 = !DIFile(filename: "", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "6d700c7d582557a012214ac1f1f8721b") +!2 = !{!3} +!3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "TestEnum", file: !4, line: 5, baseType: !5, size: 32, elements: !6, identifier: "_ZTS8TestEnum") +!4 = !DIFile(filename: "t.cpp", directory: "C:\5Csrc\5Ctest", checksumkind: CSK_MD5, checksum: "6d700c7d582557a012214ac1f1f8721b") +!5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!6 = !{!7, !8} +!7 = !DIEnumerator(name: "ENUM_A", value: 2147000000) +!8 = !DIEnumerator(name: "ENUM_B", value: -2147000000) +!9 = !{!10} +!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S", file: !4, line: 2, size: 8, flags: DIFlagTypePassByValue, elements: !11, identifier: "_ZTS1S") +!11 = !{!12} +!12 = !DIDerivedType(tag: DW_TAG_member, name: "TestConst2", scope: !10, file: !4, line: 3, baseType: !13, flags: DIFlagStaticMember, extraData: i8 97) +!13 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !14) +!14 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!15 = !{!16, !20, !22} +!16 = !DIGlobalVariableExpression(var: !17, expr: !DIExpression(DW_OP_constu, 1078523331, DW_OP_stack_value)) +!17 = distinct !DIGlobalVariable(name: "TestConst1", scope: !0, file: !4, line: 1, type: !18, isLocal: true, isDefinition: true) +!18 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !19) +!19 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float) +!20 = !DIGlobalVariableExpression(var: !21, expr: !DIExpression(DW_OP_constu, 97, DW_OP_stack_value)) +!21 = distinct !DIGlobalVariable(name: "TestConst2", scope: !0, file: !4, line: 3, type: !13, isLocal: true, isDefinition: true, declaration: !12) +!22 = !DIGlobalVariableExpression(var: !23, expr: !DIExpression(DW_OP_constu, 18446744071562551616, DW_OP_stack_value)) +!23 = distinct !DIGlobalVariable(name: "ENUM_B", scope: !0, file: !4, line: 7, type: !3, isLocal: true, isDefinition: true) +!24 = !{i32 2, !"CodeView", i32 1} +!25 = !{i32 2, !"Debug Info Version", i32 3} +!26 = !{i32 1, !"wchar_size", i32 2} +!27 = !{!"clang version 9.0.0 (https://github.com/llvm/llvm-project.git dee1891507401f396290b5d9cb5717d6b0755337)"} +!28 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !4, file: !4, line: 10, type: !29, scopeLine: 10, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !31) +!29 = !DISubroutineType(types: !30) +!30 = !{null} +!31 = !{} +!32 = !DILocation(line: 11, scope: !28) +!33 = !DILocation(line: 12, scope: !28) +!34 = !DILocation(line: 13, scope: !28) +!35 = !DILocation(line: 14, scope: !28)