[clang-tools-extra] r319098 - add new check to find OSSpinlock usage

Yan Zhang via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 27 13:30:10 PST 2017


Author: wizard
Date: Mon Nov 27 13:30:10 2017
New Revision: 319098

URL: http://llvm.org/viewvc/llvm-project?rev=319098&view=rev
Log:
add new check to find OSSpinlock usage

Summary:
This check finds the use of methods related to OSSpinlock in Objective-C code, which should be deprecated due to livelock issues.
The following method call will be detected:

- OSSpinlockLock()
- OSSpinlockTry()
- OSSpinlockUnlcok()

Reviewers: hokein, benhamilton

Reviewed By: benhamilton

Subscribers: klimek, cfe-commits, mgorny

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

Added:
    clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp
    clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-spinlock.rst
    clang-tools-extra/trunk/test/clang-tidy/objc-avoid-spinlock.m
Modified:
    clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst

Added: clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp?rev=319098&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp Mon Nov 27 13:30:10 2017
@@ -0,0 +1,37 @@
+//===--- AvoidSpinlockCheck.cpp - clang-tidy-------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "AvoidSpinlockCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace objc {
+
+void AvoidSpinlockCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      callExpr(callee((functionDecl(hasAnyName(
+                   "OSSpinlockLock", "OSSpinlockUnlock", "OSSpinlockTry")))))
+          .bind("spinlock"),
+      this);
+}
+
+void AvoidSpinlockCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *MatchedExpr = Result.Nodes.getNodeAs<CallExpr>("spinlock");
+  diag(MatchedExpr->getLocStart(),
+       "use os_unfair_lock_lock() or dispatch queue APIs instead of the "
+       "deprecated OSSpinLock");
+}
+
+}  // namespace objc
+}  // namespace tidy
+}  // namespace clang

Added: clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h?rev=319098&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h Mon Nov 27 13:30:10 2017
@@ -0,0 +1,36 @@
+//===--- AvoidSpinlockCheck.h - clang-tidy-----------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_AVOID_SPINLOCK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_AVOID_SPINLOCK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace objc {
+
+/// Finds usages of OSSpinlock, which is deprecated due to potential livelock
+/// problems.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/objc-avoid-spinlock.html
+class AvoidSpinlockCheck : public ClangTidyCheck {
+ public:
+  AvoidSpinlockCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+}  // namespace objc
+}  // namespace tidy
+}  // namespace clang
+
+#endif  // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_AVOID_SPINLOCK_H

Modified: clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt?rev=319098&r1=319097&r2=319098&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt Mon Nov 27 13:30:10 2017
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS support)
 
 add_clang_library(clangTidyObjCModule
+  AvoidSpinlockCheck.cpp
   ForbiddenSubclassingCheck.cpp
   ObjCTidyModule.cpp
   PropertyDeclarationCheck.cpp

Modified: clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp?rev=319098&r1=319097&r2=319098&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp Mon Nov 27 13:30:10 2017
@@ -10,6 +10,7 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
+#include "AvoidSpinlockCheck.h"
 #include "ForbiddenSubclassingCheck.h"
 #include "PropertyDeclarationCheck.h"
 
@@ -22,6 +23,8 @@ namespace objc {
 class ObjCModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+    CheckFactories.registerCheck<AvoidSpinlockCheck>(
+        "objc-avoid-spinlock");
     CheckFactories.registerCheck<ForbiddenSubclassingCheck>(
         "objc-forbidden-subclassing");
     CheckFactories.registerCheck<PropertyDeclarationCheck>(

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=319098&r1=319097&r2=319098&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Mon Nov 27 13:30:10 2017
@@ -57,6 +57,11 @@ The improvements are...
 Improvements to clang-tidy
 --------------------------
 
+- New `objc-avoid-spinlock
+  <http://clang.llvm.org/extra/clang-tidy/checks/objc-avoid-spinlock.html>`_ check
+
+  Add new check to detect the use of OSSpinlock.
+
 - The 'misc-move-constructor-init' check was renamed to `performance-move-constructor-init
   <http://clang.llvm.org/extra/clang-tidy/checks/performance-move-constructor-init.html>`_
 

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=319098&r1=319097&r2=319098&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Mon Nov 27 13:30:10 2017
@@ -174,6 +174,7 @@ Clang-Tidy Checks
    modernize-use-using
    mpi-buffer-deref
    mpi-type-mismatch
+   objc-avoid-spinlock
    objc-forbidden-subclassing
    objc-property-declaration
    performance-faster-string-find

Added: clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-spinlock.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-spinlock.rst?rev=319098&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-spinlock.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-spinlock.rst Mon Nov 27 13:30:10 2017
@@ -0,0 +1,15 @@
+.. title:: clang-tidy - objc-avoid-spinlock
+
+objc-avoid-spinlock
+===================
+
+Finds usages of OSSpinlock, which is deprecated due to potential
+livelock problems. 
+
+This check will detect following function invocations:
+
+- `OSSpinlockLock`
+- `OSSpinlockTry`
+- `OSSpinlockUnlock`
+
+The corresponding information about the problem of OSSpinlock: https://blog.postmates.com/why-spinlocks-are-bad-on-ios-b69fc5221058

Added: clang-tools-extra/trunk/test/clang-tidy/objc-avoid-spinlock.m
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/objc-avoid-spinlock.m?rev=319098&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/objc-avoid-spinlock.m (added)
+++ clang-tools-extra/trunk/test/clang-tidy/objc-avoid-spinlock.m Mon Nov 27 13:30:10 2017
@@ -0,0 +1,15 @@
+// RUN: %check_clang_tidy %s objc-avoid-spinlock %t
+
+typedef int OSSpinLock;
+
+ at implementation Foo
+- (void)f {
+    int i = 1;
+    OSSpinlockLock(&i);
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+    OSSpinlockTry(&i);
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+    OSSpinlockUnlock(&i);
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+}
+ at end




More information about the cfe-commits mailing list