[clang-tools-extra] r319459 - add new check to find NSError init invocation
Yan Zhang via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 30 11:05:08 PST 2017
Author: wizard
Date: Thu Nov 30 11:05:08 2017
New Revision: 319459
URL: http://llvm.org/viewvc/llvm-project?rev=319459&view=rev
Log:
add new check to find NSError init invocation
Summary:
This check will find out improper initialization of NSError objects.
According to Apple developer document, we should always use factory method errorWithDomain:code:userInfo: to create new NSError objects instead of [NSError alloc] init]. Otherwise it will lead to a warning message during runtime in Xcode.
The corresponding information about NSError creation: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html
Reviewers: hokein, benhamilton
Reviewed By: benhamilton
Subscribers: klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40528
Added:
clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.cpp
clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-nserror-init.rst
clang-tools-extra/trunk/test/clang-tidy/objc-avoid-nserror-init.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/AvoidNserrorInitCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.cpp?rev=319459&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.cpp Thu Nov 30 11:05:08 2017
@@ -0,0 +1,37 @@
+//===--- AvoidNSErrorInitCheck.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 "AvoidNSErrorInitCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace objc {
+
+void AvoidNSErrorInitCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(objcMessageExpr(hasSelector("init"),
+ hasReceiverType(asString("NSError *")))
+ .bind("nserrorInit"),
+ this);
+}
+
+void AvoidNSErrorInitCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *MatchedExpr =
+ Result.Nodes.getNodeAs<ObjCMessageExpr>("nserrorInit");
+ diag(MatchedExpr->getLocStart(),
+ "use errorWithDomain:code:userInfo: or initWithDomain:code:userInfo: to "
+ "create a new NSError");
+}
+
+} // namespace objc
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.h?rev=319459&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/objc/AvoidNserrorInitCheck.h Thu Nov 30 11:05:08 2017
@@ -0,0 +1,36 @@
+//===--- AvoidNSErrorInitCheck.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_AVOIDNSERRORINITCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_OBJC_AVOIDNSERRORINITCHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace objc {
+
+/// Finds usages of [NSSError init]. It is not the proper way of creating
+/// NSError. errorWithDomain:code:userInfo: should be used instead.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/objc-avoid-nserror-init.html
+class AvoidNSErrorInitCheck : public ClangTidyCheck {
+ public:
+ AvoidNSErrorInitCheck(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_AVOIDNSERRORINITCHECK_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=319459&r1=319458&r2=319459&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/objc/CMakeLists.txt Thu Nov 30 11:05:08 2017
@@ -1,6 +1,7 @@
set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyObjCModule
+ AvoidNSErrorInitCheck.cpp
AvoidSpinlockCheck.cpp
ForbiddenSubclassingCheck.cpp
ObjCTidyModule.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=319459&r1=319458&r2=319459&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/objc/ObjCTidyModule.cpp Thu Nov 30 11:05:08 2017
@@ -10,6 +10,7 @@
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
+#include "AvoidNSErrorInitCheck.h"
#include "AvoidSpinlockCheck.h"
#include "ForbiddenSubclassingCheck.h"
#include "PropertyDeclarationCheck.h"
@@ -23,6 +24,8 @@ namespace objc {
class ObjCModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+ CheckFactories.registerCheck<AvoidNSErrorInitCheck>(
+ "objc-avoid-nserror-init");
CheckFactories.registerCheck<AvoidSpinlockCheck>(
"objc-avoid-spinlock");
CheckFactories.registerCheck<ForbiddenSubclassingCheck>(
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=319459&r1=319458&r2=319459&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Thu Nov 30 11:05:08 2017
@@ -158,6 +158,11 @@ Improvements to clang-tidy
Finds uses of bitwise operations on signed integer types, which may lead to
undefined or implementation defined behaviour.
+- New `objc-avoid-nserror-init
+ <http://clang.llvm.org/extra/clang-tidy/checks/objc-avoid-nserror-init.html>`_ check
+
+ Add new check to detect the use of [NSError init].
+
- New `objc-avoid-spinlock
<http://clang.llvm.org/extra/clang-tidy/checks/objc-avoid-spinlock.html>`_ check
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=319459&r1=319458&r2=319459&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Thu Nov 30 11:05:08 2017
@@ -173,6 +173,7 @@ Clang-Tidy Checks
modernize-use-using
mpi-buffer-deref
mpi-type-mismatch
+ objc-avoid-nserror-init
objc-avoid-spinlock
objc-forbidden-subclassing
objc-property-declaration
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-nserror-init.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-nserror-init.rst?rev=319459&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-nserror-init.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/objc-avoid-nserror-init.rst Thu Nov 30 11:05:08 2017
@@ -0,0 +1,13 @@
+.. title:: clang-tidy - objc-avoid-nserror-init
+
+objc-avoid-nserror-init
+=======================
+
+This check will find out improper initialization of NSError objects.
+
+According to Apple developer document, we should always use factory method
+``errorWithDomain:code:userInfo:`` to create new NSError objects instead
+of ``[NSError alloc] init]``. Otherwise it will lead to a warning message
+during runtime.
+
+The corresponding information about NSError creation: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html
Added: clang-tools-extra/trunk/test/clang-tidy/objc-avoid-nserror-init.m
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/objc-avoid-nserror-init.m?rev=319459&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/objc-avoid-nserror-init.m (added)
+++ clang-tools-extra/trunk/test/clang-tidy/objc-avoid-nserror-init.m Thu Nov 30 11:05:08 2017
@@ -0,0 +1,12 @@
+// RUN: %check_clang_tidy %s objc-avoid-nserror-init %t
+ at interface NSError
++ (instancetype)alloc;
+- (instancetype)init;
+ at end
+
+ at implementation foo
+- (void)bar {
+ NSError *error = [[NSError alloc] init];
+ // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use errorWithDomain:code:userInfo: or initWithDomain:code:userInfo: to create a new NSError [objc-avoid-nserror-init]
+}
+ at end
More information about the cfe-commits
mailing list