11#include "clang/AST/Expr.h"
12#include "clang/AST/ExprCXX.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
15#include "clang/Analysis/CFG.h"
16#include "clang/Lex/Lexer.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallPtrSet.h"
30using matchers::hasUnevaluatedContext;
37 const DeclRefExpr *DeclRef;
40 bool EvaluationOrderUndefined =
false;
45 bool UseHappensInLaterLoopIteration =
false;
50class UseAfterMoveFinder {
52 UseAfterMoveFinder(ASTContext *TheContext,
53 llvm::ArrayRef<StringRef> InvalidationFunctions,
54 llvm::ArrayRef<StringRef> ReinitializationFunctions);
60 std::optional<UseAfterMove> find(Stmt *CodeBlock,
const Expr *MovingCall,
61 const DeclRefExpr *MovedVariable);
64 std::optional<UseAfterMove> findInternal(
const CFGBlock *Block,
65 const Expr *MovingCall,
66 const ValueDecl *MovedVariable);
67 void getUsesAndReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
68 llvm::SmallVectorImpl<const DeclRefExpr *> *Uses,
69 llvm::SmallPtrSetImpl<const Stmt *> *Reinits);
70 void getDeclRefs(
const CFGBlock *Block,
const Decl *MovedVariable,
71 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
72 void getReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
73 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
74 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
77 llvm::ArrayRef<StringRef> InvalidationFunctions;
78 llvm::ArrayRef<StringRef> ReinitializationFunctions;
79 std::unique_ptr<ExprSequence> Sequence;
80 std::unique_ptr<StmtToBlockMap> BlockMap;
81 llvm::SmallPtrSet<const CFGBlock *, 8> Visited;
87 return anyOf(hasAnyName(
"::std::move",
"::std::forward"),
91static StatementMatcher
93 llvm::ArrayRef<StringRef> InvalidationFunctions,
94 llvm::ArrayRef<StringRef> ReinitializationFunctions) {
95 const auto DeclRefMatcher =
96 declRefExpr(hasDeclaration(equalsNode(MovedVariable))).bind(
"declref");
98 const auto StandardContainerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
99 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
100 "::std::basic_string",
"::std::vector",
"::std::deque",
101 "::std::forward_list",
"::std::list",
"::std::set",
"::std::map",
102 "::std::multiset",
"::std::multimap",
"::std::unordered_set",
103 "::std::unordered_map",
"::std::unordered_multiset",
104 "::std::unordered_multimap"))))));
106 const auto StandardResettableOwnerTypeMatcher = hasType(
107 hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(
108 hasAnyName(
"::std::unique_ptr",
"::std::shared_ptr",
109 "::std::weak_ptr",
"::std::optional",
"::std::any"))))));
118 binaryOperation(hasOperatorName(
"="), hasLHS(DeclRefMatcher)),
121 declStmt(hasDescendant(equalsNode(MovedVariable))),
124 on(expr(DeclRefMatcher, StandardContainerTypeMatcher)),
130 callee(cxxMethodDecl(hasAnyName(
"clear",
"assign")))),
132 cxxMemberCallExpr(on(expr(DeclRefMatcher,
133 StandardResettableOwnerTypeMatcher)),
134 callee(cxxMethodDecl(hasName(
"reset")))),
136 cxxMemberCallExpr(on(DeclRefMatcher),
137 callee(cxxMethodDecl(
138 hasAttr(clang::attr::Reinitializes)))),
143 ReinitializationFunctions))),
144 anyOf(cxxMemberCallExpr(on(DeclRefMatcher)),
145 callExpr(unless(cxxMemberCallExpr()),
146 hasArgument(0, DeclRefMatcher)))),
148 callExpr(forEachArgumentWithParam(
149 unaryOperator(hasOperatorName(
"&"),
150 hasUnaryOperand(DeclRefMatcher)),
152 parmVarDecl(hasType(pointsTo(isConstQualified())))))),
155 callExpr(forEachArgumentWithParam(
156 traverse(TK_AsIs, DeclRefMatcher),
157 unless(parmVarDecl(hasType(
158 references(qualType(isConstQualified())))))),
159 unless(callee(functionDecl(
172 return anyOf(hasAncestor(typeLoc()),
173 hasAncestor(declRefExpr(
174 to(functionDecl(ast_matchers::isTemplateInstantiation())))),
175 hasAncestor(expr(hasUnevaluatedContext())));
178UseAfterMoveFinder::UseAfterMoveFinder(
179 ASTContext *TheContext, llvm::ArrayRef<StringRef> InvalidationFunctions,
180 llvm::ArrayRef<StringRef> ReinitializationFunctions)
181 : Context(TheContext), InvalidationFunctions(InvalidationFunctions),
182 ReinitializationFunctions(ReinitializationFunctions) {}
184std::optional<UseAfterMove>
185UseAfterMoveFinder::find(Stmt *
CodeBlock,
const Expr *MovingCall,
186 const DeclRefExpr *MovedVariable) {
194 CFG::BuildOptions Options;
195 Options.AddImplicitDtors =
true;
196 Options.AddTemporaryDtors =
true;
197 std::unique_ptr<CFG> TheCFG =
198 CFG::buildCFG(
nullptr,
CodeBlock, Context, Options);
202 Sequence = std::make_unique<ExprSequence>(TheCFG.get(),
CodeBlock, Context);
203 BlockMap = std::make_unique<StmtToBlockMap>(TheCFG.get(), Context);
206 const CFGBlock *MoveBlock = BlockMap->blockContainingStmt(MovingCall);
211 MoveBlock = &TheCFG->getEntry();
214 auto TheUseAfterMove =
215 findInternal(MoveBlock, MovingCall, MovedVariable->getDecl());
217 if (TheUseAfterMove) {
218 if (
const CFGBlock *UseBlock =
219 BlockMap->blockContainingStmt(TheUseAfterMove->DeclRef)) {
225 CFGReverseBlockReachabilityAnalysis CFA(*TheCFG);
226 TheUseAfterMove->UseHappensInLaterLoopIteration =
227 UseBlock == MoveBlock ? Visited.contains(UseBlock)
228 : CFA.isReachable(UseBlock, MoveBlock);
231 return TheUseAfterMove;
234std::optional<UseAfterMove>
235UseAfterMoveFinder::findInternal(
const CFGBlock *Block,
const Expr *MovingCall,
236 const ValueDecl *MovedVariable) {
237 if (Visited.contains(Block))
243 Visited.insert(Block);
246 llvm::SmallVector<const DeclRefExpr *, 1> Uses;
247 llvm::SmallPtrSet<const Stmt *, 1> Reinits;
248 getUsesAndReinits(Block, MovedVariable, &Uses, &Reinits);
254 llvm::SmallVector<const Stmt *, 1> ReinitsToDelete;
255 for (
const Stmt *Reinit : Reinits)
256 if (MovingCall && Reinit != MovingCall &&
257 Sequence->potentiallyAfter(MovingCall, Reinit))
258 ReinitsToDelete.push_back(Reinit);
259 for (
const Stmt *Reinit : ReinitsToDelete)
260 Reinits.erase(Reinit);
263 for (
const DeclRefExpr *Use : Uses) {
264 if (!MovingCall || Sequence->potentiallyAfter(Use, MovingCall)) {
268 bool HaveSavingReinit =
false;
269 for (
const Stmt *Reinit : Reinits)
270 if (!Sequence->potentiallyAfter(Reinit, Use))
271 HaveSavingReinit =
true;
273 if (!HaveSavingReinit) {
274 UseAfterMove TheUseAfterMove;
275 TheUseAfterMove.DeclRef = Use;
281 TheUseAfterMove.EvaluationOrderUndefined =
282 MovingCall !=
nullptr &&
283 Sequence->potentiallyAfter(MovingCall, Use);
285 return TheUseAfterMove;
292 if (Reinits.empty()) {
293 for (
const auto &Succ : Block->succs()) {
295 if (
auto Found = findInternal(Succ,
nullptr, MovedVariable))
304void UseAfterMoveFinder::getUsesAndReinits(
305 const CFGBlock *Block,
const ValueDecl *MovedVariable,
306 llvm::SmallVectorImpl<const DeclRefExpr *> *Uses,
307 llvm::SmallPtrSetImpl<const Stmt *> *Reinits) {
308 llvm::SmallPtrSet<const DeclRefExpr *, 1> DeclRefs;
309 llvm::SmallPtrSet<const DeclRefExpr *, 1> ReinitDeclRefs;
311 getDeclRefs(Block, MovedVariable, &DeclRefs);
312 getReinits(Block, MovedVariable, Reinits, &ReinitDeclRefs);
316 for (
const DeclRefExpr *DeclRef : DeclRefs)
317 if (!ReinitDeclRefs.contains(DeclRef))
318 Uses->push_back(DeclRef);
321 llvm::sort(*Uses, [](
const DeclRefExpr *D1,
const DeclRefExpr *D2) {
322 return D1->getExprLoc() < D2->getExprLoc();
327 const Type *TheType = VD->getType().getNonReferenceType().getTypePtrOrNull();
331 const CXXRecordDecl *RecordDecl = TheType->getAsCXXRecordDecl();
335 const IdentifierInfo *ID = RecordDecl->getIdentifier();
339 const StringRef Name = ID->getName();
340 if (Name !=
"unique_ptr" && Name !=
"shared_ptr" && Name !=
"weak_ptr")
343 return RecordDecl->getDeclContext()->isStdNamespace();
346void UseAfterMoveFinder::getDeclRefs(
347 const CFGBlock *
Block,
const Decl *MovedVariable,
348 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
350 for (
const auto &Elem : *
Block) {
351 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
355 auto AddDeclRefs = [
this,
Block,
356 DeclRefs](
const ArrayRef<BoundNodes> Matches) {
357 for (
const auto &Match : Matches) {
358 const auto *DeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
359 const auto *Operator = Match.getNodeAs<CXXOperatorCallExpr>(
"operator");
360 if (DeclRef && BlockMap->blockContainingStmt(DeclRef) ==
Block) {
363 if (Operator || !isStandardSmartPointer(DeclRef->getDecl()))
364 DeclRefs->insert(DeclRef);
369 auto DeclRefMatcher = declRefExpr(hasDeclaration(equalsNode(MovedVariable)),
373 AddDeclRefs(
match(traverse(TK_AsIs, findAll(DeclRefMatcher)), *S->getStmt(),
375 AddDeclRefs(
match(findAll(cxxOperatorCallExpr(
376 hasAnyOverloadedOperatorName(
"*",
"->",
"[]"),
377 hasArgument(0, DeclRefMatcher))
379 *S->getStmt(), *Context));
383void UseAfterMoveFinder::getReinits(
384 const CFGBlock *Block,
const ValueDecl *MovedVariable,
385 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
386 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
388 MovedVariable, InvalidationFunctions, ReinitializationFunctions);
392 for (
const auto &Elem : *Block) {
393 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
397 const SmallVector<BoundNodes, 1> Matches =
398 match(findAll(ReinitMatcher), *S->getStmt(), *Context);
400 for (
const auto &Match : Matches) {
401 const auto *TheStmt = Match.getNodeAs<Stmt>(
"reinit");
402 const auto *TheDeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
403 if (TheStmt && BlockMap->blockContainingStmt(TheStmt) == Block) {
404 Stmts->insert(TheStmt);
410 DeclRefs->insert(TheDeclRef);
429 return MoveType::Move;
430 if (
FuncDecl->getName() ==
"forward")
431 return MoveType::Forward;
434 return MoveType::Invalidation;
439 ASTContext *Context, MoveType Type) {
440 const SourceLocation UseLoc = Use.DeclRef->getExprLoc();
441 const SourceLocation MoveLoc = MovingCall->getExprLoc();
444 "'%0' used after it was %select{forwarded|moved|invalidated}1")
445 << MoveArg->getDecl()->getName() << Type;
446 Check->diag(MoveLoc,
"%select{forward|move|invalidation}0 occurred here",
449 if (Use.EvaluationOrderUndefined) {
452 "the use and %select{forward|move|invalidation}0 are unsequenced, i.e. "
453 "there is no guarantee about the order in which they are evaluated",
456 }
else if (Use.UseHappensInLaterLoopIteration) {
458 "the use happens in a later loop iteration than the "
459 "%select{forward|move|invalidation}0",
468 Options.get(
"InvalidationFunctions",
""))),
469 ReinitializationFunctions(
utils::
options::parseStringList(
470 Options.get(
"ReinitializationFunctions",
""))) {}
473 Options.store(Opts,
"InvalidationFunctions",
475 Options.store(Opts,
"ReinitializationFunctions",
484 auto TryEmplaceMatcher =
485 cxxMemberCallExpr(callee(cxxMethodDecl(hasName(
"try_emplace"))));
486 auto Arg = declRefExpr().bind(
"arg");
487 auto IsMemberCallee = callee(functionDecl(unless(isStaticStorageClass())));
488 auto CallMoveMatcher =
489 callExpr(callee(functionDecl(
getNameMatcher(InvalidationFunctions))
491 anyOf(cxxMemberCallExpr(IsMemberCallee, on(Arg)),
492 callExpr(unless(cxxMemberCallExpr(IsMemberCallee)),
493 hasArgument(0, Arg))),
495 unless(hasParent(TryEmplaceMatcher)), expr().bind(
"call-move"),
496 anyOf(hasAncestor(compoundStmt(
497 hasParent(lambdaExpr().bind(
"containing-lambda")))),
498 hasAncestor(functionDecl(anyOf(
500 hasAnyConstructorInitializer(withInitializer(
501 expr(anyOf(equalsBoundNode(
"call-move"),
503 equalsBoundNode(
"call-move")))))
504 .bind(
"containing-ctor-init"))))
505 .bind(
"containing-ctor"),
506 functionDecl().bind(
"containing-func"))))));
515 forEach(expr(ignoringParenImpCasts(CallMoveMatcher))),
522 unless(initListExpr()),
523 unless(expr(ignoringParenImpCasts(equalsBoundNode(
"call-move")))))
524 .bind(
"moving-call")),
529 const auto *ContainingCtor =
530 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"containing-ctor");
531 const auto *ContainingCtorInit =
532 Result.Nodes.getNodeAs<Expr>(
"containing-ctor-init");
533 const auto *ContainingLambda =
534 Result.Nodes.getNodeAs<LambdaExpr>(
"containing-lambda");
535 const auto *ContainingFunc =
536 Result.Nodes.getNodeAs<FunctionDecl>(
"containing-func");
537 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
538 const auto *MovingCall = Result.Nodes.getNodeAs<Expr>(
"moving-call");
539 const auto *Arg = Result.Nodes.getNodeAs<DeclRefExpr>(
"arg");
540 const auto *MoveDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"move-decl");
542 if (!MovingCall || !MovingCall->getExprLoc().isValid())
543 MovingCall = CallMove;
547 if (!Arg->getDecl()->getDeclContext()->isFunctionOrMethod())
551 llvm::SmallVector<Stmt *> CodeBlocks{};
552 if (ContainingCtor) {
553 CodeBlocks.push_back(ContainingCtor->getBody());
554 if (ContainingCtorInit) {
556 bool BeforeMove{
true};
557 for (
const CXXCtorInitializer *Init : ContainingCtor->inits()) {
558 if (BeforeMove && Init->getInit()->IgnoreImplicit() ==
559 ContainingCtorInit->IgnoreImplicit())
562 CodeBlocks.push_back(Init->getInit());
565 }
else if (ContainingLambda) {
566 CodeBlocks.push_back(ContainingLambda->getBody());
567 }
else if (ContainingFunc) {
568 CodeBlocks.push_back(ContainingFunc->getBody());
572 UseAfterMoveFinder Finder(Result.Context, InvalidationFunctions,
573 ReinitializationFunctions);
574 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 void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg, const UseAfterMove &Use, ClangTidyCheck *Check, ASTContext *Context, MoveType Type)
static bool isStandardSmartPointer(const ValueDecl *VD)
static StatementMatcher makeReinitMatcher(const ValueDecl *MovedVariable, llvm::ArrayRef< StringRef > InvalidationFunctions, llvm::ArrayRef< StringRef > ReinitializationFunctions)
static MoveType determineMoveType(const FunctionDecl *FuncDecl)
static auto getNameMatcher(llvm::ArrayRef< StringRef > InvalidationFunctions)
static StatementMatcher inDecltypeOrTemplateArg()
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[]