Skip to content

[clang][bytecode][NFC] Refactor DynamicAllocator a bit #152510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/AST/ByteCode/DynamicAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/AST/ByteCode/DynamicAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DynamicAllocator final {
}

size_t size() const { return Allocations.size(); }
bool empty() const { return Allocations.empty(); }
};

public:
Expand All @@ -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.
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 4 additions & 5 deletions clang/lib/AST/ByteCode/InterpState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,14 @@ 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);
for (const auto &[Source, Site] : Alloc.allocation_sites()) {
assert(!Site.empty());

const Expr *Source = It.first;
CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
<< (It.second.size() - 1) << Source->getSourceRange();
<< (Site.size() - 1) << Source->getSourceRange();
}
}
// Keep evaluating before C++20, since the CXXNewExpr wasn't valid there
Expand Down