[clang] [clang][bytecode][NFC] Refactor DynamicAllocator a bit (PR #152510)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 7 07:14:24 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/152510
Use empty()-style functions instead of exposing the size if we don't need it.
>From 04210a99604f81311643e9d317cec021fd7cee72 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 7 Aug 2025 16:11:41 +0200
Subject: [PATCH] [clang][bytecode][NFC] Refactor DynamicAllocator a bit
Use empty()-style functions instead of exposing the size if we don't
need it.
---
clang/lib/AST/ByteCode/DynamicAllocator.cpp | 4 ++--
clang/lib/AST/ByteCode/DynamicAllocator.h | 5 +++--
clang/lib/AST/ByteCode/InterpState.cpp | 4 ++--
3 files changed, 7 insertions(+), 6 deletions(-)
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)
More information about the cfe-commits
mailing list