Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=((id)0)" -verify %s |
| 2 | // RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=(id)0" -verify %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=0" -verify %s |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 4 | |
Jordan Rose | 87da0b7 | 2012-11-09 23:55:21 +0000 | [diff] [blame] | 5 | // RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -fobjc-arc "-Dnil=((id)0)" -verify %s |
| 6 | // RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -fobjc-arc "-Dnil=(id)0" -verify %s |
| 7 | // RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -fobjc-arc "-Dnil=0" -verify %s |
| 8 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 9 | // (test the warning flag as well) |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 10 | |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 11 | typedef signed char BOOL; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 12 | |
| 13 | @interface BaseObject |
| 14 | + (instancetype)new; |
| 15 | @end |
| 16 | |
| 17 | @interface NSObject : BaseObject |
| 18 | - (BOOL)isEqual:(id)other; |
| 19 | @end |
| 20 | |
| 21 | @interface NSNumber : NSObject |
| 22 | + (NSNumber *)numberWithInt:(int)value; |
| 23 | + (NSNumber *)numberWithDouble:(double)value; |
| 24 | + (NSNumber *)numberWithBool:(BOOL)value; |
| 25 | @end |
| 26 | |
| 27 | @interface NSArray : NSObject |
| 28 | + (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt; |
| 29 | @end |
| 30 | |
| 31 | @interface NSDictionary : NSObject |
| 32 | + (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt; |
| 33 | @end |
| 34 | |
| 35 | @interface NSString : NSObject |
| 36 | @end |
| 37 | |
| 38 | void testComparisonsWithFixits(id obj) { |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 39 | if (obj == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 40 | if (obj != @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 41 | if (@"" == obj) return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 42 | 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] | 43 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 44 | if (@[] == obj) return; // expected-warning{{direct comparison of an array literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 45 | if (@{} == obj) return; // expected-warning{{direct comparison of a dictionary literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 46 | if (@12 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 47 | if (@1.0 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 48 | if (@__objc_yes == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 49 | 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] | 50 | } |
| 51 | |
| 52 | |
| 53 | @interface BadEqualReturnString : NSString |
| 54 | - (void)isEqual:(id)other; |
| 55 | @end |
| 56 | |
| 57 | @interface BadEqualArgString : NSString |
| 58 | - (BOOL)isEqual:(int)other; |
| 59 | @end |
| 60 | |
| 61 | |
| 62 | void testComparisonsWithoutFixits() { |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 63 | 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] | 64 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 65 | if ([BadEqualReturnString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 66 | 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] | 67 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 68 | if (@"" < @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 69 | if (@"" > @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 70 | if (@"" <= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 71 | 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] | 72 | } |
| 73 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 74 | |
| 75 | #pragma clang diagnostic push |
| 76 | #pragma clang diagnostic ignored "-Wobjc-string-compare" |
| 77 | |
| 78 | void testWarningFlags(id obj) { |
| 79 | if (obj == @"") return; // no-warning |
| 80 | if (@"" == obj) return; // no-warning |
| 81 | |
| 82 | if (obj == @1) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 83 | if (@1 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}} |
| 84 | } |
| 85 | |
| 86 | #pragma clang diagnostic pop |
| 87 | |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 88 | |
| 89 | void testNilComparison() { |
| 90 | // Don't warn when comparing to nil in a macro. |
| 91 | #define RETURN_IF_NIL(x) if (x == nil || nil == x) return |
| 92 | RETURN_IF_NIL(@""); |
| 93 | RETURN_IF_NIL(@1); |
| 94 | RETURN_IF_NIL(@1.0); |
| 95 | RETURN_IF_NIL(@[]); |
| 96 | RETURN_IF_NIL(@{}); |
| 97 | RETURN_IF_NIL(@__objc_yes); |
| 98 | RETURN_IF_NIL(@(1+1)); |
| 99 | } |
| 100 | |
Benjamin Kramer | 2e85e74 | 2013-02-15 15:17:50 +0000 | [diff] [blame] | 101 | void PR15257(Class c) { |
| 102 | return c == @""; // expected-warning{{direct comparison of a string literal has undefined behavior}} |
| 103 | } |