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
This commit is contained in:
Félix Cloutier 2021-11-10 18:03:36 -08:00
parent 4602f52d48
commit 12ab3e6c84
2 changed files with 6 additions and 2 deletions

View File

@ -3389,7 +3389,8 @@ static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
}
Ty = getFunctionOrMethodResultType(D);
// replace instancetype with the class type
if (Ty.getTypePtr() == S.Context.getObjCInstanceTypeDecl()->getTypeForDecl())
auto Instancetype = S.Context.getObjCInstanceTypeDecl()->getTypeForDecl();
if (Ty->getAs<TypedefType>() == Instancetype)
if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
if (auto *Interface = OMD->getClassInterface())
Ty = S.Context.getObjCObjectPointerType(

View File

@ -2,7 +2,10 @@
@interface NSString
+(instancetype)stringWithCString:(const char *)cstr __attribute__((format_arg(1)));
+(instancetype)stringWithString:(NSString *)cstr __attribute__((format_arg(1)));
-(instancetype)initWithString:(NSString *)str __attribute__((format_arg(1)));
+(instancetype _Nonnull)nonNullableString:(NSString *)str __attribute__((format_arg(1)));
+(instancetype _Nullable)nullableString:(NSString *)str __attribute__((format_arg(1)));
@end
@protocol MaybeString