clang-tools  14.0.0git
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  // Look for std::make_pair with explicit template args. Ignore calls in
30  // templates.
31  Finder->addMatcher(
32  callExpr(unless(isInTemplateInstantiation()),
33  callee(expr(ignoringParenImpCasts(
34  declRefExpr(hasExplicitTemplateArgs(),
35  to(functionDecl(hasName("::std::make_pair"))))
36  .bind("declref")))))
37  .bind("call"),
38  this);
39 }
40 
41 void ExplicitMakePairCheck::check(const MatchFinder::MatchResult &Result) {
42  const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
43  const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declref");
44 
45  // Sanity check: The use might have overriden ::std::make_pair.
46  if (Call->getNumArgs() != 2)
47  return;
48 
49  const Expr *Arg0 = Call->getArg(0)->IgnoreParenImpCasts();
50  const Expr *Arg1 = Call->getArg(1)->IgnoreParenImpCasts();
51 
52  // If types don't match, we suggest replacing with std::pair and explicit
53  // template arguments. Otherwise just remove the template arguments from
54  // make_pair.
55  if (Arg0->getType() != Call->getArg(0)->getType() ||
56  Arg1->getType() != Call->getArg(1)->getType()) {
57  diag(Call->getBeginLoc(), "for C++11-compatibility, use pair directly")
58  << FixItHint::CreateReplacement(
59  SourceRange(DeclRef->getBeginLoc(), DeclRef->getLAngleLoc()),
60  "std::pair<");
61  } else {
62  diag(Call->getBeginLoc(),
63  "for C++11-compatibility, omit template arguments from make_pair")
64  << FixItHint::CreateRemoval(
65  SourceRange(DeclRef->getLAngleLoc(), DeclRef->getRAngleLoc()));
66  }
67 }
68 
69 } // namespace build
70 } // namespace google
71 } // namespace tidy
72 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
ExplicitMakePairCheck.h
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:258
DeclRef
const DeclRefExpr * DeclRef
Definition: UseAfterMoveCheck.cpp:50
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27