10#include "clang/Lex/Lexer.h"
11#include "llvm/Support/raw_ostream.h"
20 const ParmVarDecl *ParmVar,
21 const TemplateTypeParmDecl *TypeParmDecl,
22 DiagnosticBuilder &Diag,
23 const ASTContext &Context) {
24 const SourceManager &SM = Context.getSourceManager();
25 const LangOptions &LangOpts = Context.getLangOpts();
27 CharSourceRange CallRange =
28 Lexer::makeFileCharRange(CharSourceRange::getTokenRange(
29 Callee->getBeginLoc(), Callee->getEndLoc()),
32 if (CallRange.isValid()) {
33 const std::string TypeName =
34 (TypeParmDecl->getIdentifier() && !TypeParmDecl->isImplicit())
35 ? TypeParmDecl->getName().str()
36 : (llvm::Twine(
"decltype(") + ParmVar->getName() +
")").str();
38 const std::string ForwardName =
39 (llvm::Twine(
"forward<") + TypeName +
">").str();
45 NestedNameSpecifier *NNS = Callee->getQualifier();
50 Diag << FixItHint::CreateReplacement(CallRange,
"std::" + ForwardName);
51 }
else if (
const NamespaceDecl *Namespace = NNS->getAsNamespace()) {
52 if (Namespace->getName() ==
"std") {
53 if (!NNS->getPrefix()) {
55 Diag << FixItHint::CreateReplacement(CallRange,
56 "std::" + ForwardName);
57 }
else if (NNS->getPrefix()->getKind() == NestedNameSpecifier::Global) {
59 Diag << FixItHint::CreateReplacement(CallRange,
60 "::std::" + ForwardName);
70 auto ForwardingReferenceParmMatcher =
72 hasType(qualType(rValueReferenceType(),
73 references(templateTypeParmType(hasDeclaration(
74 templateTypeParmDecl().bind(
"type-parm-decl")))),
75 unless(references(qualType(isConstQualified()))))))
79 callExpr(callee(unresolvedLookupExpr(
80 hasAnyDeclaration(namedDecl(
81 hasUnderlyingDecl(hasName(
"::std::move")))))
84 hasArgument(0, ignoringParenImpCasts(declRefExpr(
85 to(ForwardingReferenceParmMatcher)))))
91 const MatchFinder::MatchResult &Result) {
92 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
93 const auto *UnresolvedLookup =
94 Result.Nodes.getNodeAs<UnresolvedLookupExpr>(
"lookup");
95 const auto *ParmVar = Result.Nodes.getNodeAs<ParmVarDecl>(
"parm-var");
96 const auto *TypeParmDecl =
97 Result.Nodes.getNodeAs<TemplateTypeParmDecl>(
"type-parm-decl");
101 const auto *FuncForParam = dyn_cast<FunctionDecl>(ParmVar->getDeclContext());
104 const FunctionTemplateDecl *FuncTemplate =
105 FuncForParam->getDescribedFunctionTemplate();
112 const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
113 if (!llvm::is_contained(*Params, TypeParmDecl))
116 auto Diag =
diag(CallMove->getExprLoc(),
117 "forwarding reference passed to std::move(), which may "
118 "unexpectedly cause lvalues to be moved; use "
119 "std::forward() instead");
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee, const ParmVarDecl *ParmVar, const TemplateTypeParmDecl *TypeParmDecl, DiagnosticBuilder &Diag, const ASTContext &Context)