xref: /llvm-project/clang/test/CodeGenCXX/debug-info-static-member.cpp (revision 4db54e659763401dbf2e5b1f90e9a3391e311e50)
1 // RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,NOT-MS %s
2 // RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 -std=c++98 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,NOT-MS %s
3 // RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-4 -std=c++11 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4,NOT-MS %s
4 // RUN: %clangxx -target x86_64-unknown-unknown -g -gdwarf-5 -std=c++11 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF5 %s
5 // RUN: %clangxx -target x86_64-windows-msvc -g -gdwarf-4 %s -emit-llvm -S -o - | FileCheck --check-prefixes=CHECK,DWARF4 %s
6 // PR14471
7 
8 // CHECK: @{{.*}}a{{.*}} = dso_local global i32 4, align 4, !dbg [[A:![0-9]+]]
9 // CHECK: @{{.*}}b{{.*}} = dso_local global i32 2, align 4, !dbg [[B:![0-9]+]]
10 // CHECK: @{{.*}}c{{.*}} = dso_local global i32 1, align 4, !dbg [[C:![0-9]+]]
11 
12 enum X {
13   Y
14 };
15 class C
16 {
17   static int a;
18   const static bool const_a = true;
19 protected:
20   static int b;
21 #if __cplusplus >= 201103L
22   constexpr static float const_b = 3.14;
23 #else
24   const static float const_b = 3.14;
25 #endif
26 public:
27   static int c;
28   const static int const_c = 18;
29   int d;
30   static X x_a;
31 };
32 
33 // The definition of C::a drives the emission of class C, which is
34 // why the definition of "a" comes before the declarations while
35 // "b" and "c" come after.
36 
37 // CHECK: [[A]] = !DIGlobalVariableExpression(var: [[AV:.*]], expr: !DIExpression())
38 // CHECK: [[AV]] = distinct !DIGlobalVariable(name: "a",
39 // CHECK-SAME:                                declaration: ![[DECL_A:[0-9]+]])
40 //
41 // CHECK: !DICompositeType(tag: DW_TAG_enumeration_type, name: "X"{{.*}})
42 // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "anon_static_decl_struct"
43 // DWARF4: !DIDerivedType(tag: DW_TAG_member, name: "anon_static_decl_var"
44 // DWARF5: !DIDerivedType(tag: DW_TAG_variable, name: "anon_static_decl_var"
45 // CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "static_decl_templ<int>"
46 // CHECK-NOT:              DIFlagFwdDecl
47 // CHECK-SAME:             ){{$}}
48 // DWARF4: !DIDerivedType(tag: DW_TAG_member, name: "static_decl_templ_var"
49 // DWARF5: !DIDerivedType(tag: DW_TAG_variable, name: "static_decl_templ_var"
50 
51 int C::a = 4;
52 // CHECK: [[B]] = !DIGlobalVariableExpression(var: [[BV:.*]], expr: !DIExpression())
53 // CHECK: [[BV]] = distinct !DIGlobalVariable(name: "b",
54 // CHECK-SAME:                                declaration: ![[DECL_B:[0-9]+]])
55 // DWARF4: ![[DECL_B]] = !DIDerivedType(tag: DW_TAG_member, name: "b"
56 // DWARF5: ![[DECL_B]] = !DIDerivedType(tag: DW_TAG_variable, name: "b"
57 // CHECK-NOT:                                 size:
58 // CHECK-NOT:                                 align:
59 // CHECK-NOT:                                 offset:
60 // CHECK-SAME:                                flags: DIFlagProtected | DIFlagStaticMember)
61 //
62 // CHECK: !DICompositeType(tag: DW_TAG_class_type, name: "C"{{.*}})
63 //
64 // DWARF4: ![[DECL_A]] = !DIDerivedType(tag: DW_TAG_member, name: "a"
65 // DWARF5: ![[DECL_A]] = !DIDerivedType(tag: DW_TAG_variable, name: "a"
66 // CHECK-NOT:                                 size:
67 // CHECK-NOT:                                 align:
68 // CHECK-NOT:                                 offset:
69 // CHECK-SAME:                                flags: DIFlagStaticMember)
70 //
71 // DWARF4:    ![[CONST_A_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "const_a"
72 // DWARF5:    ![[CONST_A_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "const_a"
73 // CHECK-NOT:                           size:
74 // CHECK-NOT:                           align:
75 // CHECK-NOT:                           offset:
76 // CHECK-SAME:                          flags: DIFlagStaticMember
77 // CHECK-SAME:                          extraData: i1 true
78 
79 // DWARF4:     ![[CONST_B_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "const_b"
80 // DWARF5:     ![[CONST_B_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "const_b"
81 // CHECK-NOT:                            size:
82 // CHECK-NOT:                            align:
83 // CHECK-NOT:                            offset:
84 // CHECK-SAME:                           flags: DIFlagProtected | DIFlagStaticMember
85 // CHECK-SAME:                           extraData: float
86 
87 // DWARF4: ![[DECL_C:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "c"
88 // DWARF5: ![[DECL_C:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "c"
89 // CHECK-NOT:                                 size:
90 // CHECK-NOT:                                 align:
91 // CHECK-NOT:                                 offset:
92 // CHECK-SAME:                                flags: DIFlagPublic | DIFlagStaticMember)
93 //
94 // DWARF4:     ![[CONST_C_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_member, name: "const_c"
95 // DWARF5:     ![[CONST_C_DECL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_variable, name: "const_c"
96 // CHECK-NOT:                            size:
97 // CHECK-NOT:                            align:
98 // CHECK-NOT:                            offset:
99 // CHECK-SAME:                           flags: DIFlagPublic | DIFlagStaticMember
100 // CHECK-SAME:                           extraData: i32 18
101 //
102 // DWARF4: !DIDerivedType(tag: DW_TAG_member, name: "x_a"
103 // DWARF5: !DIDerivedType(tag: DW_TAG_variable, name: "x_a"
104 // CHECK-SAME:           flags: DIFlagPublic | DIFlagStaticMember)
105 
106 int C::b = 2;
107 // CHECK: [[C]] = !DIGlobalVariableExpression(var: [[CV:.*]], expr: !DIExpression())
108 // CHECK: [[CV]] = distinct !DIGlobalVariable(name: "c", {{.*}} declaration: ![[DECL_C]])
109 int C::c = 1;
110 
main()111 int main()
112 {
113         C instance_C;
114         instance_C.d = 8;
115         return C::c;
116 }
117 
118 // CHECK-NOT: !DIGlobalVariable(name: "anon_static_decl_var"
119 
120 // Test this in an anonymous namespace to ensure the type is retained even when
121 // it doesn't get automatically retained by the string type reference machinery.
122 namespace {
123 struct anon_static_decl_struct {
124   static const int anon_static_decl_var = 117;
125 };
126 }
127 
ref()128 int ref() {
129   return anon_static_decl_struct::anon_static_decl_var;
130 }
131 
132 template<typename T>
133 struct static_decl_templ {
134   static const int static_decl_templ_var = 7;
135 };
136 
137 template<typename T>
138 const int static_decl_templ<T>::static_decl_templ_var;
139 
static_decl_templ_ref()140 int static_decl_templ_ref() {
141   return static_decl_templ<int>::static_decl_templ_var;
142 }
143 
144 // Verify that even when a static member declaration is created lazily when
145 // creating the definition, the declaration line is that of the canonical
146 // declaration, not the definition. Also, since we look at the canonical
147 // definition, we should also correctly emit the constant value (42) into the
148 // debug info.
149 struct V {
150   virtual ~V(); // cause the definition of 'V' to be omitted by no-standalone-debug optimization
151   static const int const_va = 42;
152 };
153 
154 // const_va is not emitted for MS targets.
155 // NOT-MS: !DIDerivedType(tag: DW_TAG_member, name: "const_va",
156 // NOT-MS-SAME:           line: [[@LINE-5]]
157 // NOT-MS-SAME:           extraData: i32 42
158 const int V::const_va;
159 
160 namespace x {
161 struct y {
162 // CHECK: !DIGlobalVariable(name: "z",
163 // CHECK-SAME:              scope: [[NS_X:![0-9]+]]
164 // CHECK: [[NS_X]] = !DINamespace(name: "x"
165   static int z;
166 };
167 int y::z;
168 }
169