File: ObjCDynTypePopagation.m

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,368 kB
  • sloc: cpp: 5,593,980; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (102 lines) | stat: -rw-r--r-- 2,610 bytes parent folder | download | duplicates (8)
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
// 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