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