11 #include "clang/Analysis/CFG.h" 12 #include "clang/Lex/Lexer.h" 14 #include "../utils/ExprSequence.h" 37 class UseAfterMoveFinder {
39 UseAfterMoveFinder(ASTContext *TheContext);
45 bool find(Stmt *FunctionBody,
const Expr *MovingCall,
46 const ValueDecl *MovedVariable, UseAfterMove *TheUseAfterMove);
49 bool findInternal(
const CFGBlock *Block,
const Expr *MovingCall,
50 const ValueDecl *MovedVariable,
51 UseAfterMove *TheUseAfterMove);
52 void getUsesAndReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
53 llvm::SmallVectorImpl<const DeclRefExpr *> *Uses,
54 llvm::SmallPtrSetImpl<const Stmt *> *Reinits);
55 void getDeclRefs(
const CFGBlock *Block,
const Decl *MovedVariable,
56 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
57 void getReinits(
const CFGBlock *Block,
const ValueDecl *MovedVariable,
58 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
59 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs);
62 std::unique_ptr<ExprSequence> Sequence;
63 std::unique_ptr<StmtToBlockMap> BlockMap;
64 llvm::SmallPtrSet<const CFGBlock *, 8> Visited;
78 return anyOf(hasAncestor(typeLoc()),
79 hasAncestor(declRefExpr(
80 to(functionDecl(ast_matchers::isTemplateInstantiation())))));
83 UseAfterMoveFinder::UseAfterMoveFinder(ASTContext *TheContext)
84 : Context(TheContext) {}
86 bool UseAfterMoveFinder::find(Stmt *FunctionBody,
const Expr *MovingCall,
87 const ValueDecl *MovedVariable,
88 UseAfterMove *TheUseAfterMove) {
96 CFG::BuildOptions Options;
97 Options.AddImplicitDtors =
true;
98 Options.AddTemporaryDtors =
true;
99 std::unique_ptr<CFG> TheCFG =
100 CFG::buildCFG(
nullptr, FunctionBody, Context, Options);
105 std::make_unique<ExprSequence>(TheCFG.get(), FunctionBody, Context);
106 BlockMap = std::make_unique<StmtToBlockMap>(TheCFG.get(), Context);
109 const CFGBlock *Block = BlockMap->blockContainingStmt(MovingCall);
113 return findInternal(Block, MovingCall, MovedVariable, TheUseAfterMove);
116 bool UseAfterMoveFinder::findInternal(
const CFGBlock *Block,
117 const Expr *MovingCall,
118 const ValueDecl *MovedVariable,
119 UseAfterMove *TheUseAfterMove) {
120 if (Visited.count(Block))
126 Visited.insert(Block);
129 llvm::SmallVector<const DeclRefExpr *, 1> Uses;
130 llvm::SmallPtrSet<const Stmt *, 1> Reinits;
131 getUsesAndReinits(Block, MovedVariable, &Uses, &Reinits);
135 llvm::SmallVector<const Stmt *, 1> ReinitsToDelete;
136 for (
const Stmt *Reinit : Reinits) {
137 if (MovingCall && Sequence->potentiallyAfter(MovingCall, Reinit))
138 ReinitsToDelete.push_back(Reinit);
140 for (
const Stmt *Reinit : ReinitsToDelete) {
141 Reinits.erase(Reinit);
145 for (
const DeclRefExpr *Use : Uses) {
146 if (!MovingCall || Sequence->potentiallyAfter(Use, MovingCall)) {
150 bool HaveSavingReinit =
false;
151 for (
const Stmt *Reinit : Reinits) {
152 if (!Sequence->potentiallyAfter(Reinit, Use))
153 HaveSavingReinit =
true;
156 if (!HaveSavingReinit) {
157 TheUseAfterMove->DeclRef = Use;
163 TheUseAfterMove->EvaluationOrderUndefined =
164 MovingCall !=
nullptr &&
165 Sequence->potentiallyAfter(MovingCall, Use);
174 if (Reinits.empty()) {
175 for (
const auto &Succ : Block->succs()) {
176 if (Succ && findInternal(Succ,
nullptr, MovedVariable, TheUseAfterMove))
184 void UseAfterMoveFinder::getUsesAndReinits(
185 const CFGBlock *Block,
const ValueDecl *MovedVariable,
186 llvm::SmallVectorImpl<const DeclRefExpr *> *Uses,
187 llvm::SmallPtrSetImpl<const Stmt *> *Reinits) {
188 llvm::SmallPtrSet<const DeclRefExpr *, 1> DeclRefs;
189 llvm::SmallPtrSet<const DeclRefExpr *, 1> ReinitDeclRefs;
191 getDeclRefs(Block, MovedVariable, &DeclRefs);
192 getReinits(Block, MovedVariable, Reinits, &ReinitDeclRefs);
196 for (
const DeclRefExpr *
DeclRef : DeclRefs) {
197 if (!ReinitDeclRefs.count(
DeclRef))
202 std::sort(Uses->begin(), Uses->end(),
203 [](
const DeclRefExpr *D1,
const DeclRefExpr *D2) {
204 return D1->getExprLoc() < D2->getExprLoc();
209 const Type *TheType = VD->getType().getNonReferenceType().getTypePtrOrNull();
213 const CXXRecordDecl *RecordDecl = TheType->getAsCXXRecordDecl();
217 const IdentifierInfo *ID = RecordDecl->getIdentifier();
221 StringRef
Name = ID->getName();
222 if (Name !=
"unique_ptr" && Name !=
"shared_ptr" && Name !=
"weak_ptr")
225 return RecordDecl->getDeclContext()->isStdNamespace();
228 void UseAfterMoveFinder::getDeclRefs(
229 const CFGBlock *Block,
const Decl *MovedVariable,
230 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
232 for (
const auto &Elem : *Block) {
233 Optional<CFGStmt> S = Elem.getAs<CFGStmt>();
237 auto addDeclRefs = [
this, Block,
238 DeclRefs](
const ArrayRef<BoundNodes> Matches) {
239 for (
const auto &Match : Matches) {
240 const auto *
DeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
241 const auto *Operator = Match.getNodeAs<CXXOperatorCallExpr>(
"operator");
252 auto DeclRefMatcher = declRefExpr(hasDeclaration(equalsNode(MovedVariable)),
256 addDeclRefs(
match(findAll(DeclRefMatcher), *S->getStmt(), *Context));
258 findAll(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName(
"*"),
259 hasOverloadedOperatorName(
"->"),
260 hasOverloadedOperatorName(
"[]")),
261 hasArgument(0, DeclRefMatcher))
263 *S->getStmt(), *Context));
267 void UseAfterMoveFinder::getReinits(
268 const CFGBlock *Block,
const ValueDecl *MovedVariable,
269 llvm::SmallPtrSetImpl<const Stmt *> *Stmts,
270 llvm::SmallPtrSetImpl<const DeclRefExpr *> *DeclRefs) {
271 auto DeclRefMatcher =
272 declRefExpr(hasDeclaration(equalsNode(MovedVariable))).bind(
"declref");
274 auto StandardContainerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
275 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
276 "::std::basic_string",
"::std::vector",
"::std::deque",
277 "::std::forward_list",
"::std::list",
"::std::set",
"::std::map",
278 "::std::multiset",
"::std::multimap",
"::std::unordered_set",
279 "::std::unordered_map",
"::std::unordered_multiset",
280 "::std::unordered_multimap"))))));
282 auto StandardSmartPointerTypeMatcher = hasType(hasUnqualifiedDesugaredType(
283 recordType(hasDeclaration(cxxRecordDecl(hasAnyName(
284 "::std::unique_ptr",
"::std::shared_ptr",
"::std::weak_ptr"))))));
292 binaryOperator(hasOperatorName(
"="), hasLHS(DeclRefMatcher)),
293 cxxOperatorCallExpr(hasOverloadedOperatorName(
"="),
294 hasArgument(0, DeclRefMatcher)),
297 declStmt(hasDescendant(equalsNode(MovedVariable))),
300 on(expr(DeclRefMatcher, StandardContainerTypeMatcher)),
306 callee(cxxMethodDecl(hasAnyName(
"clear",
"assign")))),
309 on(expr(DeclRefMatcher, StandardSmartPointerTypeMatcher)),
310 callee(cxxMethodDecl(hasName(
"reset")))),
314 callee(cxxMethodDecl(hasAttr(clang::attr::Reinitializes)))),
316 callExpr(forEachArgumentWithParam(
317 unaryOperator(hasOperatorName(
"&"),
318 hasUnaryOperand(DeclRefMatcher)),
319 unless(parmVarDecl(hasType(pointsTo(isConstQualified())))))),
322 callExpr(forEachArgumentWithParam(
324 unless(parmVarDecl(hasType(
325 references(qualType(isConstQualified())))))),
326 unless(callee(functionDecl(hasName(
"::std::move")))))))
331 for (
const auto &Elem : *Block) {
332 Optional<CFGStmt> S = Elem.getAs<CFGStmt>();
336 SmallVector<BoundNodes, 1> Matches =
337 match(findAll(ReinitMatcher), *S->getStmt(), *Context);
339 for (
const auto &Match : Matches) {
340 const auto *TheStmt = Match.getNodeAs<Stmt>(
"reinit");
341 const auto *TheDeclRef = Match.getNodeAs<DeclRefExpr>(
"declref");
342 if (TheStmt && BlockMap->blockContainingStmt(TheStmt) == Block) {
343 Stmts->insert(TheStmt);
349 DeclRefs->insert(TheDeclRef);
357 ASTContext *Context) {
358 SourceLocation UseLoc = Use.DeclRef->getExprLoc();
359 SourceLocation MoveLoc = MovingCall->getExprLoc();
361 Check->
diag(UseLoc,
"'%0' used after it was moved")
362 << MoveArg->getDecl()->getName();
363 Check->
diag(MoveLoc,
"move occurred here", DiagnosticIDs::Note);
364 if (Use.EvaluationOrderUndefined) {
366 "the use and move are unsequenced, i.e. there is no guarantee " 367 "about the order in which they are evaluated",
368 DiagnosticIDs::Note);
369 }
else if (UseLoc < MoveLoc || Use.DeclRef == MoveArg) {
371 "the use happens in a later loop iteration than the move",
372 DiagnosticIDs::Note);
376 void UseAfterMoveCheck::registerMatchers(MatchFinder *Finder) {
377 if (!getLangOpts().CPlusPlus11)
380 auto CallMoveMatcher =
381 callExpr(callee(functionDecl(hasName(
"::std::move"))), argumentCountIs(1),
382 hasArgument(0, declRefExpr().bind(
"arg")),
383 anyOf(hasAncestor(lambdaExpr().bind(
"containing-lambda")),
384 hasAncestor(functionDecl().bind(
"containing-func"))),
392 stmt(forEach(expr(ignoringParenImpCasts(CallMoveMatcher))),
399 unless(initListExpr()),
400 unless(expr(ignoringParenImpCasts(equalsBoundNode(
"call-move")))))
401 .bind(
"moving-call"),
405 void UseAfterMoveCheck::check(
const MatchFinder::MatchResult &Result) {
406 const auto *ContainingLambda =
407 Result.Nodes.getNodeAs<LambdaExpr>(
"containing-lambda");
408 const auto *ContainingFunc =
409 Result.Nodes.getNodeAs<FunctionDecl>(
"containing-func");
410 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
411 const auto *MovingCall = Result.Nodes.getNodeAs<Expr>(
"moving-call");
412 const auto *Arg = Result.Nodes.getNodeAs<DeclRefExpr>(
"arg");
414 if (!MovingCall || !MovingCall->getExprLoc().isValid())
415 MovingCall = CallMove;
417 Stmt *FunctionBody =
nullptr;
418 if (ContainingLambda)
419 FunctionBody = ContainingLambda->getBody();
420 else if (ContainingFunc)
421 FunctionBody = ContainingFunc->getBody();
427 if (!Arg->getDecl()->getDeclContext()->isFunctionOrMethod())
430 UseAfterMoveFinder finder(Result.Context);
432 if (finder.find(FunctionBody, MovingCall, Arg->getDecl(), &Use))
const FunctionDecl * Decl
Base class for all clang-tidy checks.
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
static constexpr llvm::StringLiteral Name
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool isStandardSmartPointer(const ValueDecl *VD)
static StatementMatcher inDecltypeOrTemplateArg()
bool EvaluationOrderUndefined
const DeclRefExpr * DeclRef
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
static void emitDiagnostic(const Expr *MovingCall, const DeclRefExpr *MoveArg, const UseAfterMove &Use, ClangTidyCheck *Check, ASTContext *Context)