misc-ps.m revision 7de20fe9aac00705dd943690563db66fa4b35b5b
1// RUN: clang -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=basic --verify -fblocks %s && 2// RUN: clang -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s && 3// RUN: clang -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=basic --verify -fblocks %s && 4// RUN: clang -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=range --verify -fblocks %s 5 6typedef struct objc_selector *SEL; 7typedef signed char BOOL; 8typedef int NSInteger; 9typedef unsigned int NSUInteger; 10typedef struct _NSZone NSZone; 11@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 12@protocol NSObject - (BOOL)isEqual:(id)object; @end 13@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end 14@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end 15@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end 16@interface NSObject <NSObject> {} - (id)init; @end 17extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); 18@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> 19- (NSUInteger)length; 20+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 21@end extern NSString * const NSBundleDidLoadNotification; 22@interface NSAssertionHandler : NSObject {} 23+ (NSAssertionHandler *)currentHandler; 24- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...; 25@end 26extern NSString * const NSConnectionReplyMode; 27 28// Reduced test case from crash in <rdar://problem/6253157> 29@interface A @end 30@implementation A 31- (void)foo:(void (^)(NSObject *x))block { 32 if (!((block != ((void *)0)))) {} 33} 34@end 35 36// Reduced test case from crash in PR 2796; 37// http://llvm.org/bugs/show_bug.cgi?id=2796 38 39unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } 40 41// Improvement to path-sensitivity involving compound assignments. 42// Addresses false positive in <rdar://problem/6268365> 43// 44 45unsigned r6268365Aux(); 46 47void r6268365() { 48 unsigned x = 0; 49 x &= r6268365Aux(); 50 unsigned j = 0; 51 52 if (x == 0) ++j; 53 if (x == 0) x = x / j; // no-warning 54} 55 56void divzeroassume(unsigned x, unsigned j) { 57 x /= j; 58 if (j == 0) x /= 0; // no-warning 59 if (j == 0) x /= j; // no-warning 60 if (j == 0) x = x / 0; // no-warning 61} 62 63void divzeroassumeB(unsigned x, unsigned j) { 64 x = x / j; 65 if (j == 0) x /= 0; // no-warning 66 if (j == 0) x /= j; // no-warning 67 if (j == 0) x = x / 0; // no-warning 68} 69 70// InitListExpr processing 71 72typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); 73__m128 return128() { 74 // This compound literal has a Vector type. We currently just 75 // return UnknownVal. 76 return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; 77} 78 79typedef long long __v2di __attribute__ ((__vector_size__ (16))); 80typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__)); 81__m128i vec128i(long long __q1, long long __q0) { 82 // This compound literal returns true for both isVectorType() and 83 // isIntegerType(). 84 return __extension__ (__m128i)(__v2di){ __q0, __q1 }; 85} 86 87// Zero-sized VLAs. 88void check_zero_sized_VLA(int x) { 89 if (x) 90 return; 91 92 int vla[x]; // expected-warning{{VLAs with no elements have undefined behavior}} 93} 94 95void check_uninit_sized_VLA() { 96 int x; 97 int vla[x]; // expected-warning{{The expression used to specify the number of elements in the VLA 'vla' evaluates to an undefined or garbage value.}} 98} 99 100// sizeof(void) 101// - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 102void handle_sizeof_void(unsigned flag) { 103 int* p = 0; 104 105 if (flag) { 106 if (sizeof(void) == 1) 107 return; 108 // Infeasible. 109 *p = 1; // no-warning 110 } 111 112 void* q; 113 114 if (!flag) { 115 if (sizeof(*q) == 1) 116 return; 117 // Infeasibe. 118 *p = 1; // no-warning 119 } 120 121 // Infeasible. 122 *p = 1; // no-warning 123} 124 125// PR 3422 126void pr3422_helper(char *p); 127void pr3422() { 128 char buf[100]; 129 char *q = &buf[10]; 130 pr3422_helper(&q[1]); 131} 132 133// PR 3543 (handle empty statement expressions) 134int pr_3543(void) { 135 ({}); 136} 137 138// <rdar://problem/6611677> 139// This test case test the use of a vector type within an array subscript 140// expression. 141typedef long long __a64vector __attribute__((__vector_size__(8))); 142typedef long long __a128vector __attribute__((__vector_size__(16))); 143static inline __a64vector __attribute__((__always_inline__, __nodebug__)) 144my_test_mm_movepi64_pi64(__a128vector a) { 145 return (__a64vector)a[0]; 146} 147 148// Test basic tracking of ivars associated with 'self'. 149@interface SelfIvarTest : NSObject { 150 int flag; 151} 152- (void)test_self_tracking; 153@end 154 155@implementation SelfIvarTest 156- (void)test_self_tracking { 157 char *p = 0; 158 char c; 159 160 if (flag) 161 p = "hello"; 162 163 if (flag) 164 c = *p; // no-warning 165} 166@end 167 168// PR 3770 169char pr3770(int x) { 170 int y = x & 0x2; 171 char *p = 0; 172 if (y == 1) 173 p = "hello"; 174 175 if (y == 1) 176 return p[0]; // no-warning 177 178 return 'a'; 179} 180 181