clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ExplicitMakePairCheck.cpp
Go to the documentation of this file.
1 //===--- 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"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace {
19 AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
20 return Node.hasExplicitTemplateArgs();
21 }
22 } // namespace
23
24 namespace tidy {
25 namespace google {
26 namespace build {
27
28 void
29 ExplicitMakePairCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
30 // 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
35 // Look for std::make_pair with explicit template args. Ignore calls in
36 // templates.
37 Finder->addMatcher(
38 callExpr(unless(isInTemplateInstantiation()),
39 callee(expr(ignoringParenImpCasts(
40 declRefExpr(hasExplicitTemplateArgs(),
41 to(functionDecl(hasName("::std::make_pair"))))
42 .bind("declref"))))).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->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
75 } // namespace google
76 } // namespace tidy
77 } // namespace clang
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
clang::tidy::AST_MATCHER
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)
Definition: ProBoundsArrayToPointerDecayCheck.cpp:25
ExplicitMakePairCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121