[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / CodeGenCXX / static-local-in-local-class.cpp
blobdd760b4b529d5119033e606b56dd331757ca2e73
1 // RUN: %clang_cc1 -triple x86_64-linux -fblocks -emit-llvm -o - %s -std=c++1y | FileCheck %s
3 // CHECK: @"_ZZZNK17pr18020_constexpr3$_1clEvENKUlvE_clEvE2l2" =
4 // CHECK: internal global ptr @"_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 ptr blockaddress(@_Z18static_local_labelPv, %{{.*}})
8 // CHECK: @"_ZZNK3$_2clEvE1x" = internal global i32 42
10 namespace pr6769 {
11 struct X {
12 static void f();
15 void X::f() {
16 static int *i;
18 struct Y {
19 static void g() {
20 i = new int();
21 *i = 100;
22 (*i) = (*i) +1;
25 (void)Y::g();
27 (void)i;
31 namespace pr7101 {
32 void foo() {
33 static int n = 0;
34 struct Helper {
35 static void Execute() {
36 n++;
39 Helper::Execute();
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.
49 namespace pr18020_lambda {
50 // Referring to l1 before emitting it used to crash.
51 auto x = []() {
52 static int l1 = 0;
53 return [] { return l1; };
55 int f() { return x()(); }
58 // CHECK-LABEL: define internal noundef i32 @"_ZZNK14pr18020_lambda3$_0clEvENKUlvE_clEv"
59 // CHECK: load i32, ptr @"_ZZNK14pr18020_lambda3$_0clEvE2l1"
61 namespace pr18020_constexpr {
62 // Taking the address of l1 in a constant expression used to crash.
63 auto x = []() {
64 static int l1 = 0;
65 return [] {
66 static int *l2 = &l1;
67 return *l2;
70 int f() { return x()(); }
73 // CHECK-LABEL: define internal noundef i32 @"_ZZNK17pr18020_constexpr3$_1clEvENKUlvE_clEv"
74 // CHECK: load ptr, ptr @"_ZZZNK17pr18020_constexpr3$_1clEvENKUlvE_clEvE2l2"
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 {
79 auto operator()() {
80 static int l1 = 0;
81 struct U {
82 int operator()() { return l1; }
84 return U();
87 static pr18020_class x;
88 int pr18020_f() { return x()(); }
90 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZZN13pr18020_classclEvEN1UclEv
91 // CHECK: load i32, ptr @_ZZN13pr18020_classclEvE2l1
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.
96 static auto deduced_return() {
97 static int n = 42;
98 struct S { int *operator()() { return &n; } };
99 return S();
101 extern "C" int call_deduced_return_operator() {
102 return *decltype(deduced_return())()();
105 // CHECK-LABEL: define{{.*}} i32 @call_deduced_return_operator()
106 // CHECK: call noundef ptr @_ZZL14deduced_returnvEN1SclEv(
107 // CHECK: load i32, ptr %
108 // CHECK: ret i32 %
110 // CHECK-LABEL: define internal noundef ptr @_ZZL14deduced_returnvEN1SclEv(ptr {{[^,]*}} %this)
111 // CHECK: ret ptr @_ZZL14deduced_returnvE1n
113 static auto block_deduced_return() {
114 auto (^b)() = ^() {
115 static int n = 42;
116 struct S { int *operator()() { return &n; } };
117 return S();
119 return b();
121 extern "C" int call_block_deduced_return() {
122 return *decltype(block_deduced_return())()();
125 // CHECK-LABEL: define{{.*}} i32 @call_block_deduced_return()
126 // CHECK: call noundef ptr @_ZZZL20block_deduced_returnvEUb_EN1SclEv(
127 // CHECK: load i32, ptr %
128 // CHECK: ret i32 %
130 // CHECK-LABEL: define internal noundef ptr @_ZZZL20block_deduced_returnvEUb_EN1SclEv(ptr {{[^,]*}} %this) #1 align 2 {
131 // CHECK: ret ptr @_ZZZL20block_deduced_returnvEUb_E1n
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();
142 void *global_label = decltype(static_local_label(0))::get();
144 // CHECK-LABEL: define linkonce_odr noundef ptr @_ZZ18static_local_labelPvEN1S3getEv()
145 // CHECK: %[[lbl:[^ ]*]] = load ptr, ptr @_ZZ18static_local_labelPvE1q
146 // CHECK: ret ptr %[[lbl]]
148 auto global_lambda = []() {
149 static int x = 42;
150 struct S { static int *get() { return &x; } };
151 return S();
153 extern "C" int use_global_lambda() {
154 return *decltype(global_lambda())::get();
156 // CHECK-LABEL: define{{.*}} i32 @use_global_lambda()
157 // CHECK: call noundef ptr @"_ZZNK3$_2clEvEN1S3getEv"()
159 // CHECK-LABEL: define internal noundef ptr @"_ZZNK3$_2clEvEN1S3getEv"()
160 // CHECK: ret ptr @"_ZZNK3$_2clEvE1x"