blob: 20a14f95c21295aa99cf7a6b5f5f0d3cfe83aa9e [file] [log] [blame]
Ted Kremenek3092dd62009-02-17 19:29:07 +00001// 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
Ted Kremenek2dabd422009-01-22 18:53:15 +00005
Ted Kremenekf684d562009-03-05 18:08:28 +00006typedef 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;
Ted Kremenek9f67ede2008-10-01 05:05:46 +000027
28// Reduced test case from crash in <rdar://problem/6253157>
Ted Kremenek9f67ede2008-10-01 05:05:46 +000029@interface A @end
30@implementation A
31- (void)foo:(void (^)(NSObject *x))block {
32 if (!((block != ((void *)0)))) {}
33}
34@end
35
Ted Kremenek6dfe2f52008-10-18 22:20:20 +000036// 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); }
Ted Kremenek9253b0f2008-10-20 23:14:31 +000040
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
Ted Kremenekc13b6e22008-10-20 23:40:25 +000056void 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
Ted Kremenek76dba7b2008-11-13 05:05:34 +000070// InitListExpr processing
71
72typedef float __m128 __attribute__((__vector_size__(16), __may_alias__));
73__m128 return128() {
Ted Kremenek062e2f92008-11-13 06:10:40 +000074 // This compound literal has a Vector type. We currently just
75 // return UnknownVal.
Ted Kremenek76dba7b2008-11-13 05:05:34 +000076 return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f };
77}
78
Ted Kremenek062e2f92008-11-13 06:10:40 +000079typedef 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
Ted Kremenek8322d6a2008-12-09 00:14:48 +000087// Zero-sized VLAs.
88void check_zero_sized_VLA(int x) {
89 if (x)
90 return;
91
Ted Kremenek159d2482008-12-09 00:44:16 +000092 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.}}
Ted Kremenek8322d6a2008-12-09 00:14:48 +000098}
Ted Kremenek062e2f92008-11-13 06:10:40 +000099
Ted Kremenek55f7bcb2008-12-15 18:51:00 +0000100// 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
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000125// 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
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000133// PR 3543 (handle empty statement expressions)
134int pr_3543(void) {
135 ({});
136}
137
Ted Kremenek265a3052009-02-24 02:23:11 +0000138// <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
Ted Kremenekf684d562009-03-05 18:08:28 +0000148// 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
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000167