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