xref: /llvm-project/clang/test/CodeGen/fake-use-lambda.cpp (revision 4424c44c8c4ec8e071f5c5999fba216d36fb92c9)
1*4424c44cSWolfgang Pieb // RUN: %clang_cc1 %s -triple=%itanium_abi_triple -O1 -emit-llvm -fextend-variable-liveness -o - | FileCheck %s
2*4424c44cSWolfgang Pieb // Make sure we don't crash compiling a lambda that is not nested in a function.
3*4424c44cSWolfgang Pieb // We also check that fake uses are properly issued in lambdas.
4*4424c44cSWolfgang Pieb 
5*4424c44cSWolfgang Pieb int glob;
6*4424c44cSWolfgang Pieb 
7*4424c44cSWolfgang Pieb extern int foo();
8*4424c44cSWolfgang Pieb 
9*4424c44cSWolfgang Pieb struct S {
10*4424c44cSWolfgang Pieb   static const int a;
11*4424c44cSWolfgang Pieb };
12*4424c44cSWolfgang Pieb 
13*4424c44cSWolfgang Pieb const int S::a = [](int b) __attribute__((noinline)) {
14*4424c44cSWolfgang Pieb   return b * foo();
15*4424c44cSWolfgang Pieb }
16*4424c44cSWolfgang Pieb (glob);
17*4424c44cSWolfgang Pieb 
18*4424c44cSWolfgang Pieb int func(int param) {
19*4424c44cSWolfgang Pieb   return ([=](int lambdaparm) __attribute__((noinline))->int {
20*4424c44cSWolfgang Pieb     int lambdalocal = lambdaparm * 2;
21*4424c44cSWolfgang Pieb     return lambdalocal;
22*4424c44cSWolfgang Pieb   }(glob));
23*4424c44cSWolfgang Pieb }
24*4424c44cSWolfgang Pieb 
25*4424c44cSWolfgang Pieb // We are looking for the first lambda's call operator, which should contain
26*4424c44cSWolfgang Pieb // 2 fake uses, one for 'b' and one for its 'this' pointer (in that order).
27*4424c44cSWolfgang Pieb // The mangled function name contains a $_0, followed by 'cl'.
28*4424c44cSWolfgang Pieb // This lambda is an orphaned lambda, i.e. one without lexical parent.
29*4424c44cSWolfgang Pieb //
30*4424c44cSWolfgang Pieb // CHECK-LABEL: define internal {{.+\"_Z.+\$_0.*cl.*\"}}
31*4424c44cSWolfgang Pieb // CHECK-NOT:   ret
32*4424c44cSWolfgang Pieb // CHECK:       fake.use(i32
33*4424c44cSWolfgang Pieb // CHECK-NOT:   ret
34*4424c44cSWolfgang Pieb // CHECK:       fake.use(ptr
35*4424c44cSWolfgang Pieb 
36*4424c44cSWolfgang Pieb // The second lambda. We are looking for 3 fake uses.
37*4424c44cSWolfgang Pieb // CHECK-LABEL: define internal {{.+\"_Z.+\$_0.*cl.*\"}}
38*4424c44cSWolfgang Pieb // CHECK-NOT:   ret
39*4424c44cSWolfgang Pieb // CHECK:       fake.use(i32
40*4424c44cSWolfgang Pieb // CHECK-NOT:   ret
41*4424c44cSWolfgang Pieb // CHECK:       fake.use(i32
42*4424c44cSWolfgang Pieb // CHECK-NOT:   ret
43*4424c44cSWolfgang Pieb // CHECK:       fake.use(ptr
44