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