diff --git a/clang-tools-extra/clang-tidy/objc/AvoidSpinlockCheck.cpp b/clang-tools-extra/clang-tidy/objc/AvoidSpinlockCheck.cpp new file mode 100644 index 000000000000..21ec3642184c --- /dev/null +++ b/clang-tools-extra/clang-tidy/objc/AvoidSpinlockCheck.cpp @@ -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("spinlock"); + diag(MatchedExpr->getLocStart(), + "use os_unfair_lock_lock() or dispatch queue APIs instead of the " + "deprecated OSSpinLock"); +} + +} // namespace objc +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/objc/AvoidSpinlockCheck.h b/clang-tools-extra/clang-tidy/objc/AvoidSpinlockCheck.h new file mode 100644 index 000000000000..d9dbf8cb74bc --- /dev/null +++ b/clang-tools-extra/clang-tidy/objc/AvoidSpinlockCheck.h @@ -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 diff --git a/clang-tools-extra/clang-tidy/objc/CMakeLists.txt b/clang-tools-extra/clang-tidy/objc/CMakeLists.txt index daec7a1d6483..dae8087df825 100644 --- a/clang-tools-extra/clang-tidy/objc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/objc/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyObjCModule + AvoidSpinlockCheck.cpp ForbiddenSubclassingCheck.cpp ObjCTidyModule.cpp PropertyDeclarationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp b/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp index 6fe12c3a53f5..674da340c6d8 100644 --- a/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp @@ -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( + "objc-avoid-spinlock"); CheckFactories.registerCheck( "objc-forbidden-subclassing"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 14e5ca520d20..b0b9358315f0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -57,6 +57,11 @@ The improvements are... Improvements to clang-tidy -------------------------- +- New `objc-avoid-spinlock + `_ check + + Add new check to detect the use of OSSpinlock. + - The 'misc-move-constructor-init' check was renamed to `performance-move-constructor-init `_ diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 5c77352c7926..c575dfdfbea4 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -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 diff --git a/clang-tools-extra/docs/clang-tidy/checks/objc-avoid-spinlock.rst b/clang-tools-extra/docs/clang-tidy/checks/objc-avoid-spinlock.rst new file mode 100644 index 000000000000..41fe82d4e2c8 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/objc-avoid-spinlock.rst @@ -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 diff --git a/clang-tools-extra/test/clang-tidy/objc-avoid-spinlock.m b/clang-tools-extra/test/clang-tidy/objc-avoid-spinlock.m new file mode 100644 index 000000000000..f9f05cb05a28 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/objc-avoid-spinlock.m @@ -0,0 +1,15 @@ +// RUN: %check_clang_tidy %s objc-avoid-spinlock %t + +typedef int OSSpinLock; + +@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] +} +@end