Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -verify %s |
| 2 | |
| 3 | // (test the warning flag as well) |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 4 | |
| 5 | typedef 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 | |
| 32 | void testComparisonsWithFixits(id obj) { |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 33 | 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 Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 37 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 38 | 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 Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 44 | } |
| 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 | |
| 56 | void testComparisonsWithoutFixits() { |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 57 | if ([BaseObject new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 58 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 59 | 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 Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 61 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 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}} |
| 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 Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame^] | 68 | |
| 69 | #pragma clang diagnostic push |
| 70 | #pragma clang diagnostic ignored "-Wobjc-string-compare" |
| 71 | |
| 72 | void 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 | |