variadic-method-types.m revision f05982b5f8f69a1d618c3bd844ab6efd3a6e2953
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.VariadicMethodTypes -analyzer-store=basic -fblocks -verify %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.VariadicMethodTypes -analyzer-store=region -fblocks -verify %s
3
4//===----------------------------------------------------------------------===//
5// The following code is reduced using delta-debugging from
6// Foundation.h (Mac OS X).
7//
8// It includes the basic definitions for the test cases below.
9// Not directly including Foundation.h directly makes this test case 
10// both svelte and portable to non-Mac platforms.
11//===----------------------------------------------------------------------===//
12
13#define nil (void*)0
14typedef const struct __CFString * CFStringRef;
15extern const CFStringRef kCGImageSourceShouldCache __attribute__((visibility("default")));
16typedef signed char BOOL;
17typedef struct _NSZone NSZone;
18typedef unsigned int NSUInteger;
19@protocol NSObject
20- (BOOL)isEqual:(id)object;
21- (oneway void)release;
22- (id)retain;
23- (id)autorelease;
24@end
25@protocol NSCopying
26- (id)copyWithZone:(NSZone *)zone;
27@end
28@protocol NSMutableCopying
29- (id)mutableCopyWithZone:(NSZone *)zone;
30@end
31@class NSCoder;
32@protocol NSCoding
33- (void)encodeWithCoder:(NSCoder *)aCoder;
34@end
35@interface NSObject <NSObject> {}
36- (id)init;
37+ (id)alloc;
38@end
39typedef struct {} NSFastEnumerationState;
40@protocol NSFastEnumeration
41- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
42@end
43@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
44@end
45@interface NSArray (NSArrayCreation)
46+ (id)arrayWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
47- (id)initWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
48@end
49@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
50@end
51@interface NSDictionary (NSDictionaryCreation)
52+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ... __attribute__((sentinel(0,1)));
53- (id)initWithObjectsAndKeys:(id)firstObject, ... __attribute__((sentinel(0,1)));
54@end
55@interface NSSet : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
56@end
57@interface NSSet (NSSetCreation)
58+ (id)setWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
59- (id)initWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
60@end
61@protocol P;
62@class C;
63
64typedef struct FooType * __attribute__ ((NSObject)) FooType;
65typedef struct BarType * BarType;
66
67
68void f(id a, id<P> b, C* c, C<P> *d, FooType fooType, BarType barType) {
69  [NSArray arrayWithObjects:@"Hello", a, b, c, d, nil];
70  [NSArray arrayWithObjects:@"Foo", ^{}, nil];
71
72  [NSArray arrayWithObjects:@"Foo", "Bar", "Baz", nil]; // expected-warning 2 {{Argument to 'NSArray' method 'arrayWithObjects:' should be an Objective-C pointer type, not 'char *'}}
73  [NSDictionary dictionaryWithObjectsAndKeys:@"Foo", "Bar", nil]; // expected-warning {{Argument to 'NSDictionary' method 'dictionaryWithObjectsAndKeys:' should be an Objective-C pointer type, not 'char *'}}
74  [NSSet setWithObjects:@"Foo", "Bar", nil]; // expected-warning {{Argument to 'NSSet' method 'setWithObjects:' should be an Objective-C pointer type, not 'char *'}}
75
76  [[[NSArray alloc] initWithObjects:@"Foo", "Bar", nil] autorelease]; // expected-warning {{Argument to method 'initWithObjects:' should be an Objective-C pointer type, not 'char *'}}
77  [[[NSDictionary alloc] initWithObjectsAndKeys:@"Foo", "Bar", nil] autorelease]; // expected-warning {{Argument to method 'initWithObjectsAndKeys:' should be an Objective-C pointer type, not 'char *'}}
78  [[[NSDictionary alloc] initWithObjectsAndKeys:@"Foo", (void*) 0, nil] autorelease]; // no-warning
79  [[[NSDictionary alloc] initWithObjectsAndKeys:@"Foo", kCGImageSourceShouldCache, nil] autorelease]; // no-warning
80  [[[NSDictionary alloc] initWithObjectsAndKeys:@"Foo", fooType, nil] autorelease]; // no-warning
81  [[[NSDictionary alloc] initWithObjectsAndKeys:@"Foo", barType, nil] autorelease]; // expected-warning {{Argument to method 'initWithObjectsAndKeys:' should be an Objective-C pointer type, not 'BarType'}}
82  [[[NSSet alloc] initWithObjects:@"Foo", "Bar", nil] autorelease]; // expected-warning {{Argument to method 'initWithObjects:' should be an Objective-C pointer type, not 'char *'}}
83}
84
85// This previously crashed the variadic argument checker.
86@protocol RDar9273215
87- (void)rdar9273215:(id)x, ...;
88@end
89
90void test_rdar9273215(id<RDar9273215> y) {
91  return [y rdar9273215:y, y];
92}
93
94