clang-tools  10.0.0
AvoidNSErrorInitCheck.cpp
Go to the documentation of this file.
1 //===--- AvoidNSErrorInitCheck.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 objc {
18 
19 void AvoidNSErrorInitCheck::registerMatchers(MatchFinder *Finder) {
20  // this check should only be applied to ObjC sources.
21  if (!getLangOpts().ObjC)
22  return;
23 
24  Finder->addMatcher(objcMessageExpr(hasSelector("init"),
25  hasReceiverType(asString("NSError *")))
26  .bind("nserrorInit"),
27  this);
28 }
29 
30 void AvoidNSErrorInitCheck::check(const MatchFinder::MatchResult &Result) {
31  const auto *MatchedExpr =
32  Result.Nodes.getNodeAs<ObjCMessageExpr>("nserrorInit");
33  diag(MatchedExpr->getBeginLoc(),
34  "use errorWithDomain:code:userInfo: or initWithDomain:code:userInfo: to "
35  "create a new NSError");
36 }
37 
38 } // namespace objc
39 } // namespace tidy
40 } // namespace clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//