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
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config ipa=dynamic-bifurcate -verify %s

#include "InlineObjCInstanceMethod.h"

void clang_analyzer_eval(int);

PublicSubClass2 *getObj();

@implementation PublicParent
- (int) getZeroOverridden {
   return 1;
}
- (int) getZero {
   return 0;
}
@end

@implementation PublicSubClass2
- (int) getZeroOverridden {
   return 0;
}

/* Test that we get the right type from call to alloc. */
+ (void) testAllocSelf {
  id a = [self alloc];
  clang_analyzer_eval([a getZeroOverridden] == 0); // expected-warning{{TRUE}}
}


+ (void) testAllocClass {
  id a = [PublicSubClass2 alloc];
  clang_analyzer_eval([a getZeroOverridden] == 0); // expected-warning{{TRUE}}
}

+ (void) testAllocSuperOverriden {
  id a = [super alloc];
  // Evaluates to 1 in the parent.
  clang_analyzer_eval([a getZeroOverridden] == 0); // expected-warning{{FALSE}} 
}

+ (void) testAllocSuper {
  id a = [super alloc];
  clang_analyzer_eval([a getZero] == 0); // expected-warning{{TRUE}}
}

+ (void) testAllocInit {
  id a = [[self alloc] init];
  clang_analyzer_eval([a getZeroOverridden] == 0); // expected-warning{{TRUE}}
}

+ (void) testNewSelf {
  id a = [self new];
  clang_analyzer_eval([a getZeroOverridden] == 0); // expected-warning{{TRUE}}
}

// Casting to parent should not pessimize the dynamic type. 
+ (void) testCastToParent {
 id a = [[self alloc] init];
 PublicParent *p = a;  
  clang_analyzer_eval([p getZeroOverridden] == 0); // expected-warning{{TRUE}}
}

// The type of parameter gets used.
+ (void)testTypeFromParam:(PublicParent*) p {
  clang_analyzer_eval([p getZero] == 0); // expected-warning{{TRUE}}
}

// Test implicit cast.
// Note, in this case, p could also be a subclass of MyParent.
+ (void) testCastFromId:(id) a {
  PublicParent *p = a;  
  clang_analyzer_eval([p getZero] == 0); // expected-warning{{TRUE}}
}
@end

// TODO: Would be nice to handle the case of dynamically obtained class info
// as well. We need a MemRegion for class types for this.
int testDynamicClass(BOOL coin) {
 Class AllocClass = (coin ? [NSObject class] : [PublicSubClass2 class]);
 id x = [[AllocClass alloc] init];
 if (coin)
   return [x getZero];
 return 1;
}

@interface UserClass : NSObject
- (PublicSubClass2 *) _newPublicSubClass2;
- (int) getZero;
- (void) callNew;
@end

@implementation UserClass
- (PublicSubClass2 *) _newPublicSubClass2 {
  return [[PublicSubClass2 alloc] init];
}
- (int) getZero { return 5; }
- (void) callNew {
  PublicSubClass2 *x = [self _newPublicSubClass2];
  clang_analyzer_eval([x getZero] == 0); //expected-warning{{TRUE}}
}
@end