summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaObjC
diff options
context:
space:
mode:
authorErik Pilkington <erik.pilkington@gmail.com>2019-08-14 16:57:11 +0000
committerErik Pilkington <erik.pilkington@gmail.com>2019-08-14 16:57:11 +0000
commitaa3855694ff45fc51bd71774c8bb15738b83ef16 (patch)
treed81ec80eb23fb10bd84987eff11e64f0dd74cdbf /clang/test/SemaObjC
parent4ae5efbe662e58836ac3a4f26467780990ca0ac2 (diff)
downloadbcm5719-llvm-aa3855694ff45fc51bd71774c8bb15738b83ef16.tar.gz
bcm5719-llvm-aa3855694ff45fc51bd71774c8bb15738b83ef16.zip
[Sema][ObjC] Fix a -Wformat false positive with localizedStringForKey
Only honour format_arg attributes on -[NSBundle localizedStringForKey] when its argument has a format specifier in it, otherwise its likely to just be a key to fetch localized strings. Fixes rdar://23622446 Differential revision: https://reviews.llvm.org/D27165 llvm-svn: 368878
Diffstat (limited to 'clang/test/SemaObjC')
-rw-r--r--clang/test/SemaObjC/format-strings-objc.m42
1 files changed, 41 insertions, 1 deletions
diff --git a/clang/test/SemaObjC/format-strings-objc.m b/clang/test/SemaObjC/format-strings-objc.m
index 767d5ac6cd6..e5a1a824abb 100644
--- a/clang/test/SemaObjC/format-strings-objc.m
+++ b/clang/test/SemaObjC/format-strings-objc.m
@@ -25,7 +25,11 @@ typedef struct _NSZone NSZone;
@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
@interface NSObject <NSObject> {} @end
typedef float CGFloat;
-@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; @end
+@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
+- (NSUInteger)length;
++(instancetype)stringWithFormat:(NSString *)fmt, ...
+ __attribute__((format(__NSString__, 1, 2)));
+@end
@interface NSSimpleCString : NSString {} @end
@interface NSConstantString : NSSimpleCString @end
extern void *_NSConstantStringClassReference;
@@ -302,3 +306,39 @@ const char *rd23622446(const char *format) {
}
@end
+
+@interface NSBundle : NSObject
+- (NSString *)localizedStringForKey:(NSString *)key
+ value:(nullable NSString *)value
+ table:(nullable NSString *)tableName
+ __attribute__((format_arg(1)));
+
+- (NSString *)someRandomMethod:(NSString *)key
+ value:(nullable NSString *)value
+ table:(nullable NSString *)tableName
+ __attribute__((format_arg(1)));
+@end
+
+void useLocalizedStringForKey(NSBundle *bndl) {
+ [NSString stringWithFormat:
+ [bndl localizedStringForKey:@"%d" // expected-warning{{more '%' conversions than data arguments}}
+ value:0
+ table:0]];
+ // No warning, @"flerp" doesn't have a format specifier.
+ [NSString stringWithFormat: [bndl localizedStringForKey:@"flerp" value:0 table:0], 43, @"flarp"];
+
+ [NSString stringWithFormat:
+ [bndl localizedStringForKey:@"%f"
+ value:0
+ table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
+
+ [NSString stringWithFormat:
+ [bndl someRandomMethod:@"%f"
+ value:0
+ table:0], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
+
+ [NSString stringWithFormat:
+ [bndl someRandomMethod:@"flerp"
+ value:0
+ table:0], 42]; // expected-warning{{data argument not used by format string}}
+}
OpenPOWER on IntegriCloud