Yan Zhang | 8c348b3 | 2017-11-30 19:05:08 +0000 | [diff] [blame] | 1 | //===--- AvoidNSErrorInitCheck.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 "AvoidNSErrorInitCheck.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 objc { |
| 19 | |
| 20 | void AvoidNSErrorInitCheck::registerMatchers(MatchFinder *Finder) { |
Yan Zhang | c7faee7 | 2018-03-07 18:59:25 +0000 | [diff] [blame] | 21 | // this check should only be applied to ObjC sources. |
| 22 | if (!getLangOpts().ObjC1 && !getLangOpts().ObjC2) { |
| 23 | return; |
| 24 | } |
Yan Zhang | 8c348b3 | 2017-11-30 19:05:08 +0000 | [diff] [blame] | 25 | Finder->addMatcher(objcMessageExpr(hasSelector("init"), |
| 26 | hasReceiverType(asString("NSError *"))) |
| 27 | .bind("nserrorInit"), |
| 28 | this); |
| 29 | } |
| 30 | |
| 31 | void AvoidNSErrorInitCheck::check(const MatchFinder::MatchResult &Result) { |
| 32 | const auto *MatchedExpr = |
| 33 | Result.Nodes.getNodeAs<ObjCMessageExpr>("nserrorInit"); |
| 34 | diag(MatchedExpr->getLocStart(), |
| 35 | "use errorWithDomain:code:userInfo: or initWithDomain:code:userInfo: to " |
| 36 | "create a new NSError"); |
| 37 | } |
| 38 | |
| 39 | } // namespace objc |
| 40 | } // namespace tidy |
| 41 | } // namespace clang |