blob: b0ca139961a0f985de5348206277bcc5984e386d [file] [log] [blame]
Jordan Rose9f63a452012-06-08 21:14:25 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3typedef unsigned char BOOL;
4
5@interface BaseObject
6+ (instancetype)new;
7@end
8
9@interface NSObject : BaseObject
10- (BOOL)isEqual:(id)other;
11@end
12
13@interface NSNumber : NSObject
14+ (NSNumber *)numberWithInt:(int)value;
15+ (NSNumber *)numberWithDouble:(double)value;
16+ (NSNumber *)numberWithBool:(BOOL)value;
17@end
18
19@interface NSArray : NSObject
20+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
21@end
22
23@interface NSDictionary : NSObject
24+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
25@end
26
27@interface NSString : NSObject
28@end
29
30void testComparisonsWithFixits(id obj) {
Jordan Rose8d872ca2012-07-17 17:46:40 +000031 if (obj == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
32 if (obj != @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
33 if (@"" == obj) return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
34 if (@"" == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
Jordan Rose9f63a452012-06-08 21:14:25 +000035
Jordan Rose8d872ca2012-07-17 17:46:40 +000036 if (@[] == obj) return; // expected-warning{{direct comparison of an array literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
37 if (@{} == obj) return; // expected-warning{{direct comparison of a dictionary literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
38 if (@12 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
39 if (@1.0 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
40 if (@__objc_yes == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
41 if (@(1+1) == obj) return; // expected-warning{{direct comparison of a boxed expression has undefined behavior}} expected-note{{use 'isEqual:' instead}}
Jordan Rose9f63a452012-06-08 21:14:25 +000042}
43
44
45@interface BadEqualReturnString : NSString
46- (void)isEqual:(id)other;
47@end
48
49@interface BadEqualArgString : NSString
50- (BOOL)isEqual:(int)other;
51@end
52
53
54void testComparisonsWithoutFixits() {
Jordan Rose8d872ca2012-07-17 17:46:40 +000055 if ([BaseObject new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000056
Jordan Rose8d872ca2012-07-17 17:46:40 +000057 if ([BadEqualReturnString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
58 if ([BadEqualArgString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000059
Jordan Rose8d872ca2012-07-17 17:46:40 +000060 if (@"" < @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
61 if (@"" > @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
62 if (@"" <= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
63 if (@"" >= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000064}
65