clang-tools 10.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 if (!getLangOpts().CPlusPlus11)
23 return;
24
25 // Swap as a function need not to be considered, because rvalue can not
26 // be bound to a non-const reference.
27 const auto ShrinkableAsMember =
28 memberExpr(member(valueDecl().bind("ContainerDecl")));
29 const auto ShrinkableAsDecl =
30 declRefExpr(hasDeclaration(valueDecl().bind("ContainerDecl")));
31 const auto CopyCtorCall = cxxConstructExpr(hasArgument(
32 0, anyOf(ShrinkableAsMember, ShrinkableAsDecl,
33 unaryOperator(has(ignoringParenImpCasts(ShrinkableAsMember))),
34 unaryOperator(has(ignoringParenImpCasts(ShrinkableAsDecl))))));
35 const auto SwapParam =
36 expr(anyOf(memberExpr(member(equalsBoundNode("ContainerDecl"))),
37 declRefExpr(hasDeclaration(equalsBoundNode("ContainerDecl"))),
38 unaryOperator(has(ignoringParenImpCasts(
39 memberExpr(member(equalsBoundNode("ContainerDecl")))))),
40 unaryOperator(has(ignoringParenImpCasts(declRefExpr(
41 hasDeclaration(equalsBoundNode("ContainerDecl"))))))));
42
43 Finder->addMatcher(
44 cxxMemberCallExpr(
45 on(hasType(hasCanonicalType(hasDeclaration(namedDecl(
46 hasAnyName("std::basic_string", "std::deque", "std::vector")))))),
47 callee(cxxMethodDecl(hasName("swap"))),
48 has(ignoringParenImpCasts(memberExpr(hasDescendant(CopyCtorCall)))),
49 hasArgument(0, SwapParam.bind("ContainerToShrink")),
50 unless(isInTemplateInstantiation()))
51 .bind("CopyAndSwapTrick"),
52 this);
53 }
54
55 void ShrinkToFitCheck::check(const MatchFinder::MatchResult &Result) {
56 const auto *MemberCall =
57 Result.Nodes.getNodeAs<CXXMemberCallExpr>("CopyAndSwapTrick");
58 const auto *Container = Result.Nodes.getNodeAs<Expr>("ContainerToShrink");
59 FixItHint Hint;
60
61 if (!MemberCall->getBeginLoc().isMacroID()) {
62 const LangOptions &Opts = getLangOpts();
63 std::string ReplacementText;
64 if (const auto *UnaryOp = llvm::dyn_cast<UnaryOperator>(Container)) {
65 ReplacementText =
66 Lexer::getSourceText(CharSourceRange::getTokenRange(
67 UnaryOp->getSubExpr()->getSourceRange()),
68 *Result.SourceManager, Opts);
69 ReplacementText += "->shrink_to_fit()";
70 } else {
71 ReplacementText = Lexer::getSourceText(
72 CharSourceRange::getTokenRange(Container->getSourceRange()),
73 *Result.SourceManager, Opts);
74 ReplacementText += ".shrink_to_fit()";
75 }
76
77 Hint = FixItHint::CreateReplacement(MemberCall->getSourceRange(),
78 ReplacementText);
79 }
80
81 diag(MemberCall->getBeginLoc(), "the shrink_to_fit method should be used "
82 "to reduce the capacity of a shrinkable "
83 "container")
84 << Hint;
85 }
86
87 } // namespace modernize
88 } // namespace tidy
89 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::clangd::getTokenRange
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
ShrinkToFitCheck.h