Skip to content

Commit 5b99728

Browse files
format_arg attribute does not support nullable instancetype return type
* The format_arg attribute tells the compiler that the attributed function returns a format string that is compatible with a format string that is being passed as a specific argument. * Several NSString methods return copies of their input, so they would ideally have the format_arg attribute. A previous differential (D112670) added support for instancetype methods having the format_arg attribute when used in the context of NSString method declarations. * D112670 failed to account that instancetype can be sugared in certain narrow (but critical) scenarios, like by using nullability specifiers. This patch resolves this problem. Differential Revision: https://reviews.llvm.org/D113636 Reviewed By: ahatanak Radar-Id: rdar://85278860
1 parent 98f298e commit 5b99728

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3398,7 +3398,8 @@ static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
33983398
}
33993399
Ty = getFunctionOrMethodResultType(D);
34003400
// replace instancetype with the class type
3401-
if (Ty.getTypePtr() == S.Context.getObjCInstanceTypeDecl()->getTypeForDecl())
3401+
auto Instancetype = S.Context.getObjCInstanceTypeDecl()->getTypeForDecl();
3402+
if (Ty->getAs<TypedefType>() == Instancetype)
34023403
if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
34033404
if (auto *Interface = OMD->getClassInterface())
34043405
Ty = S.Context.getObjCObjectPointerType(

clang/test/SemaObjC/format-arg-attribute.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
@interface NSString
44
+(instancetype)stringWithCString:(const char *)cstr __attribute__((format_arg(1)));
5-
+(instancetype)stringWithString:(NSString *)cstr __attribute__((format_arg(1)));
5+
-(instancetype)initWithString:(NSString *)str __attribute__((format_arg(1)));
6+
7+
+(instancetype _Nonnull)nonNullableString:(NSString *)str __attribute__((format_arg(1)));
8+
+(instancetype _Nullable)nullableString:(NSString *)str __attribute__((format_arg(1)));
69
@end
710

811
@protocol MaybeString

0 commit comments

Comments
 (0)