blob: 6c2f0a368af9c61ad6b6086ba3df05750c623b4f [file] [log] [blame]
Benjamin Kramer47c4d102014-07-15 09:50:32 +00001//===--- 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 Carruth3cbd71c2015-01-14 11:24:38 +000011#include "clang/AST/ASTContext.h"
Benjamin Kramer47c4d102014-07-15 09:50:32 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
Benjamin Kramer47c4d102014-07-15 09:50:32 +000014
15using namespace clang::ast_matchers;
16
17namespace clang {
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000018namespace {
Benjamin Kramer47c4d102014-07-15 09:50:32 +000019AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
20 return Node.hasExplicitTemplateArgs();
21}
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000022} // namespace
Benjamin Kramer47c4d102014-07-15 09:50:32 +000023
24namespace tidy {
Alexander Kornienkoed824e02015-03-05 13:46:14 +000025namespace google {
Benjamin Kramer47c4d102014-07-15 09:50:32 +000026namespace build {
27
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000028void ExplicitMakePairCheck::registerMatchers(
29 ast_matchers::MatchFinder *Finder) {
Aaron Ballmanec3e5d62015-09-02 16:20:42 +000030 // 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 Kramer47c4d102014-07-15 09:50:32 +000035 // Look for std::make_pair with explicit template args. Ignore calls in
36 // templates.
37 Finder->addMatcher(
Benjamin Kramer6bcbe6e2014-09-03 13:30:28 +000038 callExpr(unless(isInTemplateInstantiation()),
Benjamin Kramera5d954b2014-08-05 15:33:46 +000039 callee(expr(ignoringParenImpCasts(
Benjamin Kramerddf36de2014-07-21 09:40:52 +000040 declRefExpr(hasExplicitTemplateArgs(),
41 to(functionDecl(hasName("::std::make_pair"))))
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000042 .bind("declref")))))
43 .bind("call"),
Benjamin Kramer47c4d102014-07-15 09:50:32 +000044 this);
45}
46
47void 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 Grang7c7ea7d2016-11-08 07:50:19 +000065 SourceRange(DeclRef->getLocStart(), DeclRef->getLAngleLoc()),
66 "std::pair<");
Benjamin Kramer47c4d102014-07-15 09:50:32 +000067 } else {
68 diag(Call->getLocStart(),
69 "for C++11-compatibility, omit template arguments from make_pair")
70 << FixItHint::CreateRemoval(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000071 SourceRange(DeclRef->getLAngleLoc(), DeclRef->getRAngleLoc()));
Benjamin Kramer47c4d102014-07-15 09:50:32 +000072 }
73}
74
75} // namespace build
Alexander Kornienkoed824e02015-03-05 13:46:14 +000076} // namespace google
Benjamin Kramer47c4d102014-07-15 09:50:32 +000077} // namespace tidy
78} // namespace clang