InlineObjCClassMethod.m 9.71 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 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config ipa=dynamic-bifurcate -verify -analyzer-config eagerly-assume=false %s

void clang_analyzer_checkInlined(int);
void clang_analyzer_eval(int);

// Test inlining of ObjC class methods.

typedef signed char BOOL;
#define YES ((BOOL)1)
#define NO ((BOOL)0)
typedef struct objc_class *Class;
typedef struct objc_object {
  Class isa;
} * id;
@protocol NSObject
- (BOOL)isEqual:(id)object;
@end
@interface NSObject <NSObject> {}
+ (id)alloc;
+ (Class)class;
+ (Class)superclass;
- (id)init;
- (id)autorelease;
- (id)copy;
- (Class)class;
- (instancetype)self;
- (id)retain;
@end

// Vanila: ObjC class method is called by name.
@interface MyParent : NSObject
+ (int)getInt;
@end
@interface MyClass : MyParent
+ (int)getInt;
@end
@implementation MyClass
+ (int)testClassMethodByName {
    int y = [MyClass getInt];
    return 5/y; // expected-warning {{Division by zero}}
}
+ (int)getInt {
  return 0;
}
@end

// The definition is defined by the parent. Make sure we find it and inline.
@interface MyParentDIP : NSObject
+ (int)getInt;
@end
@interface MyClassDIP : MyParentDIP
@end
@implementation MyClassDIP
+ (int)testClassMethodByName {
    int y = [MyClassDIP getInt];
    return 5/y; // expected-warning {{Division by zero}}
}
@end
@implementation MyParentDIP
+ (int)getInt {
    return 0;
}
@end

// ObjC class method is called by name. Definition is in the category.
@interface AAA : NSObject
@end
@interface AAA (MyCat)
+ (int)getInt;
@end
int foo() {
    int y = [AAA getInt];
    return 5/y; // expected-warning {{Division by zero}}
}
@implementation AAA
@end
@implementation AAA (MyCat)
+ (int)getInt {
    return 0;
}
@end

// ObjC class method is called by name. Definition is in the parent category.
@interface PPP : NSObject
@end
@interface PPP (MyCat)
+ (int)getInt;
@end
@interface CCC : PPP
@end
int foo4() {
    int y = [CCC getInt];
    return 5/y; // expected-warning {{Division by zero}}
}
@implementation PPP
@end
@implementation PPP (MyCat)
+ (int)getInt {
    return 0;
}
@end

// There is no declaration in the class but there is one in the parent. Make 
// sure we pick the definition from the class and not the parent.
@interface MyParentTricky : NSObject
+ (int)getInt;
@end
@interface MyClassTricky : MyParentTricky
@end
@implementation MyParentTricky
+ (int)getInt {
    return 0;
}
@end
@implementation MyClassTricky
+ (int)getInt {
  return 1;
}
+ (int)testClassMethodByName {
    int y = [MyClassTricky getInt];
    return 5/y; // no-warning
}
@end

// ObjC class method is called by unknown class declaration (passed in as a 
// parameter). We should not inline in such case.
@interface MyParentUnknown : NSObject
+ (int)getInt;
@end
@interface MyClassUnknown : MyParentUnknown
+ (int)getInt;
@end
@implementation MyClassUnknown
+ (int)testClassVariableByUnknownVarDecl: (Class)cl  {
  int y = [cl getInt];
  return 3/y; // no-warning
}
+ (int)getInt {
  return 0;
}
@end

// ObjC class method call through a decl with a known type.
// Note, [self class] could be a subclass. Do we still want to inline here?
@interface MyClassKT : NSObject
@end
@interface MyClassKT (MyCatKT)
+ (int)getInt;
@end
@implementation MyClassKT (MyCatKT)
+ (int)getInt {
    return 0;
}
@end
@implementation MyClassKT
- (int)testClassMethodByKnownVarDecl {
  Class currentClass = [self class];
  int y = [currentClass getInt];
  return 5 / y; // expected-warning{{Division by zero}}
}
@end

// Another false negative due to us not reasoning about self, which in this 
// case points to the object of the class in the call site and should be equal 
// to [MyParent class].
@interface MyParentSelf : NSObject
+ (int)testSelf;
@end
@implementation MyParentSelf
+ (int)testSelf {
  if (self == [MyParentSelf class])
      return 0;
    else
      return 1;
}
@end
@interface MyClassSelf : MyParentSelf
@end
@implementation MyClassSelf
+ (int)testClassMethodByKnownVarDecl {
  int y = [MyParentSelf testSelf];
  return 5/y; // expected-warning{{Division by zero}}
}
@end
int foo2() {
  int y = [MyParentSelf testSelf];
  return 5/y; // expected-warning{{Division by zero}}
}

// TODO: We do not inline 'getNum' in the following case, where the value of 
// 'self' in call '[self getNum]' is available and evaualtes to 
// 'SelfUsedInParentChild' if it's called from fooA.
// Self region should get created before we call foo and yje call to super 
// should keep it live. 
@interface SelfUsedInParent : NSObject
+ (int)getNum;
+ (int)foo;
@end
@implementation SelfUsedInParent
+ (int)getNum {return 5;}
+ (int)foo {
  int r = [self getNum];
  clang_analyzer_eval(r == 5); // expected-warning{{TRUE}}
  return r;
}
@end
@interface SelfUsedInParentChild : SelfUsedInParent
+ (int)getNum;
+ (int)fooA;
@end
@implementation SelfUsedInParentChild
+ (int)getNum {return 0;}
+ (int)fooA {
  return [super foo];
}
@end
int checkSelfUsedInparentClassMethod() {
    return 5/[SelfUsedInParentChild fooA];
}


