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