[PATCH] D44170: [analyzer] Update GCDAsyncSemaphoreChecker to match messages send to ObjC objects

George Karpenkov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 6 18:56:31 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rC326868: [analyzer] Fix the checker for the performance anti-pattern to accept messages (authored by george.karpenkov, committed by ).
Herald added a subscriber: cfe-commits.

Repository:
  rC Clang

https://reviews.llvm.org/D44170

Files:
  lib/StaticAnalyzer/Checkers/GCDAsyncSemaphoreChecker.cpp
  test/Analysis/gcdasyncsemaphorechecker_test.m


Index: test/Analysis/gcdasyncsemaphorechecker_test.m
===================================================================
--- test/Analysis/gcdasyncsemaphorechecker_test.m
+++ test/Analysis/gcdasyncsemaphorechecker_test.m
@@ -177,7 +177,9 @@
 
 @interface Test1 : NSObject
 -(void)use_method_warn;
+-(void)use_objc_callback_warn;
 -(void)testNoWarn;
+-(void)acceptBlock:(block_t)callback;
 @end
 
 @implementation Test1
@@ -200,4 +202,33 @@
   dispatch_semaphore_wait(sema, 100);
 }
 
+-(void)acceptBlock:(block_t) callback {
+  callback();
+}
+
+-(void)use_objc_callback_warn {
+  dispatch_semaphore_t sema = dispatch_semaphore_create(0);
+
+  [self acceptBlock:^{
+      dispatch_semaphore_signal(sema);
+  }];
+  dispatch_semaphore_wait(sema, 100); // expected-warning{{Possible semaphore performance anti-pattern}}
+}
+
+void use_objc_and_c_callback(Test1 *t) {
+  dispatch_semaphore_t sema = dispatch_semaphore_create(0);
+
+  func(^{
+      dispatch_semaphore_signal(sema);
+  });
+  dispatch_semaphore_wait(sema, 100); // expected-warning{{Possible semaphore performance anti-pattern}}
+
+  dispatch_semaphore_t sema1 = dispatch_semaphore_create(0);
+
+  [t acceptBlock:^{
+      dispatch_semaphore_signal(sema1);
+  }];
+  dispatch_semaphore_wait(sema1, 100); // expected-warning{{Possible semaphore performance anti-pattern}}
+}
+
 @end
Index: lib/StaticAnalyzer/Checkers/GCDAsyncSemaphoreChecker.cpp
===================================================================
--- lib/StaticAnalyzer/Checkers/GCDAsyncSemaphoreChecker.cpp
+++ lib/StaticAnalyzer/Checkers/GCDAsyncSemaphoreChecker.cpp
@@ -111,23 +111,26 @@
       )
     ).bind(WarningBinding));
 
-  auto AcceptsBlockM =
-    forEachDescendant(callExpr(hasAnyArgument(hasType(
+  auto HasBlockArgumentM = hasAnyArgument(hasType(
             hasCanonicalType(blockPointerType())
-            ))));
+            ));
 
-  auto BlockSignallingM =
-    forEachDescendant(callExpr(hasAnyArgument(hasDescendant(callExpr(
+  auto ArgCallsSignalM = hasArgument(0, hasDescendant(callExpr(
           allOf(
               callsName("dispatch_semaphore_signal"),
               equalsBoundArgDecl(0, SemaphoreBinding)
-              ))))));
+              ))));
+
+  auto HasBlockAndCallsSignalM = allOf(HasBlockArgumentM, ArgCallsSignalM);
+
+  auto AcceptsBlockM =
+    forEachDescendant(
+      stmt(anyOf(
+        callExpr(HasBlockAndCallsSignalM),
+        objcMessageExpr(HasBlockAndCallsSignalM)
+           )));
 
-  auto FinalM = compoundStmt(
-      SemaphoreBindingM,
-      SemaphoreWaitM,
-      AcceptsBlockM,
-      BlockSignallingM);
+  auto FinalM = compoundStmt(SemaphoreBindingM, SemaphoreWaitM, AcceptsBlockM);
 
   MatchFinder F;
   Callback CB(BR, AM.getAnalysisDeclContext(D), this);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44170.137315.patch
Type: text/x-patch
Size: 2763 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180307/6ffb6914/attachment.bin>


More information about the cfe-commits mailing list