clang-tools  3.9.0
ShrinkToFitCheck.cpp
Go to the documentation of this file.
1 //===--- ShrinkToFitCheck.cpp - clang-tidy---------------------------------===//
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 "ShrinkToFitCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 #include "llvm/ADT/StringRef.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace modernize {
21 
22 void ShrinkToFitCheck::registerMatchers(MatchFinder *Finder) {
23  // Swap as a function need not to be considered, because rvalue can not
24  // be bound to a non-const reference.
25  const auto ShrinkableAsMember =
26  memberExpr(member(valueDecl().bind("ContainerDecl")));
27  const auto ShrinkableAsDecl =
28  declRefExpr(hasDeclaration(valueDecl().bind("ContainerDecl")));
29  const auto CopyCtorCall = cxxConstructExpr(hasArgument(
30  0, anyOf(ShrinkableAsMember, ShrinkableAsDecl,
31  unaryOperator(has(ignoringParenImpCasts(ShrinkableAsMember))),
32  unaryOperator(has(ignoringParenImpCasts(ShrinkableAsDecl))))));
33  const auto SwapParam =
34  expr(anyOf(memberExpr(member(equalsBoundNode("ContainerDecl"))),
35  declRefExpr(hasDeclaration(equalsBoundNode("ContainerDecl"))),
36  unaryOperator(has(ignoringParenImpCasts(
37  memberExpr(member(equalsBoundNode("ContainerDecl")))))),
38  unaryOperator(has(ignoringParenImpCasts(declRefExpr(
39  hasDeclaration(equalsBoundNode("ContainerDecl"))))))));
40 
41  Finder->addMatcher(
42  cxxMemberCallExpr(
43  on(hasType(namedDecl(
44  hasAnyName("std::basic_string", "std::deque", "std::vector")))),
45  callee(cxxMethodDecl(hasName("swap"))),
46  has(ignoringParenImpCasts(memberExpr(hasDescendant(CopyCtorCall)))),
47  hasArgument(0, SwapParam.bind("ContainerToShrink")),
48  unless(isInTemplateInstantiation()))
49  .bind("CopyAndSwapTrick"),
50  this);
51 }
52 
53 void ShrinkToFitCheck::check(const MatchFinder::MatchResult &Result) {
54  const LangOptions &Opts = Result.Context->getLangOpts();
55 
56  if (!Opts.CPlusPlus11)
57  return;
58 
59  const auto *MemberCall =
60  Result.Nodes.getNodeAs<CXXMemberCallExpr>("CopyAndSwapTrick");
61  const auto *Container = Result.Nodes.getNodeAs<Expr>("ContainerToShrink");
62  FixItHint Hint;
63 
64  if (!MemberCall->getLocStart().isMacroID()) {
65  std::string ReplacementText;
66  if (const auto *UnaryOp = llvm::dyn_cast<UnaryOperator>(Container)) {
67  ReplacementText =
68  Lexer::getSourceText(CharSourceRange::getTokenRange(
69  UnaryOp->getSubExpr()->getSourceRange()),
70  *Result.SourceManager, Opts);
71  ReplacementText += "->shrink_to_fit()";
72  } else {
73  ReplacementText = Lexer::getSourceText(
74  CharSourceRange::getTokenRange(Container->getSourceRange()),
75  *Result.SourceManager, Opts);
76  ReplacementText += ".shrink_to_fit()";
77  }
78 
79  Hint = FixItHint::CreateReplacement(MemberCall->getSourceRange(),
80  ReplacementText);
81  }
82 
83  diag(MemberCall->getLocStart(), "the shrink_to_fit method should be used "
84  "to reduce the capacity of a shrinkable "
85  "container")
86  << Hint;
87 }
88 
89 } // namespace modernize
90 } // namespace tidy
91 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
const NamedDecl * Result
Definition: USRFinder.cpp:137