Index: clang-tidy/google/AvoidThrowingObjcExceptionCheck.h =================================================================== --- /dev/null +++ clang-tidy/google/AvoidThrowingObjcExceptionCheck.h @@ -0,0 +1,39 @@ +//===--- AvoidThrowingObjcExceptionCheck.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_GOOGLE_OBJC_AVOID_THROWING_EXCEPTION_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_AVOID_THROWING_EXCEPTION_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace google { +namespace objc { + +/// The check is to find usage of @throw invocation in Objective-C code. +/// We should avoid using @throw for Objective-C exceptions according to +/// Google Objective-C Style Guide. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/google-objc-avoid-throwing-exception.html +class AvoidThrowingObjcExceptionCheck : public ClangTidyCheck { + public: + AvoidThrowingObjcExceptionCheck(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 google +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OBJC_AVOID_THROWING_EXCEPTION_H Index: clang-tidy/google/AvoidThrowingObjcExceptionCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/google/AvoidThrowingObjcExceptionCheck.cpp @@ -0,0 +1,36 @@ +//===--- AvoidThrowingObjcExceptionCheck.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 "AvoidThrowingObjcExceptionCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace google { +namespace objc { + +void AvoidThrowingObjcExceptionCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(objcThrowStmt().bind("throwStmt"), this); +} + +void AvoidThrowingObjcExceptionCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *MatchedStmt = + Result.Nodes.getNodeAs("throwStmt"); + diag(MatchedStmt->getThrowLoc(), + "avoid using @throw to handle Objective-C exceptions"); +} + +} // namespace objc +} // namespace google +} // namespace tidy +} // namespace clang Index: clang-tidy/google/CMakeLists.txt =================================================================== --- clang-tidy/google/CMakeLists.txt +++ clang-tidy/google/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp + AvoidThrowingObjcExceptionCheck.cpp DefaultArgumentsCheck.cpp ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp Index: clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tidy/google/GoogleTidyModule.cpp +++ clang-tidy/google/GoogleTidyModule.cpp @@ -15,6 +15,7 @@ #include "../readability/NamespaceCommentCheck.h" #include "../readability/RedundantSmartptrGetCheck.h" #include "AvoidCStyleCastsCheck.h" +#include "AvoidThrowingObjcExceptionCheck.h" #include "DefaultArgumentsCheck.h" #include "ExplicitConstructorCheck.h" #include "ExplicitMakePairCheck.h" @@ -49,6 +50,8 @@ "google-explicit-constructor"); CheckFactories.registerCheck( "google-global-names-in-headers"); + CheckFactories.registerCheck( + "google-objc-avoid-throwing-exception"); CheckFactories.registerCheck( "google-objc-global-variable-declaration"); CheckFactories.registerCheck( Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,11 @@ Improvements to clang-tidy -------------------------- +- New `google-avoid-throwing-objc-exception + `_ check + + Add new check to detect usage of @throw in Objective-C code, which should be avoided. + - New `objc-property-declaration `_ check Index: docs/clang-tidy/checks/google-objc-avoid-throwing-exception.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/google-objc-avoid-throwing-exception.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - google-objc-avoid-throwing-exception + +google-objc-avoid-throwing-exception +======================================= + +This check finds @throw usages in Objective-C files. According to Google's Objective-C +Guide, we should not use @throw to handle exceptions. + +The corresponding style guide rule: +http://go/objc-style#Avoid_Throwing_Exceptions \ No newline at end of file Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -60,6 +60,7 @@ google-default-arguments google-explicit-constructor google-global-names-in-headers + google-objc-avoid-throwing-exception google-objc-global-variable-declaration google-readability-braces-around-statements (redirects to readability-braces-around-statements) google-readability-casting Index: test/clang-tidy/google-objc-avoid-throwing-exception.m =================================================================== --- /dev/null +++ test/clang-tidy/google-objc-avoid-throwing-exception.m @@ -0,0 +1,11 @@ +// RUN: %check_clang_tidy %s google-objc-avoid-throwing-exception %t +@class NSString; + +@implementation Foo +- (void)f { + NSString *foo = @"foo"; + @throw foo; + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: avoid using @throw to handle Objective-C exceptions [google-objc-avoid-throwing-exception] +} +@end +