clang-tools  7.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 
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().ObjC1 && !getLangOpts().ObjC2) {
24  return;
25  }
26 
27  Finder->addMatcher(objcThrowStmt().bind("throwStmt"), this);
28  Finder->addMatcher(
29  objcMessageExpr(anyOf(hasSelector("raise:format:"),
30  hasSelector("raise:format:arguments:")),
31  hasReceiverType(asString("NSException")))
32  .bind("raiseException"),
33  this);
34 }
35 
36 void AvoidThrowingObjCExceptionCheck::check(
37  const MatchFinder::MatchResult &Result) {
38  const auto *MatchedStmt =
39  Result.Nodes.getNodeAs<ObjCAtThrowStmt>("throwStmt");
40  const auto *MatchedExpr =
41  Result.Nodes.getNodeAs<ObjCMessageExpr>("raiseException");
42  auto SourceLoc = MatchedStmt == nullptr ? MatchedExpr->getSelectorStartLoc()
43  : MatchedStmt->getThrowLoc();
44  diag(SourceLoc,
45  "pass in NSError ** instead of throwing exception to indicate "
46  "Objective-C errors");
47 }
48 
49 } // namespace objc
50 } // namespace google
51 } // namespace tidy
52 } // namespace clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//