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