xref: /llvm-project/clang/test/SemaObjC/objc-literal-nsnumber.m (revision c00c901f1cb714ebd053de5c6af564dc81754d3e)
1// RUN: %clang_cc1  -fsyntax-only -fblocks -triple x86_64-apple-darwin10 -verify %s
2
3#if __LP64__
4typedef unsigned long NSUInteger;
5typedef long NSInteger;
6#else
7typedef unsigned int NSUInteger;
8typedef int NSInteger;
9#endif
10
11void checkNSNumberUnavailableDiagnostic(void) {
12  id num = @1000; // expected-error {{definition of class NSNumber must be available to use Objective-C numeric literals}}
13
14  int x = 1000;
15  id num1 = @(x); // expected-error {{definition of class NSNumber must be available to use Objective-C numeric literals}}\
16                  // expected-error {{illegal type 'int' used in a boxed expression}}
17}
18
19@class NSNumber; // expected-note 2 {{forward declaration of class here}}
20
21void checkNSNumberFDDiagnostic(void) {
22  id num = @1000; // expected-error {{definition of class NSNumber must be available to use Objective-C numeric literals}}
23
24  int x = 1000;
25  id num1 = @(x); // expected-error {{definition of class NSNumber must be available to use Objective-C numeric literals}}\
26                  // expected-error {{illegal type 'int' used in a boxed expression}}
27}
28
29@interface NSObject
30+ (NSObject*)nsobject;
31@end
32
33@interface NSNumber : NSObject
34+ (NSNumber *)numberWithChar:(char)value;
35+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
36+ (NSNumber *)numberWithShort:(short)value;
37+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
38+ (NSNumber *)numberWithInt:(int)value;
39+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
40+ (NSNumber *)numberWithLong:(long)value;
41+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
42+ (NSNumber *)numberWithLongLong:(long long)value;
43+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
44+ (NSNumber *)numberWithFloat:(float)value;
45+ (NSNumber *)numberWithInteger:(NSInteger)value ;
46+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value ;
47@end
48
49int big = 1391126400;
50int thousand = 1000;
51int main(void) {
52  NSNumber * N = @3.1415926535;  // expected-error {{declaration of 'numberWithDouble:' is missing in NSNumber class}}
53  NSNumber *noNumber = @__objc_yes; // expected-error {{declaration of 'numberWithBool:' is missing in NSNumber class}}
54  NSNumber * NInt = @1000;
55  NSNumber * NLongDouble = @1000.0l; // expected-error{{'long double' is not a valid literal type for NSNumber}}
56  id character = @ 'a';
57
58  NSNumber *NNegativeInt = @-1000;
59  NSNumber *NPositiveInt = @+1000;
60  NSNumber *NNegativeFloat = @-1000.1f;
61  NSNumber *NPositiveFloat = @+1000.1f;
62
63  int five = 5;
64  @-five; // expected-error{{@- must be followed by a number to form an NSNumber object}}
65  @+five; // expected-error{{@+ must be followed by a number to form an NSNumber object}}
66  NSNumber *av = @(1391126400000);
67  NSNumber *bv = @(1391126400 * 1000); // expected-warning {{overflow in expression; result is -443'003'904 with type 'int'}}
68  NSNumber *cv = @(big * thousand);
69}
70
71// Dictionary test
72@class NSDictionary;  // expected-note {{forward declaration of class here}}
73
74NSDictionary *err(void) {
75  return @{@"name" : @"value"}; // expected-error {{definition of class NSDictionary must be available to use Objective-C dictionary literals}}
76}
77
78@interface NSDate : NSObject
79+ (NSDate *) date;
80@end
81
82@protocol NSCopying
83- copy;
84@end
85
86@interface NSDictionary : NSObject
87+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id<NSCopying> [])keys count:(NSUInteger)cnt;
88@end
89
90@interface NSString<NSCopying>
91@end
92
93id NSUserName(void);
94
95int Int(void);
96
97NSDictionary * blocks(void) {
98  return @{ @"task" : ^ { return 17; } };
99}
100
101NSDictionary * warn(void) {
102  NSDictionary *dictionary = @{@"name" : NSUserName(),
103                               @"date" : [NSDate date],
104                               @"name2" : @"other",
105                               NSObject.nsobject : @"nsobject" }; // expected-warning{{passing 'NSObject *' to parameter of incompatible type 'const id<NSCopying>'}}
106  NSDictionary *dictionary2 = @{@"name" : Int()}; // expected-error {{collection element of type 'int' is not an Objective-C object}}
107
108  NSObject *o;
109  NSDictionary *dictionary3 = @{o : o, // expected-warning{{passing 'NSObject *' to parameter of incompatible type 'const id<NSCopying>'}}
110                               @"date" : [NSDate date] };
111  return dictionary3;
112}
113
114typedef float BOOL;
115
116BOOL radar11231426(void) {
117        return __objc_yes;
118}
119
120id stringBoxingNoSuchMethod(const char *str) {
121  return @(str); // expected-error {{declaration of 'stringWithUTF8String:' is missing in NSString class}}
122}
123