blob: f1aa8ecd91e22066f5a8dbfd4d8f8baf457f7b74 [file] [log] [blame]
Jordan Rosed5209ae2012-07-17 17:46:48 +00001// 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 Roseeec207f2012-07-17 17:46:44 +00004
5// (test the warning flag as well)
Jordan Rose9f63a452012-06-08 21:14:25 +00006
Jordan Rosed5209ae2012-07-17 17:46:48 +00007typedef signed char BOOL;
Jordan Rose9f63a452012-06-08 21:14:25 +00008
9@interface BaseObject
10+ (instancetype)new;
11@end
12
13@interface NSObject : BaseObject
14- (BOOL)isEqual:(id)other;
15@end
16
17@interface NSNumber : NSObject
18+ (NSNumber *)numberWithInt:(int)value;
19+ (NSNumber *)numberWithDouble:(double)value;
20+ (NSNumber *)numberWithBool:(BOOL)value;
21@end
22
23@interface NSArray : NSObject
24+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
25@end
26
27@interface NSDictionary : NSObject
28+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
29@end
30
31@interface NSString : NSObject
32@end
33
34void testComparisonsWithFixits(id obj) {
Jordan Rose8d872ca2012-07-17 17:46:40 +000035 if (obj == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
36 if (obj != @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
37 if (@"" == obj) return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
38 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 +000039
Jordan Rose8d872ca2012-07-17 17:46:40 +000040 if (@[] == obj) return; // expected-warning{{direct comparison of an array literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
41 if (@{} == obj) return; // expected-warning{{direct comparison of a dictionary literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
42 if (@12 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
43 if (@1.0 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
44 if (@__objc_yes == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
45 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 +000046}
47
48
49@interface BadEqualReturnString : NSString
50- (void)isEqual:(id)other;
51@end
52
53@interface BadEqualArgString : NSString
54- (BOOL)isEqual:(int)other;
55@end
56
57
58void testComparisonsWithoutFixits() {
Jordan Rose8d872ca2012-07-17 17:46:40 +000059 if ([BaseObject new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000060
Jordan Rose8d872ca2012-07-17 17:46:40 +000061 if ([BadEqualReturnString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
62 if ([BadEqualArgString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000063
Jordan Rose8d872ca2012-07-17 17:46:40 +000064 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}}
66 if (@"" <= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
67 if (@"" >= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
Jordan Rose9f63a452012-06-08 21:14:25 +000068}
69
Jordan Roseeec207f2012-07-17 17:46:44 +000070
71#pragma clang diagnostic push
72#pragma clang diagnostic ignored "-Wobjc-string-compare"
73
74void testWarningFlags(id obj) {
75 if (obj == @"") return; // no-warning
76 if (@"" == obj) return; // no-warning
77
78 if (obj == @1) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
79 if (@1 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
80}
81
82#pragma clang diagnostic pop
83
Jordan Rosed5209ae2012-07-17 17:46:48 +000084
85void testNilComparison() {
86 // Don't warn when comparing to nil in a macro.
87#define RETURN_IF_NIL(x) if (x == nil || nil == x) return
88 RETURN_IF_NIL(@"");
89 RETURN_IF_NIL(@1);
90 RETURN_IF_NIL(@1.0);
91 RETURN_IF_NIL(@[]);
92 RETURN_IF_NIL(@{});
93 RETURN_IF_NIL(@__objc_yes);
94 RETURN_IF_NIL(@(1+1));
95}
96