reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -Wno-objc-root-class -analyzer-checker=core,deadcode,debug.ExprInspection -analyzer-config inline-lambdas=true -verify %s

int clang_analyzer_eval(int);

@interface Super
- (void)superMethod;
@end

@interface Sub : Super {
  int _ivar1;
  int _ivar2;
}
@end

@implementation Sub
- (void)callMethodOnSuperInCXXLambda; {
  // Explicit capture.
  [self]() {
    [super superMethod];
  }();

  // Implicit capture.
  [=]() {
    [super superMethod];
  }();
}

// Make sure to properly handle super-calls when a block captures
// a local variable named 'self'.
- (void)callMethodOnSuperInCXXLambdaWithRedefinedSelf; {
  /*__weak*/ Sub *weakSelf = self;
  // Implicit capture. (Sema outlaws explicit capture of a redefined self
  // and a call to super [which uses the original self]).
  [=]() {
    Sub *self = weakSelf;
    [=]() {
      [super superMethod];
    }();
  }();
}

- (void)swapIvars {
  int tmp = _ivar1;
  _ivar1 = _ivar2;
  _ivar2 = tmp;
}

- (void)callMethodOnSelfInCXXLambda; {
  _ivar1 = 7;
  _ivar2 = 8;
  [self]() {
    [self swapIvars];
  }();

  clang_analyzer_eval(_ivar1 == 8); // expected-warning{{TRUE}}
  clang_analyzer_eval(_ivar2 == 7); // expected-warning{{TRUE}}
}

@end

int getValue();
void useValue(int v);

void castToBlockNoDeadStore() {
  int v = getValue(); // no-warning

  (void)(void(^)())[v]() { // This capture should count as a use, so no dead store warning above.
  };
}

void takesBlock(void(^block)());

void passToFunctionTakingBlockNoDeadStore() {
  int v = 7; // no-warning
  int x = 8; // no-warning
  takesBlock([&v, x]() {
    (void)v;
  });
}

void castToBlockAndInline() {
  int result = ((int(^)(int))[](int p) {
    return p;
  })(7);

  clang_analyzer_eval(result == 7); // expected-warning{{TRUE}}
}

void castToBlockWithCaptureAndInline() {
  int y = 7;

  auto lambda = [y]{ return y; };
  int(^block)() = lambda;

  int result = block();
  clang_analyzer_eval(result == 7); // expected-warning{{TRUE}}
}

void castMutableLambdaToBlock() {
  int x = 0;

  auto lambda = [x]() mutable {
    x = x + 1;
    return x;
   };

  // The block should copy the lambda before capturing.
  int(^block)() = lambda;

  int r1 = block();
  clang_analyzer_eval(r1 == 1); // expected-warning{{TRUE}}

  int r2 = block();
  clang_analyzer_eval(r2 == 2); // expected-warning{{TRUE}}

  // Because block copied the lambda, r3 should be 1.
  int r3 = lambda();
  clang_analyzer_eval(r3 == 1); // expected-warning{{TRUE}}

  // Aliasing the block shouldn't copy the lambda.
  int(^blockAlias)() = block;

  int r4 = blockAlias();
  clang_analyzer_eval(r4 == 3); // expected-warning{{TRUE}}

  int r5 = block();
  clang_analyzer_eval(r5 == 4); // expected-warning{{TRUE}}

  // Another copy of lambda
  int(^blockSecondCopy)() = lambda;
  int r6 = blockSecondCopy();
  clang_analyzer_eval(r6 == 2); // expected-warning{{TRUE}}
}

void castLambdaInLocalBlock() {
  // Make sure we don't emit a spurious diagnostic about the address of a block
  // escaping in the implicit conversion operator method for lambda-to-block
  // conversions.
  auto lambda = []{ }; // no-warning

  void(^block)() = lambda;
  (void)block;
}