[clang] [clang][bytecode][NFC] Refactor DynamicAllocator a bit (PR #152510)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 7 07:14:57 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Use empty()-style functions instead of exposing the size if we don't need it.
---
Full diff: https://github.com/llvm/llvm-project/pull/152510.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/DynamicAllocator.cpp (+2-2)
- (modified) clang/lib/AST/ByteCode/DynamicAllocator.h (+3-2)
- (modified) clang/lib/AST/ByteCode/InterpState.cpp (+2-2)
``````````diff
diff --git a/clang/lib/AST/ByteCode/DynamicAllocator.cpp b/clang/lib/AST/ByteCode/DynamicAllocator.cpp
index 9b8b664df6afd..bbef94101b36f 100644
--- a/clang/lib/AST/ByteCode/DynamicAllocator.cpp
+++ b/clang/lib/AST/ByteCode/DynamicAllocator.cpp
@@ -128,7 +128,7 @@ bool DynamicAllocator::deallocate(const Expr *Source,
return false;
auto &Site = It->second;
- assert(Site.size() > 0);
+ assert(!Site.empty());
// Find the Block to delete.
auto AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) {
@@ -144,7 +144,7 @@ bool DynamicAllocator::deallocate(const Expr *Source,
S.deallocate(B);
Site.Allocations.erase(AllocIt);
- if (Site.size() == 0)
+ if (Site.empty())
AllocationSites.erase(It);
return true;
diff --git a/clang/lib/AST/ByteCode/DynamicAllocator.h b/clang/lib/AST/ByteCode/DynamicAllocator.h
index cff09bf4f6a6e..cba5e347e950b 100644
--- a/clang/lib/AST/ByteCode/DynamicAllocator.h
+++ b/clang/lib/AST/ByteCode/DynamicAllocator.h
@@ -55,6 +55,7 @@ class DynamicAllocator final {
}
size_t size() const { return Allocations.size(); }
+ bool empty() const { return Allocations.empty(); }
};
public:
@@ -65,8 +66,6 @@ class DynamicAllocator final {
void cleanup();
- unsigned getNumAllocations() const { return AllocationSites.size(); }
-
/// Allocate ONE element of the given descriptor.
Block *allocate(const Descriptor *D, unsigned EvalID, Form AllocForm);
/// Allocate \p NumElements primitive elements of the given type.
@@ -96,6 +95,8 @@ class DynamicAllocator final {
return llvm::make_range(AllocationSites.begin(), AllocationSites.end());
}
+ bool hasAllocations() const { return !AllocationSites.empty(); }
+
private:
llvm::DenseMap<const Expr *, AllocationSite> AllocationSites;
diff --git a/clang/lib/AST/ByteCode/InterpState.cpp b/clang/lib/AST/ByteCode/InterpState.cpp
index b3c0a67f8ff3e..edc1e15b1ebb0 100644
--- a/clang/lib/AST/ByteCode/InterpState.cpp
+++ b/clang/lib/AST/ByteCode/InterpState.cpp
@@ -101,11 +101,11 @@ void InterpState::deallocate(Block *B) {
}
bool InterpState::maybeDiagnoseDanglingAllocations() {
- bool NoAllocationsLeft = (Alloc.getNumAllocations() == 0);
+ bool NoAllocationsLeft = !Alloc.hasAllocations();
if (!checkingPotentialConstantExpression()) {
for (const auto &It : Alloc.allocation_sites()) {
- assert(It.second.size() > 0);
+ assert(!It.second.empty());
const Expr *Source = It.first;
CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
``````````
</details>
https://github.com/llvm/llvm-project/pull/152510
More information about the cfe-commits
mailing list