[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Modules / lambda-in-variable.cpp
blob303879fa70c2790a06c5dd6b63ec948849596006
1 // RUN: rm -rf %t
2 // RUN: mkdir -p %t
3 // RUN: split-file %s %t
4 //
5 // RUN: %clang_cc1 -std=c++20 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%t/module.modulemap %t/use.cpp -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s
6 // RUN: %clang_cc1 -DDEFINE_LOCALLY -std=c++20 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%t/module.modulemap %t/use.cpp -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s
8 //--- module.modulemap
9 module a { header "a.h" export * }
10 module b { header "b.h" export * }
11 module c { header "c.h" export * }
13 //--- nonmodular.h
14 void not_constant();
16 template<typename T> struct A {
17 template<T M> static inline T N = [] { not_constant(); return M; } ();
20 template<typename T, T M> inline T N = [] { not_constant(); return M; } ();
22 template<typename T, T M> inline auto L = [] {};
24 template<typename T> int Z;
26 // These lambdas should not be merged, despite having the same context decl and
27 // mangling number (but different signatures).
28 inline auto MultipleLambdas = ((void)[](int*) { return 1; }, [] { return 2; });
30 //--- a.h
31 #include "nonmodular.h"
33 //--- b.h
34 #include "a.h"
36 int b1() { return A<int>::N<1>; }
37 int b2() { return N<int, 1>; }
39 inline auto x1 = L<int, 1>;
40 inline auto x2 = L<int, 2>;
42 inline constexpr int *P = &Z<decltype([] { static int n; return &n; }())>;
43 inline constexpr int *xP = P;
45 static_assert(!__is_same(decltype(x1), decltype(x2)));
47 //--- c.h
48 #include "a.h"
50 int c1() { return A<int>::N<2>; }
51 int c2() { return N<int, 2>; }
53 inline auto y2 = L<int, 2>;
54 inline auto y1 = L<int, 1>;
56 inline constexpr int *P = &Z<decltype([] { static int n; return &n; }())>;
57 inline constexpr int *yP = P;
59 //--- use.cpp
60 #ifdef DEFINE_LOCALLY
61 #include "nonmodular.h"
63 inline constexpr int *P = &Z<decltype([] { static int n; return &n; }())>;
64 inline constexpr int *zP = P;
66 auto z0 = L<int, 0>;
67 auto z2 = L<int, 2>;
68 auto z1 = L<int, 1>;
69 #endif
71 #include "b.h"
72 #include "c.h"
74 int b1v = b1();
75 int b2v = b2();
76 int c1v = c1();
77 int c2v = c2();
79 // We should merge together matching lambdas.
80 static_assert(__is_same(decltype(x1), decltype(y1)));
81 static_assert(__is_same(decltype(x2), decltype(y2)));
82 static_assert(!__is_same(decltype(x1), decltype(x2)));
83 static_assert(!__is_same(decltype(y1), decltype(y2)));
84 static_assert(!__is_same(decltype(x1), decltype(y2)));
85 static_assert(!__is_same(decltype(x2), decltype(y1)));
86 static_assert(xP == yP);
87 #ifdef DEFINE_LOCALLY
88 static_assert(!__is_same(decltype(x1), decltype(z0)));
89 static_assert(!__is_same(decltype(x2), decltype(z0)));
90 static_assert(__is_same(decltype(x1), decltype(z1)));
91 static_assert(__is_same(decltype(x2), decltype(z2)));
92 static_assert(xP == zP);
93 #endif
95 static_assert(MultipleLambdas() == 2);
97 // We should not merge the instantiated lambdas from `b.h` and `c.h` together,
98 // even though they will both have anonymous declaration number #1 within
99 // A<int> and within the TU, respectively.
101 // CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_Z1NIiLi1EE) {
102 // CHECK: load i8, ptr @_ZGV1NIiLi1EE, align 8
103 // CHECK: call {{.*}} i32 @_ZNK1NIiLi1EEMUlvE_clEv(
104 // CHECK: store i32 {{.*}}, ptr @_Z1NIiLi1EE
106 // CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_ZN1AIiE1NILi1EEE) {
107 // CHECK: load i8, ptr @_ZGVN1AIiE1NILi1EEE, align 8
108 // CHECK: call {{.*}} i32 @_ZNK1AIiE1NILi1EEMUlvE_clEv(
109 // CHECK: store i32 {{.*}}, ptr @_ZN1AIiE1NILi1EEE
111 // CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_Z1NIiLi2EE) {
112 // CHECK: load i8, ptr @_ZGV1NIiLi2EE, align 8
113 // CHECK: call {{.*}} i32 @_ZNK1NIiLi2EEMUlvE_clEv(
114 // CHECK: store i32 {{.*}}, ptr @_Z1NIiLi2EE
116 // CHECK-LABEL: define {{.*}}global_var_init{{.*}} comdat($_ZN1AIiE1NILi2EEE) {
117 // CHECK: load i8, ptr @_ZGVN1AIiE1NILi2EEE, align 8
118 // CHECK: call {{.*}} i32 @_ZNK1AIiE1NILi2EEMUlvE_clEv(
119 // CHECK: store i32 {{.*}}, ptr @_ZN1AIiE1NILi2EEE