Skip to content

Commit f3d534c

Browse files
committed
Revert "[AST] Use APIntStorage to fix memory leak in EnumConstantDecl. (#78311)"
This reverts commit 4737959. Missed an lldb update.
1 parent 59a2653 commit f3d534c

File tree

8 files changed

+63
-100
lines changed

8 files changed

+63
-100
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,6 @@ Bug Fixes in This Version
675675
Fixes (`#67317 <https://github.com/llvm/llvm-project/issues/67317>`_)
676676
- Clang now properly diagnoses use of stand-alone OpenMP directives after a
677677
label (including ``case`` or ``default`` labels).
678-
- Fix compiler memory leak for enums with underlying type larger than 64 bits.
679-
Fixes (`#78311 <https://github.com/llvm/llvm-project/pull/78311>`_)
680678

681679
Before:
682680

clang/include/clang/AST/APNumericStorage.h

Lines changed: 0 additions & 71 deletions
This file was deleted.

clang/include/clang/AST/Decl.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#ifndef LLVM_CLANG_AST_DECL_H
1414
#define LLVM_CLANG_AST_DECL_H
1515

16-
#include "clang/AST/APNumericStorage.h"
1716
#include "clang/AST/APValue.h"
1817
#include "clang/AST/ASTContextAllocate.h"
1918
#include "clang/AST/DeclAccessPair.h"
@@ -3252,16 +3251,15 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
32523251
/// that is defined. For example, in "enum X {a,b}", each of a/b are
32533252
/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
32543253
/// TagType for the X EnumDecl.
3255-
class EnumConstantDecl : public ValueDecl,
3256-
public Mergeable<EnumConstantDecl>,
3257-
public APIntStorage {
3254+
class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> {
32583255
Stmt *Init; // an integer constant expression
3259-
bool IsUnsigned;
3256+
llvm::APSInt Val; // The value.
32603257

32613258
protected:
3262-
EnumConstantDecl(const ASTContext &C, DeclContext *DC, SourceLocation L,
3259+
EnumConstantDecl(DeclContext *DC, SourceLocation L,
32633260
IdentifierInfo *Id, QualType T, Expr *E,
3264-
const llvm::APSInt &V);
3261+
const llvm::APSInt &V)
3262+
: ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {}
32653263

32663264
public:
32673265
friend class StmtIteratorBase;
@@ -3274,15 +3272,10 @@ class EnumConstantDecl : public ValueDecl,
32743272

32753273
const Expr *getInitExpr() const { return (const Expr*) Init; }
32763274
Expr *getInitExpr() { return (Expr*) Init; }
3277-
llvm::APSInt getInitVal() const {
3278-
return llvm::APSInt(getValue(), IsUnsigned);
3279-
}
3275+
const llvm::APSInt &getInitVal() const { return Val; }
32803276

32813277
void setInitExpr(Expr *E) { Init = (Stmt*) E; }
3282-
void setInitVal(const ASTContext &C, const llvm::APSInt &V) {
3283-
setValue(C, V);
3284-
IsUnsigned = V.isUnsigned();
3285-
}
3278+
void setInitVal(const llvm::APSInt &V) { Val = V; }
32863279

32873280
SourceRange getSourceRange() const override LLVM_READONLY;
32883281

clang/include/clang/AST/Expr.h

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#ifndef LLVM_CLANG_AST_EXPR_H
1414
#define LLVM_CLANG_AST_EXPR_H
1515

16-
#include "clang/AST/APNumericStorage.h"
1716
#include "clang/AST/APValue.h"
1817
#include "clang/AST/ASTVector.h"
1918
#include "clang/AST/ComputeDependence.h"
@@ -1480,6 +1479,57 @@ class DeclRefExpr final
14801479
}
14811480
};
14821481

