[clang] 77f1e09 - [-Wcompletion-handler] Don't recognize init methods as conventional

Valeriy Savchenko via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 7 03:50:21 PDT 2021


Author: Valeriy Savchenko
Date: 2021-04-07T13:50:01+03:00
New Revision: 77f1e096e8a0a0f37a4c5f8a0bacc7c60f44f0a1

URL: https://github.com/llvm/llvm-project/commit/77f1e096e8a0a0f37a4c5f8a0bacc7c60f44f0a1
DIFF: https://github.com/llvm/llvm-project/commit/77f1e096e8a0a0f37a4c5f8a0bacc7c60f44f0a1.diff

LOG: [-Wcompletion-handler] Don't recognize init methods as conventional

rdar://75704162

Differential Revision: https://reviews.llvm.org/D99601

Added: 
    

Modified: 
    clang/lib/Analysis/CalledOnceCheck.cpp
    clang/test/SemaObjC/warn-called-once.m

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/CalledOnceCheck.cpp b/clang/lib/Analysis/CalledOnceCheck.cpp
index 9fa8ac30404c3..db094129a9608 100644
--- a/clang/lib/Analysis/CalledOnceCheck.cpp
+++ b/clang/lib/Analysis/CalledOnceCheck.cpp
@@ -1011,11 +1011,16 @@ class CalledOnceChecker : public ConstStmtVisitor<CalledOnceChecker> {
     return llvm::None;
   }
 
+  /// Return true if the specified selector represents init method.
+  static bool isInitMethod(Selector MethodSelector) {
+    return MethodSelector.getMethodFamily() == OMF_init;
+  }
+
   /// Return true if the specified selector piece matches conventions.
   static bool isConventionalSelectorPiece(Selector MethodSelector,
                                           unsigned PieceIndex,
                                           QualType PieceType) {
-    if (!isConventional(PieceType)) {
+    if (!isConventional(PieceType) || isInitMethod(MethodSelector)) {
       return false;
     }
 

diff  --git a/clang/test/SemaObjC/warn-called-once.m b/clang/test/SemaObjC/warn-called-once.m
index ff2778d4bd0a4..27030dd94a825 100644
--- a/clang/test/SemaObjC/warn-called-once.m
+++ b/clang/test/SemaObjC/warn-called-once.m
@@ -13,6 +13,7 @@
 @protocol NSObject
 @end
 @interface NSObject <NSObject>
+- (instancetype)init;
 - (id)copy;
 - (id)class;
 - autorelease;
@@ -1235,4 +1236,13 @@ - (void)test_cleanup_2:(int)cond
   handler(); // expected-warning{{completion handler is called twice}}
 }
 
+- (void)initWithAdditions:(int)cond
+           withCompletion:(void (^)(void))handler {
+  self = [self init];
+  if (self) {
+    escape(handler);
+  }
+  // no-warning
+}
+
 @end


        


More information about the cfe-commits mailing list