Yan Zhang | 8c348b3 | 2017-11-30 19:05:08 +0000 | [diff] [blame] | 1 | //===--- AvoidNSErrorInitCheck.cpp - clang-tidy----------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Yan Zhang | 8c348b3 | 2017-11-30 19:05:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "AvoidNSErrorInitCheck.h" |
| 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) { |
Yan Zhang | c7faee7 | 2018-03-07 18:59:25 +0000 | [diff] [blame] | 20 | // this check should only be applied to ObjC sources. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 21 | if (!getLangOpts().ObjC) |
Yan Zhang | c7faee7 | 2018-03-07 18:59:25 +0000 | [diff] [blame] | 22 | return; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 23 | |
Yan Zhang | 8c348b3 | 2017-11-30 19:05:08 +0000 | [diff] [blame] | 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"); |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 33 | diag(MatchedExpr->getBeginLoc(), |
Yan Zhang | 8c348b3 | 2017-11-30 19:05:08 +0000 | [diff] [blame] | 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 |