1482+
/// Used by IntegerLiteral/FloatingLiteral to store the numeric without
1483+
/// leaking memory.
1484+
///
1485+
/// For large floats/integers, APFloat/APInt will allocate memory from the heap
1486+
/// to represent these numbers. Unfortunately, when we use a BumpPtrAllocator
1487+
/// to allocate IntegerLiteral/FloatingLiteral nodes the memory associated with
1488+
/// the APFloat/APInt values will never get freed. APNumericStorage uses
1489+
/// ASTContext's allocator for memory allocation.
1490+
class APNumericStorage {
1491+
union {
1492+
uint64_t VAL; ///< Used to store the <= 64 bits integer value.
1493+
uint64_t *pVal; ///< Used to store the >64 bits integer value.
1494+
};
1495+
unsigned BitWidth;
1496+
1497+
bool hasAllocation() const { return llvm::APInt::getNumWords(BitWidth) > 1; }
1498+
1499+
APNumericStorage(const APNumericStorage &) = delete;
1500+
void operator=(const APNumericStorage &) = delete;
1501+
1502+
protected:
1503+
APNumericStorage() : VAL(0), BitWidth(0) { }
1504+
1505+
llvm::APInt getIntValue() const {
1506+
unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1507+
if (NumWords > 1)
1508+
return llvm::APInt(BitWidth, NumWords, pVal);
1509+
else
1510+
return llvm::APInt(BitWidth, VAL);
1511+
}
1512+
void setIntValue(const ASTContext &C, const llvm::APInt &Val);
1513+
};
1514+
1515+
class APIntStorage : private APNumericStorage {
1516+
public:
1517+
llvm::APInt getValue() const { return getIntValue(); }
1518+
void setValue(const ASTContext &C, const llvm::APInt &Val) {
1519+
setIntValue(C, Val);
1520+
}
1521+
};
1522+
1523+
class APFloatStorage : private APNumericStorage {
1524+
public:
1525+
llvm::APFloat getValue(const llvm::fltSemantics &Semantics) const {
1526+
return llvm::APFloat(Semantics, getIntValue());
1527+
}
1528+
void setValue(const ASTContext &C, const llvm::APFloat &Val) {
1529+
setIntValue(C, Val.bitcastToAPInt());
1530+
}
1531+
};
1532+
14831533
class IntegerLiteral : public Expr, public APIntStorage {
14841534
SourceLocation Loc;
14851535

clang/lib/AST/Decl.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5369,23 +5369,16 @@ void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); }
53695369
bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); }
53705370
void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); }
53715371

5372-
EnumConstantDecl::EnumConstantDecl(const ASTContext &C, DeclContext *DC,
5373-
SourceLocation L, IdentifierInfo *Id,
5374-
QualType T, Expr *E, const llvm::APSInt &V)
5375-
: ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt *)E) {
5376-
setInitVal(C, V);
5377-
}
5378-
53795372
EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
53805373
SourceLocation L,
53815374
IdentifierInfo *Id, QualType T,
53825375
Expr *E, const llvm::APSInt &V) {
5383-
return new (C, CD) EnumConstantDecl(C, CD, L, Id, T, E, V);
5376+
return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V);
53845377
}
53855378

53865379
EnumConstantDecl *
53875380
EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
5388-
return new (C, ID) EnumConstantDecl(C, nullptr, SourceLocation(), nullptr,
5381+
return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr,
53895382
QualType(), nullptr, llvm::APSInt());
53905383
}
53915384

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13047,7 +13047,7 @@ Value *CodeGenFunction::EmitBPFBuiltinExpr(unsigned BuiltinID,
1304713047
const auto *DR = cast<DeclRefExpr>(CE->getSubExpr());
1304813048
const auto *Enumerator = cast<EnumConstantDecl>(DR->getDecl());
1304913049

13050-
auto InitVal = Enumerator->getInitVal();
13050+
auto &InitVal = Enumerator->getInitVal();
1305113051
std::string InitValStr;
1305213052
if (InitVal.isNegative() || InitVal > uint64_t(INT64_MAX))
1305313053
InitValStr = std::to_string(InitVal.getSExtValue());

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20214,7 +20214,7 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
2021420214
// Adjust the APSInt value.
2021520215
InitVal = InitVal.extOrTrunc(NewWidth);
2021620216
InitVal.setIsSigned(NewSign);
20217-
ECD->setInitVal(Context, InitVal);
20217+
ECD->setInitVal(InitVal);
2021820218

2021920219
// Adjust the Expr initializer and type.
2022020220
if (ECD->getInitExpr() &&

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
910910
VisitValueDecl(ECD);
911911
if (Record.readInt())
912912
ECD->setInitExpr(Record.readExpr());
913-
ECD->setInitVal(Reader.getContext(), Record.readAPSInt());
913+
ECD->setInitVal(Record.readAPSInt());
914914
mergeMergeable(ECD);
915915
}
916916

0 commit comments

Comments
 (0)