clang-tools  14.0.0git
AvoidThrowingObjCExceptionCheck.cpp
Go to the documentation of this file.
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 
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 
20 void 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 
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
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:258
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
AvoidThrowingObjCExceptionCheck.h