blob: a07e0fff0240b144598588a9c94bcc14f9eabe97 [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
28void
29ExplicitMakePairCheck::registerMatchers(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"))))
Benjamin Kramera5d954b2014-08-05 15:33:46 +000042 .bind("declref"))))).bind("call"),
Benjamin Kramer47c4d102014-07-15 09:50:32 +000043 this);
44}
45
46void ExplicitMakePairCheck::check(const MatchFinder::MatchResult &Result) {
47 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
48 const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declref");
49
50 // Sanity check: The use might have overriden ::std::make_pair.
51 if (Call->getNumArgs() != 2)
52 return;
53
54 const Expr *Arg0 = Call->getArg(0)->IgnoreParenImpCasts();
55 const Expr *Arg1 = Call->getArg(1)->IgnoreParenImpCasts();
56
57 // If types don't match, we suggest replacing with std::pair and explicit
58 // template arguments. Otherwise just remove the template arguments from
59 // make_pair.
60 if (Arg0->getType() != Call->getArg(0)->getType() ||
61 Arg1->getType() != Call->getArg(1)->getType()) {
62 diag(Call->getLocStart(), "for C++11-compatibility, use pair directly")
63 << FixItHint::CreateReplacement(
64 SourceRange(DeclRef->getLocStart(), DeclRef->getLAngleLoc()),
65 "std::pair<");
66 } else {
67 diag(Call->getLocStart(),
68 "for C++11-compatibility, omit template arguments from make_pair")
69 << FixItHint::CreateRemoval(
70 SourceRange(DeclRef->getLAngleLoc(), DeclRef->getRAngleLoc()));
71 }
72}
73
74} // namespace build
Alexander Kornienkoed824e02015-03-05 13:46:14 +000075} // namespace google
Benjamin Kramer47c4d102014-07-15 09:50:32 +000076} // namespace tidy
77} // namespace clang