1 //===--- AvoidThrowingObjCExceptionCheck.cpp - clang-tidy------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "AvoidThrowingObjCExceptionCheck.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 13 using namespace clang::ast_matchers; 14 15 namespace clang { 16 namespace tidy { 17 namespace google { 18 namespace objc { 19 registerMatchers(MatchFinder * Finder)20void AvoidThrowingObjCExceptionCheck::registerMatchers(MatchFinder *Finder) { 21 22 Finder->addMatcher(objcThrowStmt().bind("throwStmt"), this); 23 Finder->addMatcher( 24 objcMessageExpr(anyOf(hasSelector("raise:format:"), 25 hasSelector("raise:format:arguments:")), 26 hasReceiverType(asString("NSException"))) 27 .bind("raiseException"), 28 this); 29 } 30 check(const MatchFinder::MatchResult & Result)31void AvoidThrowingObjCExceptionCheck::check( 32 const MatchFinder::MatchResult &Result) { 33 const auto *MatchedStmt = 34 Result.Nodes.getNodeAs<ObjCAtThrowStmt>("throwStmt"); 35 const auto *MatchedExpr = 36 Result.Nodes.getNodeAs<ObjCMessageExpr>("raiseException"); 37 auto SourceLoc = MatchedStmt == nullptr ? MatchedExpr->getSelectorStartLoc() 38 : MatchedStmt->getThrowLoc(); 39 diag(SourceLoc, 40 "pass in NSError ** instead of throwing exception to indicate " 41 "Objective-C errors"); 42 } 43 44 } // namespace objc 45 } // namespace google 46 } // namespace tidy 47 } // namespace clang 48