CheckNSError.m revision e1cea75e70d76f55157749a7bcad319050492945
1// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-new-cast -analyzer-constraints=basic -verify %s &&
3// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
4// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s &&
5// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-new-cast -analyzer-constraints=range -verify %s &&
6// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
7
8
9typedef signed char BOOL;
10typedef int NSInteger;
11typedef struct _NSZone NSZone;
12@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
13@protocol NSObject  - (BOOL)isEqual:(id)object; @end
14@protocol NSCopying  - (id)copyWithZone:(NSZone *)zone; @end
15@protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder; @end
16@interface NSObject <NSObject> {} @end
17@class NSDictionary;
18@interface NSError : NSObject <NSCopying, NSCoding> {}
19+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict;
20@end
21extern NSString * const NSXMLParserErrorDomain ;
22
23@interface A
24- (void)myMethodWhichMayFail:(NSError **)error;
25- (BOOL)myMethodWhichMayFail2:(NSError **)error;
26@end
27
28@implementation A
29- (void)myMethodWhichMayFail:(NSError **)error {   // expected-warning {{Method accepting NSError** should have a non-void return value to indicate whether or not an error occured.}}
30  *error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // expected-warning {{Potential null dereference.}}
31}
32
33- (BOOL)myMethodWhichMayFail2:(NSError **)error {  // no-warning
34  if (error) *error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // no-warning
35  return 0;
36}
37@end
38
39struct __CFError {};
40typedef struct __CFError* CFErrorRef;
41
42void foo(CFErrorRef* error) { // expected-warning {{Function accepting CFErrorRef* should have a non-void return value to indicate whether or not an error occured.}}
43  *error = 0;  // expected-warning {{Potential null dereference.}}
44}
45
46int f1(CFErrorRef* error) {
47  if (error) *error = 0; // no-warning
48  return 0;
49}
50
51int f2(CFErrorRef* error) {
52  if (0 != error) *error = 0; // no-warning
53  return 0;
54}
55
56int f3(CFErrorRef* error) {
57  if (error != 0) *error = 0; // no-warning
58  return 0;
59}
60
61
62