-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Avoid re-evaluating field bitwidth #117732
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
Conversation
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang-codegen Author: Timm Baeder (tbaederr) ChangesPatch is 35.40 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/117732.diff 15 Files Affected:
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 8c39ef3d5a9fa6..db3c86a09bfb49 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3089,17 +3089,27 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
// Active member if ISK is ISK_CapturedVLAType.
const VariableArrayType *CapturedVLAType;
};
+ unsigned BitWidthValue = 0;
protected:
FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
- InClassInitStyle InitStyle)
+ TypeSourceInfo *TInfo, bool Mutable, InClassInitStyle InitStyle)
+ : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), BitField(false),
+ Mutable(Mutable), StorageKind((InitStorageKind)InitStyle),
+ CachedFieldIndex(0), Init() {}
+
+ FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
+ SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, Expr *BW, unsigned BitWidthValue,
+ bool Mutable, InClassInitStyle InitStyle)
: DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), BitField(false),
Mutable(Mutable), StorageKind((InitStorageKind)InitStyle),
CachedFieldIndex(0), Init() {
- if (BW)
+ if (BW) {
setBitWidth(BW);
+ this->BitWidthValue = BitWidthValue;
+ }
}
public:
@@ -3109,7 +3119,15 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
static FieldDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
+ TypeSourceInfo *TInfo, Expr *BW,
+ unsigned BitWidthValue, bool Mutable,
+ InClassInitStyle InitStyle);
+
+ /// For non-bit-fields.
+ static FieldDecl *Create(const ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc, SourceLocation IdLoc,
+ const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, bool Mutable,
InClassInitStyle InitStyle);
static FieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
diff --git a/clang/include/clang/AST/DeclObjC.h b/clang/include/clang/AST/DeclObjC.h
index 4663603f797545..17bccfda784fed 100644
--- a/clang/include/clang/AST/DeclObjC.h
+++ b/clang/include/clang/AST/DeclObjC.h
@@ -1960,8 +1960,15 @@ class ObjCIvarDecl : public FieldDecl {
ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
- bool synthesized)
- : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
+ unsigned BWValue, bool synthesized)
+ : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW, BWValue,
+ /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
+ DeclAccess(ac), Synthesized(synthesized) {}
+
+ ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
+ SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, AccessControl ac, bool synthesized)
+ : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo,
/*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
DeclAccess(ac), Synthesized(synthesized) {}
@@ -1970,7 +1977,8 @@ class ObjCIvarDecl : public FieldDecl {
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, AccessControl ac,
- Expr *BW = nullptr, bool synthesized = false);
+ Expr *BW = nullptr, unsigned BWValue = 0,
+ bool synthesized = false);
static ObjCIvarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
@@ -2028,11 +2036,19 @@ class ObjCIvarDecl : public FieldDecl {
/// Represents a field declaration created by an \@defs(...).
class ObjCAtDefsFieldDecl : public FieldDecl {
ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
- SourceLocation IdLoc, IdentifierInfo *Id,
- QualType T, Expr *BW)
+ SourceLocation IdLoc, IdentifierInfo *Id, QualType T,
+ Expr *BW, unsigned BWValue)
+ : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
+ /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
+ BW, BWValue, /*Mutable=*/false,
+ /*HasInit=*/ICIS_NoInit) {}
+
+ ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
+ SourceLocation IdLoc, IdentifierInfo *Id, QualType T)
: FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
/*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
- BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
+ /*Mutable=*/false,
+ /*HasInit=*/ICIS_NoInit) {}
void anchor() override;
@@ -2040,7 +2056,11 @@ class ObjCAtDefsFieldDecl : public FieldDecl {
static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation StartLoc,
SourceLocation IdLoc, IdentifierInfo *Id,
- QualType T, Expr *BW);
+ QualType T, Expr *BW, unsigned BWValue);
+ static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc,
+ SourceLocation IdLoc, IdentifierInfo *Id,
+ QualType T);
static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C,
GlobalDeclID ID);
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 24abd5d95dd844..5c82a75f491e37 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4277,7 +4277,7 @@ class Sema final : public SemaBase {
/// Returns false on success.
ExprResult VerifyBitField(SourceLocation FieldLoc,
const IdentifierInfo *FieldName, QualType FieldTy,
- bool IsMsStruct, Expr *BitWidth);
+ bool IsMsStruct, Expr *BitWidth, unsigned &BWV);
/// IsValueInFlagEnum - Determine if a value is allowed as part of a flag
/// enum. If AllowMask is true, then we also allow the complement of a valid
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 80e8c5b9df58e7..26631f9a5c7f9b 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -7969,11 +7969,10 @@ TypedefDecl *ASTContext::getCFConstantStringDecl() const {
// Create fields
for (unsigned i = 0; i < Count; ++i) {
- FieldDecl *Field =
- FieldDecl::Create(*this, CFConstantStringTagDecl, SourceLocation(),
- SourceLocation(), &Idents.get(Fields[i].Name),
- Fields[i].Type, /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ *this, CFConstantStringTagDecl, SourceLocation(), SourceLocation(),
+ &Idents.get(Fields[i].Name), Fields[i].Type, /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
CFConstantStringTagDecl->addDecl(Field);
}
@@ -8039,7 +8038,7 @@ QualType ASTContext::getBlockDescriptorType() const {
FieldDecl *Field = FieldDecl::Create(
*this, RD, SourceLocation(), SourceLocation(),
&Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit);
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
RD->addDecl(Field);
}
@@ -8078,7 +8077,6 @@ QualType ASTContext::getBlockDescriptorExtendedType() const {
FieldDecl *Field = FieldDecl::Create(
*this, RD, SourceLocation(), SourceLocation(),
&Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
/*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
RD->addDecl(Field);
@@ -9436,15 +9434,11 @@ CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9489,14 +9483,10 @@ static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ *Context, VaListTagDecl, SourceLocation(), SourceLocation(),
+ &Context->Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9547,15 +9537,11 @@ CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9599,16 +9585,12 @@ CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) {
VaListDecl->startDefinition();
// void * __ap;
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get("__ap"),
- Context->getPointerType(Context->VoidTy),
- /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get("__ap"),
+ Context->getPointerType(Context->VoidTy),
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListDecl->addDecl(Field);
@@ -9650,15 +9632,11 @@ CreateSystemZBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9704,7 +9682,6 @@ static TypedefDecl *CreateHexagonBuiltinVaListDecl(const ASTContext *Context) {
const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
/*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
/*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index a0cd57e2e5ee0d..517d3332d07e07 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4231,7 +4231,7 @@ ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
FieldDecl *ToField;
if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
- ToType, ToTInfo, ToBitWidth, D->isMutable(),
+ ToType, ToTInfo, ToBitWidth, 0, D->isMutable(),
D->getInClassInitStyle()))
return ToField;
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 741e908cf9bc56..e92cc00b23ed5c 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -4555,16 +4555,26 @@ unsigned FunctionDecl::getODRHash() {
FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
+ TypeSourceInfo *TInfo, Expr *BW,
+ unsigned BitWidthValue, bool Mutable,
InClassInitStyle InitStyle) {
return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
- BW, Mutable, InitStyle);
+ BW, BitWidthValue, Mutable, InitStyle);
+}
+
+FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc, SourceLocation IdLoc,
+ const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, bool Mutable,
+ InClassInitStyle InitStyle) {
+ return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
+ nullptr, 0, Mutable, InitStyle);
}
FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
- return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(),
- SourceLocation(), nullptr, QualType(), nullptr,
- nullptr, false, ICIS_NoInit);
+ return new (C, ID)
+ FieldDecl(Field, nullptr, SourceLocation(), SourceLocation(), nullptr,
+ QualType(), nullptr, false, ICIS_NoInit);
}
bool FieldDecl::isAnonymousStructOrUnion() const {
@@ -4601,6 +4611,9 @@ void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) {
unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
assert(isBitField() && "not a bitfield");
+ // FIXME: 0 might be the actual value of the bitwidth.
+ if (BitWidthValue != 0)
+ return BitWidthValue;
return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
}
diff --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp
index 5c107325df30c6..a2ed670cd941ca 100644
--- a/clang/lib/AST/DeclObjC.cpp
+++ b/clang/lib/AST/DeclObjC.cpp
@@ -1833,7 +1833,8 @@ ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, AccessControl ac,
- Expr *BW, bool synthesized) {
+ Expr *BW, unsigned BWValue,
+ bool synthesized) {
if (DC) {
// Ivar's can only appear in interfaces, implementations (via synthesized
// properties), and class extensions (via direct declaration, or synthesized
@@ -1861,13 +1862,13 @@ ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
}
return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
- synthesized);
+ BWValue, synthesized);
}
ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
- return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
- nullptr, QualType(), nullptr,
- ObjCIvarDecl::None, nullptr, false);
+ return new (C, ID)
+ ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(), nullptr,
+ QualType(), nullptr, ObjCIvarDecl::None, false);
}
ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() {
@@ -1905,18 +1906,27 @@ QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
void ObjCAtDefsFieldDecl::anchor() {}
-ObjCAtDefsFieldDecl
-*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
- SourceLocation StartLoc, SourceLocation IdLoc,
- IdentifierInfo *Id, QualType T, Expr *BW) {
- return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
+ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc,
+ SourceLocation IdLoc,
+ IdentifierInfo *Id, QualType T,
+ Expr *BW, unsigned BWValue) {
+ return new (C, DC)
+ ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW, BWValue);
+}
+
+ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc,
+ SourceLocation IdLoc,
+ ...
[truncated]
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesPatch is 35.40 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/117732.diff 15 Files Affected:
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 8c39ef3d5a9fa6..db3c86a09bfb49 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3089,17 +3089,27 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
// Active member if ISK is ISK_CapturedVLAType.
const VariableArrayType *CapturedVLAType;
};
+ unsigned BitWidthValue = 0;
protected:
FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
- InClassInitStyle InitStyle)
+ TypeSourceInfo *TInfo, bool Mutable, InClassInitStyle InitStyle)
+ : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), BitField(false),
+ Mutable(Mutable), StorageKind((InitStorageKind)InitStyle),
+ CachedFieldIndex(0), Init() {}
+
+ FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
+ SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, Expr *BW, unsigned BitWidthValue,
+ bool Mutable, InClassInitStyle InitStyle)
: DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), BitField(false),
Mutable(Mutable), StorageKind((InitStorageKind)InitStyle),
CachedFieldIndex(0), Init() {
- if (BW)
+ if (BW) {
setBitWidth(BW);
+ this->BitWidthValue = BitWidthValue;
+ }
}
public:
@@ -3109,7 +3119,15 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
static FieldDecl *Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
+ TypeSourceInfo *TInfo, Expr *BW,
+ unsigned BitWidthValue, bool Mutable,
+ InClassInitStyle InitStyle);
+
+ /// For non-bit-fields.
+ static FieldDecl *Create(const ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc, SourceLocation IdLoc,
+ const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, bool Mutable,
InClassInitStyle InitStyle);
static FieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
diff --git a/clang/include/clang/AST/DeclObjC.h b/clang/include/clang/AST/DeclObjC.h
index 4663603f797545..17bccfda784fed 100644
--- a/clang/include/clang/AST/DeclObjC.h
+++ b/clang/include/clang/AST/DeclObjC.h
@@ -1960,8 +1960,15 @@ class ObjCIvarDecl : public FieldDecl {
ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
- bool synthesized)
- : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
+ unsigned BWValue, bool synthesized)
+ : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW, BWValue,
+ /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
+ DeclAccess(ac), Synthesized(synthesized) {}
+
+ ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
+ SourceLocation IdLoc, const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, AccessControl ac, bool synthesized)
+ : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo,
/*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
DeclAccess(ac), Synthesized(synthesized) {}
@@ -1970,7 +1977,8 @@ class ObjCIvarDecl : public FieldDecl {
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, AccessControl ac,
- Expr *BW = nullptr, bool synthesized = false);
+ Expr *BW = nullptr, unsigned BWValue = 0,
+ bool synthesized = false);
static ObjCIvarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
@@ -2028,11 +2036,19 @@ class ObjCIvarDecl : public FieldDecl {
/// Represents a field declaration created by an \@defs(...).
class ObjCAtDefsFieldDecl : public FieldDecl {
ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
- SourceLocation IdLoc, IdentifierInfo *Id,
- QualType T, Expr *BW)
+ SourceLocation IdLoc, IdentifierInfo *Id, QualType T,
+ Expr *BW, unsigned BWValue)
+ : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
+ /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
+ BW, BWValue, /*Mutable=*/false,
+ /*HasInit=*/ICIS_NoInit) {}
+
+ ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
+ SourceLocation IdLoc, IdentifierInfo *Id, QualType T)
: FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
/*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
- BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
+ /*Mutable=*/false,
+ /*HasInit=*/ICIS_NoInit) {}
void anchor() override;
@@ -2040,7 +2056,11 @@ class ObjCAtDefsFieldDecl : public FieldDecl {
static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation StartLoc,
SourceLocation IdLoc, IdentifierInfo *Id,
- QualType T, Expr *BW);
+ QualType T, Expr *BW, unsigned BWValue);
+ static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc,
+ SourceLocation IdLoc, IdentifierInfo *Id,
+ QualType T);
static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C,
GlobalDeclID ID);
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 24abd5d95dd844..5c82a75f491e37 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4277,7 +4277,7 @@ class Sema final : public SemaBase {
/// Returns false on success.
ExprResult VerifyBitField(SourceLocation FieldLoc,
const IdentifierInfo *FieldName, QualType FieldTy,
- bool IsMsStruct, Expr *BitWidth);
+ bool IsMsStruct, Expr *BitWidth, unsigned &BWV);
/// IsValueInFlagEnum - Determine if a value is allowed as part of a flag
/// enum. If AllowMask is true, then we also allow the complement of a valid
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 80e8c5b9df58e7..26631f9a5c7f9b 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -7969,11 +7969,10 @@ TypedefDecl *ASTContext::getCFConstantStringDecl() const {
// Create fields
for (unsigned i = 0; i < Count; ++i) {
- FieldDecl *Field =
- FieldDecl::Create(*this, CFConstantStringTagDecl, SourceLocation(),
- SourceLocation(), &Idents.get(Fields[i].Name),
- Fields[i].Type, /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ *this, CFConstantStringTagDecl, SourceLocation(), SourceLocation(),
+ &Idents.get(Fields[i].Name), Fields[i].Type, /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
CFConstantStringTagDecl->addDecl(Field);
}
@@ -8039,7 +8038,7 @@ QualType ASTContext::getBlockDescriptorType() const {
FieldDecl *Field = FieldDecl::Create(
*this, RD, SourceLocation(), SourceLocation(),
&Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit);
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
RD->addDecl(Field);
}
@@ -8078,7 +8077,6 @@ QualType ASTContext::getBlockDescriptorExtendedType() const {
FieldDecl *Field = FieldDecl::Create(
*this, RD, SourceLocation(), SourceLocation(),
&Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
/*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
RD->addDecl(Field);
@@ -9436,15 +9434,11 @@ CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9489,14 +9483,10 @@ static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ *Context, VaListTagDecl, SourceLocation(), SourceLocation(),
+ &Context->Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9547,15 +9537,11 @@ CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9599,16 +9585,12 @@ CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) {
VaListDecl->startDefinition();
// void * __ap;
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get("__ap"),
- Context->getPointerType(Context->VoidTy),
- /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get("__ap"),
+ Context->getPointerType(Context->VoidTy),
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListDecl->addDecl(Field);
@@ -9650,15 +9632,11 @@ CreateSystemZBuiltinVaListDecl(const ASTContext *Context) {
// Create fields
for (unsigned i = 0; i < NumFields; ++i) {
- FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
- VaListTagDecl,
- SourceLocation(),
- SourceLocation(),
- &Context->Idents.get(FieldNames[i]),
- FieldTypes[i], /*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
- /*Mutable=*/false,
- ICIS_NoInit);
+ FieldDecl *Field = FieldDecl::Create(
+ const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
+ SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
+ /*TInfo=*/nullptr,
+ /*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
}
@@ -9704,7 +9682,6 @@ static TypedefDecl *CreateHexagonBuiltinVaListDecl(const ASTContext *Context) {
const_cast<ASTContext &>(*Context), VaListTagDecl, SourceLocation(),
SourceLocation(), &Context->Idents.get(FieldNames[i]), FieldTypes[i],
/*TInfo=*/nullptr,
- /*BitWidth=*/nullptr,
/*Mutable=*/false, ICIS_NoInit);
Field->setAccess(AS_public);
VaListTagDecl->addDecl(Field);
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index a0cd57e2e5ee0d..517d3332d07e07 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4231,7 +4231,7 @@ ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
FieldDecl *ToField;
if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
- ToType, ToTInfo, ToBitWidth, D->isMutable(),
+ ToType, ToTInfo, ToBitWidth, 0, D->isMutable(),
D->getInClassInitStyle()))
return ToField;
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 741e908cf9bc56..e92cc00b23ed5c 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -4555,16 +4555,26 @@ unsigned FunctionDecl::getODRHash() {
FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
SourceLocation StartLoc, SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
- TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
+ TypeSourceInfo *TInfo, Expr *BW,
+ unsigned BitWidthValue, bool Mutable,
InClassInitStyle InitStyle) {
return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
- BW, Mutable, InitStyle);
+ BW, BitWidthValue, Mutable, InitStyle);
+}
+
+FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc, SourceLocation IdLoc,
+ const IdentifierInfo *Id, QualType T,
+ TypeSourceInfo *TInfo, bool Mutable,
+ InClassInitStyle InitStyle) {
+ return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
+ nullptr, 0, Mutable, InitStyle);
}
FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
- return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(),
- SourceLocation(), nullptr, QualType(), nullptr,
- nullptr, false, ICIS_NoInit);
+ return new (C, ID)
+ FieldDecl(Field, nullptr, SourceLocation(), SourceLocation(), nullptr,
+ QualType(), nullptr, false, ICIS_NoInit);
}
bool FieldDecl::isAnonymousStructOrUnion() const {
@@ -4601,6 +4611,9 @@ void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) {
unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
assert(isBitField() && "not a bitfield");
+ // FIXME: 0 might be the actual value of the bitwidth.
+ if (BitWidthValue != 0)
+ return BitWidthValue;
return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
}
diff --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp
index 5c107325df30c6..a2ed670cd941ca 100644
--- a/clang/lib/AST/DeclObjC.cpp
+++ b/clang/lib/AST/DeclObjC.cpp
@@ -1833,7 +1833,8 @@ ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
SourceLocation IdLoc,
const IdentifierInfo *Id, QualType T,
TypeSourceInfo *TInfo, AccessControl ac,
- Expr *BW, bool synthesized) {
+ Expr *BW, unsigned BWValue,
+ bool synthesized) {
if (DC) {
// Ivar's can only appear in interfaces, implementations (via synthesized
// properties), and class extensions (via direct declaration, or synthesized
@@ -1861,13 +1862,13 @@ ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
}
return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
- synthesized);
+ BWValue, synthesized);
}
ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
- return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
- nullptr, QualType(), nullptr,
- ObjCIvarDecl::None, nullptr, false);
+ return new (C, ID)
+ ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(), nullptr,
+ QualType(), nullptr, ObjCIvarDecl::None, false);
}
ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() {
@@ -1905,18 +1906,27 @@ QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
void ObjCAtDefsFieldDecl::anchor() {}
-ObjCAtDefsFieldDecl
-*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
- SourceLocation StartLoc, SourceLocation IdLoc,
- IdentifierInfo *Id, QualType T, Expr *BW) {
- return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
+ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc,
+ SourceLocation IdLoc,
+ IdentifierInfo *Id, QualType T,
+ Expr *BW, unsigned BWValue) {
+ return new (C, DC)
+ ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW, BWValue);
+}
+
+ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
+ SourceLocation StartLoc,
+ SourceLocation IdLoc,
+ ...
[truncated]
|
Wouldn't wrapping the value in a ConstantExpr achieve the same outcome? |
I was hoping that saving the value as an
So, you know... barely any difference in both cases I guess. |
The ConstantExpr version is much simpler, so I have a preference for that |
df3ab05
to
ca2184f
Compare
5516ef9
to
65e1008
Compare
Meh, saving a |
The idea is that you'd store an The instantiation of a |
Most recent result: http://llvm-compile-time-tracker.com/compare.php?from=6f16a8bf17ac9a171b5435ee53c3d2bef657bdad&to=65e10089b0ca8e2103e6fcf6b082d2bebd3ef458&stat=instructions%3Au
I was more thinking about other consumers of clang as as a library, e.g. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems really reasonable to me. I'd like @cor3ntin to do a final approval here, but this seems right to me (though I might have gotten lost in the various only vaguely-related parts of this change :) ).
Ping @cor3ntin |
663a7c5
to
980c977
Compare
Ping |
1 similar comment
Ping |
I was/still am OK with this, but wanted @cor3ntin to take a final pass. |
Yep, that's what I was pinging for :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neat.
Sorry for the delay
Python binding tests fail for unrelated reasons:
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/13647 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/18/builds/9471 Here is the relevant piece of the build log for the reference
|
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.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/59/builds/10743 Here is the relevant piece of the build log for the reference
|
This reverts commit 59bdea2.
This reverts commit 59bdea2.
Save the bitwidth value as a
ConstantExpr
with the value set. Remove theASTContext
parameter fromgetBitWidthValue()
, so the latter simply returns the value from theConstantExpr
instead of constant-evaluating the bitwidth expression every time it is called.