clang-tools 8.0.0

AvoidThrowingObjCExceptionCheck.cpp
Go to the documentation of this file.
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 // this check should only be applied to ObjC sources.
23 if (!getLangOpts().ObjC)
24 return;
25
26 Finder->addMatcher(objcThrowStmt().bind("throwStmt"), this);
27 Finder->addMatcher(
28 objcMessageExpr(anyOf(hasSelector("raise:format:"),
29 hasSelector("raise:format:arguments:")),
30 hasReceiverType(asString("NSException")))
31 .bind("raiseException"),
32 this);
33 }
34
35 void AvoidThrowingObjCExceptionCheck::check(
36 const MatchFinder::MatchResult &Result) {
37 const auto *MatchedStmt =
38 Result.Nodes.getNodeAs<ObjCAtThrowStmt>("throwStmt");
39 const auto *MatchedExpr =
40 Result.Nodes.getNodeAs<ObjCMessageExpr>("raiseException");
41 auto SourceLoc = MatchedStmt == nullptr ? MatchedExpr->getSelectorStartLoc()
42 : MatchedStmt->getThrowLoc();
43 diag(SourceLoc,
44 "pass in NSError ** instead of throwing exception to indicate "
45 "Objective-C errors");
46 }
47
48 } // namespace objc
49 } // namespace google
50 } // namespace tidy
51 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
AvoidThrowingObjCExceptionCheck.h