[clang] [analyzer][NFC] Modernize iterator-based loop in LiveVariables::computeLiveness (PR #157670)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 9 06:41:29 PDT 2025
https://github.com/steakhal updated https://github.com/llvm/llvm-project/pull/157670
>From 36480cff2af13ba75d94a306dabea372bb2b3614 Mon Sep 17 00:00:00 2001
From: Balazs Benics <balazs.benics at sonarsource.com>
Date: Tue, 9 Sep 2025 11:32:45 +0200
Subject: [PATCH 1/3] [analyzer][NFC] Modernize iterator-based loop in
LiveVariables::computeLiveness
---
clang/lib/Analysis/LiveVariables.cpp | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Analysis/LiveVariables.cpp b/clang/lib/Analysis/LiveVariables.cpp
index 891e766407722..c23a092df6cfb 100644
--- a/clang/lib/Analysis/LiveVariables.cpp
+++ b/clang/lib/Analysis/LiveVariables.cpp
@@ -556,9 +556,8 @@ LiveVariables::computeLiveness(AnalysisDeclContext &AC, bool killAtAssign) {
// Merge the values of all successor blocks.
LivenessValues val;
- for (CFGBlock::const_succ_iterator it = block->succ_begin(),
- ei = block->succ_end(); it != ei; ++it) {
- if (const CFGBlock *succ = *it) {
+ for (const CFGBlock *succ : block->succs()) {
+ if (succ) {
val = LV->merge(val, LV->blocksBeginToLiveness[succ]);
}
}
>From b47ce7baf1fe5058dd847b4fb9f77dee27c442c1 Mon Sep 17 00:00:00 2001
From: Balazs Benics <balazs.benics at sonarsource.com>
Date: Tue, 9 Sep 2025 11:33:33 +0200
Subject: [PATCH 2/3] [analyzer][NFC] Modernize iterator-based loops in
LiveVariablesImpl::dumpBlockLiveness
Convert multiple iterator-based loops to range-based for loops:
- DenseMap iteration over blocksEndToLiveness
- std::vector iteration over CFGBlocks
- ImmutableSet iteration over liveDecls
- std::vector iteration over declVec
---
clang/lib/Analysis/LiveVariables.cpp | 28 ++++++++--------------------
1 file changed, 8 insertions(+), 20 deletions(-)
diff --git a/clang/lib/Analysis/LiveVariables.cpp b/clang/lib/Analysis/LiveVariables.cpp
index c23a092df6cfb..486a5bb8fa987 100644
--- a/clang/lib/Analysis/LiveVariables.cpp
+++ b/clang/lib/Analysis/LiveVariables.cpp
@@ -585,38 +585,26 @@ void LiveVariables::dumpBlockLiveness(const SourceManager &M) {
void LiveVariablesImpl::dumpBlockLiveness(const SourceManager &M) {
std::vector<const CFGBlock *> vec;
- for (const auto &KV : blocksEndToLiveness) {
- vec.push_back(KV.first);
- }
+ vec.reserve(blocksEndToLiveness.size());
+ llvm::append_range(vec, llvm::make_first_range(blocksEndToLiveness));
llvm::sort(vec, [](const CFGBlock *A, const CFGBlock *B) {
return A->getBlockID() < B->getBlockID();
});
std::vector<const VarDecl*> declVec;
- for (std::vector<const CFGBlock *>::iterator
- it = vec.begin(), ei = vec.end(); it != ei; ++it) {
- llvm::errs() << "\n[ B" << (*it)->getBlockID()
+ for (const CFGBlock *block : vec) {
+ llvm::errs() << "\n[ B" << block->getBlockID()
<< " (live variables at block exit) ]\n";
-
- LiveVariables::LivenessValues vals = blocksEndToLiveness[*it];
declVec.clear();
-
- for (llvm::ImmutableSet<const VarDecl *>::iterator si =
- vals.liveDecls.begin(),
- se = vals.liveDecls.end(); si != se; ++si) {
- declVec.push_back(*si);
- }
-
+ llvm::append_range(declVec, blocksEndToLiveness[block].liveDecls);
llvm::sort(declVec, [](const Decl *A, const Decl *B) {
return A->getBeginLoc() < B->getBeginLoc();
});
- for (std::vector<const VarDecl*>::iterator di = declVec.begin(),
- de = declVec.end(); di != de; ++di) {
- llvm::errs() << " " << (*di)->getDeclName().getAsString()
- << " <";
- (*di)->getLocation().print(llvm::errs(), M);
+ for (const VarDecl *VD : declVec) {
+ llvm::errs() << " " << VD->getDeclName().getAsString() << " <";
+ VD->getLocation().print(llvm::errs(), M);
llvm::errs() << ">\n";
}
}
>From 727e1dcfd424806147fac7edb7537b4c3e9c4c0d Mon Sep 17 00:00:00 2001
From: Balazs Benics <balazs.benics at sonarsource.com>
Date: Tue, 9 Sep 2025 11:35:14 +0200
Subject: [PATCH 3/3] [analyzer][NFC] Modernize iterator-based loop in
mergeSets template function
---
clang/lib/Analysis/LiveVariables.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Analysis/LiveVariables.cpp b/clang/lib/Analysis/LiveVariables.cpp
index 486a5bb8fa987..4e9410ba26ca8 100644
--- a/clang/lib/Analysis/LiveVariables.cpp
+++ b/clang/lib/Analysis/LiveVariables.cpp
@@ -90,8 +90,8 @@ namespace {
if (A.isEmpty())
return B;
- for (typename SET::iterator it = B.begin(), ei = B.end(); it != ei; ++it) {
- A = A.add(*it);
+ for (const auto &element : B) {
+ A = A.add(element);
}
return A;
}
More information about the cfe-commits
mailing list