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