[PATCH] D49119: [Sema][ObjC] Issue a warning when a method declared in a protocol is non-escaping but the corresponding method in the implementation is escaping.

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 9 19:03:50 PDT 2018


ahatanak created this revision.
ahatanak added reviewers: arphaman, vsapsai, dcoughlin.
ahatanak added a project: clang.
Herald added a subscriber: dexonsmith.

This patch makes Sema issue a warning when there is an extension that conforms to a protocol that declares a non-escaping method and the corresponding method in the implementation isn't non-escaping.

rdar://problem/39548196


Repository:
  rC Clang

https://reviews.llvm.org/D49119

Files:
  lib/Sema/SemaDeclObjC.cpp
  test/SemaObjCXX/noescape.mm


Index: test/SemaObjCXX/noescape.mm
===================================================================
--- test/SemaObjCXX/noescape.mm
+++ test/SemaObjCXX/noescape.mm
@@ -88,3 +88,20 @@
 
   S5<&noescapeFunc2> ne1;
 }
+
+ at protocol NoescapeProt
+-(void) m0:(int*)__attribute__((noescape)) p; // expected-note {{parameter of overridden method is annotated with __attribute__((noescape))}}
+ at end
+
+__attribute__((objc_root_class))
+ at interface C3
+-(void) m0:(int*) p;
+ at end
+
+ at interface C3 () <NoescapeProt>
+ at end
+
+ at implementation C3
+-(void) m0:(int*) p { // expected-warning {{parameter of overriding method should be annotated with __attribute__((noescape))}}
+}
+ at end
Index: lib/Sema/SemaDeclObjC.cpp
===================================================================
--- lib/Sema/SemaDeclObjC.cpp
+++ lib/Sema/SemaDeclObjC.cpp
@@ -109,6 +109,16 @@
   return true;
 }
 
+static void diagnoseNoescape(const ParmVarDecl *NewMD, const ParmVarDecl *OldMD,
+                             Sema &S) {
+  // A parameter of the overriding method should be annotated with noescape
+  // if the corresponding parameter of the overridden method is annotated.
+  if (OldMD->hasAttr<NoEscapeAttr>() && !NewMD->hasAttr<NoEscapeAttr>()) {
+    S.Diag(NewMD->getLocation(), diag::warn_overriding_method_missing_noescape);
+    S.Diag(OldMD->getLocation(), diag::note_overridden_marked_noescape);
+  }
+}
+
 void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, 
                                    const ObjCMethodDecl *Overridden) {
   if (Overridden->hasRelatedResultType() && 
@@ -192,13 +202,7 @@
       Diag(oldDecl->getLocation(), diag::note_previous_decl) << "parameter";
     }
 
-    // A parameter of the overriding method should be annotated with noescape
-    // if the corresponding parameter of the overridden method is annotated.
-    if (oldDecl->hasAttr<NoEscapeAttr>() && !newDecl->hasAttr<NoEscapeAttr>()) {
-      Diag(newDecl->getLocation(),
-           diag::warn_overriding_method_missing_noescape);
-      Diag(oldDecl->getLocation(), diag::note_overridden_marked_noescape);
-    }
+    diagnoseNoescape(newDecl, oldDecl, *this);
   }
 }
 
@@ -4663,6 +4667,22 @@
             << ObjCMethod->getDeclName();
         }
       }
+
+      // Warn if a method declared in a protocol to which a category or
+      // extension conforms is non-escaping and the implementation's method is
+      // escaping.
+      for (auto *C : IDecl->visible_categories())
+        for (auto &P : C->protocols())
+          if (auto *IMD = P->lookupMethod(ObjCMethod->getSelector(),
+                                          ObjCMethod->isInstanceMethod())) {
+            assert(ObjCMethod->parameters().size() ==
+                       IMD->parameters().size() &&
+                   "Methods have different number of parameters");
+            auto OI = IMD->param_begin(), OE = IMD->param_end();
+            auto NI = ObjCMethod->param_begin();
+            for (; OI != OE; ++OI, ++NI)
+              diagnoseNoescape(*NI, *OI, *this);
+          }
     }
   } else {
     cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49119.154751.patch
Type: text/x-patch
Size: 3131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180710/b4210b92/attachment.bin>


More information about the cfe-commits mailing list