xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/static-local-in-local-class.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-linux -fblocks -emit-llvm -o - %s -std=c++1y | FileCheck %s
2f4a2713aSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc // CHECK: @"_ZZNK3$_2clEvE1x" = internal global i32 42
4*0a6a1f1dSLionel Sambuc // CHECK: @_ZZ18static_local_labelPvE1q = linkonce_odr global i8* blockaddress(@_Z18static_local_labelPv, %{{.*}})
5*0a6a1f1dSLionel Sambuc // CHECK: @_ZZZL20block_deduced_returnvEUb_E1n = internal global i32 42
6*0a6a1f1dSLionel Sambuc // CHECK: @_ZZL14deduced_returnvE1n = internal global i32 42
7*0a6a1f1dSLionel Sambuc // CHECK: @"_ZZZNK17pr18020_constexpr3$_1clEvENKUlvE_clEvE2l2" =
8*0a6a1f1dSLionel Sambuc // CHECK: internal global i32* @"_ZZNK17pr18020_constexpr3$_1clEvE2l1"
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc namespace pr6769 {
11f4a2713aSLionel Sambuc struct X {
12f4a2713aSLionel Sambuc   static void f();
13f4a2713aSLionel Sambuc };
14f4a2713aSLionel Sambuc 
f()15f4a2713aSLionel Sambuc void X::f() {
16f4a2713aSLionel Sambuc   static int *i;
17f4a2713aSLionel Sambuc   {
18f4a2713aSLionel Sambuc     struct Y {
19f4a2713aSLionel Sambuc       static void g() {
20f4a2713aSLionel Sambuc         i = new int();
21f4a2713aSLionel Sambuc 	*i = 100;
22f4a2713aSLionel Sambuc 	(*i) = (*i) +1;
23f4a2713aSLionel Sambuc       }
24f4a2713aSLionel Sambuc     };
25f4a2713aSLionel Sambuc     (void)Y::g();
26f4a2713aSLionel Sambuc   }
27f4a2713aSLionel Sambuc   (void)i;
28f4a2713aSLionel Sambuc }
29*0a6a1f1dSLionel Sambuc }
30f4a2713aSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc namespace pr7101 {
foo()32f4a2713aSLionel Sambuc void foo() {
33f4a2713aSLionel Sambuc     static int n = 0;
34f4a2713aSLionel Sambuc     struct Helper {
35f4a2713aSLionel Sambuc         static void Execute() {
36f4a2713aSLionel Sambuc             n++;
37f4a2713aSLionel Sambuc         }
38f4a2713aSLionel Sambuc     };
39f4a2713aSLionel Sambuc     Helper::Execute();
40f4a2713aSLionel Sambuc }
41*0a6a1f1dSLionel Sambuc }
42f4a2713aSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc // These tests all break the assumption that the static var decl has to be
44*0a6a1f1dSLionel Sambuc // emitted before use of the var decl.  This happens because we defer emission
45*0a6a1f1dSLionel Sambuc // of variables with internal linkage and no initialization side effects, such
46*0a6a1f1dSLionel Sambuc // as 'x'.  Then we hit operator()() in 'f', and emit the callee before we emit
47*0a6a1f1dSLionel Sambuc // the arguments, so we emit the innermost function first.
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc namespace pr18020_lambda {
50*0a6a1f1dSLionel Sambuc // Referring to l1 before emitting it used to crash.
__anon28cb41ac0102() 51*0a6a1f1dSLionel Sambuc auto x = []() {
52*0a6a1f1dSLionel Sambuc   static int l1 = 0;
53*0a6a1f1dSLionel Sambuc   return [] { return l1; };
54*0a6a1f1dSLionel Sambuc };
f()55*0a6a1f1dSLionel Sambuc int f() { return x()(); }
56*0a6a1f1dSLionel Sambuc }
57*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZNK14pr18020_lambda3$_0clEvENKUlvE_clEv"
59*0a6a1f1dSLionel Sambuc // CHECK: load i32* @"_ZZNK14pr18020_lambda3$_0clEvE2l1"
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc namespace pr18020_constexpr {
62*0a6a1f1dSLionel Sambuc // Taking the address of l1 in a constant expression used to crash.
__anon28cb41ac0302() 63*0a6a1f1dSLionel Sambuc auto x = []() {
64*0a6a1f1dSLionel Sambuc   static int l1 = 0;
65*0a6a1f1dSLionel Sambuc   return [] {
66*0a6a1f1dSLionel Sambuc     static int *l2 = &l1;
67*0a6a1f1dSLionel Sambuc     return *l2;
68*0a6a1f1dSLionel Sambuc   };
69*0a6a1f1dSLionel Sambuc };
f()70*0a6a1f1dSLionel Sambuc int f() { return x()(); }
71*0a6a1f1dSLionel Sambuc }
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZZNK17pr18020_constexpr3$_1clEvENKUlvE_clEv"
74*0a6a1f1dSLionel Sambuc // CHECK: load i32** @"_ZZZNK17pr18020_constexpr3$_1clEvENKUlvE_clEvE2l2"
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc // Lambda-less reduction that references l1 before emitting it.  This didn't
77*0a6a1f1dSLionel Sambuc // crash if you put it in a namespace.
78*0a6a1f1dSLionel Sambuc struct pr18020_class {
operator ()pr18020_class79*0a6a1f1dSLionel Sambuc   auto operator()() {
80*0a6a1f1dSLionel Sambuc     static int l1 = 0;
81*0a6a1f1dSLionel Sambuc     struct U {
82*0a6a1f1dSLionel Sambuc       int operator()() { return l1; }
83*0a6a1f1dSLionel Sambuc     };
84*0a6a1f1dSLionel Sambuc     return U();
85*0a6a1f1dSLionel Sambuc   }
86*0a6a1f1dSLionel Sambuc };
87*0a6a1f1dSLionel Sambuc static pr18020_class x;
pr18020_f()88*0a6a1f1dSLionel Sambuc int pr18020_f() { return x()(); }
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZN13pr18020_classclEvEN1UclEv
91*0a6a1f1dSLionel Sambuc // CHECK: load i32* @_ZZN13pr18020_classclEvE2l1
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc // In this test case, the function containing the static local will not be
94*0a6a1f1dSLionel Sambuc // emitted because it is unneeded. However, the operator call of the inner class
95*0a6a1f1dSLionel Sambuc // is called, and the static local is referenced and must be emitted.
deduced_return()96*0a6a1f1dSLionel Sambuc static auto deduced_return() {
97*0a6a1f1dSLionel Sambuc   static int n = 42;
98*0a6a1f1dSLionel Sambuc   struct S { int *operator()() { return &n; } };
99*0a6a1f1dSLionel Sambuc   return S();
100*0a6a1f1dSLionel Sambuc }
call_deduced_return_operator()101*0a6a1f1dSLionel Sambuc extern "C" int call_deduced_return_operator() {
102*0a6a1f1dSLionel Sambuc   return *decltype(deduced_return())()();
103*0a6a1f1dSLionel Sambuc }
104*0a6a1f1dSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define i32 @call_deduced_return_operator()
106*0a6a1f1dSLionel Sambuc // CHECK: call i32* @_ZZL14deduced_returnvEN1SclEv(
107*0a6a1f1dSLionel Sambuc // CHECK: load i32* %
108*0a6a1f1dSLionel Sambuc // CHECK: ret i32 %
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32* @_ZZL14deduced_returnvEN1SclEv(%struct.S* %this)
111*0a6a1f1dSLionel Sambuc // CHECK: ret i32* @_ZZL14deduced_returnvE1n
112*0a6a1f1dSLionel Sambuc 
block_deduced_return()113*0a6a1f1dSLionel Sambuc static auto block_deduced_return() {
114*0a6a1f1dSLionel Sambuc   auto (^b)() = ^() {
115*0a6a1f1dSLionel Sambuc     static int n = 42;
116*0a6a1f1dSLionel Sambuc     struct S { int *operator()() { return &n; } };
117*0a6a1f1dSLionel Sambuc     return S();
118*0a6a1f1dSLionel Sambuc   };
119*0a6a1f1dSLionel Sambuc   return b();
120*0a6a1f1dSLionel Sambuc }
call_block_deduced_return()121*0a6a1f1dSLionel Sambuc extern "C" int call_block_deduced_return() {
122*0a6a1f1dSLionel Sambuc   return *decltype(block_deduced_return())()();
123*0a6a1f1dSLionel Sambuc }
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define i32 @call_block_deduced_return()
126*0a6a1f1dSLionel Sambuc // CHECK: call i32* @_ZZZL20block_deduced_returnvEUb_EN1SclEv(
127*0a6a1f1dSLionel Sambuc // CHECK: load i32* %
128*0a6a1f1dSLionel Sambuc // CHECK: ret i32 %
129*0a6a1f1dSLionel Sambuc 
130*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32* @_ZZZL20block_deduced_returnvEUb_EN1SclEv(%struct.S.6* %this) #0 align 2 {
131*0a6a1f1dSLionel Sambuc // CHECK: ret i32* @_ZZZL20block_deduced_returnvEUb_E1n
132*0a6a1f1dSLionel Sambuc 
static_local_label(void * p)133*0a6a1f1dSLionel Sambuc inline auto static_local_label(void *p) {
134*0a6a1f1dSLionel Sambuc   if (p)
135*0a6a1f1dSLionel Sambuc     goto *p;
136*0a6a1f1dSLionel Sambuc   static void *q = &&label;
137*0a6a1f1dSLionel Sambuc   struct S { static void *get() { return q; } };
138*0a6a1f1dSLionel Sambuc   return S();
139*0a6a1f1dSLionel Sambuc label:
140*0a6a1f1dSLionel Sambuc   __builtin_abort();
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc void *global_label = decltype(static_local_label(0))::get();
143*0a6a1f1dSLionel Sambuc 
144*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define linkonce_odr i8* @_ZZ18static_local_labelPvEN1S3getEv()
145*0a6a1f1dSLionel Sambuc // CHECK: %[[lbl:[^ ]*]] = load i8** @_ZZ18static_local_labelPvE1q
146*0a6a1f1dSLionel Sambuc // CHECK: ret i8* %[[lbl]]
147*0a6a1f1dSLionel Sambuc 
__anon28cb41ac0502() 148*0a6a1f1dSLionel Sambuc auto global_lambda = []() {
149*0a6a1f1dSLionel Sambuc   static int x = 42;
150*0a6a1f1dSLionel Sambuc   struct S { static int *get() { return &x; } };
151*0a6a1f1dSLionel Sambuc   return S();
152*0a6a1f1dSLionel Sambuc };
use_global_lambda()153*0a6a1f1dSLionel Sambuc extern "C" int use_global_lambda() {
154*0a6a1f1dSLionel Sambuc   return *decltype(global_lambda())::get();
155*0a6a1f1dSLionel Sambuc }
156*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define i32 @use_global_lambda()
157*0a6a1f1dSLionel Sambuc // CHECK: call i32* @"_ZZNK3$_2clEvEN1S3getEv"()
158*0a6a1f1dSLionel Sambuc 
159*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32* @"_ZZNK3$_2clEvEN1S3getEv"()
160*0a6a1f1dSLionel Sambuc // CHECK: ret i32* @"_ZZNK3$_2clEvE1x"
161