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