Skip to content

Commit 3b5cc2d

Browse files
authored
[clang][bytecode][NFC] Refactor DynamicAllocator a bit (#152510)
Use empty()-style functions instead of exposing the size if we don't need it.
1 parent 3fa34f1 commit 3b5cc2d

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

clang/lib/AST/ByteCode/DynamicAllocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ bool DynamicAllocator::deallocate(const Expr *Source,
128128
return false;
129129

130130
auto &Site = It->second;
131-
assert(Site.size() > 0);
131+
assert(!Site.empty());
132132

133133
// Find the Block to delete.
134134
auto AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) {
@@ -144,7 +144,7 @@ bool DynamicAllocator::deallocate(const Expr *Source,
144144
S.deallocate(B);
145145
Site.Allocations.erase(AllocIt);
146146

147-
if (Site.size() == 0)
147+
if (Site.empty())
148148
AllocationSites.erase(It);
149149

150150
return true;

clang/lib/AST/ByteCode/DynamicAllocator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class DynamicAllocator final {
5555
}
5656

5757
size_t size() const { return Allocations.size(); }
58+
bool empty() const { return Allocations.empty(); }
5859
};
5960

6061
public:
@@ -65,8 +66,6 @@ class DynamicAllocator final {
6566

6667
void cleanup();
6768

68-
unsigned getNumAllocations() const { return AllocationSites.size(); }
69-
7069
/// Allocate ONE element of the given descriptor.
7170
Block *allocate(const Descriptor *D, unsigned EvalID, Form AllocForm);
7271
/// Allocate \p NumElements primitive elements of the given type.
@@ -96,6 +95,8 @@ class DynamicAllocator final {
9695
return llvm::make_range(AllocationSites.begin(), AllocationSites.end());
9796
}
9897

98+
bool hasAllocations() const { return !AllocationSites.empty(); }
99+
99100
private:
100101
llvm::DenseMap<const Expr *, AllocationSite> AllocationSites;
101102

clang/lib/AST/ByteCode/InterpState.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,14 @@ void InterpState::deallocate(Block *B) {
101101
}
102102

103103
bool InterpState::maybeDiagnoseDanglingAllocations() {
104-
bool NoAllocationsLeft = (Alloc.getNumAllocations() == 0);
104+
bool NoAllocationsLeft = !Alloc.hasAllocations();
105105

106106
if (!checkingPotentialConstantExpression()) {
107-
for (const auto &It : Alloc.allocation_sites()) {
108-
assert(It.second.size() > 0);
107+
for (const auto &[Source, Site] : Alloc.allocation_sites()) {
108+
assert(!Site.empty());
109109

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

0 commit comments

Comments
 (0)