Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 1 | //===--- ExplicitMakePairCheck.cpp - clang-tidy -----------------*- C++ -*-===// |
| 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 "ExplicitMakePairCheck.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchers.h" |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
Alexander Kornienko | 50d7f461 | 2015-06-17 13:11:37 +0000 | [diff] [blame] | 18 | namespace { |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 19 | AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) { |
| 20 | return Node.hasExplicitTemplateArgs(); |
| 21 | } |
Alexander Kornienko | 50d7f461 | 2015-06-17 13:11:37 +0000 | [diff] [blame] | 22 | } // namespace |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 23 | |
| 24 | namespace tidy { |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 25 | namespace google { |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 26 | namespace build { |
| 27 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame^] | 28 | void ExplicitMakePairCheck::registerMatchers( |
| 29 | ast_matchers::MatchFinder *Finder) { |
Aaron Ballman | ec3e5d6 | 2015-09-02 16:20:42 +0000 | [diff] [blame] | 30 | // Only register the matchers for C++; the functionality currently does not |
| 31 | // provide any benefit to other languages, despite being benign. |
| 32 | if (!getLangOpts().CPlusPlus) |
| 33 | return; |
| 34 | |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 35 | // Look for std::make_pair with explicit template args. Ignore calls in |
| 36 | // templates. |
| 37 | Finder->addMatcher( |
Benjamin Kramer | 6bcbe6e | 2014-09-03 13:30:28 +0000 | [diff] [blame] | 38 | callExpr(unless(isInTemplateInstantiation()), |
Benjamin Kramer | a5d954b | 2014-08-05 15:33:46 +0000 | [diff] [blame] | 39 | callee(expr(ignoringParenImpCasts( |
Benjamin Kramer | ddf36de | 2014-07-21 09:40:52 +0000 | [diff] [blame] | 40 | declRefExpr(hasExplicitTemplateArgs(), |
| 41 | to(functionDecl(hasName("::std::make_pair")))) |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame^] | 42 | .bind("declref"))))) |
| 43 | .bind("call"), |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 44 | this); |
| 45 | } |
| 46 | |
| 47 | void ExplicitMakePairCheck::check(const MatchFinder::MatchResult &Result) { |
| 48 | const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call"); |
| 49 | const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declref"); |
| 50 | |
| 51 | // Sanity check: The use might have overriden ::std::make_pair. |
| 52 | if (Call->getNumArgs() != 2) |
| 53 | return; |
| 54 | |
| 55 | const Expr *Arg0 = Call->getArg(0)->IgnoreParenImpCasts(); |
| 56 | const Expr *Arg1 = Call->getArg(1)->IgnoreParenImpCasts(); |
| 57 | |
| 58 | // If types don't match, we suggest replacing with std::pair and explicit |
| 59 | // template arguments. Otherwise just remove the template arguments from |
| 60 | // make_pair. |
| 61 | if (Arg0->getType() != Call->getArg(0)->getType() || |
| 62 | Arg1->getType() != Call->getArg(1)->getType()) { |
| 63 | diag(Call->getLocStart(), "for C++11-compatibility, use pair directly") |
| 64 | << FixItHint::CreateReplacement( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame^] | 65 | SourceRange(DeclRef->getLocStart(), DeclRef->getLAngleLoc()), |
| 66 | "std::pair<"); |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 67 | } else { |
| 68 | diag(Call->getLocStart(), |
| 69 | "for C++11-compatibility, omit template arguments from make_pair") |
| 70 | << FixItHint::CreateRemoval( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame^] | 71 | SourceRange(DeclRef->getLAngleLoc(), DeclRef->getRAngleLoc())); |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | } // namespace build |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 76 | } // namespace google |
Benjamin Kramer | 47c4d10 | 2014-07-15 09:50:32 +0000 | [diff] [blame] | 77 | } // namespace tidy |
| 78 | } // namespace clang |