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
35 static void 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.
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.
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
{
82 int operator()() { return l1
; }
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() {
98 struct S
{ int *operator()() { return &n
; } };
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 %
110 // CHECK-LABEL: define internal noundef ptr @_ZZL14deduced_returnvEN1SclEv(ptr {{[^,]*}} %this)
111 // CHECK: ret ptr @_ZZL14deduced_returnvE1n
113 static auto block_deduced_return() {
116 struct S
{ int *operator()() { return &n
; } };
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 %
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
) {
136 static void *q
= &&label
;
137 struct S
{ static void *get() { return q
; } };
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
= []() {
150 struct S
{ static int *get() { return &x
; } };
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"