clang-tools 23.0.0git
UseAfterMoveCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "UseAfterMoveCheck.h"
10
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"
20
22#include "../utils/Matchers.h"
24#include <optional>
25
26using namespace clang::ast_matchers;
27using namespace clang::tidy::utils;
28
29namespace clang::tidy::bugprone {
30
31using matchers::hasUnevaluatedContext;
32
33namespace {
34AST_MATCHER_P(Expr, hasParentIgnoringParenImpCasts,
35 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
36 const Expr *E = &Node;
37 do {
38 const DynTypedNodeList Parents = Finder->getASTContext().getParents(*E);
39 if (Parents.size() != 1)
40 return false;
41 E = Parents[0].get<Expr>();
42 if (!E)
43 return false;
44 } while (isa<ImplicitCastExpr, ParenExpr>(E));
45
46 return InnerMatcher.matches(*E, Finder, Builder);
47}
48
49/// Contains information about a use-after-move.
50struct UseAfterMove {
51 // The DeclRefExpr that constituted the use of the object.
52 const DeclRefExpr *DeclRef;
53
54 // Is the order in which the move and the use are evaluated undefined?
55 bool EvaluationOrderUndefined = false;
56
57 // Does the use happen in a later loop iteration than the move?
58 //
59 // We default to false and change it to true if required in find().
60 bool UseHappensInLaterLoopIteration = false;
61};
62
63/// Finds uses of a variable after a move (and maintains state required by the
64/// various internal helper functions).
65class UseAfterMoveFinder {
66public:
67 UseAfterMoveFinder(ASTContext *TheContext,
68 llvm::ArrayRef<StringRef> InvalidationFunctions,
69 llvm::ArrayRef<StringRef> ReinitializationFunctions,
70 const CXXRecordDecl *MovedAs);
71
72 // Within the given code block, finds the first use of 'MovedVariable' that
73 // occurs after 'MovingCall' (the expression that performs the move). If a
74 // use-after-move is found, writes information about it to 'TheUseAfterMove'.
75 // Returns whether a use-after-move was found.
76 std::optional<UseAfterMove> find(Stmt *CodeBlock, const Expr *MovingCall,
77 const DeclRefExpr *MovedVariable);
78
79private:
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);
91
92 ASTContext *Context;
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;
99};
100
101} // namespace
102
103static auto getNameMatcher(llvm::ArrayRef<StringRef> InvalidationFunctions) {
104 return anyOf(hasAnyName("::std::move", "::std::forward"),
105 matchers::matchesAnyListedRegexName(InvalidationFunctions));
106}
107
108static StatementMatcher
109makeReinitMatcher(const ValueDecl *MovedVariable,
110 llvm::ArrayRef<StringRef> InvalidationFunctions,
111 llvm::ArrayRef<StringRef> ReinitializationFunctions) {
112 const auto DeclRefMatcher =
113 declRefExpr(hasDeclaration(equalsNode(MovedVariable))).bind("declref");
114
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"))))));
122
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"))))));
127
128 // Matches different types of reinitialization.
129 return stmt(
130 anyOf(
131 // Assignment. In addition to the overloaded assignment
132 // operator, test for built-in assignment as well, since
133 // template functions may be instantiated to use std::move() on
134 // built-in types.
135 binaryOperation(hasOperatorName("="),
136 hasLHS(ignoringParenImpCasts(DeclRefMatcher))),
137 // Declaration. We treat this as a type of reinitialization
138 // too, so we don't need to treat it separately.
139 declStmt(hasDescendant(equalsNode(MovedVariable))),
140 // clear() and assign() on standard containers.
141 cxxMemberCallExpr(
142 on(expr(DeclRefMatcher, StandardContainerTypeMatcher)),
143 // To keep the matcher simple, we check for assign() calls
144 // on all standard containers, even though only vector,
145 // deque, forward_list and list have assign(). If assign()
146 // is called on any of the other containers, this will be
147 // flagged by a compile error anyway.
148 callee(cxxMethodDecl(hasAnyName("clear", "assign")))),
149 // reset() on standard smart pointers.
150 cxxMemberCallExpr(on(expr(DeclRefMatcher,
151 StandardResettableOwnerTypeMatcher)),
152 callee(cxxMethodDecl(hasName("reset")))),
153 // Methods that have the [[clang::reinitializes]] attribute.
154 cxxMemberCallExpr(
155 on(DeclRefMatcher),
156 callee(cxxMethodDecl(hasAttr(attr::Reinitializes)))),
157 // Functions that are specified in ReinitializationFunctions
158 // option.
159 callExpr(
160 callee(functionDecl(matchers::matchesAnyListedRegexName(
161 ReinitializationFunctions))),
162 anyOf(cxxMemberCallExpr(on(DeclRefMatcher)),
163 callExpr(unless(cxxMemberCallExpr()),
164 hasArgument(0, DeclRefMatcher)))),
165 // Passing variable to a function as a non-const pointer.
166 callExpr(forEachArgumentWithParam(
167 unaryOperator(hasOperatorName("&"),
168 hasUnaryOperand(DeclRefMatcher)),
169 unless(
170 parmVarDecl(hasType(pointsTo(isConstQualified())))))),
171 // Passing variable to a function as a non-const lvalue
172 // reference (unless that function is std::move()).
173 callExpr(forEachArgumentWithParam(
174 traverse(TK_AsIs, DeclRefMatcher),
175 unless(parmVarDecl(hasType(
176 references(qualType(isConstQualified())))))),
177 unless(callee(functionDecl(
178 getNameMatcher(InvalidationFunctions)))))))
179 .bind("reinit");
180}
181
182// Matches nodes that are
183// - Part of a decltype argument or class template argument (we check this by
184// seeing if they are children of a TypeLoc), or
185// - Part of a function template argument (we check this by seeing if they are
186// children of a DeclRefExpr that references a function template).
187// DeclRefExprs that fulfill these conditions should not be counted as a use or
188// move.
189static StatementMatcher inDecltypeOrTemplateArg() {
190 return anyOf(hasAncestor(typeLoc()),
191 hasAncestor(declRefExpr(
192 to(functionDecl(ast_matchers::isTemplateInstantiation())))),
193 hasAncestor(expr(hasUnevaluatedContext())));
194}
195
196UseAfterMoveFinder::UseAfterMoveFinder(
197 ASTContext *TheContext, llvm::ArrayRef<StringRef> InvalidationFunctions,
198 llvm::ArrayRef<StringRef> ReinitializationFunctions,
199 const CXXRecordDecl *MovedAs)
200 : Context(TheContext), InvalidationFunctions(InvalidationFunctions),
201 ReinitializationFunctions(ReinitializationFunctions), MovedAs(MovedAs) {}
202
203std::optional<UseAfterMove>
204UseAfterMoveFinder::find(Stmt *CodeBlock, const Expr *MovingCall,
205 const DeclRefExpr *MovedVariable) {
206 // Generate the CFG manually instead of through an AnalysisDeclContext because
207 // it seems the latter can't be used to generate a CFG for the body of a
208 // lambda.
209 //
210 // We include implicit and temporary destructors in the CFG so that
211 // destructors marked [[noreturn]] are handled correctly in the control flow
212 // analysis. (These are used in some styles of assertion macros.)
213 CFG::BuildOptions Options;
214 Options.AddImplicitDtors = true;
215 Options.AddTemporaryDtors = true;
216 std::unique_ptr<CFG> TheCFG =
217 CFG::buildCFG(nullptr, CodeBlock, Context, Options);
218 if (!TheCFG)
219 return std::nullopt;
220
221 Sequence = std::make_unique<ExprSequence>(TheCFG.get(), CodeBlock, Context);
222 BlockMap = std::make_unique<StmtToBlockMap>(TheCFG.get(), Context);
223 Visited.clear();
224
225 const CFGBlock *MoveBlock = BlockMap->blockContainingStmt(MovingCall);
226 if (!MoveBlock) {
227 // This can happen if MovingCall is in a constructor initializer, which is
228 // not included in the CFG because the CFG is built only from the function
229 // body.
230 MoveBlock = &TheCFG->getEntry();
231 }
232
233 auto TheUseAfterMove =
234 findInternal(MoveBlock, MovingCall, MovedVariable->getDecl());
235
236 if (TheUseAfterMove) {
237 if (const CFGBlock *UseBlock =
238 BlockMap->blockContainingStmt(TheUseAfterMove->DeclRef)) {
239 // Does the use happen in a later loop iteration than the move?
240 // - If they are in the same CFG block, we know the use happened in a
241 // later iteration if we visited that block a second time.
242 // - Otherwise, we know the use happened in a later iteration if the
243 // move is reachable from the use.
244 CFGReverseBlockReachabilityAnalysis CFA(*TheCFG);
245 TheUseAfterMove->UseHappensInLaterLoopIteration =
246 UseBlock == MoveBlock ? Visited.contains(UseBlock)
247 : CFA.isReachable(UseBlock, MoveBlock);
248 }
249 }
250 return TheUseAfterMove;
251}
252
253std::optional<UseAfterMove>
254UseAfterMoveFinder::findInternal(const CFGBlock *Block, const Expr *MovingCall,
255 const ValueDecl *MovedVariable) {
256 if (Visited.contains(Block))
257 return std::nullopt;
258
259 // Mark the block as visited (except if this is the block containing the
260 // std::move() and it's being visited the first time).
261 if (!MovingCall)
262 Visited.insert(Block);
263
264 // Get all uses and reinits in the block.
265 SmallVector<const DeclRefExpr *, 1> Uses;
266 llvm::SmallPtrSet<const Stmt *, 1> Reinits;
267 getUsesAndReinits(Block, MovedVariable, &Uses, &Reinits);
268
269 // Ignore all reinitializations where the move potentially comes after the
270 // reinit.
271 // If `Reinit` is identical to `MovingCall`, we're looking at a move-to-self
272 // (e.g. `a = std::move(a)`). Count these as reinitializations.
273 SmallVector<const Stmt *, 1> ReinitsToDelete;
274 for (const Stmt *Reinit : Reinits)
275 if (MovingCall && Reinit != MovingCall &&
276 Sequence->potentiallyAfter(MovingCall, Reinit))
277 ReinitsToDelete.push_back(Reinit);
278 for (const Stmt *Reinit : ReinitsToDelete)
279 Reinits.erase(Reinit);
280
281 // Find all uses that potentially come after the move.
282 for (const DeclRefExpr *Use : Uses) {
283 if (!MovingCall || Sequence->potentiallyAfter(Use, MovingCall)) {
284 // Does the use have a saving reinit? A reinit is saving if it definitely
285 // comes before the use, i.e. if there's no potential that the reinit is
286 // after the use.
287 bool HaveSavingReinit = false;
288 for (const Stmt *Reinit : Reinits)
289 if (!Sequence->potentiallyAfter(Reinit, Use))
290 HaveSavingReinit = true;
291
292 if (!HaveSavingReinit) {
293 UseAfterMove TheUseAfterMove;
294 TheUseAfterMove.DeclRef = Use;
295
296 // Is this a use-after-move that depends on order of evaluation?
297 // This is the case if the move potentially comes after the use (and we
298 // already know that use potentially comes after the move, which taken
299 // together tells us that the ordering is unclear).
300 TheUseAfterMove.EvaluationOrderUndefined =
301 MovingCall != nullptr &&
302 Sequence->potentiallyAfter(MovingCall, Use);
303
304 return TheUseAfterMove;
305 }
306 }
307 }
308
309 // If the object wasn't reinitialized, call ourselves recursively on all
310 // successors.
311 if (Reinits.empty()) {
312 for (const auto &Succ : Block->succs()) {
313 if (Succ) {
314 if (auto Found = findInternal(Succ, nullptr, MovedVariable))
315 return Found;
316 }
317 }
318 }
319
320 return std::nullopt;
321}
322
323void UseAfterMoveFinder::getUsesAndReinits(
324 const CFGBlock *Block, const ValueDecl *MovedVariable,
325 SmallVectorImpl<const DeclRefExpr *> *Uses,
326 llvm::SmallPtrSetImpl<const Stmt *> *Reinits) {
327 llvm::SmallPtrSet<const DeclRefExpr *, 1> DeclRefs;
328 llvm::SmallPtrSet<const DeclRefExpr *, 1> ReinitDeclRefs;
329
330 getDeclRefs(Block, MovedVariable, &DeclRefs);
331 getReinits(Block, MovedVariable, Reinits, &ReinitDeclRefs);
332
333 // All references to the variable that aren't reinitializations are uses.
334 Uses->clear();
335 for (const DeclRefExpr *DeclRef : DeclRefs)
336 if (!ReinitDeclRefs.contains(DeclRef))
337 Uses->push_back(DeclRef);
338
339 // Sort the uses by their occurrence in the source code.
340 llvm::sort(*Uses, [](const DeclRefExpr *D1, const DeclRefExpr *D2) {
341 return D1->getExprLoc() < D2->getExprLoc();
342 });
343}
344
345static std::optional<StringRef> getStringLiteral(const Expr *E) {
346 assert(E);
347 if (const auto *SL = dyn_cast<StringLiteral>(E->IgnoreParenImpCasts()))
348 return SL->getString();
349 return std::nullopt;
350}
351
352// User defined types can use [[clang::annotate]] to mark smart-pointer-like
353// types with a specified move from state that matches the standard smart
354// pointer's moved-from state (nullptr).
355static bool isNullAfterMoveAnnotate(const AnnotateAttr *Attr) {
356 if (Attr->getAnnotation() != "clang-tidy")
357 return false;
358
359 if (Attr->args_size() != 2)
360 return false;
361
362 std::optional<StringRef> Plugin = getStringLiteral(Attr->args_begin()[0]);
363 std::optional<StringRef> Annotation = getStringLiteral(Attr->args_begin()[1]);
364
365 return Plugin && Annotation && *Plugin == "bugprone-use-after-move" &&
366 *Annotation == "null_after_move";
367}
368
369static bool isSpecifiedAfterMove(const ValueDecl *VD) {
370 const Type *TheType = VD->getType().getNonReferenceType().getTypePtrOrNull();
371 if (!TheType)
372 return false;
373
374 const CXXRecordDecl *RecordDecl = TheType->getAsCXXRecordDecl();
375 if (!RecordDecl)
376 return false;
377
378 // Use the definition for the declaration, as it is the expected place to add
379 // the annotations.
380 if (const CXXRecordDecl *DefinitionDecl = RecordDecl->getDefinition()) {
381 for (const auto *Attr : DefinitionDecl->specific_attrs<AnnotateAttr>())
382 if (isNullAfterMoveAnnotate(Attr))
383 return true;
384 }
385
386 // Standard smart pointers have a well-specified moved-from state (nullptr).
387 const IdentifierInfo *ID = RecordDecl->getIdentifier();
388 if (!ID)
389 return false;
390
391 const StringRef Name = ID->getName();
392 if (Name != "unique_ptr" && Name != "shared_ptr" && Name != "weak_ptr")
393 return false;
394
395 return RecordDecl->getDeclContext()->isStdNamespace();
396}
397
398void UseAfterMoveFinder::getDeclRefs(
399 const CFGBlock *Block, const Decl *MovedVariable,
400 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
401 DeclRefs->clear();
402 for (const auto &Elem : *Block) {
403 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
404 if (!S)
405 continue;
406
407 auto AddDeclRefs = [this, Block,
408 DeclRefs](const ArrayRef<BoundNodes> Matches) {
409 for (const auto &Match : Matches) {
410 const auto *DeclRef = Match.getNodeAs<DeclRefExpr>("declref");
411 const auto *Member = Match.getNodeAs<MemberExpr>("member-expr");
412 const auto *Operator = Match.getNodeAs<CXXOperatorCallExpr>("operator");
413 // Non-moved member as the move only implies a base class.
414 if (Member && MovedAs && !isa<CXXMethodDecl>(Member->getMemberDecl()) &&
415 !MovedAs->hasMemberName(Member->getMemberDecl()->getIdentifier())) {
416 continue;
417 }
418 if (DeclRef && BlockMap->blockContainingStmt(DeclRef) == Block) {
419 // Ignore uses of a standard smart pointer or classes annotated as
420 // "null_after_move" (smart-pointer-like behavior) that don't
421 // dereference the pointer.
422 if (Operator || !isSpecifiedAfterMove(DeclRef->getDecl()))
423 DeclRefs->insert(DeclRef);
424 }
425 }
426 };
427
428 auto DeclRefMatcher =
429 declRefExpr(hasDeclaration(equalsNode(MovedVariable)),
430 unless(inDecltypeOrTemplateArg()),
431 unless(hasParentIgnoringParenImpCasts(
432 memberExpr(hasDeclaration(cxxDestructorDecl())))),
433 optionally(hasParentIgnoringParenImpCasts(
434 memberExpr().bind("member-expr"))))
435 .bind("declref");
436
437 AddDeclRefs(match(traverse(TK_AsIs, findAll(DeclRefMatcher)), *S->getStmt(),
438 *Context));
439 AddDeclRefs(match(findAll(cxxOperatorCallExpr(
440 hasAnyOverloadedOperatorName("*", "->", "[]"),
441 hasArgument(0, DeclRefMatcher))
442 .bind("operator")),
443 *S->getStmt(), *Context));
444 }
445}
446
447void UseAfterMoveFinder::getReinits(
448 const CFGBlock *Block, const ValueDecl *MovedVariable,
449 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
450 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
451 const auto ReinitMatcher = makeReinitMatcher(
452 MovedVariable, InvalidationFunctions, ReinitializationFunctions);
453
454 Stmts->clear();
455 DeclRefs->clear();
456 for (const auto &Elem : *Block) {
457 std::optional<CFGStmt> S = Elem.getAs<CFGStmt>();
458 if (!S)
459 continue;
460
461 const SmallVector<BoundNodes, 1> Matches =
462 match(findAll(ReinitMatcher), *S->getStmt(), *Context);
463
464 for (const auto &Match : Matches) {
465 const auto *TheStmt = Match.getNodeAs<Stmt>("reinit");
466 const auto *TheDeclRef = Match.getNodeAs<DeclRefExpr>("declref");
467 if (TheStmt && BlockMap->blockContainingStmt(TheStmt) == Block) {
468 Stmts->insert(TheStmt);
469
470 // We count DeclStmts as reinitializations, but they don't have a
471 // DeclRefExpr associated with them -- so we need to check 'TheDeclRef'
472 // before adding it to the set.
473 if (TheDeclRef)
474 DeclRefs->insert(TheDeclRef);
475 }
476 }
477 }
478}
479
480namespace {
481
482enum MoveType {
483 Forward = 0, // std::forward
484 Move = 1, // std::move
485 Invalidation = 2, // other
486};
487
488} // namespace
489
490static MoveType determineMoveType(const FunctionDecl *FuncDecl) {
491 if (FuncDecl->isInStdNamespace()) {
492 if (FuncDecl->getName() == "move")
493 return MoveType::Move;
494 if (FuncDecl->getName() == "forward")
495 return MoveType::Forward;
496 }
497
498 return MoveType::Invalidation;
499}
500
501static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg,
502 const UseAfterMove &Use, ClangTidyCheck *Check,
503 ASTContext *Context, MoveType Type,
504 const FunctionDecl *MoveDecl) {
505 const SourceLocation UseLoc = Use.DeclRef->getExprLoc();
506 const SourceLocation MoveLoc = MovingCall->getExprLoc();
507
508 Check->diag(
509 UseLoc,
510 "'%0' used after it was %select{forwarded|moved|invalidated by %2}1")
511 << MoveArg->getDecl()->getName() << Type << MoveDecl;
512 Check->diag(MoveLoc, "%select{forward|move|invalidation}0 occurred here",
513 DiagnosticIDs::Note)
514 << Type;
515 if (Use.EvaluationOrderUndefined) {
516 Check->diag(
517 UseLoc,
518 "the use and %select{forward|move|invalidation}0 are unsequenced, i.e. "
519 "there is no guarantee about the order in which they are evaluated",
520 DiagnosticIDs::Note)
521 << Type;
522 } else if (Use.UseHappensInLaterLoopIteration) {
523 Check->diag(UseLoc,
524 "the use happens in a later loop iteration than the "
525 "%select{forward|move|invalidation}0",
526 DiagnosticIDs::Note)
527 << Type;
528 }
529}
530
532 : ClangTidyCheck(Name, Context),
533 InvalidationFunctions(utils::options::parseStringList(
534 Options.get("InvalidationFunctions", ""))),
535 ReinitializationFunctions(utils::options::parseStringList(
536 Options.get("ReinitializationFunctions", ""))) {}
537
539 Options.store(Opts, "InvalidationFunctions",
540 utils::options::serializeStringList(InvalidationFunctions));
541 Options.store(Opts, "ReinitializationFunctions",
542 utils::options::serializeStringList(ReinitializationFunctions));
543}
544
545void UseAfterMoveCheck::registerMatchers(MatchFinder *Finder) {
546 // try_emplace is a common maybe-moving function that returns a
547 // bool to tell callers whether it moved. Ignore std::move inside
548 // try_emplace to avoid false positives as we don't track uses of
549 // the bool.
550 auto TryEmplaceMatcher =
551 cxxMemberCallExpr(callee(cxxMethodDecl(hasName("try_emplace"))));
552 auto Arg = declRefExpr().bind("arg");
553 auto IsMemberCallee = callee(functionDecl(unless(isStaticStorageClass())));
554 auto CallMoveMatcher = callExpr(
555 callee(functionDecl(getNameMatcher(InvalidationFunctions))
556 .bind("move-decl")),
557 anyOf(cxxMemberCallExpr(IsMemberCallee, on(Arg)),
558 callExpr(unless(cxxMemberCallExpr(IsMemberCallee)),
559 hasArgument(0, Arg))),
560 unless(inDecltypeOrTemplateArg()), unless(hasParent(TryEmplaceMatcher)),
561 expr().bind("call-move"),
562 optionally(hasParent(implicitCastExpr(hasCastKind(CK_DerivedToBase))
563 .bind("optional-cast"))),
564 anyOf(hasAncestor(compoundStmt(
565 hasParent(lambdaExpr().bind("containing-lambda")))),
566 hasAncestor(functionDecl(
567 anyOf(cxxConstructorDecl(
568 hasAnyConstructorInitializer(withInitializer(
569 expr(anyOf(equalsBoundNode("call-move"),
570 hasDescendant(expr(
571 equalsBoundNode("call-move")))))
572 .bind("containing-ctor-init"))))
573 .bind("containing-ctor"),
574 functionDecl().bind("containing-func"))))));
575
576 Finder->addMatcher(
577 traverse(
578 TK_AsIs,
579 // To find the Stmt that we assume performs the actual move, we look
580 // for the direct ancestor of the std::move() that isn't one of the
581 // node types ignored by ignoringParenImpCasts().
582 stmt(
583 forEach(expr(ignoringParenImpCasts(CallMoveMatcher))),
584 // Don't allow an InitListExpr to be the moving call. An
585 // InitListExpr has both a syntactic and a semantic form, and the
586 // parent-child relationships are different between the two. This
587 // could cause an InitListExpr to be analyzed as the moving call
588 // in addition to the Expr that we actually want, resulting in two
589 // diagnostics with different code locations for the same move.
590 unless(initListExpr()),
591 unless(expr(ignoringParenImpCasts(equalsBoundNode("call-move")))))
592 .bind("moving-call")),
593 this);
594}
595
596void UseAfterMoveCheck::check(const MatchFinder::MatchResult &Result) {
597 const auto *ContainingCtor =
598 Result.Nodes.getNodeAs<CXXConstructorDecl>("containing-ctor");
599 const auto *ContainingCtorInit =
600 Result.Nodes.getNodeAs<Expr>("containing-ctor-init");
601 const auto *ContainingLambda =
602 Result.Nodes.getNodeAs<LambdaExpr>("containing-lambda");
603 const auto *ContainingFunc =
604 Result.Nodes.getNodeAs<FunctionDecl>("containing-func");
605 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>("call-move");
606 const auto *MovingCall = Result.Nodes.getNodeAs<Expr>("moving-call");
607 const auto *Arg = Result.Nodes.getNodeAs<DeclRefExpr>("arg");
608 const auto *MoveDecl = Result.Nodes.getNodeAs<FunctionDecl>("move-decl");
609 const auto *ParentCast =
610 Result.Nodes.getNodeAs<ImplicitCastExpr>("optional-cast");
611
612 if (!MovingCall || !MovingCall->getExprLoc().isValid())
613 MovingCall = CallMove;
614
615 // Ignore the std::move if the variable that was passed to it isn't a local
616 // variable.
617 if (!Arg->getDecl()->getDeclContext()->isFunctionOrMethod())
618 return;
619
620 // Collect all code blocks that could use the arg after move.
621 SmallVector<Stmt *> CodeBlocks{};
622 if (ContainingCtor) {
623 CodeBlocks.push_back(ContainingCtor->getBody());
624 if (ContainingCtorInit) {
625 // Collect the constructor initializer expressions.
626 bool BeforeMove{true};
627 for (const CXXCtorInitializer *Init : ContainingCtor->inits()) {
628 if (BeforeMove && Init->getInit()->IgnoreImplicit() ==
629 ContainingCtorInit->IgnoreImplicit())
630 BeforeMove = false;
631 if (!BeforeMove)
632 CodeBlocks.push_back(Init->getInit());
633 }
634 }
635 } else if (ContainingLambda) {
636 CodeBlocks.push_back(ContainingLambda->getBody());
637 } else if (ContainingFunc) {
638 CodeBlocks.push_back(ContainingFunc->getBody());
639 }
640
641 const CXXRecordDecl *MovedAs =
642 ParentCast ? ParentCast->getType()->getAsCXXRecordDecl() : nullptr;
643
644 for (Stmt *CodeBlock : CodeBlocks) {
645 UseAfterMoveFinder Finder(Result.Context, InvalidationFunctions,
646 ReinitializationFunctions, MovedAs);
647 if (auto Use = Finder.find(CodeBlock, MovingCall, Arg))
648 emitDiagnostic(MovingCall, Arg, *Use, this, Result.Context,
649 determineMoveType(MoveDecl), MoveDecl);
650 }
651}
652
653} // namespace clang::tidy::bugprone
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)
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[]