Skip to content

Commit 22dea69

Browse files
committed
[clang][ObjC] allow the use of NSAttributedString * argument type with format attribute
This is useful for APIs that want to accept an attributed NSString as their format string rdar://79163229
1 parent f47d00c commit 22dea69

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3471,7 +3471,7 @@ static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
34713471
} else if (Kind == NSStringFormat) {
34723472
// FIXME: do we need to check if the type is NSString*? What are the
34733473
// semantics?
3474-
if (!isNSStringType(Ty, S.Context)) {
3474+
if (!isNSStringType(Ty, S.Context, /*AllowNSAttributedString=*/true)) {
34753475
S.Diag(AL.getLoc(), diag::err_format_attribute_not)
34763476
<< "an NSString" << IdxExpr->getSourceRange()
34773477
<< getFunctionOrMethodParamRange(D, ArgIdx);

clang/test/SemaObjC/format-strings-objc.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ @interface NSSimpleCString : NSString {} @end
3434
@interface NSConstantString : NSSimpleCString @end
3535
extern void *_NSConstantStringClassReference;
3636

37+
@interface NSAttributedString : NSObject
38+
+(instancetype)stringWithFormat:(NSAttributedString *)fmt, ...
39+
__attribute__((format(__NSString__, 1, 2)));
40+
@end
41+
3742
typedef const struct __CFString * CFStringRef;
3843
extern void CFStringCreateWithFormat(CFStringRef format, ...) __attribute__((format(CFString, 1, 2)));
3944
#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
@@ -317,6 +322,9 @@ - (NSString *)someRandomMethod:(NSString *)key
317322
value:(nullable NSString *)value
318323
table:(nullable NSString *)tableName
319324
__attribute__((format_arg(1)));
325+
326+
- (NSAttributedString *)someMethod2:(NSString *)key
327+
__attribute__((format_arg(1)));
320328
@end
321329

322330
void useLocalizedStringForKey(NSBundle *bndl) {
@@ -341,4 +349,9 @@ void useLocalizedStringForKey(NSBundle *bndl) {
341349
[bndl someRandomMethod:@"flerp"
342350
value:0
343351
table:0], 42]; // expected-warning{{data argument not used by format string}}
352+
353+
[NSAttributedString stringWithFormat:
354+
[bndl someMethod2: @"test"], 5]; // expected-warning{{data argument not used by format string}}
355+
[NSAttributedString stringWithFormat:
356+
[bndl someMethod2: @"%f"], 42]; // expected-warning{{format specifies type 'double' but the argument has type 'int'}}
344357
}

0 commit comments

Comments
 (0)