Skip to content

Commit 59bdea2

Browse files
committed
Revert "[clang] Avoid re-evaluating field bitwidth (llvm#117732)"
This reverts commit 81fc3ad. This breaks some LLDB tests, e.g. SymbolFile/DWARF/x86/no_unique_address-with-bitfields.cpp: lldb: ../llvm-project/clang/lib/AST/Decl.cpp:4604: unsigned int clang::FieldDecl::getBitWidthValue() const: Assertion `isa<ConstantExpr>(getBitWidth())' failed.
1 parent 346fad5 commit 59bdea2

38 files changed

+94
-96
lines changed

clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ AST_MATCHER(FieldDecl, hasIntBitwidth) {
3838
assert(Node.isBitField());
3939
const ASTContext &Ctx = Node.getASTContext();
4040
unsigned IntBitWidth = Ctx.getIntWidth(Ctx.IntTy);
41-
unsigned CurrentBitWidth = Node.getBitWidthValue();
41+
unsigned CurrentBitWidth = Node.getBitWidthValue(Ctx);
4242
return IntBitWidth == CurrentBitWidth;
4343
}
4444

clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static MagnitudeBits calcMagnitudeBits(const ASTContext &Context,
124124
unsigned SignedBits = IntExprType->isUnsignedIntegerType() ? 0U : 1U;
125125

126126
if (const auto *BitField = IntExpr->getSourceBitField()) {
127-
unsigned BitFieldWidth = BitField->getBitWidthValue();
127+
unsigned BitFieldWidth = BitField->getBitWidthValue(Context);
128128
return {BitFieldWidth - SignedBits, BitFieldWidth};
129129
}
130130

clang-tools-extra/clang-tidy/hicpp/MultiwayPathsCoveredCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void MultiwayPathsCoveredCheck::handleSwitchWithoutDefault(
160160
}
161161
if (const auto *BitfieldDecl =
162162
Result.Nodes.getNodeAs<FieldDecl>("bitfield")) {
163-
return twoPow(BitfieldDecl->getBitWidthValue());
163+
return twoPow(BitfieldDecl->getBitWidthValue(*Result.Context));
164164
}
165165

166166
return static_cast<std::size_t>(0);

clang-tools-extra/clangd/Hover.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ void addLayoutInfo(const NamedDecl &ND, HoverInfo &HI) {
10181018
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Record);
10191019
HI.Offset = Layout.getFieldOffset(FD->getFieldIndex());
10201020
if (FD->isBitField())
1021-
HI.Size = FD->getBitWidthValue();
1021+
HI.Size = FD->getBitWidthValue(Ctx);
10221022
else if (auto Size = Ctx.getTypeSizeInCharsIfKnown(FD->getType()))
10231023
HI.Size = FD->isZeroSize(Ctx) ? 0 : Size->getQuantity() * 8;
10241024
if (HI.Size) {

clang/include/clang/AST/Decl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3142,9 +3142,7 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
31423142

31433143
/// Computes the bit width of this field, if this is a bit field.
31443144
/// May not be called on non-bitfields.
3145-
/// Note that in order to successfully use this function, the bitwidth
3146-
/// expression must be a ConstantExpr with a valid integer result set.
3147-
unsigned getBitWidthValue() const;
3145+
unsigned getBitWidthValue(const ASTContext &Ctx) const;
31483146

31493147
/// Set the bit-field width for this member.
31503148
// Note: used by some clients (i.e., do not remove it).
@@ -3175,7 +3173,7 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
31753173
/// Is this a zero-length bit-field? Such bit-fields aren't really bit-fields
31763174
/// at all and instead act as a separator between contiguous runs of other
31773175
/// bit-fields.
3178-
bool isZeroLengthBitField() const;
3176+
bool isZeroLengthBitField(const ASTContext &Ctx) const;
31793177

31803178
/// Determine if this field is a subobject of zero size, that is, either a
31813179
/// zero-length bit-field or a field of empty class type with the

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ AST_MATCHER(FieldDecl, isBitField) {
708708
/// fieldDecl(hasBitWidth(2))
709709
/// matches 'int a;' and 'int c;' but not 'int b;'.
710710
AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) {
711-
return Node.isBitField() && Node.getBitWidthValue() == Width;
711+
return Node.isBitField() &&
712+
Node.getBitWidthValue(Finder->getASTContext()) == Width;
712713
}
713714

714715
/// Matches non-static data members that have an in-class initializer.

clang/lib/AST/ASTContext.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,7 +2795,7 @@ getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context,
27952795
if (Field->isUnnamedBitField())
27962796
return 0;
27972797

2798-
int64_t BitfieldSize = Field->getBitWidthValue();
2798+
int64_t BitfieldSize = Field->getBitWidthValue(Context);
27992799
if (IsBitIntType) {
28002800
if ((unsigned)BitfieldSize >
28012801
cast<BitIntType>(Field->getType())->getNumBits())
@@ -7769,7 +7769,7 @@ QualType ASTContext::isPromotableBitField(Expr *E) const {
77697769

77707770
QualType FT = Field->getType();
77717771

7772-
uint64_t BitWidth = Field->getBitWidthValue();
7772+
uint64_t BitWidth = Field->getBitWidthValue(*this);
77737773
uint64_t IntSize = getTypeSize(IntTy);
77747774
// C++ [conv.prom]p5:
77757775
// A prvalue for an integral bit-field can be converted to a prvalue of type
@@ -8797,7 +8797,7 @@ static void EncodeBitField(const ASTContext *Ctx, std::string& S,
87978797
S += getObjCEncodingForPrimitiveType(Ctx, BT);
87988798
}
87998799
}
8800-
S += llvm::utostr(FD->getBitWidthValue());
8800+
S += llvm::utostr(FD->getBitWidthValue(*Ctx));
88018801
}
88028802

88038803
// Helper function for determining whether the encoded type string would include
@@ -9223,7 +9223,7 @@ void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
92239223
}
92249224

92259225
for (FieldDecl *Field : RDecl->fields()) {
9226-
if (!Field->isZeroLengthBitField() && Field->isZeroSize(*this))
9226+
if (!Field->isZeroLengthBitField(*this) && Field->isZeroSize(*this))
92279227
continue;
92289228
uint64_t offs = layout.getFieldOffset(Field->getFieldIndex());
92299229
FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
@@ -9320,7 +9320,7 @@ void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
93209320
if (field->isBitField()) {
93219321
EncodeBitField(this, S, field->getType(), field);
93229322
#ifndef NDEBUG
9323-
CurOffs += field->getBitWidthValue();
9323+
CurOffs += field->getBitWidthValue(*this);
93249324
#endif
93259325
} else {
93269326
QualType qt = field->getType();

clang/lib/AST/ByteCode/Interp.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,8 @@ bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F,
14711471
return false;
14721472
const Pointer &Field = This.atField(FieldOffset);
14731473
const auto &Value = S.Stk.pop<T>();
1474-
Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1474+
Field.deref<T>() =
1475+
Value.truncate(F->Decl->getBitWidthValue(S.getASTContext()));
14751476
Field.initialize();
14761477
return true;
14771478
}
@@ -1494,7 +1495,8 @@ bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
14941495
assert(F->isBitField());
14951496
const T &Value = S.Stk.pop<T>();
14961497
const Pointer &Field = S.Stk.peek<Pointer>().atField(F->Offset);
1497-
Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1498+
Field.deref<T>() =
1499+
Value.truncate(F->Decl->getBitWidthValue(S.getASTContext()));
14981500
Field.activate();
14991501
Field.initialize();
15001502
return true;
@@ -1748,7 +1750,7 @@ bool StoreBitField(InterpState &S, CodePtr OpPC) {
17481750
if (Ptr.canBeInitialized())
17491751
Ptr.initialize();
17501752
if (const auto *FD = Ptr.getField())
1751-
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
1753+
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getASTContext()));
17521754
else
17531755
Ptr.deref<T>() = Value;
17541756
return true;
@@ -1763,7 +1765,7 @@ bool StoreBitFieldPop(InterpState &S, CodePtr OpPC) {
17631765
if (Ptr.canBeInitialized())
17641766
Ptr.initialize();
17651767
if (const auto *FD = Ptr.getField())
1766-
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
1768+
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getASTContext()));
17671769
else
17681770
Ptr.deref<T>() = Value;
17691771
return true;

clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ bool clang::interp::readPointerToBuffer(const Context &Ctx,
269269
Bits BitWidth = FullBitWidth;
270270

271271
if (const FieldDecl *FD = P.getField(); FD && FD->isBitField())
272-
BitWidth = Bits(std::min(FD->getBitWidthValue(),
272+
BitWidth = Bits(std::min(FD->getBitWidthValue(ASTCtx),
273273
(unsigned)FullBitWidth.getQuantity()));
274274
else if (T == PT_Bool && PackedBools)
275275
BitWidth = Bits(1);
@@ -301,8 +301,8 @@ bool clang::interp::readPointerToBuffer(const Context &Ctx,
301301
assert(NumBits.isFullByte());
302302
assert(NumBits.getQuantity() <= FullBitWidth.getQuantity());
303303
F.bitcastToMemory(Buff.get());
304-
// Now, only (maybe) swap the actual size of the float, excluding
305-
// the padding bits.
304+
// Now, only (maybe) swap the actual size of the float, excluding the
305+
// padding bits.
306306
if (llvm::sys::IsBigEndianHost)
307307
swapBytes(Buff.get(), NumBits.roundToBytes());
308308

@@ -406,7 +406,7 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
406406

407407
Bits BitWidth;
408408
if (const FieldDecl *FD = P.getField(); FD && FD->isBitField())
409-
BitWidth = Bits(std::min(FD->getBitWidthValue(),
409+
BitWidth = Bits(std::min(FD->getBitWidthValue(ASTCtx),
410410
(unsigned)FullBitWidth.getQuantity()));
411411
else if (T == PT_Bool && PackedBools)
412412
BitWidth = Bits(1);

clang/lib/AST/Decl.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,24 +4599,18 @@ void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) {
45994599
Init = NewInit;
46004600
}
46014601

4602-
unsigned FieldDecl::getBitWidthValue() const {
4602+
unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
46034603
assert(isBitField() && "not a bitfield");
4604-
assert(isa<ConstantExpr>(getBitWidth()));
4605-
assert(cast<ConstantExpr>(getBitWidth())->hasAPValueResult());
4606-
assert(cast<ConstantExpr>(getBitWidth())->getAPValueResult().isInt());
4607-
return cast<ConstantExpr>(getBitWidth())
4608-
->getAPValueResult()
4609-
.getInt()
4610-
.getZExtValue();
4604+
return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
46114605
}
46124606

4613-
bool FieldDecl::isZeroLengthBitField() const {
4607+
bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const {
46144608
return isUnnamedBitField() && !getBitWidth()->isValueDependent() &&
4615-
getBitWidthValue() == 0;
4609+
getBitWidthValue(Ctx) == 0;
46164610
}
46174611

46184612
bool FieldDecl::isZeroSize(const ASTContext &Ctx) const {
4619-
if (isZeroLengthBitField())
4613+
if (isZeroLengthBitField(Ctx))
46204614
return true;
46214615

46224616
// C++2a [intro.object]p7:

0 commit comments

Comments
 (0)