diff options
| author | Yan Zhang <ynzhang@google.com> | 2017-11-27 21:30:10 +0000 |
|---|---|---|
| committer | Yan Zhang <ynzhang@google.com> | 2017-11-27 21:30:10 +0000 |
| commit | 2f20b36cc3dc5ef5709b09d22f6bbbcbe55456ae (patch) | |
| tree | 25bc7496eab9f3f0bc8d23ca483e55fcc0f22667 /clang-tools-extra/clang-tidy | |
| parent | 824d71a9c5f260719691acaeb9773af7c33fd2bf (diff) | |
| download | bcm5719-llvm-2f20b36cc3dc5ef5709b09d22f6bbbcbe55456ae.tar.gz bcm5719-llvm-2f20b36cc3dc5ef5709b09d22f6bbbcbe55456ae.zip | |
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
llvm-svn: 319098
Diffstat (limited to 'clang-tools-extra/clang-tidy')
4 files changed, 77 insertions, 0 deletions
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 00000000000..21ec3642184 --- /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<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 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 00000000000..d9dbf8cb74b --- /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 daec7a1d648..dae8087df82 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 6fe12c3a53f..674da340c6d 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<AvoidSpinlockCheck>( + "objc-avoid-spinlock"); CheckFactories.registerCheck<ForbiddenSubclassingCheck>( "objc-forbidden-subclassing"); CheckFactories.registerCheck<PropertyDeclarationCheck>( |

