blob: ea42a176ef03f8f02fc4de181d131c9854495577 [file] [log] [blame]
Jordan Roseeec207f2012-07-17 17:46:44 +00001// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -verify %s
2
3// (test the warning flag as well)
Jordan Rose9f63a452012-06-08 21:14:25 +00004
5typedef unsigned char BOOL;
6
7@interface BaseObject
8+ (instancetype)new;
9@end
10
11@interface NSObject : BaseObject
12- (BOOL)isEqual:(id)other;
13@end
14
15@interface NSNumber : NSObject
16+ (NSNumber *)numberWithInt:(int)value;
17+ (NSNumber *)numberWithDouble:(double)value;
18+ (NSNumber *)numberWithBool:(BOOL)value;
19@end
20
21@interface NSArray : NSObject
22+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
23@end
24
25@interface NSDictionary : NSObject
26+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
27@end
28
29@interface NSString : NSObject
30@end
31
32void testComparisonsWithFixits(id obj) {
Jordan Rose8d872ca2012-07-17 17:46:40 +000033 if (obj == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
34 if (obj != @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
35 if (@"" == obj) return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
36 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 +000037
Jordan Rose8d872ca2012-07-17 17:46:40 +000038 if (@[] == obj) return; // expected-warning{{direct comparison of an array literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
39 if (@{} == obj) return; // expected-warning{{direct comparison of a dictionary literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
40 if (@12 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
41 if (@1.0 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
42 if (@__objc_yes == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
43 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 +000044}
45
46
47@interface BadEqualReturnString : NSString
48- (void)isEqual:(id)other;
49@end
50
51@interface BadEqualArgString : NSString
52- (BOOL)isEqual:(int)other;
53@end
54
55
56void testComparisonsWithoutFixits() {
Jordan Rose8d872ca2012-07-17 17:46:40 +000057 if ([BaseObject new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000058
Jordan Rose8d872ca2012-07-17 17:46:40 +000059 if ([BadEqualReturnString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
60 if ([BadEqualArgString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000061
Jordan Rose8d872ca2012-07-17 17:46:40 +000062 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}}
64 if (@"" <= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
65 if (@"" >= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000066}
67
Jordan Roseeec207f2012-07-17 17:46:44 +000068
69#pragma clang diagnostic push
70#pragma clang diagnostic ignored "-Wobjc-string-compare"
71
72void testWarningFlags(id obj) {
73 if (obj == @"") return; // no-warning
74 if (@"" == obj) return; // no-warning
75
76 if (obj == @1) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
77 if (@1 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
78}
79
80#pragma clang diagnostic pop
81