blob: c7034828eafbd1cea55ccc795d2615974b170bdb [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 {
18
19namespace ast_matchers {
20AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
21 return Node.hasExplicitTemplateArgs();
22}
23} // namespace ast_matchers
24
25namespace tidy {
Alexander Kornienkoed824e02015-03-05 13:46:14 +000026namespace google {
Benjamin Kramer47c4d102014-07-15 09:50:32 +000027namespace build {
28
29void
30ExplicitMakePairCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
31 // Look for std::make_pair with explicit template args. Ignore calls in
32 // templates.
33 Finder->addMatcher(
Benjamin Kramer6bcbe6e2014-09-03 13:30:28 +000034 callExpr(unless(isInTemplateInstantiation()),
Benjamin Kramera5d954b2014-08-05 15:33:46 +000035 callee(expr(ignoringParenImpCasts(
Benjamin Kramerddf36de2014-07-21 09:40:52 +000036 declRefExpr(hasExplicitTemplateArgs(),
37 to(functionDecl(hasName("::std::make_pair"))))
Benjamin Kramera5d954b2014-08-05 15:33:46 +000038 .bind("declref"))))).bind("call"),
Benjamin Kramer47c4d102014-07-15 09:50:32 +000039 this);
40}
41
42void ExplicitMakePairCheck::check(const MatchFinder::MatchResult &Result) {
43 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
44 const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declref");
45
46 // Sanity check: The use might have overriden ::std::make_pair.
47 if (Call->getNumArgs() != 2)
48 return;
49
50 const Expr *Arg0 = Call->getArg(0)->IgnoreParenImpCasts();
51 const Expr *Arg1 = Call->getArg(1)->IgnoreParenImpCasts();
52
53 // If types don't match, we suggest replacing with std::pair and explicit
54 // template arguments. Otherwise just remove the template arguments from
55 // make_pair.
56 if (Arg0->getType() != Call->getArg(0)->getType() ||
57 Arg1->getType() != Call->getArg(1)->getType()) {
58 diag(Call->getLocStart(), "for C++11-compatibility, use pair directly")
59 << FixItHint::CreateReplacement(
60 SourceRange(DeclRef->getLocStart(), DeclRef->getLAngleLoc()),
61 "std::pair<");
62 } else {
63 diag(Call->getLocStart(),
64 "for C++11-compatibility, omit template arguments from make_pair")
65 << FixItHint::CreateRemoval(
66 SourceRange(DeclRef->getLAngleLoc(), DeclRef->getRAngleLoc()));
67 }
68}
69
70} // namespace build
Alexander Kornienkoed824e02015-03-05 13:46:14 +000071} // namespace google
Benjamin Kramer47c4d102014-07-15 09:50:32 +000072} // namespace tidy
73} // namespace clang