Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame^] | 1 | //===--- UnnecessaryCopyInitialization.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 "UnnecessaryCopyInitialization.h" |
| 11 | |
| 12 | #include "../utils/LexerUtils.h" |
| 13 | #include "../utils/Matchers.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace performance { |
| 18 | |
| 19 | using namespace ::clang::ast_matchers; |
| 20 | |
| 21 | namespace { |
| 22 | AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); } |
| 23 | AST_MATCHER(QualType, isPointerType) { return Node->isPointerType(); } |
| 24 | } // namespace |
| 25 | |
| 26 | void UnnecessaryCopyInitialization::registerMatchers( |
| 27 | ast_matchers::MatchFinder *Finder) { |
| 28 | auto ConstReference = referenceType(pointee(qualType(isConstQualified()))); |
| 29 | auto ConstOrConstReference = |
| 30 | allOf(anyOf(ConstReference, isConstQualified()), |
| 31 | unless(allOf(isPointerType(), unless(pointerType(pointee(qualType( |
| 32 | isConstQualified()))))))); |
| 33 | // Match method call expressions where the this argument is a const |
| 34 | // type or const reference. This returned const reference is highly likely to |
| 35 | // outlive the local const reference of the variable being declared. |
| 36 | // The assumption is that the const reference being returned either points |
| 37 | // to a global static variable or to a member of the called object. |
| 38 | auto ConstRefReturningMethodCallOfConstParam = cxxMemberCallExpr( |
| 39 | callee(cxxMethodDecl(returns(ConstReference))), |
| 40 | on(declRefExpr(to(varDecl(hasType(qualType(ConstOrConstReference))))))); |
| 41 | auto ConstRefReturningFunctionCall = |
| 42 | callExpr(callee(functionDecl(returns(ConstReference))), |
| 43 | unless(callee(cxxMethodDecl()))); |
| 44 | Finder->addMatcher( |
| 45 | varDecl( |
| 46 | isLocalVarDecl(), hasType(isConstQualified()), |
| 47 | hasType(matchers::isExpensiveToCopy()), |
| 48 | hasInitializer(cxxConstructExpr( |
| 49 | hasDeclaration(cxxConstructorDecl(isCopyConstructor())), |
| 50 | hasArgument(0, anyOf(ConstRefReturningFunctionCall, |
| 51 | ConstRefReturningMethodCallOfConstParam))))) |
| 52 | .bind("varDecl"), |
| 53 | this); |
| 54 | } |
| 55 | |
| 56 | void UnnecessaryCopyInitialization::check( |
| 57 | const ast_matchers::MatchFinder::MatchResult &Result) { |
| 58 | const auto *Var = Result.Nodes.getNodeAs<VarDecl>("varDecl"); |
| 59 | SourceLocation AmpLocation = Var->getLocation(); |
| 60 | auto Token = lexer_utils::getPreviousNonCommentToken(*Result.Context, |
| 61 | Var->getLocation()); |
| 62 | if (!Token.is(tok::unknown)) { |
| 63 | AmpLocation = Token.getLocation().getLocWithOffset(Token.getLength()); |
| 64 | } |
| 65 | diag(Var->getLocation(), |
| 66 | "the const qualified variable '%0' is copy-constructed from a " |
| 67 | "const reference; consider making it a const reference") |
| 68 | << Var->getName() << FixItHint::CreateInsertion(AmpLocation, "&"); |
| 69 | } |
| 70 | |
| 71 | } // namespace performance |
| 72 | } // namespace tidy |
| 73 | } // namespace clang |