[PATCH] D112670: __attribute__((format_arg(__NSString__, N))) does not support instancetype in NSString interface

FĂ©lix Cloutier via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 28 15:29:59 PDT 2021


fcloutier updated this revision to Diff 383179.
fcloutier added a comment.

Forgot to run clang-format.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112670/new/

https://reviews.llvm.org/D112670

Files:
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/SemaObjC/format-arg-attribute.m


Index: clang/test/SemaObjC/format-arg-attribute.m
===================================================================
--- clang/test/SemaObjC/format-arg-attribute.m
+++ clang/test/SemaObjC/format-arg-attribute.m
@@ -1,6 +1,9 @@
 // RUN: %clang_cc1 -verify -fsyntax-only %s
 
- at class NSString;
+ at interface NSString
++(instancetype)stringWithCString:(const char *)cstr __attribute__((format_arg(1)));
+ at end
+
 @class NSAttributedString;
 
 extern NSString *fa2 (const NSString *) __attribute__((format_arg(1)));
Index: clang/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -156,24 +156,42 @@
   return false;
 }
 
+static inline bool isNSStringInterface(ASTContext &Ctx, ObjCInterfaceDecl *Cls,
+                                       bool AllowNSAttributedString = false) {
+  if (!Cls)
+    return false;
+
+  IdentifierInfo *ClsName = Cls->getIdentifier();
+
+  if (ClsName == &Ctx.Idents.get("NSAttributedString"))
+    return AllowNSAttributedString;
+  // FIXME: Should we walk the chain of classes?
+  return ClsName == &Ctx.Idents.get("NSString") ||
+         ClsName == &Ctx.Idents.get("NSMutableString");
+}
+
 static inline bool isNSStringType(QualType T, ASTContext &Ctx,
                                   bool AllowNSAttributedString = false) {
   const auto *PT = T->getAs<ObjCObjectPointerType>();
   if (!PT)
     return false;
 
-  ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
-  if (!Cls)
+  return isNSStringInterface(Ctx, PT->getObjectType()->getInterface(),
+                             AllowNSAttributedString);
+}
+
+static inline bool
+isInstancetypeNSStringType(ASTContext &Ctx, QualType T, Decl *D,
+                           bool AllowNSAttributedString = false) {
+  if (T.getTypePtr() != Ctx.getObjCInstanceTypeDecl()->getTypeForDecl())
     return false;
 
-  IdentifierInfo* ClsName = Cls->getIdentifier();
+  auto *OMD = dyn_cast<ObjCMethodDecl>(D);
+  if (!OMD)
+    return false;
 
-  if (AllowNSAttributedString &&
-      ClsName == &Ctx.Idents.get("NSAttributedString"))
-    return true;
-  // FIXME: Should we walk the chain of classes?
-  return ClsName == &Ctx.Idents.get("NSString") ||
-         ClsName == &Ctx.Idents.get("NSMutableString");
+  return isNSStringInterface(Ctx, OMD->getClassInterface(),
+                             AllowNSAttributedString);
 }
 
 static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
@@ -3390,6 +3408,7 @@
   Ty = getFunctionOrMethodResultType(D);
   if (!isNSStringType(Ty, S.Context, /*AllowNSAttributedString=*/true) &&
       !isCFStringType(Ty, S.Context) &&
+      !isInstancetypeNSStringType(S.Context, Ty, D) &&
       (!Ty->isPointerType() ||
        !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
     S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112670.383179.patch
Type: text/x-patch
Size: 2914 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211028/5450b2f7/attachment.bin>


More information about the cfe-commits mailing list