11#include "clang/AST/Expr.h"
12#include "clang/AST/ExprCXX.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Analysis/CFG.h"
15#include "clang/Lex/Lexer.h"
16#include "llvm/ADT/STLExtras.h"
18#include "../utils/ExprSequence.h"
19#include "../utils/Matchers.h"
27using matchers::hasUnevaluatedContext;
42class UseAfterMoveFinder {
44 UseAfterMoveFinder(ASTContext *TheContext);
50 bool find(Stmt *CodeBlock,
const Expr *MovingCall,
51 const ValueDecl *MovedVariable, UseAfterMove *TheUseAfterMove);
54 bool findInternal(
const CFGBlock *Block,
const Expr *MovingCall,
55 const ValueDecl *MovedVariable,
56 UseAfterMove *TheUseAfterMove);
57 void getUsesAndReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
59 llvm::SmallPtrSetImpl<const Stmt *> *Reinits);
60 void getDeclRefs(
const CFGBlock *Block,
const Decl *MovedVariable,
61 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
62 void getReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
63 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
64 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
67 std::unique_ptr<ExprSequence> Sequence;
68 std::unique_ptr<StmtToBlockMap> BlockMap;
69 llvm::SmallPtrSet<const CFGBlock *, 8> Visited;
82 return anyOf(hasAncestor(typeLoc()),
83 hasAncestor(declRefExpr(
84 to(functionDecl(ast_matchers::isTemplateInstantiation())))),
85 hasAncestor(expr(hasUnevaluatedContext())));
88UseAfterMoveFinder::UseAfterMoveFinder(ASTContext *TheContext)
89 : Context(TheContext) {}
91bool UseAfterMoveFinder::find(Stmt *CodeBlock,
const Expr *MovingCall,
92 const ValueDecl *MovedVariable,
93 UseAfterMove *TheUseAfterMove) {
101 CFG::BuildOptions Options;
102 Options.AddImplicitDtors =
true;
103 Options.AddTemporaryDtors =
true;
104 std::unique_ptr<CFG> TheCFG =
105 CFG::buildCFG(
nullptr, CodeBlock, Context, Options);
109 Sequence = std::make_unique<ExprSequence>(TheCFG.get(), CodeBlock, Context);
110 BlockMap = std::make_unique<StmtToBlockMap>(TheCFG.get(), Context);
113 const CFGBlock *Block = BlockMap->blockContainingStmt(MovingCall);
118 Block = &TheCFG->getEntry();
121 return findInternal(Block, MovingCall, MovedVariable, TheUseAfterMove);
124bool UseAfterMoveFinder::findInternal(
const CFGBlock *Block,
125 const Expr *MovingCall,
126 const ValueDecl *MovedVariable,
127 UseAfterMove *TheUseAfterMove) {
128 if (Visited.count(Block))
134 Visited.insert(Block);
137 llvm::SmallVector<const DeclRefExpr *, 1> Uses;
138 llvm::SmallPtrSet<const Stmt *, 1> Reinits;
139 getUsesAndReinits(Block, MovedVariable, &Uses, &Reinits);
145 llvm::SmallVector<const Stmt *, 1> ReinitsToDelete;
146 for (
const Stmt *Reinit : Reinits) {
147 if (MovingCall && Reinit != MovingCall &&
148 Sequence->potentiallyAfter(MovingCall, Reinit))
149 ReinitsToDelete.push_back(Reinit);
151 for (
const Stmt *Reinit : ReinitsToDelete) {
152 Reinits.erase(Reinit);
156 for (
const DeclRefExpr *Use : Uses) {
157 if (!MovingCall || Sequence->potentiallyAfter(Use, MovingCall)) {
161 bool HaveSavingReinit =
false;
162 for (
const Stmt *Reinit : Reinits) {
163 if (!Sequence->potentiallyAfter(Reinit, Use))
164 HaveSavingReinit =
true;
167 if (!HaveSavingReinit) {
168 TheUseAfterMove->DeclRef = Use;
174 TheUseAfterMove->EvaluationOrderUndefined =
175 MovingCall !=
nullptr &&
176 Sequence->potentiallyAfter(MovingCall, Use);
185 if (Reinits.empty()) {
186 for (
const auto &Succ : Block->succs()) {
187 if (Succ && findInternal(Succ,
nullptr, MovedVariable, TheUseAfterMove))
195void UseAfterMoveFinder::getUsesAndReinits(
196 const CFGBlock *Block,
const ValueDecl *MovedVariable,
198 llvm::SmallPtrSetImpl<const Stmt *> *Reinits) {
199 llvm::SmallPtrSet<const DeclRefExpr *, 1> DeclRefs;
200 llvm::SmallPtrSet<const DeclRefExpr *, 1> ReinitDeclRefs;
202 getDeclRefs(Block, MovedVariable, &DeclRefs);
203 getReinits(Block, MovedVariable, Reinits, &ReinitDeclRefs);
207 for (
const DeclRefExpr *
DeclRef : DeclRefs) {
208 if (!ReinitDeclRefs.count(
DeclRef))
213 llvm::sort(*Uses, [](
const DeclRefExpr *D1,
const DeclRefExpr *D2) {
214 return D1->getExprLoc() < D2->getExprLoc();
219 const Type *TheType = VD->getType().getNonReferenceType().getTypePtrOrNull();
223 const CXXRecordDecl *RecordDecl = TheType->getAsCXXRecordDecl();
227 const IdentifierInfo *
ID = RecordDecl->getIdentifier();
231 StringRef
Name =
ID->getName();
232 if (
Name !=
"unique_ptr" &&
Name !=
"shared_ptr" &&
Name !=
"weak_ptr")
235 return RecordDecl->getDeclContext()->isStdNamespace();
238void UseAfterMoveFinder::getDeclRefs(
239 const CFGBlock *Block,
const Decl *MovedVariable,
240 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
242 for (
const auto &Elem : *Block) {
243 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
247 auto AddDeclRefs = [
this, Block,
248 DeclRefs](
const ArrayRef<BoundNodes> Matches) {
249 for (
const auto &Match : Matches) {
250 const auto *
DeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
251 const auto *Operator = Match.getNodeAs<CXXOperatorCallExpr>(
"operator");
255 if (Operator || !isStandardSmartPointer(
DeclRef->getDecl())) {
262 auto DeclRefMatcher = declRefExpr(hasDeclaration(equalsNode(MovedVariable)),
266 AddDeclRefs(
match(traverse(TK_AsIs, findAll(DeclRefMatcher)), *S->getStmt(),
268 AddDeclRefs(
match(findAll(cxxOperatorCallExpr(
269 hasAnyOverloadedOperatorName(
"*",
"->",
"[]"),
270 hasArgument(0, DeclRefMatcher))
272 *S->getStmt(), *Context));
276void UseAfterMoveFinder::getReinits(
277 const CFGBlock *Block,
const ValueDecl *MovedVariable,
278 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
279 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
280 auto DeclRefMatcher =
281 declRefExpr(hasDeclaration(equalsNode(MovedVariable))).bind(
"declref");
283 auto StandardContainerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
284 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
285 "::std::basic_string",
"::std::vector",
"::std::deque",
286 "::std::forward_list",
"::std::list",
"::std::set",
"::std::map",
287 "::std::multiset",
"::std::multimap",
"::std::unordered_set",
288 "::std::unordered_map",
"::std::unordered_multiset",
289 "::std::unordered_multimap"))))));
291 auto StandardSmartPointerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
292 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
293 "::std::unique_ptr",
"::std::shared_ptr",
"::std::weak_ptr"))))));
301 binaryOperation(hasOperatorName(
"="), hasLHS(DeclRefMatcher)),
304 declStmt(hasDescendant(equalsNode(MovedVariable))),
307 on(expr(DeclRefMatcher, StandardContainerTypeMatcher)),
313 callee(cxxMethodDecl(hasAnyName(
"clear",
"assign")))),
316 on(expr(DeclRefMatcher, StandardSmartPointerTypeMatcher)),
317 callee(cxxMethodDecl(hasName(
"reset")))),
321 callee(cxxMethodDecl(hasAttr(clang::attr::Reinitializes)))),
323 callExpr(forEachArgumentWithParam(
324 unaryOperator(hasOperatorName(
"&"),
325 hasUnaryOperand(DeclRefMatcher)),
326 unless(parmVarDecl(hasType(pointsTo(isConstQualified())))))),
329 callExpr(forEachArgumentWithParam(
330 traverse(TK_AsIs, DeclRefMatcher),
331 unless(parmVarDecl(hasType(
332 references(qualType(isConstQualified())))))),
333 unless(callee(functionDecl(
334 hasAnyName(
"::std::move",
"::std::forward")))))))
339 for (
const auto &Elem : *Block) {
340 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
344 SmallVector<BoundNodes, 1> Matches =
345 match(findAll(ReinitMatcher), *S->getStmt(), *Context);
347 for (
const auto &Match : Matches) {
348 const auto *TheStmt = Match.getNodeAs<Stmt>(
"reinit");
349 const auto *TheDeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
350 if (TheStmt && BlockMap->blockContainingStmt(TheStmt) == Block) {
351 Stmts->insert(TheStmt);
357 DeclRefs->insert(TheDeclRef);
369 if (FuncDecl->getName() ==
"move")
370 return MoveType::Move;
371 if (FuncDecl->getName() ==
"forward")
372 return MoveType::Forward;
374 llvm_unreachable(
"Invalid move type");
380 const SourceLocation UseLoc = Use.DeclRef->getExprLoc();
381 const SourceLocation MoveLoc = MovingCall->getExprLoc();
383 const bool IsMove = (
Type == MoveType::Move);
385 Check->
diag(UseLoc,
"'%0' used after it was %select{forwarded|moved}1")
386 << MoveArg->getDecl()->getName() << IsMove;
387 Check->
diag(MoveLoc,
"%select{forward|move}0 occurred here",
390 if (Use.EvaluationOrderUndefined) {
393 "the use and %select{forward|move}0 are unsequenced, i.e. "
394 "there is no guarantee about the order in which they are evaluated",
397 }
else if (UseLoc < MoveLoc || Use.DeclRef == MoveArg) {
399 "the use happens in a later loop iteration than the "
400 "%select{forward|move}0",
406void UseAfterMoveCheck::registerMatchers(MatchFinder *Finder) {
411 auto TryEmplaceMatcher =
412 cxxMemberCallExpr(callee(cxxMethodDecl(hasName(
"try_emplace"))));
413 auto CallMoveMatcher =
414 callExpr(argumentCountIs(1),
415 callee(functionDecl(hasAnyName(
"::std::move",
"::std::forward"))
417 hasArgument(0, declRefExpr().bind(
"arg")),
419 unless(hasParent(TryEmplaceMatcher)), expr().bind(
"call-move"),
420 anyOf(hasAncestor(compoundStmt(
421 hasParent(lambdaExpr().bind(
"containing-lambda")))),
422 hasAncestor(functionDecl(anyOf(
424 hasAnyConstructorInitializer(withInitializer(
425 expr(anyOf(equalsBoundNode(
"call-move"),
427 equalsBoundNode(
"call-move")))))
428 .bind(
"containing-ctor-init"))))
429 .bind(
"containing-ctor"),
430 functionDecl().bind(
"containing-func"))))));
439 forEach(expr(ignoringParenImpCasts(CallMoveMatcher))),
446 unless(initListExpr()),
447 unless(expr(ignoringParenImpCasts(equalsBoundNode(
"call-move")))))
448 .bind(
"moving-call")),
452void UseAfterMoveCheck::check(
const MatchFinder::MatchResult &Result) {
453 const auto *ContainingCtor =
454 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"containing-ctor");
455 const auto *ContainingCtorInit =
456 Result.Nodes.getNodeAs<Expr>(
"containing-ctor-init");
457 const auto *ContainingLambda =
458 Result.Nodes.getNodeAs<LambdaExpr>(
"containing-lambda");
459 const auto *ContainingFunc =
460 Result.Nodes.getNodeAs<FunctionDecl>(
"containing-func");
461 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
462 const auto *MovingCall = Result.Nodes.getNodeAs<Expr>(
"moving-call");
463 const auto *Arg = Result.Nodes.getNodeAs<DeclRefExpr>(
"arg");
464 const auto *MoveDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"move-decl");
466 if (!MovingCall || !MovingCall->getExprLoc().isValid())
467 MovingCall = CallMove;
471 if (!Arg->getDecl()->getDeclContext()->isFunctionOrMethod())
475 llvm::SmallVector<Stmt *> CodeBlocks{};
476 if (ContainingCtor) {
477 CodeBlocks.push_back(ContainingCtor->getBody());
478 if (ContainingCtorInit) {
480 bool BeforeMove{
true};
481 for (CXXCtorInitializer *Init : ContainingCtor->inits()) {
482 if (BeforeMove && Init->getInit()->IgnoreImplicit() ==
483 ContainingCtorInit->IgnoreImplicit())
486 CodeBlocks.push_back(Init->getInit());
489 }
else if (ContainingLambda) {
490 CodeBlocks.push_back(ContainingLambda->getBody());
491 }
else if (ContainingFunc) {
492 CodeBlocks.push_back(ContainingFunc->getBody());
495 for (Stmt *CodeBlock : CodeBlocks) {
496 UseAfterMoveFinder Finder(Result.Context);
498 if (Finder.find(CodeBlock, MovingCall, Arg->getDecl(), &Use))
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
const DeclRefExpr * DeclRef
bool EvaluationOrderUndefined
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg, const UseAfterMove &Use, ClangTidyCheck *Check, ASTContext *Context, MoveType Type)
bool isStandardSmartPointer(const ValueDecl *VD)
static MoveType determineMoveType(const FunctionDecl *FuncDecl)
static StatementMatcher inDecltypeOrTemplateArg()