blob: fea624f1a6670a2b02b723e18575f1885b3fd6e4 [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 Rose6deae7c2012-07-09 16:54:44 +000031 if (obj == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior; use -isEqual: instead}}
32 if (obj != @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior; use -isEqual: instead}}
33 if (@"" == obj) return; // expected-warning{{direct comparison of a string literal has undefined behavior; use -isEqual: instead}}
34 if (@"" == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior; use -isEqual: instead}}
Jordan Rose9f63a452012-06-08 21:14:25 +000035
Jordan Rose6deae7c2012-07-09 16:54:44 +000036 if (@[] == obj) return; // expected-warning{{direct comparison of an array literal has undefined behavior; use -isEqual: instead}}
37 if (@{} == obj) return; // expected-warning{{direct comparison of a dictionary literal has undefined behavior; use -isEqual: instead}}
38 if (@12 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior; use -isEqual: instead}}
39 if (@1.0 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior; use -isEqual: instead}}
40 if (@__objc_yes == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior; use -isEqual: instead}}
41 if (@(1+1) == obj) return; // expected-warning{{direct comparison of a boxed expression has undefined behavior; 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() {
55 // All of these verifications use regex form to ensure we /don't/ append
56 // "use -isEqual: instead" to any of these.
57
Jordan Rose6deae7c2012-07-09 16:54:44 +000058 if ([BaseObject new] == @"") return; // expected-warning-re{{direct comparison of a string literal has undefined behavior$}}
Jordan Rose9f63a452012-06-08 21:14:25 +000059
Jordan Rose6deae7c2012-07-09 16:54:44 +000060 if ([BadEqualReturnString new] == @"") return; // expected-warning-re{{direct comparison of a string literal has undefined behavior$}}
61 if ([BadEqualArgString new] == @"") return; // expected-warning-re{{direct comparison of a string literal has undefined behavior$}}
Jordan Rose9f63a452012-06-08 21:14:25 +000062
Jordan Rose6deae7c2012-07-09 16:54:44 +000063 if (@"" < @"") return; // expected-warning-re{{direct comparison of a string literal has undefined behavior$}}
64 if (@"" > @"") return; // expected-warning-re{{direct comparison of a string literal has undefined behavior$}}
65 if (@"" <= @"") return; // expected-warning-re{{direct comparison of a string literal has undefined behavior$}}
66 if (@"" >= @"") return; // expected-warning-re{{direct comparison of a string literal has undefined behavior$}}
Jordan Rose9f63a452012-06-08 21:14:25 +000067}
68