130 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "MoveForwardingReferenceCheck.h"10#include "clang/Lex/Lexer.h"11 12using namespace clang::ast_matchers;13 14namespace clang::tidy::bugprone {15 16static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,17 const ParmVarDecl *ParmVar,18 const TemplateTypeParmDecl *TypeParmDecl,19 DiagnosticBuilder &Diag,20 const ASTContext &Context) {21 const SourceManager &SM = Context.getSourceManager();22 const LangOptions &LangOpts = Context.getLangOpts();23 24 const CharSourceRange CallRange =25 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(26 Callee->getBeginLoc(), Callee->getEndLoc()),27 SM, LangOpts);28 29 if (CallRange.isValid()) {30 const std::string TypeName =31 (TypeParmDecl->getIdentifier() && !TypeParmDecl->isImplicit())32 ? TypeParmDecl->getName().str()33 : (llvm::Twine("decltype(") + ParmVar->getName() + ")").str();34 35 const std::string ForwardName =36 (llvm::Twine("forward<") + TypeName + ">").str();37 38 // Create a replacement only if we see a "standard" way of calling39 // std::move(). This will hopefully prevent erroneous replacements if the40 // code does unusual things (e.g. create an alias for std::move() in41 // another namespace).42 const NestedNameSpecifier NNS = Callee->getQualifier();43 switch (NNS.getKind()) {44 case NestedNameSpecifier::Kind::Null:45 // Called as "move" (i.e. presumably the code had a "using std::move;").46 // We still conservatively put a "std::" in front of the forward because47 // we don't know whether the code also had a "using std::forward;".48 Diag << FixItHint::CreateReplacement(CallRange, "std::" + ForwardName);49 break;50 case NestedNameSpecifier::Kind::Namespace: {51 auto [Namespace, Prefix] = NNS.getAsNamespaceAndPrefix();52 if (Namespace->getName() == "std") {53 if (!Prefix) {54 // Called as "std::move".55 Diag << FixItHint::CreateReplacement(CallRange,56 "std::" + ForwardName);57 } else if (Prefix.getKind() == NestedNameSpecifier::Kind::Global) {58 // Called as "::std::move".59 Diag << FixItHint::CreateReplacement(CallRange,60 "::std::" + ForwardName);61 }62 }63 break;64 }65 default:66 return;67 }68 }69}70 71void MoveForwardingReferenceCheck::registerMatchers(MatchFinder *Finder) {72 // Matches a ParmVarDecl for a forwarding reference, i.e. a non-const rvalue73 // reference of a function template parameter type.74 auto ForwardingReferenceParmMatcher =75 parmVarDecl(76 hasType(qualType(rValueReferenceType(),77 references(templateTypeParmType(hasDeclaration(78 templateTypeParmDecl().bind("type-parm-decl")))),79 unless(references(qualType(isConstQualified()))))))80 .bind("parm-var");81 82 Finder->addMatcher(83 callExpr(callee(unresolvedLookupExpr(84 hasAnyDeclaration(namedDecl(85 hasUnderlyingDecl(hasName("::std::move")))))86 .bind("lookup")),87 argumentCountIs(1),88 hasArgument(0, ignoringParenImpCasts(declRefExpr(89 to(ForwardingReferenceParmMatcher)))))90 .bind("call-move"),91 this);92}93 94void MoveForwardingReferenceCheck::check(95 const MatchFinder::MatchResult &Result) {96 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>("call-move");97 const auto *UnresolvedLookup =98 Result.Nodes.getNodeAs<UnresolvedLookupExpr>("lookup");99 const auto *ParmVar = Result.Nodes.getNodeAs<ParmVarDecl>("parm-var");100 const auto *TypeParmDecl =101 Result.Nodes.getNodeAs<TemplateTypeParmDecl>("type-parm-decl");102 103 // Get the FunctionDecl and FunctionTemplateDecl containing the function104 // parameter.105 const auto *FuncForParam = dyn_cast<FunctionDecl>(ParmVar->getDeclContext());106 if (!FuncForParam)107 return;108 const FunctionTemplateDecl *FuncTemplate =109 FuncForParam->getDescribedFunctionTemplate();110 if (!FuncTemplate)111 return;112 113 // Check that the template type parameter belongs to the same function114 // template as the function parameter of that type. (This implies that type115 // deduction will happen on the type.)116 const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();117 if (!llvm::is_contained(*Params, TypeParmDecl))118 return;119 120 auto Diag = diag(CallMove->getExprLoc(),121 "forwarding reference passed to std::move(), which may "122 "unexpectedly cause lvalues to be moved; use "123 "std::forward() instead");124 125 replaceMoveWithForward(UnresolvedLookup, ParmVar, TypeParmDecl, Diag,126 *Result.Context);127}128 129} // namespace clang::tidy::bugprone130