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