| 1 | //===--- MoveConstArgCheck.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 "MoveConstArgCheck.h" |
| 10 | |
| 11 | #include "clang/Lex/Lexer.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang::tidy::performance { |
| 16 | |
| 17 | static void replaceCallWithArg(const CallExpr *Call, DiagnosticBuilder &Diag, |
| 18 | const SourceManager &SM, |
| 19 | const LangOptions &LangOpts) { |
| 20 | const Expr *Arg = Call->getArg(0); |
| 21 | |
| 22 | CharSourceRange BeforeArgumentsRange = Lexer::makeFileCharRange( |
| 23 | CharSourceRange::getCharRange(Call->getBeginLoc(), Arg->getBeginLoc()), |
| 24 | SM, LangOpts); |
| 25 | CharSourceRange AfterArgumentsRange = Lexer::makeFileCharRange( |
| 26 | CharSourceRange::getCharRange(Call->getEndLoc(), |
| 27 | Call->getEndLoc().getLocWithOffset(1)), |
| 28 | SM, LangOpts); |
| 29 | |
| 30 | if (BeforeArgumentsRange.isValid() && AfterArgumentsRange.isValid()) { |
| 31 | Diag << FixItHint::CreateRemoval(BeforeArgumentsRange) |
| 32 | << FixItHint::CreateRemoval(AfterArgumentsRange); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void MoveConstArgCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 37 | Options.store(Opts, "CheckTriviallyCopyableMove", CheckTriviallyCopyableMove); |
| 38 | Options.store(Opts, "CheckMoveToConstRef", CheckMoveToConstRef); |
| 39 | } |
| 40 | |
| 41 | void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) { |
| 42 | auto MoveCallMatcher = |
| 43 | callExpr(callee(functionDecl(hasName("::std::move"))), argumentCountIs(1), |
| 44 | unless(isInTemplateInstantiation())) |
| 45 | .bind("call-move"); |
| 46 | |
| 47 | Finder->addMatcher(MoveCallMatcher, this); |
| 48 | |
| 49 | auto ConstTypeParmMatcher = |
| 50 | qualType(references(isConstQualified())).bind("invocation-parm-type"); |
| 51 | auto RValueTypeParmMatcher = |
| 52 | qualType(rValueReferenceType()).bind("invocation-parm-type"); |
| 53 | // Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr. |
| 54 | auto ArgumentWithParamMatcher = forEachArgumentWithParam( |
| 55 | MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher), |
| 56 | hasType(RValueTypeParmMatcher))) |
| 57 | .bind("invocation-parm")); |
| 58 | // Matches respective types of arguments for a CallExpr or CXXConstructExpr |
| 59 | // and it works on calls through function pointers as well. |
| 60 | auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType( |
| 61 | MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher)); |
| 62 | |
| 63 | Finder->addMatcher( |
| 64 | invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher)) |
| 65 | .bind("receiving-expr"), |
| 66 | this); |
| 67 | } |
| 68 | |
| 69 | bool IsRValueReferenceParam(const Expr *Invocation, |
| 70 | const QualType *InvocationParmType, |
| 71 | const Expr *Arg) { |
| 72 | if (Invocation && (*InvocationParmType)->isRValueReferenceType() && |
| 73 | Arg->isLValue()) { |
| 74 | if (!Invocation->getType()->isRecordType()) |
| 75 | return true; |
| 76 | else { |
| 77 | if (const auto *ConstructCallExpr = |
| 78 | dyn_cast<CXXConstructExpr>(Invocation)) { |
| 79 | if (const auto *ConstructorDecl = ConstructCallExpr->getConstructor()) { |
| 80 | if (!ConstructorDecl->isCopyOrMoveConstructor() && |
| 81 | !ConstructorDecl->isDefaultConstructor()) |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | void MoveConstArgCheck::check(const MatchFinder::MatchResult &Result) { |
| 91 | const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>("call-move"); |
| 92 | const auto *ReceivingExpr = Result.Nodes.getNodeAs<Expr>("receiving-expr"); |
| 93 | const auto *InvocationParm = |
| 94 | Result.Nodes.getNodeAs<ParmVarDecl>("invocation-parm"); |
| 95 | const auto *InvocationParmType = |
| 96 | Result.Nodes.getNodeAs<QualType>("invocation-parm-type"); |
| 97 | |
| 98 | // Skipping matchers which have been matched. |
| 99 | if (!ReceivingExpr && AlreadyCheckedMoves.contains(CallMove)) |
| 100 | return; |
| 101 | |
| 102 | if (ReceivingExpr) |
| 103 | AlreadyCheckedMoves.insert(CallMove); |
| 104 | |
| 105 | const Expr *Arg = CallMove->getArg(0); |
| 106 | SourceManager &SM = Result.Context->getSourceManager(); |
| 107 | |
| 108 | CharSourceRange MoveRange = |
| 109 | CharSourceRange::getCharRange(CallMove->getSourceRange()); |
| 110 | CharSourceRange FileMoveRange = |
| 111 | Lexer::makeFileCharRange(MoveRange, SM, getLangOpts()); |
| 112 | if (!FileMoveRange.isValid()) |
| 113 | return; |
| 114 | |
| 115 | bool IsConstArg = Arg->getType().isConstQualified(); |
| 116 | bool IsTriviallyCopyable = |
| 117 | Arg->getType().isTriviallyCopyableType(*Result.Context); |
| 118 | |
| 119 | if (IsConstArg || IsTriviallyCopyable) { |
| 120 | if (const CXXRecordDecl *R = Arg->getType()->getAsCXXRecordDecl()) { |
| 121 | // According to [expr.prim.lambda]p3, "whether the closure type is |
| 122 | // trivially copyable" property can be changed by the implementation of |
| 123 | // the language, so we shouldn't rely on it when issuing diagnostics. |
| 124 | if (R->isLambda()) |
| 125 | return; |
| 126 | // Don't warn when the type is not copyable. |
| 127 | for (const auto *Ctor : R->ctors()) { |
| 128 | if (Ctor->isCopyConstructor() && Ctor->isDeleted()) |
| 129 | return; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (!IsConstArg && IsTriviallyCopyable && !CheckTriviallyCopyableMove) |
| 134 | return; |
| 135 | |
| 136 | bool IsVariable = isa<DeclRefExpr>(Arg); |
| 137 | // std::move shouldn't be removed when an lvalue wrapped by std::move is |
| 138 | // passed to the function with an rvalue reference parameter. |
| 139 | bool IsRVRefParam = |
| 140 | IsRValueReferenceParam(ReceivingExpr, InvocationParmType, Arg); |
| 141 | const auto *Var = |
| 142 | IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() : nullptr; |
| 143 | |
| 144 | { |
| 145 | auto Diag = diag(FileMoveRange.getBegin(), |
| 146 | "std::move of the %select{|const }0" |
| 147 | "%select{expression|variable %5}1 " |
| 148 | "%select{|of the trivially-copyable type %6 }2" |
| 149 | "has no effect%select{; remove std::move()|}3" |
| 150 | "%select{| or make the variable non-const}4") |
| 151 | << IsConstArg << IsVariable << IsTriviallyCopyable |
| 152 | << IsRVRefParam |
| 153 | << (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var |
| 154 | << Arg->getType(); |
| 155 | if (!IsRVRefParam) |
| 156 | replaceCallWithArg(CallMove, Diag, SM, getLangOpts()); |
| 157 | } |
| 158 | if (IsRVRefParam) { |
| 159 | // Generate notes for an invocation with an rvalue reference parameter. |
| 160 | const auto *ReceivingCallExpr = dyn_cast<CallExpr>(ReceivingExpr); |
| 161 | const auto *ReceivingConstructExpr = |
| 162 | dyn_cast<CXXConstructExpr>(ReceivingExpr); |
| 163 | // Skipping the invocation which is a template instantiation. |
| 164 | if ((!ReceivingCallExpr || !ReceivingCallExpr->getDirectCallee() || |
| 165 | ReceivingCallExpr->getDirectCallee()->isTemplateInstantiation()) && |
| 166 | (!ReceivingConstructExpr || |
| 167 | !ReceivingConstructExpr->getConstructor() || |
| 168 | ReceivingConstructExpr->getConstructor()->isTemplateInstantiation())) |
| 169 | return; |
| 170 | |
| 171 | const NamedDecl *FunctionName = nullptr; |
| 172 | FunctionName = |
| 173 | ReceivingCallExpr |
| 174 | ? ReceivingCallExpr->getDirectCallee()->getUnderlyingDecl() |
| 175 | : ReceivingConstructExpr->getConstructor()->getUnderlyingDecl(); |
| 176 | |
| 177 | QualType NoRefType = (*InvocationParmType)->getPointeeType(); |
| 178 | PrintingPolicy PolicyWithSuppressedTag(getLangOpts()); |
| 179 | PolicyWithSuppressedTag.SuppressTagKeyword = true; |
| 180 | PolicyWithSuppressedTag.SuppressUnwrittenScope = true; |
| 181 | std::string ExpectParmTypeName = |
| 182 | NoRefType.getAsString(PolicyWithSuppressedTag); |
| 183 | if (!NoRefType->isPointerType()) { |
| 184 | NoRefType.addConst(); |
| 185 | ExpectParmTypeName = |
| 186 | NoRefType.getAsString(PolicyWithSuppressedTag) + " &"; |
| 187 | } |
| 188 | |
| 189 | diag(InvocationParm->getLocation(), |
| 190 | "consider changing the %ordinal0 parameter of %1 from %2 to '%3'", |
| 191 | DiagnosticIDs::Note) |
| 192 | << (InvocationParm->getFunctionScopeIndex() + 1) << FunctionName |
| 193 | << *InvocationParmType << ExpectParmTypeName; |
| 194 | } |
| 195 | } else if (ReceivingExpr && CheckMoveToConstRef) { |
| 196 | if ((*InvocationParmType)->isRValueReferenceType()) |
| 197 | return; |
| 198 | |
| 199 | auto Diag = diag(FileMoveRange.getBegin(), |
| 200 | "passing result of std::move() as a const reference " |
| 201 | "argument; no move will actually happen"); |
| 202 | |
| 203 | replaceCallWithArg(CallMove, Diag, SM, getLangOpts()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | } // namespace clang::tidy::performance |