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(hasName(
"::std::move")))))))
338 for (
const auto &Elem : *Block) {
339 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
343 SmallVector<BoundNodes, 1> Matches =
344 match(findAll(ReinitMatcher), *S->getStmt(), *Context);
346 for (
const auto &Match : Matches) {
347 const auto *TheStmt = Match.getNodeAs<Stmt>(
"reinit");
348 const auto *TheDeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
349 if (TheStmt && BlockMap->blockContainingStmt(TheStmt) == Block) {
350 Stmts->insert(TheStmt);
356 DeclRefs->insert(TheDeclRef);
364 ASTContext *Context) {
365 SourceLocation UseLoc = Use.DeclRef->getExprLoc();
366 SourceLocation MoveLoc = MovingCall->getExprLoc();
368 Check->
diag(UseLoc,
"'%0' used after it was moved")
369 << MoveArg->getDecl()->getName();
370 Check->
diag(MoveLoc,
"move occurred here", DiagnosticIDs::Note);
371 if (Use.EvaluationOrderUndefined) {
373 "the use and move are unsequenced, i.e. there is no guarantee "
374 "about the order in which they are evaluated",
375 DiagnosticIDs::Note);
376 }
else if (UseLoc < MoveLoc || Use.DeclRef == MoveArg) {
378 "the use happens in a later loop iteration than the move",
379 DiagnosticIDs::Note);
383void UseAfterMoveCheck::registerMatchers(MatchFinder *Finder) {
388 auto TryEmplaceMatcher =
389 cxxMemberCallExpr(callee(cxxMethodDecl(hasName(
"try_emplace"))));
390 auto CallMoveMatcher =
391 callExpr(argumentCountIs(1), callee(functionDecl(hasName(
"::std::move"))),
392 hasArgument(0, declRefExpr().bind(
"arg")),
394 unless(hasParent(TryEmplaceMatcher)), expr().bind(
"call-move"),
395 anyOf(hasAncestor(compoundStmt(
396 hasParent(lambdaExpr().bind(
"containing-lambda")))),
397 hasAncestor(functionDecl(anyOf(
399 hasAnyConstructorInitializer(withInitializer(
400 expr(anyOf(equalsBoundNode(
"call-move"),
402 equalsBoundNode(
"call-move")))))
403 .bind(
"containing-ctor-init"))))
404 .bind(
"containing-ctor"),
405 functionDecl().bind(
"containing-func"))))));
414 forEach(expr(ignoringParenImpCasts(CallMoveMatcher))),
421 unless(initListExpr()),
422 unless(expr(ignoringParenImpCasts(equalsBoundNode(
"call-move")))))
423 .bind(
"moving-call")),
427void UseAfterMoveCheck::check(
const MatchFinder::MatchResult &Result) {
428 const auto *ContainingCtor =
429 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"containing-ctor");
430 const auto *ContainingCtorInit =
431 Result.Nodes.getNodeAs<Expr>(
"containing-ctor-init");
432 const auto *ContainingLambda =
433 Result.Nodes.getNodeAs<LambdaExpr>(
"containing-lambda");
434 const auto *ContainingFunc =
435 Result.Nodes.getNodeAs<FunctionDecl>(
"containing-func");
436 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
437 const auto *MovingCall = Result.Nodes.getNodeAs<Expr>(
"moving-call");
438 const auto *Arg = Result.Nodes.getNodeAs<DeclRefExpr>(
"arg");
440 if (!MovingCall || !MovingCall->getExprLoc().isValid())
441 MovingCall = CallMove;
445 if (!Arg->getDecl()->getDeclContext()->isFunctionOrMethod())
449 llvm::SmallVector<Stmt *> CodeBlocks{};
450 if (ContainingCtor) {
451 CodeBlocks.push_back(ContainingCtor->getBody());
452 if (ContainingCtorInit) {
454 bool BeforeMove{
true};
455 for (CXXCtorInitializer *Init : ContainingCtor->inits()) {
456 if (BeforeMove && Init->getInit()->IgnoreImplicit() ==
457 ContainingCtorInit->IgnoreImplicit())
460 CodeBlocks.push_back(Init->getInit());
463 }
else if (ContainingLambda) {
464 CodeBlocks.push_back(ContainingLambda->getBody());
465 }
else if (ContainingFunc) {
466 CodeBlocks.push_back(ContainingFunc->getBody());
469 for (Stmt *CodeBlock : CodeBlocks) {
470 UseAfterMoveFinder Finder(Result.Context);
472 if (Finder.find(CodeBlock, MovingCall, Arg->getDecl(), &Use))
const FunctionDecl * Decl
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)
bool isStandardSmartPointer(const ValueDecl *VD)
static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg, const UseAfterMove &Use, ClangTidyCheck *Check, ASTContext *Context)
static StatementMatcher inDecltypeOrTemplateArg()