[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Yan Zhang via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL319098: add new check to find OSSpinlock usage (authored by 
Wizard).

Repository:
  rL LLVM

https://reviews.llvm.org/D40325

Files:
  clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp
  clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h
  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
  clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-spinlock.rst
  clang-tools-extra/trunk/test/clang-tidy/objc-avoid-spinlock.m

Index: clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp
===
--- clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp
+++ clang-tools-extra/trunk/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 @@
 class ObjCModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories ) override {
+CheckFactories.registerCheck(
+"objc-avoid-spinlock");
 CheckFactories.registerCheck(
 "objc-forbidden-subclassing");
 CheckFactories.registerCheck(
Index: clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt
===
--- clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt
+++ clang-tools-extra/trunk/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
Index: clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h
===
--- clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.h
+++ clang-tools-extra/trunk/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 ) override;
+};
+
+}  // namespace objc
+}  // namespace tidy
+}  // namespace clang
+
+#endif  // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_AVOID_SPINLOCK_H
Index: clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp
===
--- clang-tools-extra/trunk/clang-tidy/objc/AvoidSpinlockCheck.cpp
+++ clang-tools-extra/trunk/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 ) {
+  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
Index: 

[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Yan Zhang via Phabricator via cfe-commits
Wizard updated this revision to Diff 124456.
Wizard added a comment.
Herald added a subscriber: klimek.

fix conflict


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D40325

Files:
  clang-tidy/objc/AvoidSpinlockCheck.cpp
  clang-tidy/objc/AvoidSpinlockCheck.h
  clang-tidy/objc/CMakeLists.txt
  clang-tidy/objc/ObjCTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/objc-avoid-spinlock.rst
  test/clang-tidy/objc-avoid-spinlock.m

Index: test/clang-tidy/objc-avoid-spinlock.m
===
--- /dev/null
+++ 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();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+OSSpinlockTry();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+OSSpinlockUnlock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+}
+@end
Index: docs/clang-tidy/checks/objc-avoid-spinlock.rst
===
--- /dev/null
+++ 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
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -174,6 +174,7 @@
modernize-use-using
mpi-buffer-deref
mpi-type-mismatch
+   objc-avoid-spinlock
objc-forbidden-subclassing
objc-property-declaration
performance-faster-string-find
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,11 @@
 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
   `_
 
Index: clang-tidy/objc/ObjCTidyModule.cpp
===
--- clang-tidy/objc/ObjCTidyModule.cpp
+++ 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 @@
 class ObjCModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories ) override {
+CheckFactories.registerCheck(
+"objc-avoid-spinlock");
 CheckFactories.registerCheck(
 "objc-forbidden-subclassing");
 CheckFactories.registerCheck(
Index: clang-tidy/objc/CMakeLists.txt
===
--- clang-tidy/objc/CMakeLists.txt
+++ 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
Index: clang-tidy/objc/AvoidSpinlockCheck.h
===
--- /dev/null
+++ 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:
+/// 

[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Ben Hamilton via Phabricator via cfe-commits
benhamilton added inline comments.



Comment at: test/clang-tidy/objc-avoid-spinlock.m:4
+typedef int OSSpinLock;
+void OSSpinlockTry(OSSpinLock *__lock) {}
+

Wizard wrote:
> benhamilton wrote:
> > Not sure why you declared (and defined?) this one but not the others.
> > 
> > Either declare them all (no definition needed) or don't..
> > 
> Oh I added this when I tested the first method since I thought it is needed. 
> But looks like I don't have to. Removed it.
It probably raises a compiler warning since the function isn't declared, but 
that does not cause the test to fail.


https://reviews.llvm.org/D40325



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Yan Zhang via Phabricator via cfe-commits
Wizard updated this revision to Diff 124448.
Wizard added a comment.

fix test


https://reviews.llvm.org/D40325

Files:
  clang-tidy/objc/AvoidSpinlockCheck.cpp
  clang-tidy/objc/AvoidSpinlockCheck.h
  clang-tidy/objc/CMakeLists.txt
  clang-tidy/objc/ObjCTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/objc-avoid-spinlock.rst
  test/clang-tidy/objc-avoid-spinlock.m

Index: test/clang-tidy/objc-avoid-spinlock.m
===
--- /dev/null
+++ 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();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+OSSpinlockTry();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+OSSpinlockUnlock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use os_unfair_lock_lock() or dispatch queue APIs instead of the deprecated OSSpinLock [objc-avoid-spinlock]
+}
+@end
Index: docs/clang-tidy/checks/objc-avoid-spinlock.rst
===
--- /dev/null
+++ 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
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -175,6 +175,7 @@
modernize-use-using
mpi-buffer-deref
mpi-type-mismatch
+   objc-avoid-spinlock
objc-forbidden-subclassing
objc-property-declaration
performance-faster-string-find
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,11 @@
 Improvements to clang-tidy
 --
 
+- New `objc-avoid-spinlock
+  `_ check
+
+  Add new check to detect the use of OSSpinlock.
+
 - New `google-avoid-throwing-objc-exception
   `_ check
 
Index: clang-tidy/objc/ObjCTidyModule.cpp
===
--- clang-tidy/objc/ObjCTidyModule.cpp
+++ 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 @@
 class ObjCModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories ) override {
+CheckFactories.registerCheck(
+"objc-avoid-spinlock");
 CheckFactories.registerCheck(
 "objc-forbidden-subclassing");
 CheckFactories.registerCheck(
Index: clang-tidy/objc/CMakeLists.txt
===
--- clang-tidy/objc/CMakeLists.txt
+++ 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
Index: clang-tidy/objc/AvoidSpinlockCheck.h
===
--- /dev/null
+++ 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 {
+ 

[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Yan Zhang via Phabricator via cfe-commits
Wizard marked an inline comment as done.
Wizard added inline comments.



Comment at: test/clang-tidy/objc-avoid-spinlock.m:4
+typedef int OSSpinLock;
+void OSSpinlockTry(OSSpinLock *__lock) {}
+

benhamilton wrote:
> Not sure why you declared (and defined?) this one but not the others.
> 
> Either declare them all (no definition needed) or don't..
> 
Oh I added this when I tested the first method since I thought it is needed. 
But looks like I don't have to. Removed it.


https://reviews.llvm.org/D40325



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Ben Hamilton via Phabricator via cfe-commits
benhamilton added a comment.

Thanks, looks good. Just one question about the test.




Comment at: test/clang-tidy/objc-avoid-spinlock.m:4
+typedef int OSSpinLock;
+void OSSpinlockTry(OSSpinLock *__lock) {}
+

Not sure why you declared (and defined?) this one but not the others.

Either declare them all (no definition needed) or don't..



https://reviews.llvm.org/D40325



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Yan Zhang via Phabricator via cfe-commits
Wizard marked 4 inline comments as done.
Wizard added inline comments.



Comment at: clang-tidy/objc/CMakeLists.txt:4
 add_clang_library(clangTidyObjCModule
+  AvoidSpinlockCheck.cpp
   ForbiddenSubclassingCheck.cpp

benhamilton wrote:
> IMHO this is really a check which should apply to products built on Apple 
> SDKs, not for Objective-C.
> 
> I don't know if that means we should move this to an `apple` submodule or if 
> there is a better solution.
> 
> You don't have to move it in this review, but let's open up a discussion to 
> figure out where non-ObjC checks should go.
Sure. It would be a good time to move them when we have more `apple` checks 
like this.


https://reviews.llvm.org/D40325



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Yan Zhang via Phabricator via cfe-commits
Wizard updated this revision to Diff 124427.
Wizard added a comment.

address comments


https://reviews.llvm.org/D40325

Files:
  clang-tidy/objc/AvoidSpinlockCheck.cpp
  clang-tidy/objc/AvoidSpinlockCheck.h
  clang-tidy/objc/CMakeLists.txt
  clang-tidy/objc/ObjCTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/objc-avoid-spinlock.rst
  test/clang-tidy/objc-avoid-spinlock.m

Index: test/clang-tidy/objc-avoid-spinlock.m
===
--- /dev/null
+++ test/clang-tidy/objc-avoid-spinlock.m
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s objc-avoid-spinlock %t
+
+typedef int OSSpinLock;
+void OSSpinlockTry(OSSpinLock *__lock) {}
+
+@implementation Foo
+- (void)f {
+int i = 1;
+OSSpinlockLock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: deprecated usage of OSSpinlock; Please use other locks or dispatch queue [objc-avoid-spinlock]
+OSSpinlockTry();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: deprecated usage of OSSpinlock; Please use other locks or dispatch queue [objc-avoid-spinlock]
+OSSpinlockUnlock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: deprecated usage of OSSpinlock; Please use other locks or dispatch queue [objc-avoid-spinlock]
+}
+@end
Index: docs/clang-tidy/checks/objc-avoid-spinlock.rst
===
--- /dev/null
+++ 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 in Objective-C files, 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
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -175,6 +175,7 @@
modernize-use-using
mpi-buffer-deref
mpi-type-mismatch
+   objc-avoid-spinlock
objc-forbidden-subclassing
objc-property-declaration
performance-faster-string-find
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,11 @@
 Improvements to clang-tidy
 --
 
+- New `objc-avoid-spinlock
+  `_ check
+
+  Add new check to detect the use of OSSpinlock in Objective-C files.
+
 - New `google-avoid-throwing-objc-exception
   `_ check
 
Index: clang-tidy/objc/ObjCTidyModule.cpp
===
--- clang-tidy/objc/ObjCTidyModule.cpp
+++ 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 @@
 class ObjCModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories ) override {
+CheckFactories.registerCheck(
+"objc-avoid-spinlock");
 CheckFactories.registerCheck(
 "objc-forbidden-subclassing");
 CheckFactories.registerCheck(
Index: clang-tidy/objc/CMakeLists.txt
===
--- clang-tidy/objc/CMakeLists.txt
+++ 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
Index: clang-tidy/objc/AvoidSpinlockCheck.h
===
--- /dev/null
+++ 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 in Objective-C files, which is deprecated due to
+/// potential livelock problems.
+///
+/// For the user-facing documentation see:
+/// 

[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-27 Thread Ben Hamilton via Phabricator via cfe-commits
benhamilton added inline comments.



Comment at: clang-tidy/objc/AvoidSpinlockCheck.cpp:22-24
+  if (!getLangOpts().ObjC1 && !getLangOpts().ObjC2) {
+return;
+  }

Why? `OSSpinLock()` calls should also be avoided in C++.

I think you should remove this.




Comment at: clang-tidy/objc/AvoidSpinlockCheck.cpp:27
+  callExpr(
+  callee((functionDecl(matchesName("::OSSpinlock(Lock|Unlock|Try)")
+  .bind("spinlock"),

`matchesName()` uses a regular expression, and is many orders of magnitude 
slower than `hasAnyName(A, B, C)`.

Can you change to `hasAnyName()`, please?




Comment at: clang-tidy/objc/CMakeLists.txt:4
 add_clang_library(clangTidyObjCModule
+  AvoidSpinlockCheck.cpp
   ForbiddenSubclassingCheck.cpp

IMHO this is really a check which should apply to products built on Apple SDKs, 
not for Objective-C.

I don't know if that means we should move this to an `apple` submodule or if 
there is a better solution.

You don't have to move it in this review, but let's open up a discussion to 
figure out where non-ObjC checks should go.



Comment at: docs/clang-tidy/checks/objc-avoid-spinlock.rst:13
+- `OSSpinlockTry`
+- `OSSpinlockUnlcok`
+

Typo: Unlcok -> Unlock



https://reviews.llvm.org/D40325



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-22 Thread Yan Zhang via Phabricator via cfe-commits
Wizard updated this revision to Diff 124011.
Wizard marked an inline comment as done.
Wizard added a comment.

fix nit


https://reviews.llvm.org/D40325

Files:
  clang-tidy/objc/AvoidSpinlockCheck.cpp
  clang-tidy/objc/AvoidSpinlockCheck.h
  clang-tidy/objc/CMakeLists.txt
  clang-tidy/objc/ObjCTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/objc-avoid-spinlock.rst
  test/clang-tidy/objc-avoid-spinlock.m

Index: test/clang-tidy/objc-avoid-spinlock.m
===
--- /dev/null
+++ test/clang-tidy/objc-avoid-spinlock.m
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s objc-avoid-spinlock %t
+
+typedef int OSSpinLock;
+void OSSpinlockTry(OSSpinLock *__lock) {}
+
+@implementation Foo
+- (void)f {
+int i = 1;
+OSSpinlockLock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: OSSpinlock is deprecated on iOS. Please use other locks or dispatch queue. [objc-avoid-spinlock]
+OSSpinlockTry();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: OSSpinlock is deprecated on iOS. Please use other locks or dispatch queue. [objc-avoid-spinlock]
+OSSpinlockUnlock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: OSSpinlock is deprecated on iOS. Please use other locks or dispatch queue. [objc-avoid-spinlock]
+}
+@end
Index: docs/clang-tidy/checks/objc-avoid-spinlock.rst
===
--- /dev/null
+++ 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 in Objective-C files, which is deprecated due to potential
+livelock problems. 
+
+This check will detect following function invocations:
+
+- `OSSpinlockLock`
+- `OSSpinlockTry`
+- `OSSpinlockUnlcok`
+
+The corresponding information about the problem of OSSpinlock: https://blog.postmates.com/why-spinlocks-are-bad-on-ios-b69fc5221058
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -175,6 +175,7 @@
modernize-use-using
mpi-buffer-deref
mpi-type-mismatch
+   objc-avoid-spinlock
objc-forbidden-subclassing
objc-property-declaration
performance-faster-string-find
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,11 @@
 Improvements to clang-tidy
 --
 
+- New `objc-avoid-spinlock
+  `_ check
+
+  Add new check to detect the use of OSSpinlock in Objective-C files.
+
 - New `google-avoid-throwing-objc-exception
   `_ check
 
Index: clang-tidy/objc/ObjCTidyModule.cpp
===
--- clang-tidy/objc/ObjCTidyModule.cpp
+++ 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 @@
 class ObjCModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories ) override {
+CheckFactories.registerCheck(
+"objc-avoid-spinlock");
 CheckFactories.registerCheck(
 "objc-forbidden-subclassing");
 CheckFactories.registerCheck(
Index: clang-tidy/objc/CMakeLists.txt
===
--- clang-tidy/objc/CMakeLists.txt
+++ 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
Index: clang-tidy/objc/AvoidSpinlockCheck.h
===
--- /dev/null
+++ 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 in Objective-C files, which is deprecated due to
+/// potential livelock problems.
+///
+/// For the user-facing documentation see:
+/// 

[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-22 Thread Haojian Wu via Phabricator via cfe-commits
hokein added inline comments.



Comment at: clang-tidy/objc/AvoidSpinlockCheck.cpp:35
+  diag(MatchedExpr->getLocStart(),
+   "OSSpinlock is deprecated on iOS. Please use other locks or dispatch "
+   "queue.");

hokein wrote:
> nit: clang-tidy message is not a complete message, please follow the rule.
> 
> The `OSSpinlock` is also deprecated on macOS, I think? from 
> https://developer.apple.com/documentation/os/1646466-os_unfair_lock_lock.
oops, sorry for my typo: s/is not a complete message/is not a complete sentence.


https://reviews.llvm.org/D40325



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-22 Thread Haojian Wu via Phabricator via cfe-commits
hokein added a comment.

The implementation looks good, I will let Ben to do the Object-C specific 
review since he probably knows more about it.




Comment at: clang-tidy/objc/AvoidSpinlockCheck.cpp:35
+  diag(MatchedExpr->getLocStart(),
+   "OSSpinlock is deprecated on iOS. Please use other locks or dispatch "
+   "queue.");

nit: clang-tidy message is not a complete message, please follow the rule.

The `OSSpinlock` is also deprecated on macOS, I think? from 
https://developer.apple.com/documentation/os/1646466-os_unfair_lock_lock.


https://reviews.llvm.org/D40325



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D40325: add new check to find OSSpinlock usage

2017-11-21 Thread Yan Zhang via Phabricator via cfe-commits
Wizard updated this revision to Diff 123860.
Wizard added a comment.

add doc to header file


https://reviews.llvm.org/D40325

Files:
  clang-tidy/objc/AvoidSpinlockCheck.cpp
  clang-tidy/objc/AvoidSpinlockCheck.h
  clang-tidy/objc/CMakeLists.txt
  clang-tidy/objc/ObjCTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/objc-avoid-spinlock.rst
  test/clang-tidy/objc-avoid-spinlock.m

Index: test/clang-tidy/objc-avoid-spinlock.m
===
--- /dev/null
+++ test/clang-tidy/objc-avoid-spinlock.m
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s objc-avoid-spinlock %t
+
+typedef int OSSpinLock;
+void OSSpinlockTry(OSSpinLock *__lock) {}
+
+@implementation Foo
+- (void)f {
+int i = 1;
+OSSpinlockLock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: OSSpinlock is deprecated on iOS. Please use other locks or dispatch queue. [objc-avoid-spinlock]
+OSSpinlockTry();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: OSSpinlock is deprecated on iOS. Please use other locks or dispatch queue. [objc-avoid-spinlock]
+OSSpinlockUnlock();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: OSSpinlock is deprecated on iOS. Please use other locks or dispatch queue. [objc-avoid-spinlock]
+}
+@end
Index: docs/clang-tidy/checks/objc-avoid-spinlock.rst
===
--- /dev/null
+++ 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 in Objective-C files, which is deprecated due to potential
+livelock problems. 
+
+This check will detect following function invocations:
+
+- `OSSpinlockLock`
+- `OSSpinlockTry`
+- `OSSpinlockUnlcok`
+
+The corresponding information about the problem of OSSpinlock: https://blog.postmates.com/why-spinlocks-are-bad-on-ios-b69fc5221058
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -175,6 +175,7 @@
modernize-use-using
mpi-buffer-deref
mpi-type-mismatch
+   objc-avoid-spinlock
objc-forbidden-subclassing
objc-property-declaration
performance-faster-string-find
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,11 @@
 Improvements to clang-tidy
 --
 
+- New `objc-avoid-spinlock
+  `_ check
+
+  Add new check to detect the use of OSSpinlock in Objective-C files.
+
 - New `google-avoid-throwing-objc-exception
   `_ check
 
Index: clang-tidy/objc/ObjCTidyModule.cpp
===
--- clang-tidy/objc/ObjCTidyModule.cpp
+++ 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 @@
 class ObjCModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories ) override {
+CheckFactories.registerCheck(
+"objc-avoid-spinlock");
 CheckFactories.registerCheck(
 "objc-forbidden-subclassing");
 CheckFactories.registerCheck(
Index: clang-tidy/objc/CMakeLists.txt
===
--- clang-tidy/objc/CMakeLists.txt
+++ 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
Index: clang-tidy/objc/AvoidSpinlockCheck.h
===
--- /dev/null
+++ 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 in Objective-C files, which is deprecated due to
+/// potential livelock problems.
+///
+/// For the user-facing documentation see:
+///