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