Skip to content

Merge format string attribute changes to apple/llvm-project #4736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3397,6 +3397,13 @@ static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
return;
}
Ty = getFunctionOrMethodResultType(D);
// replace instancetype with the class type
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(
QualType(Interface->getTypeForDecl(), 0));
if (!isNSStringType(Ty, S.Context, /*AllowNSAttributedString=*/true) &&
!isCFStringType(Ty, S.Context) &&
(!Ty->isPointerType() ||
Expand Down
13 changes: 12 additions & 1 deletion clang/test/SemaObjC/format-arg-attribute.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// RUN: %clang_cc1 -verify -fsyntax-only %s

@class NSString;
@interface NSString
+(instancetype)stringWithCString:(const char *)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
-(instancetype)maybeString:(const char *)cstr __attribute__((format_arg(1))); // expected-error {{function does not return string type}}
@end

@class NSAttributedString;

extern NSString *fa2 (const NSString *) __attribute__((format_arg(1)));
Expand Down