https://github.com/efriedma-quic created https://github.com/llvm/llvm-project/pull/185335
As discussed in #182203, use enums instead. I tried to name/use them appropriately, but I'm not sure sure I'm really happy with the results; suggestions welcome. >From 48d28f4d0c2b62ffd859ababde43c972b9e13d2a Mon Sep 17 00:00:00 2001 From: Eli Friedman <[email protected]> Date: Sun, 8 Mar 2026 18:36:39 -0700 Subject: [PATCH] [clang][NFC] Clean up InitializedEntitiy booleans. As discussed in #182203, use enums instead. I tried to name/use them appropriately, but I'm not sure sure I'm really happy with the results; suggestions welcome. --- clang/include/clang/Sema/Initialization.h | 75 +++++++++++++---------- clang/lib/Sema/SemaDeclCXX.cpp | 19 +++--- clang/lib/Sema/SemaExprCXX.cpp | 9 +-- clang/lib/Sema/SemaInit.cpp | 2 +- 4 files changed, 60 insertions(+), 45 deletions(-) diff --git a/clang/include/clang/Sema/Initialization.h b/clang/include/clang/Sema/Initialization.h index 1978ac41c58a4..b9d6a63a6c6e4 100644 --- a/clang/include/clang/Sema/Initialization.h +++ b/clang/include/clang/Sema/Initialization.h @@ -136,6 +136,19 @@ class alignas(8) InitializedEntity { // that diagnostic text needs to be updated as well. }; + enum NRVOKind { NoNRVO, NRVOAllowed }; + + enum NewArrayKind { + KnownLengthNewArray, + UnknownLengthNewArray, + }; + + enum ImplicitFieldInitKind { NotImplicitFieldInit, ImplicitFieldInit }; + + enum DefaultMemberInitKind { NotDefaultMemberInit, DefaultMemberInit }; + + enum ParenAggInitKind { NotParenAggInit, ParenAggInit }; + private: /// The kind of entity being initialized. EntityKind Kind; @@ -159,11 +172,11 @@ class alignas(8) InitializedEntity { /// Whether the entity being initialized may end up using the /// named return value optimization (NRVO). - bool NRVO; + NRVOKind IsNRVO; /// When Kind == EK_New, whether this is initializing an array of runtime /// size (which needs an array filler). - bool VariableLengthArrayNew; + NewArrayKind IsVariableLengthArrayNew; }; struct VD { @@ -174,11 +187,11 @@ class alignas(8) InitializedEntity { /// When Kind == EK_Member, whether this is an implicit member /// initialization in a copy or move constructor. These can perform array /// copies. - bool IsImplicitFieldInit; + ImplicitFieldInitKind IsImplicitFieldInit; /// When Kind == EK_Member, whether this is the initial initialization /// check for a default member initializer. - bool IsDefaultMemberInit; + DefaultMemberInitKind IsDefaultMemberInit; }; struct C { @@ -225,24 +238,24 @@ class alignas(8) InitializedEntity { /// Create the initialization entity for a variable. InitializedEntity(VarDecl *Var, EntityKind EK = EK_Variable) - : Kind(EK), Type(Var->getType()), Variable{Var, false, false} {} + : Kind(EK), Type(Var->getType()), + Variable{Var, NotImplicitFieldInit, NotDefaultMemberInit} {} /// Create the initialization entity for the result of a /// function, throwing an object, performing an explicit cast, or /// initializing a parameter for which there is no declaration. InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type, - bool NRVO = false, bool VariableLengthArrayNew = false) + NRVOKind IsNRVO = NoNRVO, + NewArrayKind VariableLengthArrayNew = KnownLengthNewArray) : Kind(Kind), Type(Type) { - new (&LocAndNRVO) LN; - LocAndNRVO.Location = Loc; - LocAndNRVO.NRVO = NRVO; - LocAndNRVO.VariableLengthArrayNew = VariableLengthArrayNew; + new (&LocAndNRVO) LN{Loc, IsNRVO, VariableLengthArrayNew}; } /// Create the initialization entity for a member subobject. InitializedEntity(FieldDecl *Member, const InitializedEntity *Parent, - bool Implicit, bool DefaultMemberInit, - bool IsParenAggInit = false) + ImplicitFieldInitKind Implicit, + DefaultMemberInitKind DefaultMemberInit, + ParenAggInitKind IsParenAggInit) : Kind(IsParenAggInit ? EK_ParenAggInitMember : EK_Member), Parent(Parent), Type(Member->getType()), Variable{Member, Implicit, DefaultMemberInit} {} @@ -254,9 +267,7 @@ class alignas(8) InitializedEntity { /// Create the initialization entity for a lambda capture. InitializedEntity(IdentifierInfo *VarID, QualType FieldType, SourceLocation Loc) : Kind(EK_LambdaCapture), Type(FieldType) { - new (&Capture) C; - Capture.VarID = VarID; - Capture.Location = Loc; + new (&Capture) C{VarID, Loc}; } public: @@ -307,7 +318,7 @@ class alignas(8) InitializedEntity { Entity.Kind = EK_TemplateParameter; Entity.Type = T; Entity.Parent = nullptr; - Entity.Variable = {Param, false, false}; + Entity.Variable = {Param, NotImplicitFieldInit, NotDefaultMemberInit}; return Entity; } @@ -340,10 +351,11 @@ class alignas(8) InitializedEntity { } /// Create the initialization entity for an object allocated via new. - static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type, - bool VariableLengthArrayNew) { - return InitializedEntity(EK_New, NewLoc, Type, /*NRVO=*/false, - VariableLengthArrayNew); + static InitializedEntity + InitializeNew(SourceLocation NewLoc, QualType Type, + NewArrayKind IsVariableLengthArrayNew) { + return InitializedEntity(EK_New, NewLoc, Type, NoNRVO, + IsVariableLengthArrayNew); } /// Create the initialization entity for a temporary. @@ -392,32 +404,33 @@ class alignas(8) InitializedEntity { /// Create the initialization entity for a member subobject. static InitializedEntity - InitializeMember(FieldDecl *Member, - const InitializedEntity *Parent = nullptr, - bool Implicit = false) { - return InitializedEntity(Member, Parent, Implicit, false); + InitializeMember(FieldDecl *Member, const InitializedEntity *Parent = nullptr, + ImplicitFieldInitKind Implicit = NotImplicitFieldInit) { + return InitializedEntity(Member, Parent, Implicit, NotDefaultMemberInit, + NotParenAggInit); } /// Create the initialization entity for a member subobject. static InitializedEntity InitializeMember(IndirectFieldDecl *Member, const InitializedEntity *Parent = nullptr, - bool Implicit = false) { - return InitializedEntity(Member->getAnonField(), Parent, Implicit, false); + ImplicitFieldInitKind Implicit = NotImplicitFieldInit) { + return InitializedEntity(Member->getAnonField(), Parent, Implicit, + NotDefaultMemberInit, NotParenAggInit); } /// Create the initialization entity for a member subobject initialized via /// parenthesized aggregate init. static InitializedEntity InitializeMemberFromParenAggInit(FieldDecl *Member) { - return InitializedEntity(Member, /*Parent=*/nullptr, /*Implicit=*/false, - /*DefaultMemberInit=*/false, - /*IsParenAggInit=*/true); + return InitializedEntity(Member, /*Parent=*/nullptr, NotImplicitFieldInit, + NotDefaultMemberInit, NotParenAggInit); } /// Create the initialization entity for a default member initializer. static InitializedEntity InitializeMemberFromDefaultMemberInitializer(FieldDecl *Member) { - return InitializedEntity(Member, nullptr, false, true); + return InitializedEntity(Member, nullptr, NotImplicitFieldInit, + DefaultMemberInit, NotParenAggInit); } /// Create the initialization entity for an array element. @@ -513,7 +526,7 @@ class alignas(8) InitializedEntity { /// Determine whether this is an array new with an unknown bound. bool isVariableLengthArrayNew() const { - return getKind() == EK_New && LocAndNRVO.VariableLengthArrayNew; + return getKind() == EK_New && LocAndNRVO.IsVariableLengthArrayNew; } /// Is this the implicit initialization of a member of a class from diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 2ae6e5de0e3ee..469868b039b58 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -5044,10 +5044,10 @@ BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, } InitializedEntity Entity = - Indirect ? InitializedEntity::InitializeMember(Indirect, nullptr, - /*Implicit*/ true) - : InitializedEntity::InitializeMember(Field, nullptr, - /*Implicit*/ true); + Indirect ? InitializedEntity::InitializeMember( + Indirect, nullptr, InitializedEntity::ImplicitFieldInit) + : InitializedEntity::InitializeMember( + Field, nullptr, InitializedEntity::ImplicitFieldInit); // Direct-initialize to use the copy constructor. InitializationKind InitKind = @@ -5078,10 +5078,10 @@ BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, if (FieldBaseElementType->isRecordType()) { InitializedEntity InitEntity = - Indirect ? InitializedEntity::InitializeMember(Indirect, nullptr, - /*Implicit*/ true) - : InitializedEntity::InitializeMember(Field, nullptr, - /*Implicit*/ true); + Indirect ? InitializedEntity::InitializeMember( + Indirect, nullptr, InitializedEntity::ImplicitFieldInit) + : InitializedEntity::InitializeMember( + Field, nullptr, InitializedEntity::ImplicitFieldInit); InitializationKind InitKind = InitializationKind::CreateDefault(Loc); @@ -5282,7 +5282,8 @@ static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, if (DIE.isInvalid()) return true; - auto Entity = InitializedEntity::InitializeMember(Field, nullptr, true); + auto Entity = InitializedEntity::InitializeMember( + Field, nullptr, InitializedEntity::ImplicitFieldInit); SemaRef.checkInitializerLifetime(Entity, DIE.get()); CXXCtorInitializer *Init; diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 5a5bbf4d900dc..1312c78f96751 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -2206,9 +2206,8 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, << /*array*/ 2 << (*ArraySize ? (*ArraySize)->getSourceRange() : TypeRange)); - InitializedEntity Entity = - InitializedEntity::InitializeNew(StartLoc, AllocType, - /*VariableLengthArrayNew=*/false); + InitializedEntity Entity = InitializedEntity::InitializeNew( + StartLoc, AllocType, InitializedEntity::KnownLengthNewArray); AllocType = DeduceTemplateSpecializationFromInitializer( AllocTypeInfo, Entity, Kind, Exprs); if (AllocType.isNull()) @@ -2592,7 +2591,9 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, bool VariableLengthArrayNew = ArraySize && *ArraySize && !KnownArraySize; InitializedEntity Entity = InitializedEntity::InitializeNew( - StartLoc, InitType, VariableLengthArrayNew); + StartLoc, InitType, + VariableLengthArrayNew ? InitializedEntity::UnknownLengthNewArray + : InitializedEntity::KnownLengthNewArray); InitializationSequence InitSeq(*this, Entity, Kind, Exprs); ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, Exprs); if (FullInit.isInvalid()) diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 48be03bb2f0f8..9dd0b948747e3 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -3847,7 +3847,7 @@ bool InitializedEntity::allowsNRVO() const { switch (getKind()) { case EK_Result: case EK_Exception: - return LocAndNRVO.NRVO; + return LocAndNRVO.IsNRVO; case EK_StmtExprResult: case EK_Variable: _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
