1 //===--- AvoidThrowingObjCExceptionCheck.cpp - clang-tidy------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "AvoidThrowingObjCExceptionCheck.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 14 using namespace clang::ast_matchers; 15 16 namespace clang { 17 namespace tidy { 18 namespace google { 19 namespace objc { 20 21 void AvoidThrowingObjCExceptionCheck::registerMatchers(MatchFinder *Finder) { 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 31 void 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