2012-07-18 01:46:48 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=((id)0)" -verify %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=(id)0" -verify %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=0" -verify %s
|
2012-07-18 01:46:44 +08:00
|
|
|
|
2012-11-10 07:55:21 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -fobjc-arc "-Dnil=((id)0)" -verify %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -fobjc-arc "-Dnil=(id)0" -verify %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -fobjc-arc "-Dnil=0" -verify %s
|
|
|
|
|
2012-07-18 01:46:44 +08:00
|
|
|
// (test the warning flag as well)
|
2012-06-09 05:14:25 +08:00
|
|
|
|
2012-07-18 01:46:48 +08:00
|
|
|
typedef signed char BOOL;
|
2012-06-09 05:14:25 +08:00
|
|
|
|
|
|
|
@interface BaseObject
|
|
|
|
+ (instancetype)new;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSObject : BaseObject
|
|
|
|
- (BOOL)isEqual:(id)other;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSNumber : NSObject
|
|
|
|
+ (NSNumber *)numberWithInt:(int)value;
|
|
|
|
+ (NSNumber *)numberWithDouble:(double)value;
|
|
|
|
+ (NSNumber *)numberWithBool:(BOOL)value;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSArray : NSObject
|
|
|
|
+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSDictionary : NSObject
|
|
|
|
+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSString : NSObject
|
|
|
|
@end
|
|
|
|
|
|
|
|
void testComparisonsWithFixits(id obj) {
|
2012-07-18 01:46:40 +08:00
|
|
|
if (obj == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (obj != @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@"" == obj) return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@"" == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
|
|
|
|
if (@[] == obj) return; // expected-warning{{direct comparison of an array literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@{} == obj) return; // expected-warning{{direct comparison of a dictionary literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@12 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@1.0 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@__objc_yes == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@(1+1) == obj) return; // expected-warning{{direct comparison of a boxed expression has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
2012-06-09 05:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@interface BadEqualReturnString : NSString
|
|
|
|
- (void)isEqual:(id)other;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface BadEqualArgString : NSString
|
|
|
|
- (BOOL)isEqual:(int)other;
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
void testComparisonsWithoutFixits() {
|
2012-07-18 01:46:40 +08:00
|
|
|
if ([BaseObject new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
2012-06-09 05:14:25 +08:00
|
|
|
|
2012-07-18 01:46:40 +08:00
|
|
|
if ([BadEqualReturnString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
|
|
|
if ([BadEqualArgString new] == @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
2012-06-09 05:14:25 +08:00
|
|
|
|
2012-07-18 01:46:40 +08:00
|
|
|
if (@"" < @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
|
|
|
if (@"" > @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
|
|
|
if (@"" <= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
|
|
|
if (@"" >= @"") return; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
2012-06-09 05:14:25 +08:00
|
|
|
}
|
|
|
|
|
2012-07-18 01:46:44 +08:00
|
|
|
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wobjc-string-compare"
|
|
|
|
|
|
|
|
void testWarningFlags(id obj) {
|
|
|
|
if (obj == @"") return; // no-warning
|
|
|
|
if (@"" == obj) return; // no-warning
|
|
|
|
|
|
|
|
if (obj == @1) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
if (@1 == obj) return; // expected-warning{{direct comparison of a numeric literal has undefined behavior}} expected-note{{use 'isEqual:' instead}}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
2012-07-18 01:46:48 +08:00
|
|
|
|
|
|
|
void testNilComparison() {
|
|
|
|
// Don't warn when comparing to nil in a macro.
|
|
|
|
#define RETURN_IF_NIL(x) if (x == nil || nil == x) return
|
|
|
|
RETURN_IF_NIL(@"");
|
|
|
|
RETURN_IF_NIL(@1);
|
|
|
|
RETURN_IF_NIL(@1.0);
|
|
|
|
RETURN_IF_NIL(@[]);
|
|
|
|
RETURN_IF_NIL(@{});
|
|
|
|
RETURN_IF_NIL(@__objc_yes);
|
|
|
|
RETURN_IF_NIL(@(1+1));
|
|
|
|
}
|
|
|
|
|
2013-02-15 23:17:50 +08:00
|
|
|
void PR15257(Class c) {
|
|
|
|
return c == @""; // expected-warning{{direct comparison of a string literal has undefined behavior}}
|
|
|
|
}
|