diff options
author | Congcong Cai <congcongcai0907@163.com> | 2024-10-03 16:16:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-03 16:16:49 +0800 |
commit | e984d11d7257343da366d9fa03749a43a6d6af72 (patch) | |
tree | b763bacc23db29d16e649fd7418fbcb154ba9e74 /clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp | |
parent | eb4a91dcea7703480b7d8ac818710c1aba3499c5 (diff) | |
download | llvm-e984d11d7257343da366d9fa03749a43a6d6af72.zip llvm-e984d11d7257343da366d9fa03749a43a6d6af72.tar.gz llvm-e984d11d7257343da366d9fa03749a43a6d6af72.tar.bz2 |
[clang-tidy] loop convert can handle lambda init capture (#109159)
Fixes: #109083
Current implement ignore the whole lambda capture. This patch wants to
do traverse for capture init expr also
Diffstat (limited to 'clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp index c0bf490..90c16bf 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp @@ -777,7 +777,7 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, Expr *Init) { if (C->capturesVariable()) { - const ValueDecl *VDecl = C->getCapturedVar(); + ValueDecl *VDecl = C->getCapturedVar(); if (areSameVariable(IndexVar, VDecl)) { // FIXME: if the index is captured, it will count as an usage and the // alias (if any) won't work, because it is only used in case of having @@ -787,6 +787,8 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE, : Usage::UK_CaptureByRef, C->getLocation())); } + if (VDecl->isInitCapture()) + TraverseStmtImpl(cast<VarDecl>(VDecl)->getInit()); } return VisitorBase::TraverseLambdaCapture(LE, C, Init); } @@ -816,6 +818,17 @@ bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) { return true; } +bool ForLoopIndexUseVisitor::TraverseStmtImpl(Stmt *S) { + // All this pointer swapping is a mechanism for tracking immediate parentage + // of Stmts. + const Stmt *OldNextParent = NextStmtParent; + CurrStmtParent = NextStmtParent; + NextStmtParent = S; + bool Result = VisitorBase::TraverseStmt(S); + NextStmtParent = OldNextParent; + return Result; +} + bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) { // If this is an initialization expression for a lambda capture, prune the // traversal so that we don't end up diagnosing the contained DeclRefExpr as @@ -828,15 +841,7 @@ bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) { return true; } } - - // All this pointer swapping is a mechanism for tracking immediate parentage - // of Stmts. - const Stmt *OldNextParent = NextStmtParent; - CurrStmtParent = NextStmtParent; - NextStmtParent = S; - bool Result = VisitorBase::TraverseStmt(S); - NextStmtParent = OldNextParent; - return Result; + return TraverseStmtImpl(S); } std::string VariableNamer::createIndexName() { |