blob: c36ae8f4d639205ea46c7accd48b6208def50fff [file] [log] [blame]
Ted Kremenek48af2a92009-02-25 22:32:02 +00001// RUN: clang -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=range --verify -fblocks %s -analyzer-eagerly-assume
2
Ted Kremenek6ae8a362009-03-13 16:32:54 +00003// Delta-reduced header stuff (needed for test cases).
4typedef signed char BOOL;
5typedef unsigned int NSUInteger;
6typedef struct _NSZone NSZone;
7@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
8@protocol NSObject - (BOOL)isEqual:(id)object;
9- (oneway void)release;
10@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
11@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
12@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
13@end @interface NSObject <NSObject> {}
14+ (id)alloc;
15@end typedef struct {}
16NSFastEnumerationState;
17@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
18@end @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
19@end @interface NSMutableArray : NSArray - (void)addObject:(id)anObject;
20- (BOOL)isEqualToString:(NSString *)aString;
21@end @interface NSAutoreleasePool : NSObject {}
22- (void)drain;
23- (id)init;
24@end
25
26// This test case tests that (x != 0) is eagerly evaluated before stored to
27// 'y'. This test case complements recoverCastedSymbol (see below) because
28// the symbolic expression is stored to 'y' (which is a short instead of an
29// int). recoverCastedSymbol() only recovers path-sensitivity when the
30// symbolic expression is literally the branch condition.
31//
Ted Kremenek48af2a92009-02-25 22:32:02 +000032void handle_assign_of_condition(int x) {
33 // The cast to 'short' causes us to lose symbolic constraint.
34 short y = (x != 0);
35 char *p = 0;
36 if (y) {
37 // This should be infeasible.
38 if (!(x != 0)) {
39 *p = 1; // no-warning
40 }
41 }
42}
43
Ted Kremenek6ae8a362009-03-13 16:32:54 +000044// From <rdar://problem/6619921>
45//
46// In this test case, 'needsAnArray' is a signed char. The analyzer tracks
47// a symbolic value for this variable, but in the branch condition it is
48// promoted to 'int'. Currently the analyzer doesn't reason well about
49// promotions of symbolic values, so this test case tests the logic in
50// 'recoverCastedSymbol()' (GRExprEngine.cpp) to test that we recover
51// path-sensitivity and use the symbol for 'needsAnArray' in the branch
52// condition.
53//
54void handle_symbolic_cast_in_condition(void) {
55 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
56
57 BOOL needsAnArray = [@"aString" isEqualToString:@"anotherString"];
58 NSMutableArray* array = needsAnArray ? [[NSMutableArray alloc] init] : 0;
59 if(needsAnArray)
60 [array release];
61
62 [pool drain];
63}