Skip to content

Commit 75cd916

Browse files
authored
Merge pull request swiftlang#4736 from apple-fcloutier/fcloutier/format-string-attrs
Merge format string attribute changes to apple/llvm-project
2 parents 8e550ca + 5b99728 commit 75cd916

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,13 @@ static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
33973397
return;
33983398
}
33993399
Ty = getFunctionOrMethodResultType(D);
3400+
// replace instancetype with the class type
3401+
auto Instancetype = S.Context.getObjCInstanceTypeDecl()->getTypeForDecl();
3402+
if (Ty->getAs<TypedefType>() == Instancetype)
3403+
if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
3404+
if (auto *Interface = OMD->getClassInterface())
3405+
Ty = S.Context.getObjCObjectPointerType(
3406+
QualType(Interface->getTypeForDecl(), 0));
34003407
if (!isNSStringType(Ty, S.Context, /*AllowNSAttributedString=*/true) &&
34013408
!isCFStringType(Ty, S.Context) &&
34023409
(!Ty->isPointerType() ||

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
// RUN: %clang_cc1 -verify -fsyntax-only %s
22

3-
@class NSString;
3+
@interface NSString
4+
+(instancetype)stringWithCString:(const char *)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)));
9+
@end
10+
11+
@protocol MaybeString
12+
-(instancetype)maybeString:(const char *)cstr __attribute__((format_arg(1))); // expected-error {{function does not return string type}}
13+
@end
14+
415
@class NSAttributedString;
516

617
extern NSString *fa2 (const NSString *) __attribute__((format_arg(1)));

0 commit comments

Comments
 (0)