lambdas-generalized-capture.cpp
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=core,deadcode,debug.ExprInspection -verify %s
int clang_analyzer_eval(int);
void generalizedCapture() {
int v = 7;
auto lambda = [x=v]() {
return x;
};
int result = lambda();
clang_analyzer_eval(result == 7); // expected-warning {{TRUE}}
}
void sideEffectsInGeneralizedCapture() {
int v = 7;
auto lambda = [x=v++]() {
return x;
};
clang_analyzer_eval(v == 8); // expected-warning {{TRUE}}
int r1 = lambda();
int r2 = lambda();
clang_analyzer_eval(r1 == 7); // expected-warning {{TRUE}}
clang_analyzer_eval(r2 == 7); // expected-warning {{TRUE}}
clang_analyzer_eval(v == 8); // expected-warning {{TRUE}}
}
int addOne(int p) {
return p + 1;
}
void inliningInGeneralizedCapture() {
int v = 7;
auto lambda = [x=addOne(v)]() {
return x;
};
int result = lambda();
clang_analyzer_eval(result == 8); // expected-warning {{TRUE}}
}
void caseSplitInGeneralizedCapture(bool p) {
auto lambda = [x=(p ? 1 : 2)]() {
return x;
};
int result = lambda();
clang_analyzer_eval(result == 1); // expected-warning {{FALSE}} expected-warning {{TRUE}}
}