clang-tools  9.0.0
ExplicitMakePairCheck.cpp
Go to the documentation of this file.
1 //===--- ExplicitMakePairCheck.cpp - clang-tidy -----------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace {
18 AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
19  return Node.hasExplicitTemplateArgs();
20 }
21 } // namespace
22 
23 namespace tidy {
24 namespace google {
25 namespace build {
26 
27 void ExplicitMakePairCheck::registerMatchers(
28  ast_matchers::MatchFinder *Finder) {
29  // Only register the matchers for C++; the functionality currently does not
30  // provide any benefit to other languages, despite being benign.
31  if (!getLangOpts().CPlusPlus)
32  return;
33 
34  // Look for std::make_pair with explicit template args. Ignore calls in
35  // templates.
36  Finder->addMatcher(
37  callExpr(unless(isInTemplateInstantiation()),
38  callee(expr(ignoringParenImpCasts(
39  declRefExpr(hasExplicitTemplateArgs(),
40  to(functionDecl(hasName("::std::make_pair"))))
41  .bind("declref")))))
42  .bind("call"),
43  this);
44 }
45 
46 void 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->getBeginLoc(), "for C++11-compatibility, use pair directly")
63  << FixItHint::CreateReplacement(
64  SourceRange(DeclRef->getBeginLoc(), DeclRef->getLAngleLoc()),
65  "std::pair<");
66  } else {
67  diag(Call->getBeginLoc(),
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
75 } // namespace google
76 } // namespace tidy
77 } // namespace clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36
const DeclRefExpr * DeclRef