11#include "clang/AST/Attr.h"
12#include "clang/AST/Expr.h"
13#include "clang/AST/ExprCXX.h"
14#include "clang/ASTMatchers/ASTMatchers.h"
15#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
16#include "clang/Analysis/CFG.h"
17#include "clang/Lex/Lexer.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallPtrSet.h"
31using matchers::hasUnevaluatedContext;
35 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
36 const Expr *E = &Node;
38 const DynTypedNodeList
Parents = Finder->getASTContext().getParents(*E);
44 }
while (isa<ImplicitCastExpr, ParenExpr>(E));
46 return InnerMatcher.matches(*E, Finder, Builder);
52 const DeclRefExpr *DeclRef;
55 bool EvaluationOrderUndefined =
false;
60 bool UseHappensInLaterLoopIteration =
false;
65class UseAfterMoveFinder {
67 UseAfterMoveFinder(ASTContext *TheContext,
68 llvm::ArrayRef<StringRef> InvalidationFunctions,
69 llvm::ArrayRef<StringRef> ReinitializationFunctions,
70 const CXXRecordDecl *MovedAs);
76 std::optional<UseAfterMove> find(Stmt *CodeBlock,
const Expr *MovingCall,
77 const DeclRefExpr *MovedVariable);
80 std::optional<UseAfterMove> findInternal(
const CFGBlock *Block,
81 const Expr *MovingCall,
82 const ValueDecl *MovedVariable);
83 void getUsesAndReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
84 SmallVectorImpl<const DeclRefExpr *> *Uses,
85 llvm::SmallPtrSetImpl<const Stmt *> *Reinits);
86 void getDeclRefs(
const CFGBlock *Block,
const Decl *MovedVariable,
87 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
88 void getReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
89 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
90 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
93 llvm::ArrayRef<StringRef> InvalidationFunctions;
94 llvm::ArrayRef<StringRef> ReinitializationFunctions;
95 const CXXRecordDecl *MovedAs;
96 std::unique_ptr<ExprSequence> Sequence;
97 std::unique_ptr<StmtToBlockMap> BlockMap;
98 llvm::SmallPtrSet<const CFGBlock *, 8> Visited;
104 return anyOf(hasAnyName(
"::std::move",
"::std::forward"),
108static StatementMatcher
110 llvm::ArrayRef<StringRef> InvalidationFunctions,
111 llvm::ArrayRef<StringRef> ReinitializationFunctions) {
112 const auto DeclRefMatcher =
113 declRefExpr(hasDeclaration(equalsNode(MovedVariable))).bind(
"declref");
115 const auto StandardContainerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
116 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
117 "::std::basic_string",
"::std::vector",
"::std::deque",
118 "::std::forward_list",
"::std::list",
"::std::set",
"::std::map",
119 "::std::multiset",
"::std::multimap",
"::std::unordered_set",
120 "::std::unordered_map",
"::std::unordered_multiset",
121 "::std::unordered_multimap"))))));
123 const auto StandardResettableOwnerTypeMatcher = hasType(
124 hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(
125 hasAnyName(
"::std::unique_ptr",
"::std::shared_ptr",
126 "::std::weak_ptr",
"::std::optional",
"::std::any"))))));
135 binaryOperation(hasOperatorName(
"="),
136 hasLHS(ignoringParenImpCasts(DeclRefMatcher))),
141 hasOperatorName(
"="),
142 hasLHS(ignoringImplicit(ignoringParenImpCasts(
143 callExpr(callee(functionDecl(hasName(
"::std::tie"))),
144 hasAnyArgument(ignoringParenImpCasts(
145 DeclRefMatcher))))))),
148 declStmt(hasDescendant(equalsNode(MovedVariable))),
151 on(expr(DeclRefMatcher, StandardContainerTypeMatcher)),
157 callee(cxxMethodDecl(hasAnyName(
"clear",
"assign")))),
159 cxxMemberCallExpr(on(expr(DeclRefMatcher,
160 StandardResettableOwnerTypeMatcher)),
161 callee(cxxMethodDecl(hasName(
"reset")))),
165 callee(cxxMethodDecl(hasAttr(attr::Reinitializes)))),
170 ReinitializationFunctions))),
171 anyOf(cxxMemberCallExpr(on(DeclRefMatcher)),
172 callExpr(unless(cxxMemberCallExpr()),
173 hasArgument(0, DeclRefMatcher)))),
175 callExpr(forEachArgumentWithParam(
176 unaryOperator(hasOperatorName(
"&"),
177 hasUnaryOperand(DeclRefMatcher)),
179 parmVarDecl(hasType(pointsTo(isConstQualified())))))),
182 callExpr(forEachArgumentWithParam(
183 traverse(TK_AsIs, DeclRefMatcher),
184 unless(parmVarDecl(hasType(
185 references(qualType(isConstQualified())))))),
186 unless(callee(functionDecl(
199 return anyOf(hasAncestor(typeLoc()),
200 hasAncestor(declRefExpr(
201 to(functionDecl(ast_matchers::isTemplateInstantiation())))),
202 hasAncestor(expr(hasUnevaluatedContext())));
205UseAfterMoveFinder::UseAfterMoveFinder(
206 ASTContext *TheContext, llvm::ArrayRef<StringRef> InvalidationFunctions,
207 llvm::ArrayRef<StringRef> ReinitializationFunctions,
208 const CXXRecordDecl *MovedAs)
209 : Context(TheContext), InvalidationFunctions(InvalidationFunctions),
210 ReinitializationFunctions(ReinitializationFunctions), MovedAs(MovedAs) {}
212std::optional<UseAfterMove>
213UseAfterMoveFinder::find(Stmt *
CodeBlock,
const Expr *MovingCall,
214 const DeclRefExpr *MovedVariable) {
222 CFG::BuildOptions Options;
223 Options.AddImplicitDtors =
true;
224 Options.AddTemporaryDtors =
true;
225 std::unique_ptr<CFG> TheCFG =
226 CFG::buildCFG(
nullptr,
CodeBlock, Context, Options);
230 Sequence = std::make_unique<ExprSequence>(TheCFG.get(),
CodeBlock, Context);
231 BlockMap = std::make_unique<StmtToBlockMap>(TheCFG.get(), Context);
234 const CFGBlock *MoveBlock = BlockMap->blockContainingStmt(MovingCall);
239 MoveBlock = &TheCFG->getEntry();
242 auto TheUseAfterMove =
243 findInternal(MoveBlock, MovingCall, MovedVariable->getDecl());
245 if (TheUseAfterMove) {
246 if (
const CFGBlock *UseBlock =
247 BlockMap->blockContainingStmt(TheUseAfterMove->DeclRef)) {
253 CFGReverseBlockReachabilityAnalysis CFA(*TheCFG);
254 TheUseAfterMove->UseHappensInLaterLoopIteration =
255 UseBlock == MoveBlock ? Visited.contains(UseBlock)
256 : CFA.isReachable(UseBlock, MoveBlock);
259 return TheUseAfterMove;
262std::optional<UseAfterMove>
263UseAfterMoveFinder::findInternal(
const CFGBlock *Block,
const Expr *MovingCall,
264 const ValueDecl *MovedVariable) {
265 if (Visited.contains(Block))
271 Visited.insert(Block);
274 SmallVector<const DeclRefExpr *, 1> Uses;
275 llvm::SmallPtrSet<const Stmt *, 1> Reinits;
276 getUsesAndReinits(Block, MovedVariable, &Uses, &Reinits);
282 SmallVector<const Stmt *, 1> ReinitsToDelete;
283 for (
const Stmt *Reinit : Reinits)
284 if (MovingCall && Reinit != MovingCall &&
285 Sequence->potentiallyAfter(MovingCall, Reinit))
286 ReinitsToDelete.push_back(Reinit);
287 for (
const Stmt *Reinit : ReinitsToDelete)
288 Reinits.erase(Reinit);
291 for (
const DeclRefExpr *Use : Uses) {
292 if (!MovingCall || Sequence->potentiallyAfter(Use, MovingCall)) {
296 bool HaveSavingReinit =
false;
297 for (
const Stmt *Reinit : Reinits)
298 if (!Sequence->potentiallyAfter(Reinit, Use))
299 HaveSavingReinit =
true;
301 if (!HaveSavingReinit) {
302 UseAfterMove TheUseAfterMove;
303 TheUseAfterMove.DeclRef = Use;
309 TheUseAfterMove.EvaluationOrderUndefined =
310 MovingCall !=
nullptr &&
311 Sequence->potentiallyAfter(MovingCall, Use);
313 return TheUseAfterMove;
320 if (Reinits.empty()) {
321 for (
const auto &Succ : Block->succs()) {
323 if (
auto Found = findInternal(Succ,
nullptr, MovedVariable))
332void UseAfterMoveFinder::getUsesAndReinits(
333 const CFGBlock *Block,
const ValueDecl *MovedVariable,
334 SmallVectorImpl<const DeclRefExpr *> *Uses,
335 llvm::SmallPtrSetImpl<const Stmt *> *Reinits) {
336 llvm::SmallPtrSet<const DeclRefExpr *, 1> DeclRefs;
337 llvm::SmallPtrSet<const DeclRefExpr *, 1> ReinitDeclRefs;
339 getDeclRefs(Block, MovedVariable, &DeclRefs);
340 getReinits(Block, MovedVariable, Reinits, &ReinitDeclRefs);
344 for (
const DeclRefExpr *DeclRef : DeclRefs)
345 if (!ReinitDeclRefs.contains(DeclRef))
346 Uses->push_back(DeclRef);
349 llvm::sort(*Uses, [](
const DeclRefExpr *D1,
const DeclRefExpr *D2) {
350 return D1->getExprLoc() < D2->getExprLoc();
356 if (
const auto *SL = dyn_cast<StringLiteral>(E->IgnoreParenImpCasts()))
357 return SL->getString();
365 if (Attr->getAnnotation() !=
"clang-tidy")
368 if (Attr->args_size() != 2)
372 std::optional<StringRef> Annotation =
getStringLiteral(Attr->args_begin()[1]);
374 return Plugin && Annotation && *Plugin ==
"bugprone-use-after-move" &&
375 *Annotation ==
"null_after_move";
379 const Type *TheType = VD->getType().getNonReferenceType().getTypePtrOrNull();
383 const CXXRecordDecl *RecordDecl = TheType->getAsCXXRecordDecl();
389 if (
const CXXRecordDecl *DefinitionDecl = RecordDecl->getDefinition()) {
390 for (
const auto *Attr : DefinitionDecl->specific_attrs<AnnotateAttr>())
396 const IdentifierInfo *ID = RecordDecl->getIdentifier();
400 const StringRef Name = ID->getName();
401 if (Name !=
"unique_ptr" && Name !=
"shared_ptr" && Name !=
"weak_ptr")
404 return RecordDecl->getDeclContext()->isStdNamespace();
407void UseAfterMoveFinder::getDeclRefs(
408 const CFGBlock *
Block,
const Decl *MovedVariable,
409 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
411 for (
const auto &Elem : *
Block) {
412 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
416 auto AddDeclRefs = [
this,
Block,
417 DeclRefs](
const ArrayRef<BoundNodes> Matches) {
418 for (
const auto &Match : Matches) {
419 const auto *DeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
420 const auto *Member = Match.getNodeAs<MemberExpr>(
"member-expr");
421 const auto *Operator = Match.getNodeAs<CXXOperatorCallExpr>(
"operator");
423 if (Member && MovedAs && !isa<CXXMethodDecl>(Member->getMemberDecl()) &&
424 !MovedAs->hasMemberName(Member->getMemberDecl()->getIdentifier())) {
427 if (DeclRef && BlockMap->blockContainingStmt(DeclRef) ==
Block) {
431 if (Operator || !isSpecifiedAfterMove(DeclRef->getDecl()))
432 DeclRefs->insert(DeclRef);
437 auto DeclRefMatcher =
438 declRefExpr(hasDeclaration(equalsNode(MovedVariable)),
440 unless(hasParentIgnoringParenImpCasts(
441 memberExpr(hasDeclaration(cxxDestructorDecl())))),
442 optionally(hasParentIgnoringParenImpCasts(
443 memberExpr().bind(
"member-expr"))))
446 AddDeclRefs(
match(traverse(TK_AsIs, findAll(DeclRefMatcher)), *S->getStmt(),
448 AddDeclRefs(
match(findAll(cxxOperatorCallExpr(
449 hasAnyOverloadedOperatorName(
"*",
"->",
"[]"),
450 hasArgument(0, DeclRefMatcher))
452 *S->getStmt(), *Context));
456void UseAfterMoveFinder::getReinits(
457 const CFGBlock *Block,
const ValueDecl *MovedVariable,
458 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
459 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
461 MovedVariable, InvalidationFunctions, ReinitializationFunctions);
465 for (
const auto &Elem : *Block) {
466 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
470 const SmallVector<BoundNodes, 1> Matches =
471 match(findAll(ReinitMatcher), *S->getStmt(), *Context);
473 for (
const auto &Match : Matches) {
474 const auto *TheStmt = Match.getNodeAs<Stmt>(
"reinit");
475 const auto *TheDeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
476 if (TheStmt && BlockMap->blockContainingStmt(TheStmt) == Block) {
477 Stmts->insert(TheStmt);
483 DeclRefs->insert(TheDeclRef);
502 return MoveType::Move;
503 if (
FuncDecl->getName() ==
"forward")
504 return MoveType::Forward;
507 return MoveType::Invalidation;
512 const ASTContext *Context, MoveType Type,
513 const FunctionDecl *MoveDecl) {
514 const SourceLocation UseLoc = Use.DeclRef->getExprLoc();
515 const SourceLocation MoveLoc = MovingCall->getExprLoc();
519 "'%0' used after it was %select{forwarded|moved|invalidated by %2}1")
520 << MoveArg->getDecl()->getName() << Type << MoveDecl;
521 Check->diag(MoveLoc,
"%select{forward|move|invalidation}0 occurred here",
524 if (Use.EvaluationOrderUndefined) {
527 "the use and %select{forward|move|invalidation}0 are unsequenced, i.e. "
528 "there is no guarantee about the order in which they are evaluated",
531 }
else if (Use.UseHappensInLaterLoopIteration) {
533 "the use happens in a later loop iteration than the "
534 "%select{forward|move|invalidation}0",
543 Options.get(
"InvalidationFunctions",
""))),
544 ReinitializationFunctions(
utils::
options::parseStringList(
545 Options.get(
"ReinitializationFunctions",
""))) {}
548 Options.store(Opts,
"InvalidationFunctions",
550 Options.store(Opts,
"ReinitializationFunctions",
559 auto TryEmplaceMatcher =
560 cxxMemberCallExpr(callee(cxxMethodDecl(hasName(
"try_emplace"))));
561 auto Arg = declRefExpr().bind(
"arg");
562 auto IsMemberCallee = callee(functionDecl(unless(isStaticStorageClass())));
563 auto DerivedToBaseCast =
564 implicitCastExpr(hasCastKind(CK_DerivedToBase)).bind(
"optional-cast");
565 auto CallMoveMatcher = callExpr(
568 anyOf(cxxMemberCallExpr(IsMemberCallee, on(Arg)),
569 callExpr(unless(cxxMemberCallExpr(IsMemberCallee)),
570 hasArgument(0, Arg))),
572 expr().bind(
"call-move"),
574 anyOf(hasParent(DerivedToBaseCast),
576 0, traverse(TK_AsIs, expr(hasParent(DerivedToBaseCast)))))),
577 anyOf(hasAncestor(compoundStmt(
578 hasParent(lambdaExpr().bind(
"containing-lambda")))),
579 hasAncestor(functionDecl(
580 anyOf(cxxConstructorDecl(
581 hasAnyConstructorInitializer(withInitializer(
582 expr(anyOf(equalsBoundNode(
"call-move"),
584 equalsBoundNode(
"call-move")))))
585 .bind(
"containing-ctor-init"))))
586 .bind(
"containing-ctor"),
587 functionDecl().bind(
"containing-func"))))));
596 forEach(expr(ignoringParenImpCasts(CallMoveMatcher))),
603 unless(initListExpr()),
604 unless(expr(ignoringParenImpCasts(equalsBoundNode(
"call-move")))))
605 .bind(
"moving-call")),
610 const auto *ContainingCtor =
611 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"containing-ctor");
612 const auto *ContainingCtorInit =
613 Result.Nodes.getNodeAs<Expr>(
"containing-ctor-init");
614 const auto *ContainingLambda =
615 Result.Nodes.getNodeAs<LambdaExpr>(
"containing-lambda");
616 const auto *ContainingFunc =
617 Result.Nodes.getNodeAs<FunctionDecl>(
"containing-func");
618 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
619 const auto *MovingCall = Result.Nodes.getNodeAs<Expr>(
"moving-call");
620 const auto *Arg = Result.Nodes.getNodeAs<DeclRefExpr>(
"arg");
621 const auto *MoveDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"move-decl");
622 const auto *ParentCast =
623 Result.Nodes.getNodeAs<ImplicitCastExpr>(
"optional-cast");
625 if (!MovingCall || !MovingCall->getExprLoc().isValid())
626 MovingCall = CallMove;
630 if (!Arg->getDecl()->getDeclContext()->isFunctionOrMethod())
635 if (ContainingCtor) {
636 CodeBlocks.push_back(ContainingCtor->getBody());
637 if (ContainingCtorInit) {
639 bool BeforeMove{
true};
640 for (
const CXXCtorInitializer *Init : ContainingCtor->inits()) {
641 if (BeforeMove && Init->getInit()->IgnoreImplicit() ==
642 ContainingCtorInit->IgnoreImplicit())
645 CodeBlocks.push_back(Init->getInit());
648 }
else if (ContainingLambda) {
649 CodeBlocks.push_back(ContainingLambda->getBody());
650 }
else if (ContainingFunc) {
651 CodeBlocks.push_back(ContainingFunc->getBody());
654 const CXXRecordDecl *MovedAs =
655 ParentCast ? ParentCast->getType()->getAsCXXRecordDecl() :
nullptr;
658 UseAfterMoveFinder Finder(Result.Context, InvalidationFunctions,
659 ReinitializationFunctions, MovedAs);
660 if (
auto Use = Finder.find(
CodeBlock, MovingCall, Arg))
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
UseAfterMoveCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
static StatementMatcher makeReinitMatcher(const ValueDecl *MovedVariable, llvm::ArrayRef< StringRef > InvalidationFunctions, llvm::ArrayRef< StringRef > ReinitializationFunctions)
static MoveType determineMoveType(const FunctionDecl *FuncDecl)
static bool isSpecifiedAfterMove(const ValueDecl *VD)
static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg, const UseAfterMove &Use, ClangTidyCheck *Check, const ASTContext *Context, MoveType Type, const FunctionDecl *MoveDecl)
static std::optional< StringRef > getStringLiteral(const Expr *E)
static auto getNameMatcher(llvm::ArrayRef< StringRef > InvalidationFunctions)
static StatementMatcher inDecltypeOrTemplateArg()
static bool isNullAfterMoveAnnotate(const AnnotateAttr *Attr)
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedRegexName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap
static constexpr const char FuncDecl[]