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;
38 const DeclRefExpr *DeclRef;
41 bool EvaluationOrderUndefined =
false;
46 bool UseHappensInLaterLoopIteration =
false;
51class UseAfterMoveFinder {
53 UseAfterMoveFinder(ASTContext *TheContext,
54 llvm::ArrayRef<StringRef> InvalidationFunctions,
55 llvm::ArrayRef<StringRef> ReinitializationFunctions);
61 std::optional<UseAfterMove> find(Stmt *CodeBlock,
const Expr *MovingCall,
62 const DeclRefExpr *MovedVariable);
65 std::optional<UseAfterMove> findInternal(
const CFGBlock *Block,
66 const Expr *MovingCall,
67 const ValueDecl *MovedVariable);
68 void getUsesAndReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
69 SmallVectorImpl<const DeclRefExpr *> *Uses,
70 llvm::SmallPtrSetImpl<const Stmt *> *Reinits);
71 void getDeclRefs(
const CFGBlock *Block,
const Decl *MovedVariable,
72 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
73 void getReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
74 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
75 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
78 llvm::ArrayRef<StringRef> InvalidationFunctions;
79 llvm::ArrayRef<StringRef> ReinitializationFunctions;
80 std::unique_ptr<ExprSequence> Sequence;
81 std::unique_ptr<StmtToBlockMap> BlockMap;
82 llvm::SmallPtrSet<const CFGBlock *, 8> Visited;
88 return anyOf(hasAnyName(
"::std::move",
"::std::forward"),
92static StatementMatcher
94 llvm::ArrayRef<StringRef> InvalidationFunctions,
95 llvm::ArrayRef<StringRef> ReinitializationFunctions) {
96 const auto DeclRefMatcher =
97 declRefExpr(hasDeclaration(equalsNode(MovedVariable))).bind(
"declref");
99 const auto StandardContainerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
100 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
101 "::std::basic_string",
"::std::vector",
"::std::deque",
102 "::std::forward_list",
"::std::list",
"::std::set",
"::std::map",
103 "::std::multiset",
"::std::multimap",
"::std::unordered_set",
104 "::std::unordered_map",
"::std::unordered_multiset",
105 "::std::unordered_multimap"))))));
107 const auto StandardResettableOwnerTypeMatcher = hasType(
108 hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(
109 hasAnyName(
"::std::unique_ptr",
"::std::shared_ptr",
110 "::std::weak_ptr",
"::std::optional",
"::std::any"))))));
119 binaryOperation(hasOperatorName(
"="), hasLHS(DeclRefMatcher)),
122 declStmt(hasDescendant(equalsNode(MovedVariable))),
125 on(expr(DeclRefMatcher, StandardContainerTypeMatcher)),
131 callee(cxxMethodDecl(hasAnyName(
"clear",
"assign")))),
133 cxxMemberCallExpr(on(expr(DeclRefMatcher,
134 StandardResettableOwnerTypeMatcher)),
135 callee(cxxMethodDecl(hasName(
"reset")))),
139 callee(cxxMethodDecl(hasAttr(attr::Reinitializes)))),
144 ReinitializationFunctions))),
145 anyOf(cxxMemberCallExpr(on(DeclRefMatcher)),
146 callExpr(unless(cxxMemberCallExpr()),
147 hasArgument(0, DeclRefMatcher)))),
149 callExpr(forEachArgumentWithParam(
150 unaryOperator(hasOperatorName(
"&"),
151 hasUnaryOperand(DeclRefMatcher)),
153 parmVarDecl(hasType(pointsTo(isConstQualified())))))),
156 callExpr(forEachArgumentWithParam(
157 traverse(TK_AsIs, DeclRefMatcher),
158 unless(parmVarDecl(hasType(
159 references(qualType(isConstQualified())))))),
160 unless(callee(functionDecl(
173 return anyOf(hasAncestor(typeLoc()),
174 hasAncestor(declRefExpr(
175 to(functionDecl(ast_matchers::isTemplateInstantiation())))),
176 hasAncestor(expr(hasUnevaluatedContext())));
179UseAfterMoveFinder::UseAfterMoveFinder(
180 ASTContext *TheContext, llvm::ArrayRef<StringRef> InvalidationFunctions,
181 llvm::ArrayRef<StringRef> ReinitializationFunctions)
182 : Context(TheContext), InvalidationFunctions(InvalidationFunctions),
183 ReinitializationFunctions(ReinitializationFunctions) {}
185std::optional<UseAfterMove>
186UseAfterMoveFinder::find(Stmt *
CodeBlock,
const Expr *MovingCall,
187 const DeclRefExpr *MovedVariable) {
195 CFG::BuildOptions Options;
196 Options.AddImplicitDtors =
true;
197 Options.AddTemporaryDtors =
true;
198 std::unique_ptr<CFG> TheCFG =
199 CFG::buildCFG(
nullptr,
CodeBlock, Context, Options);
203 Sequence = std::make_unique<ExprSequence>(TheCFG.get(),
CodeBlock, Context);
204 BlockMap = std::make_unique<StmtToBlockMap>(TheCFG.get(), Context);
207 const CFGBlock *MoveBlock = BlockMap->blockContainingStmt(MovingCall);
212 MoveBlock = &TheCFG->getEntry();
215 auto TheUseAfterMove =
216 findInternal(MoveBlock, MovingCall, MovedVariable->getDecl());
218 if (TheUseAfterMove) {
219 if (
const CFGBlock *UseBlock =
220 BlockMap->blockContainingStmt(TheUseAfterMove->DeclRef)) {
226 CFGReverseBlockReachabilityAnalysis CFA(*TheCFG);
227 TheUseAfterMove->UseHappensInLaterLoopIteration =
228 UseBlock == MoveBlock ? Visited.contains(UseBlock)
229 : CFA.isReachable(UseBlock, MoveBlock);
232 return TheUseAfterMove;
235std::optional<UseAfterMove>
236UseAfterMoveFinder::findInternal(
const CFGBlock *Block,
const Expr *MovingCall,
237 const ValueDecl *MovedVariable) {
238 if (Visited.contains(Block))
244 Visited.insert(Block);
247 SmallVector<const DeclRefExpr *, 1> Uses;
248 llvm::SmallPtrSet<const Stmt *, 1> Reinits;
249 getUsesAndReinits(Block, MovedVariable, &Uses, &Reinits);
255 SmallVector<const Stmt *, 1> ReinitsToDelete;
256 for (
const Stmt *Reinit : Reinits)
257 if (MovingCall && Reinit != MovingCall &&
258 Sequence->potentiallyAfter(MovingCall, Reinit))
259 ReinitsToDelete.push_back(Reinit);
260 for (
const Stmt *Reinit : ReinitsToDelete)
261 Reinits.erase(Reinit);
264 for (
const DeclRefExpr *Use : Uses) {
265 if (!MovingCall || Sequence->potentiallyAfter(Use, MovingCall)) {
269 bool HaveSavingReinit =
false;
270 for (
const Stmt *Reinit : Reinits)
271 if (!Sequence->potentiallyAfter(Reinit, Use))
272 HaveSavingReinit =
true;
274 if (!HaveSavingReinit) {
275 UseAfterMove TheUseAfterMove;
276 TheUseAfterMove.DeclRef = Use;
282 TheUseAfterMove.EvaluationOrderUndefined =
283 MovingCall !=
nullptr &&
284 Sequence->potentiallyAfter(MovingCall, Use);
286 return TheUseAfterMove;
293 if (Reinits.empty()) {
294 for (
const auto &Succ : Block->succs()) {
296 if (
auto Found = findInternal(Succ,
nullptr, MovedVariable))
305void UseAfterMoveFinder::getUsesAndReinits(
306 const CFGBlock *Block,
const ValueDecl *MovedVariable,
307 SmallVectorImpl<const DeclRefExpr *> *Uses,
308 llvm::SmallPtrSetImpl<const Stmt *> *Reinits) {
309 llvm::SmallPtrSet<const DeclRefExpr *, 1> DeclRefs;
310 llvm::SmallPtrSet<const DeclRefExpr *, 1> ReinitDeclRefs;
312 getDeclRefs(Block, MovedVariable, &DeclRefs);
313 getReinits(Block, MovedVariable, Reinits, &ReinitDeclRefs);
317 for (
const DeclRefExpr *DeclRef : DeclRefs)
318 if (!ReinitDeclRefs.contains(DeclRef))
319 Uses->push_back(DeclRef);
322 llvm::sort(*Uses, [](
const DeclRefExpr *D1,
const DeclRefExpr *D2) {
323 return D1->getExprLoc() < D2->getExprLoc();
329 if (
const auto *SL = dyn_cast<StringLiteral>(E->IgnoreParenImpCasts()))
330 return SL->getString();
338 if (Attr->getAnnotation() !=
"clang-tidy")
341 if (Attr->args_size() != 2)
345 std::optional<StringRef> Annotation =
getStringLiteral(Attr->args_begin()[1]);
347 return Plugin && Annotation && *Plugin ==
"bugprone-use-after-move" &&
348 *Annotation ==
"null_after_move";
352 const Type *TheType = VD->getType().getNonReferenceType().getTypePtrOrNull();
356 const CXXRecordDecl *RecordDecl = TheType->getAsCXXRecordDecl();
362 if (
const CXXRecordDecl *DefinitionDecl = RecordDecl->getDefinition()) {
363 for (
const auto *Attr : DefinitionDecl->specific_attrs<AnnotateAttr>())
369 const IdentifierInfo *ID = RecordDecl->getIdentifier();
373 const StringRef Name = ID->getName();
374 if (Name !=
"unique_ptr" && Name !=
"shared_ptr" && Name !=
"weak_ptr")
377 return RecordDecl->getDeclContext()->isStdNamespace();
380void UseAfterMoveFinder::getDeclRefs(
381 const CFGBlock *
Block,
const Decl *MovedVariable,
382 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
384 for (
const auto &Elem : *
Block) {
385 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
389 auto AddDeclRefs = [
this,
Block,
390 DeclRefs](
const ArrayRef<BoundNodes> Matches) {
391 for (
const auto &Match : Matches) {
392 const auto *DeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
393 const auto *Operator = Match.getNodeAs<CXXOperatorCallExpr>(
"operator");
394 if (DeclRef && BlockMap->blockContainingStmt(DeclRef) ==
Block) {
398 if (Operator || !isSpecifiedAfterMove(DeclRef->getDecl()))
399 DeclRefs->insert(DeclRef);
404 auto DeclRefMatcher = declRefExpr(hasDeclaration(equalsNode(MovedVariable)),
408 AddDeclRefs(
match(traverse(TK_AsIs, findAll(DeclRefMatcher)), *S->getStmt(),
410 AddDeclRefs(
match(findAll(cxxOperatorCallExpr(
411 hasAnyOverloadedOperatorName(
"*",
"->",
"[]"),
412 hasArgument(0, DeclRefMatcher))
414 *S->getStmt(), *Context));
418void UseAfterMoveFinder::getReinits(
419 const CFGBlock *Block,
const ValueDecl *MovedVariable,
420 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
421 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
423 MovedVariable, InvalidationFunctions, ReinitializationFunctions);
427 for (
const auto &Elem : *Block) {
428 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
432 const SmallVector<BoundNodes, 1> Matches =
433 match(findAll(ReinitMatcher), *S->getStmt(), *Context);
435 for (
const auto &Match : Matches) {
436 const auto *TheStmt = Match.getNodeAs<Stmt>(
"reinit");
437 const auto *TheDeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
438 if (TheStmt && BlockMap->blockContainingStmt(TheStmt) == Block) {
439 Stmts->insert(TheStmt);
445 DeclRefs->insert(TheDeclRef);
464 return MoveType::Move;
465 if (
FuncDecl->getName() ==
"forward")
466 return MoveType::Forward;
469 return MoveType::Invalidation;
474 ASTContext *Context, MoveType Type,
475 const FunctionDecl *MoveDecl) {
476 const SourceLocation UseLoc = Use.DeclRef->getExprLoc();
477 const SourceLocation MoveLoc = MovingCall->getExprLoc();
481 "'%0' used after it was %select{forwarded|moved|invalidated by %2}1")
482 << MoveArg->getDecl()->getName() << Type << MoveDecl;
483 Check->diag(MoveLoc,
"%select{forward|move|invalidation}0 occurred here",
486 if (Use.EvaluationOrderUndefined) {
489 "the use and %select{forward|move|invalidation}0 are unsequenced, i.e. "
490 "there is no guarantee about the order in which they are evaluated",
493 }
else if (Use.UseHappensInLaterLoopIteration) {
495 "the use happens in a later loop iteration than the "
496 "%select{forward|move|invalidation}0",
505 Options.get(
"InvalidationFunctions",
""))),
506 ReinitializationFunctions(
utils::
options::parseStringList(
507 Options.get(
"ReinitializationFunctions",
""))) {}
510 Options.store(Opts,
"InvalidationFunctions",
512 Options.store(Opts,
"ReinitializationFunctions",
521 auto TryEmplaceMatcher =
522 cxxMemberCallExpr(callee(cxxMethodDecl(hasName(
"try_emplace"))));
523 auto Arg = declRefExpr().bind(
"arg");
524 auto IsMemberCallee = callee(functionDecl(unless(isStaticStorageClass())));
525 auto CallMoveMatcher =
526 callExpr(callee(functionDecl(
getNameMatcher(InvalidationFunctions))
528 anyOf(cxxMemberCallExpr(IsMemberCallee, on(Arg)),
529 callExpr(unless(cxxMemberCallExpr(IsMemberCallee)),
530 hasArgument(0, Arg))),
532 unless(hasParent(TryEmplaceMatcher)), expr().bind(
"call-move"),
533 anyOf(hasAncestor(compoundStmt(
534 hasParent(lambdaExpr().bind(
"containing-lambda")))),
535 hasAncestor(functionDecl(anyOf(
537 hasAnyConstructorInitializer(withInitializer(
538 expr(anyOf(equalsBoundNode(
"call-move"),
540 equalsBoundNode(
"call-move")))))
541 .bind(
"containing-ctor-init"))))
542 .bind(
"containing-ctor"),
543 functionDecl().bind(
"containing-func"))))));
552 forEach(expr(ignoringParenImpCasts(CallMoveMatcher))),
559 unless(initListExpr()),
560 unless(expr(ignoringParenImpCasts(equalsBoundNode(
"call-move")))))
561 .bind(
"moving-call")),
566 const auto *ContainingCtor =
567 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"containing-ctor");
568 const auto *ContainingCtorInit =
569 Result.Nodes.getNodeAs<Expr>(
"containing-ctor-init");
570 const auto *ContainingLambda =
571 Result.Nodes.getNodeAs<LambdaExpr>(
"containing-lambda");
572 const auto *ContainingFunc =
573 Result.Nodes.getNodeAs<FunctionDecl>(
"containing-func");
574 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
575 const auto *MovingCall = Result.Nodes.getNodeAs<Expr>(
"moving-call");
576 const auto *Arg = Result.Nodes.getNodeAs<DeclRefExpr>(
"arg");
577 const auto *MoveDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"move-decl");
579 if (!MovingCall || !MovingCall->getExprLoc().isValid())
580 MovingCall = CallMove;
584 if (!Arg->getDecl()->getDeclContext()->isFunctionOrMethod())
589 if (ContainingCtor) {
590 CodeBlocks.push_back(ContainingCtor->getBody());
591 if (ContainingCtorInit) {
593 bool BeforeMove{
true};
594 for (
const CXXCtorInitializer *Init : ContainingCtor->inits()) {
595 if (BeforeMove && Init->getInit()->IgnoreImplicit() ==
596 ContainingCtorInit->IgnoreImplicit())
599 CodeBlocks.push_back(Init->getInit());
602 }
else if (ContainingLambda) {
603 CodeBlocks.push_back(ContainingLambda->getBody());
604 }
else if (ContainingFunc) {
605 CodeBlocks.push_back(ContainingFunc->getBody());
609 UseAfterMoveFinder Finder(Result.Context, InvalidationFunctions,
610 ReinitializationFunctions);
611 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 std::optional< StringRef > getStringLiteral(const Expr *E)
static auto getNameMatcher(llvm::ArrayRef< StringRef > InvalidationFunctions)
static StatementMatcher inDecltypeOrTemplateArg()
static bool isNullAfterMoveAnnotate(const AnnotateAttr *Attr)
static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg, const UseAfterMove &Use, ClangTidyCheck *Check, ASTContext *Context, MoveType Type, const FunctionDecl *MoveDecl)
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[]