blob: 0af916654f46aaee6c818581ed443a881353c0fb [file] [log] [blame]
Devin Coughlinb6029b72015-11-25 22:35:37 +00001// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fblocks -Wno-objc-root-class -analyze -analyzer-checker=core,deadcode,debug.ExprInspection -analyzer-config inline-lambdas=true -verify %s
Devin Coughlin1d405832015-11-15 17:48:22 +00002
3int clang_analyzer_eval(int);
4
5@interface Super
6- (void)superMethod;
7@end
8
9@interface Sub : Super {
10 int _ivar1;
11 int _ivar2;
12}
13@end
14
15
16@implementation Sub
17- (void)callMethodOnSuperInCXXLambda; {
18 // Explicit capture.
19 [self]() {
20 [super superMethod];
21 }();
22
23 // Implicit capture.
24 [=]() {
25 [super superMethod];
26 }();
27}
28
29- (void)swapIvars {
30 int tmp = _ivar1;
31 _ivar1 = _ivar2;
32 _ivar2 = tmp;
33}
34
35- (void)callMethodOnSelfInCXXLambda; {
36 _ivar1 = 7;
37 _ivar2 = 8;
38 [self]() {
39 [self swapIvars];
40 }();
41
42 clang_analyzer_eval(_ivar1 == 8); // expected-warning{{TRUE}}
43 clang_analyzer_eval(_ivar2 == 7); // expected-warning{{TRUE}}
44}
45
46@end
Devin Coughlinb6029b72015-11-25 22:35:37 +000047
48int getValue();
49void useValue(int v);
50
51void castToBlockNoDeadStore() {
52 int v = getValue(); // no-warning
53
54 (void)(void(^)())[v]() { // This capture should count as a use, so no dead store warning above.
55 };
56}
57
58void takesBlock(void(^block)());
59
60void passToFunctionTakingBlockNoDeadStore() {
61 int v = 7; // no-warning
62 int x = 8; // no-warning
63 takesBlock([&v, x]() {
64 (void)v;
65 });
66}
67
68void castToBlockAndInline() {
69 int result = ((int(^)(int))[](int p) {
70 return p;
71 })(7);
72
73 // FIXME: This should be TRUE. We're not handling lambda to block conversions
74 // properly in ExprEngine::VisitBlockExpr.
75 clang_analyzer_eval(result == 7); // expected-warning{{UNKNOWN}}
76}
77
78void castLambdaInLocalBlock() {
Devin Coughlindfde6552015-12-03 19:41:24 +000079 // Make sure we don't emit a spurious diagnostic about the address of a block
80 // escaping in the implicit conversion operator method for lambda-to-block
81 // conversions.
82 auto lambda = []{ }; // no-warning
Devin Coughlinb6029b72015-11-25 22:35:37 +000083
84 void(^block)() = lambda;
85 (void)block;
86}