@interface Rdar15037033 : NSObject
@end

void rdar15037033() {
  [Rdar15037033 forwardDeclaredMethod]; // expected-warning {{class method '+forwardDeclaredMethod' not found}}
  [Rdar15037033 forwardDeclaredVariadicMethod:1, 2, 3, 0]; // expected-warning {{class method '+forwardDeclaredVariadicMethod:' not found}}
}

@implementation Rdar15037033

+ (void)forwardDeclaredMethod {
  clang_analyzer_checkInlined(1); // expected-warning{{TRUE}}
}

+ (void)forwardDeclaredVariadicMethod:(int)x, ... {
  clang_analyzer_checkInlined(0); // no-warning
}
@end

@interface SelfClassTestParent : NSObject
-(unsigned)returns10;
+(unsigned)returns20;
+(unsigned)returns30;
@end

@interface SelfClassTest : SelfClassTestParent
- (unsigned)returns10;
+ (unsigned)returns20;
+ (unsigned)returns30;
@end

@implementation SelfClassTestParent
- (unsigned)returns10 {
  return 100;
}
+ (unsigned)returns20 {
  return 100;
}
+ (unsigned)returns30 {
  return 100;
}

- (void)testSelfReassignment {
  // Check that we didn't hardcode type for self.
  self = [[[SelfClassTest class] alloc] init];
  Class actuallyChildClass = [self class];
  unsigned result = [actuallyChildClass returns30];
  clang_analyzer_eval(result == 30); // expected-warning{{TRUE}}
}
@end

@implementation SelfClassTest
- (unsigned)returns10 {
  return 10;
}
+ (unsigned)returns20 {
  return 20;
}
+ (unsigned)returns30 {
  return 30;
}
+ (BOOL)isClass {
  return YES;
}
- (BOOL)isClass {
  return NO;
}
+ (SelfClassTest *)create {
  return [[self alloc] init];
}
+ (void)classMethod {
  unsigned result1 = [self returns20];
  clang_analyzer_eval(result1 == 20); // expected-warning{{TRUE}}

  unsigned result2 = [[self class] returns30];
  clang_analyzer_eval(result2 == 30); // expected-warning{{TRUE}}

  unsigned result3 = [[super class] returns30];
  clang_analyzer_eval(result3 == 100); // expected-warning{{TRUE}}

  // Check that class info is propagated with data
  Class class41 = [self class];
  Class class42 = class41;
  unsigned result4 = [class42 returns30];
  clang_analyzer_eval(result4 == 30); // expected-warning{{TRUE}}

  Class class51 = [super class];
  Class class52 = class51;
  unsigned result5 = [class52 returns30];
  clang_analyzer_eval(result5 == 100); // expected-warning{{TRUE}}
}
- (void)instanceMethod {
  unsigned result0 = [self returns10];
  clang_analyzer_eval(result0 == 10); // expected-warning{{TRUE}}

  unsigned result2 = [[self class] returns30];
  clang_analyzer_eval(result2 == 30); // expected-warning{{TRUE}}

  unsigned result3 = [[super class] returns30];
  clang_analyzer_eval(result3 == 100); // expected-warning{{TRUE}}

  // Check that class info is propagated with data
  Class class41 = [self class];
  Class class42 = class41;
  unsigned result4 = [class42 returns30];
  clang_analyzer_eval(result4 == 30); // expected-warning{{TRUE}}

  Class class51 = [super class];
  Class class52 = class51;
  unsigned result5 = [class52 returns30];
  clang_analyzer_eval(result5 == 100); // expected-warning{{TRUE}}

  // Check that we inline class methods when class object is a receiver
  Class class6 = [self class];
  BOOL calledClassMethod = [class6 isClass];
  clang_analyzer_eval(calledClassMethod == YES); // expected-warning{{TRUE}}

  // Check that class info is propagated through the 'self' method
  Class class71 = [self class];
  Class class72 = [class71 self];
  unsigned result7 = [class72 returns30];
  clang_analyzer_eval(result7 == 30); // expected-warning{{TRUE}}

  // Check that 'class' and 'super' info from direct invocation of the
  // corresponding class methods is propagated with data
  Class class8 = [SelfClassTest class];
  unsigned result8 = [class8 returns30];
  clang_analyzer_eval(result8 == 30); // expected-warning{{TRUE}}

  Class class9 = [SelfClassTest superclass];
  unsigned result9 = [class9 returns30];
  clang_analyzer_eval(result9 == 100); // expected-warning{{TRUE}}

  // Check that we get class from a propagated type
  SelfClassTestParent *selfAsParent10 = [[SelfClassTest alloc] init];
  Class class10 = [selfAsParent10 class];
  unsigned result10 = [class10 returns30];
  clang_analyzer_eval(result10 == 30); // expected-warning{{TRUE}}

  SelfClassTestParent *selfAsParent11 = [[[self class] alloc] init];
  Class class11 = [selfAsParent11 class];
  unsigned result11 = [class11 returns30];
  clang_analyzer_eval(result11 == 30); // expected-warning{{TRUE}}
}
@end

@interface Parent : NSObject
+ (int)a;
+ (int)b;
@end
@interface Child : Parent
@end
@interface Other : NSObject
+(void)run;
@end
int main(int argc, const char * argv[]) {
  @autoreleasepool {
    [Other run];
  }
  return 0;
}
@implementation Other
+(void)run {
  int result = [Child a];
  // TODO: This should return 100.
  clang_analyzer_eval(result == 12); // expected-warning{{TRUE}}
}
@end
@implementation Parent
+ (int)a; {
  return [self b];
}
+ (int)b; {
  return 12;
}
@end
@implementation Child
+ (int)b; {
  return 100;
}
@end