1; RUN: llc < %s -filetype=obj | llvm-readobj - --codeview | FileCheck %s 2; RUN: llc --try-experimental-debuginfo-iterators < %s -filetype=obj | llvm-readobj - --codeview | FileCheck %s 3; 4; This test verifies global variables are emitted within the correct scope. 5; 6; -- global_visibility.cpp ---------------------------------------------------- 7; int global_int = 0; 8; 9; template <typename T> struct A { 10; static T comdat_int; 11; static T set(T value) { 12; T r = comdat_int; 13; comdat_int = value; 14; return r; 15; }; 16; }; 17; 18; template <typename T> T A<T>::comdat_int = 42; 19; 20; void foo() { 21; static int local_int = 1; 22; { 23; static int nested_int = 2; 24; local_int = nested_int; 25; } 26; local_int = A<int>::set(42); 27; } 28; 29; void bar() { 30; static int local_int = 3; 31; { 32; static int nested_int = 4; 33; local_int = nested_int; 34; } 35; local_int = A<unsigned>::set(42); 36; } 37; ----------------------------------------------------------------------------- 38; 39; $ clang -S -emit-llvm -g -gcodeview global_visibility.cpp 40; 41; NOTE: The scope for both DIGlobalVariable's named "nested_int" should refer 42; to the appropriate DILexicalBlock, not a DISubprogram. 43; 44 45; CHECK: CodeViewDebugInfo [ 46; CHECK: Section: .debug$S (9) 47 48; CHECK: Subsection [ 49; CHECK: SubSectionType: Symbols (0xF1) 50; CHECK: GlobalProcIdSym { 51; CHECK: Kind: S_GPROC32_ID (0x1147) 52; CHECK: DisplayName: foo 53; CHECK: LinkageName: ?foo@@YAXXZ 54; CHECK: } 55; CHECK: DataSym { 56; CHECK: Kind: S_LDATA32 (0x110C) 57; CHECK: DisplayName: local_int 58; CHECK: LinkageName: ?local_int@?1??foo@@YAXXZ@4HA 59; CHECK: } 60; CHECK: DataSym { 61; CHECK: Kind: S_LDATA32 (0x110C) 62; CHECK: DisplayName: nested_int 63; CHECK: LinkageName: ?nested_int@?1??foo@@YAXXZ@4HA 64; CHECK: } 65; CHECK: ProcEnd { 66; CHECK: Kind: S_PROC_ID_END (0x114F) 67; CHECK: } 68; CHECK: ] 69; CHECK: Subsection [ 70; CHECK: SubSectionType: Symbols (0xF1) 71; CHECK: GlobalProcIdSym { 72; CHECK: Kind: S_GPROC32_ID (0x1147) 73; CHECK: DisplayName: bar 74; CHECK: LinkageName: ?bar@@YAXXZ 75; CHECK: } 76; CHECK: DataSym { 77; CHECK: Kind: S_LDATA32 (0x110C) 78; CHECK: DisplayName: local_int 79; CHECK: LinkageName: ?local_int@?1??bar@@YAXXZ@4HA 80; CHECK: } 81; CHECK: DataSym { 82; CHECK: Kind: S_LDATA32 (0x110C) 83; CHECK: DisplayName: nested_int 84; CHECK: LinkageName: ?nested_int@?1??bar@@YAXXZ@4HA 85; CHECK: } 86; CHECK: ProcEnd { 87; CHECK: Kind: S_PROC_ID_END (0x114F) 88; CHECK: } 89; CHECK: ] 90; CHECK: Subsection [ 91; CHECK: SubSectionType: Symbols (0xF1) 92; CHECK: GlobalData { 93; CHECK: Kind: S_GDATA32 (0x110D) 94; CHECK: DisplayName: global_int 95; CHECK: LinkageName: ?global_int@@3HA 96; CHECK: } 97; CHECK: ] 98; CHECK: ] 99; CHECK: CodeViewDebugInfo [ 100; CHECK: Section: .debug$S (16) 101; CHECK: Subsection [ 102; CHECK: SubSectionType: Symbols (0xF1) 103; CHECK: GlobalData { 104; CHECK: Kind: S_GDATA32 (0x110D) 105; CHECK: DisplayName: A<int>::comdat_int 106; CHECK: LinkageName: ?comdat_int@?$A@H@@2HA 107; CHECK: } 108; CHECK: ] 109; CHECK: ] 110; CHECK: CodeViewDebugInfo [ 111; CHECK: Section: .debug$S (17) 112; CHECK: Subsection [ 113; CHECK: SubSectionType: Symbols (0xF1) 114; CHECK: GlobalData { 115; CHECK: Kind: S_GDATA32 (0x110D) 116; CHECK: DisplayName: A<unsigned int>::comdat_int 117; CHECK: LinkageName: ?comdat_int@?$A@I@@2IA 118; CHECK: } 119; CHECK: ] 120; CHECK: ] 121; 122 123; ModuleID = 'a.cpp' 124source_filename = "a.cpp" 125target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" 126target triple = "x86_64-pc-windows-msvc19.25.28614" 127 128$"?set@?$A@H@@SAHH@Z" = comdat any 129 130$"?set@?$A@I@@SAII@Z" = comdat any 131 132$"?comdat_int@?$A@H@@2HA" = comdat any 133 134$"?comdat_int@?$A@I@@2IA" = comdat any 135 136@"?global_int@@3HA" = dso_local global i32 0, align 4, !dbg !0 137@"?local_int@?1??foo@@YAXXZ@4HA" = internal global i32 1, align 4, !dbg !6 138@"?nested_int@?1??foo@@YAXXZ@4HA" = internal global i32 2, align 4, !dbg !12 139@"?local_int@?1??bar@@YAXXZ@4HA" = internal global i32 3, align 4, !dbg !14 140@"?nested_int@?1??bar@@YAXXZ@4HA" = internal global i32 4, align 4, !dbg !17 141@"?comdat_int@?$A@H@@2HA" = linkonce_odr dso_local global i32 42, comdat, align 4, !dbg !19 142@"?comdat_int@?$A@I@@2IA" = linkonce_odr dso_local global i32 42, comdat, align 4, !dbg !29 143 144; Function Attrs: noinline optnone uwtable 145define dso_local void @"?foo@@YAXXZ"() #0 !dbg !8 { 146entry: 147 %0 = load i32, ptr @"?nested_int@?1??foo@@YAXXZ@4HA", align 4, !dbg !45 148 store i32 %0, ptr @"?local_int@?1??foo@@YAXXZ@4HA", align 4, !dbg !45 149 %call = call i32 @"?set@?$A@H@@SAHH@Z"(i32 42), !dbg !47 150 store i32 %call, ptr @"?local_int@?1??foo@@YAXXZ@4HA", align 4, !dbg !47 151 ret void, !dbg !48 152} 153 154; Function Attrs: noinline nounwind optnone uwtable 155define linkonce_odr dso_local i32 @"?set@?$A@H@@SAHH@Z"(i32 %value) #1 comdat align 2 !dbg !49 { 156entry: 157 %value.addr = alloca i32, align 4 158 %r = alloca i32, align 4 159 store i32 %value, ptr %value.addr, align 4 160 call void @llvm.dbg.declare(metadata ptr %value.addr, metadata !50, metadata !DIExpression()), !dbg !51 161 call void @llvm.dbg.declare(metadata ptr %r, metadata !52, metadata !DIExpression()), !dbg !53 162 %0 = load i32, ptr @"?comdat_int@?$A@H@@2HA", align 4, !dbg !53 163 store i32 %0, ptr %r, align 4, !dbg !53 164 %1 = load i32, ptr %value.addr, align 4, !dbg !54 165 store i32 %1, ptr @"?comdat_int@?$A@H@@2HA", align 4, !dbg !54 166 %2 = load i32, ptr %r, align 4, !dbg !55 167 ret i32 %2, !dbg !55 168} 169 170; Function Attrs: noinline optnone uwtable 171define dso_local void @"?bar@@YAXXZ"() #0 !dbg !16 { 172entry: 173 %0 = load i32, ptr @"?nested_int@?1??bar@@YAXXZ@4HA", align 4, !dbg !56 174 store i32 %0, ptr @"?local_int@?1??bar@@YAXXZ@4HA", align 4, !dbg !56 175 %call = call i32 @"?set@?$A@I@@SAII@Z"(i32 42), !dbg !58 176 store i32 %call, ptr @"?local_int@?1??bar@@YAXXZ@4HA", align 4, !dbg !58 177 ret void, !dbg !59 178} 179 180; Function Attrs: noinline nounwind optnone uwtable 181define linkonce_odr dso_local i32 @"?set@?$A@I@@SAII@Z"(i32 %value) #1 comdat align 2 !dbg !60 { 182entry: 183 %value.addr = alloca i32, align 4 184 %r = alloca i32, align 4 185 store i32 %value, ptr %value.addr, align 4 186 call void @llvm.dbg.declare(metadata ptr %value.addr, metadata !61, metadata !DIExpression()), !dbg !62 187 call void @llvm.dbg.declare(metadata ptr %r, metadata !63, metadata !DIExpression()), !dbg !64 188 %0 = load i32, ptr @"?comdat_int@?$A@I@@2IA", align 4, !dbg !64 189 store i32 %0, ptr %r, align 4, !dbg !64 190 %1 = load i32, ptr %value.addr, align 4, !dbg !65 191 store i32 %1, ptr @"?comdat_int@?$A@I@@2IA", align 4, !dbg !65 192 %2 = load i32, ptr %r, align 4, !dbg !66 193 ret i32 %2, !dbg !66 194} 195 196; Function Attrs: nounwind readnone speculatable willreturn 197declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 198 199attributes #0 = { noinline optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "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-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 200attributes #1 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "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-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 201attributes #2 = { nounwind readnone speculatable willreturn } 202 203!llvm.dbg.cu = !{!2} 204!llvm.module.flags = !{!40, !41, !42, !43} 205!llvm.ident = !{!44} 206 207!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) 208!1 = distinct !DIGlobalVariable(name: "global_int", linkageName: "?global_int@@3HA", scope: !2, file: !3, line: 1, type: !11, isLocal: false, isDefinition: true) 209!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version 11.0.0 (https://github.com/llvm/llvm-project.git 202f144bffd0be254a829924195e1b8ebabcbb79)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, nameTableKind: None) 210!3 = !DIFile(filename: "a.cpp", directory: "F:\\llvm-project\\__test", checksumkind: CSK_MD5, checksum: "66a5399777dc9d37656fb00438bca542") 211!4 = !{} 212!5 = !{!0, !6, !12, !14, !17, !19, !29} 213!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression()) 214!7 = distinct !DIGlobalVariable(name: "local_int", scope: !8, file: !3, line: 15, type: !11, isLocal: true, isDefinition: true) 215!8 = distinct !DISubprogram(name: "foo", linkageName: "?foo@@YAXXZ", scope: !3, file: !3, line: 14, type: !9, scopeLine: 14, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !4) 216!9 = !DISubroutineType(types: !10) 217!10 = !{null} 218!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 219!12 = !DIGlobalVariableExpression(var: !13, expr: !DIExpression()) 220!13 = distinct !DIGlobalVariable(name: "nested_int", scope: !8, file: !3, line: 17, type: !11, isLocal: true, isDefinition: true) 221!14 = !DIGlobalVariableExpression(var: !15, expr: !DIExpression()) 222!15 = distinct !DIGlobalVariable(name: "local_int", scope: !16, file: !3, line: 24, type: !11, isLocal: true, isDefinition: true) 223!16 = distinct !DISubprogram(name: "bar", linkageName: "?bar@@YAXXZ", scope: !3, file: !3, line: 23, type: !9, scopeLine: 23, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !4) 224!17 = !DIGlobalVariableExpression(var: !18, expr: !DIExpression()) 225!18 = distinct !DIGlobalVariable(name: "nested_int", scope: !16, file: !3, line: 26, type: !11, isLocal: true, isDefinition: true) 226!19 = !DIGlobalVariableExpression(var: !20, expr: !DIExpression()) 227!20 = distinct !DIGlobalVariable(name: "comdat_int", linkageName: "?comdat_int@?$A@H@@2HA", scope: !2, file: !3, line: 12, type: !11, isLocal: false, isDefinition: true, declaration: !21) 228!21 = !DIDerivedType(tag: DW_TAG_member, name: "comdat_int", scope: !22, file: !3, line: 4, baseType: !11, flags: DIFlagStaticMember) 229!22 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "A<int>", file: !3, line: 3, size: 8, flags: DIFlagTypePassByValue, elements: !23, templateParams: !27, identifier: ".?AU?$A@H@@") 230!23 = !{!21, !24} 231!24 = !DISubprogram(name: "set", linkageName: "?set@?$A@H@@SAHH@Z", scope: !22, file: !3, line: 5, type: !25, scopeLine: 5, flags: DIFlagPrototyped | DIFlagStaticMember, spFlags: 0) 232!25 = !DISubroutineType(types: !26) 233!26 = !{!11, !11} 234!27 = !{!28} 235!28 = !DITemplateTypeParameter(name: "T", type: !11) 236!29 = !DIGlobalVariableExpression(var: !30, expr: !DIExpression()) 237!30 = distinct !DIGlobalVariable(name: "comdat_int", linkageName: "?comdat_int@?$A@I@@2IA", scope: !2, file: !3, line: 12, type: !31, isLocal: false, isDefinition: true, declaration: !32) 238!31 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) 239!32 = !DIDerivedType(tag: DW_TAG_member, name: "comdat_int", scope: !33, file: !3, line: 4, baseType: !31, flags: DIFlagStaticMember) 240!33 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "A<unsigned int>", file: !3, line: 3, size: 8, flags: DIFlagTypePassByValue, elements: !34, templateParams: !38, identifier: ".?AU?$A@I@@") 241!34 = !{!32, !35} 242!35 = !DISubprogram(name: "set", linkageName: "?set@?$A@I@@SAII@Z", scope: !33, file: !3, line: 5, type: !36, scopeLine: 5, flags: DIFlagPrototyped | DIFlagStaticMember, spFlags: 0) 243!36 = !DISubroutineType(types: !37) 244!37 = !{!31, !31} 245!38 = !{!39} 246!39 = !DITemplateTypeParameter(name: "T", type: !31) 247!40 = !{i32 2, !"CodeView", i32 1} 248!41 = !{i32 2, !"Debug Info Version", i32 3} 249!42 = !{i32 1, !"wchar_size", i32 2} 250!43 = !{i32 7, !"PIC Level", i32 2} 251!44 = !{!"clang version 11.0.0 (https://github.com/llvm/llvm-project.git 202f144bffd0be254a829924195e1b8ebabcbb79)"} 252!45 = !DILocation(line: 18, scope: !46) 253!46 = distinct !DILexicalBlock(scope: !8, file: !3, line: 16) 254!47 = !DILocation(line: 20, scope: !8) 255!48 = !DILocation(line: 21, scope: !8) 256!49 = distinct !DISubprogram(name: "set", linkageName: "?set@?$A@H@@SAHH@Z", scope: !22, file: !3, line: 5, type: !25, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, declaration: !24, retainedNodes: !4) 257!50 = !DILocalVariable(name: "value", arg: 1, scope: !49, file: !3, line: 5, type: !11) 258!51 = !DILocation(line: 5, scope: !49) 259!52 = !DILocalVariable(name: "r", scope: !49, file: !3, line: 6, type: !11) 260!53 = !DILocation(line: 6, scope: !49) 261!54 = !DILocation(line: 7, scope: !49) 262!55 = !DILocation(line: 8, scope: !49) 263!56 = !DILocation(line: 27, scope: !57) 264!57 = distinct !DILexicalBlock(scope: !16, file: !3, line: 25) 265!58 = !DILocation(line: 29, scope: !16) 266!59 = !DILocation(line: 30, scope: !16) 267!60 = distinct !DISubprogram(name: "set", linkageName: "?set@?$A@I@@SAII@Z", scope: !33, file: !3, line: 5, type: !36, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, declaration: !35, retainedNodes: !4) 268!61 = !DILocalVariable(name: "value", arg: 1, scope: !60, file: !3, line: 5, type: !31) 269!62 = !DILocation(line: 5, scope: !60) 270!63 = !DILocalVariable(name: "r", scope: !60, file: !3, line: 6, type: !31) 271!64 = !DILocation(line: 6, scope: !60) 272!65 = !DILocation(line: 7, scope: !60) 273!66 = !DILocation(line: 8, scope: !60) 274