Skip to content

Commit

Permalink
[NFC][Sema] Move Sema::AssignmentAction into its own scoped enum (#…
Browse files Browse the repository at this point in the history
…106453)

The primary motivation behind this is to allow the enum type to be
referred to earlier in the Sema.h file which is needed for #106321.

It was requested in #106321 that a scoped enum be used (rather than
moving the enum declaration earlier in the Sema class declaration).
Unfortunately doing this creates a lot of churn as all use sites of the
enum constants had to be changed. Appologies to all downstream forks in
advanced.

Note the AA_ prefix has been dropped from the enum value names as they
are now redundant.
  • Loading branch information
delcypher authored Aug 29, 2024
1 parent e9eaf19 commit ff04c5b
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 117 deletions.
31 changes: 18 additions & 13 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ class SemaPPCallbacks;
class TemplateDeductionInfo;
} // namespace sema

// AssignmentAction - This is used by all the assignment diagnostic functions
// to represent what is actually causing the operation
enum class AssignmentAction {
Assigning,
Passing,
Returning,
Converting,
Initializing,
Sending,
Casting,
Passing_CFAudited
};
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
const AssignmentAction &AA) {
DB << llvm::to_underlying(AA);
return DB;
}

namespace threadSafety {
class BeforeSet;
void threadSafetyCleanup(BeforeSet *Cache);
Expand Down Expand Up @@ -6493,19 +6511,6 @@ class Sema final : public SemaBase {
/// cleanup that are created by the current full expression.
SmallVector<ExprWithCleanups::CleanupObject, 8> ExprCleanupObjects;

// AssignmentAction - This is used by all the assignment diagnostic functions
// to represent what is actually causing the operation
enum AssignmentAction {
AA_Assigning,
AA_Passing,
AA_Returning,
AA_Converting,
AA_Initializing,
AA_Sending,
AA_Casting,
AA_Passing_CFAudited
};

/// Determine whether the use of this declaration is valid, without
/// emitting diagnostics.
bool CanUseDecl(NamedDecl *D, bool TreatUnavailableAsInvalid);
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Sema/SemaARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ bool SemaARM::CheckNeonBuiltinFunctionCall(const TargetInfo &TI,
if (RHS.isInvalid())
return true;
if (SemaRef.DiagnoseAssignmentResult(ConvTy, Arg->getBeginLoc(), LHSTy,
RHSTy, RHS.get(), Sema::AA_Assigning))
RHSTy, RHS.get(),
AssignmentAction::Assigning))
return true;
}

Expand Down Expand Up @@ -921,7 +922,7 @@ bool SemaARM::CheckARMBuiltinExclusiveCall(unsigned BuiltinID,
CastNeeded = CK_BitCast;
Diag(DRE->getBeginLoc(), diag::ext_typecheck_convert_discards_qualifiers)
<< PointerArg->getType() << Context.getPointerType(AddrType)
<< Sema::AA_Passing << PointerArg->getSourceRange();
<< AssignmentAction::Passing << PointerArg->getSourceRange();
}

// Finally, do the cast and replace the argument with the corrected version.
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2673,7 +2673,7 @@ void CastOperation::checkAddressSpaceCast(QualType SrcType, QualType DestType) {
? DestPPointee.getAddressSpace() != SrcPPointee.getAddressSpace()
: !DestPPointee.isAddressSpaceOverlapping(SrcPPointee)) {
Self.Diag(OpRange.getBegin(), DiagID)
<< SrcType << DestType << Sema::AA_Casting
<< SrcType << DestType << AssignmentAction::Casting
<< SrcExpr.get()->getSourceRange();
if (!Nested)
SrcExpr = ExprError();
Expand Down Expand Up @@ -3213,7 +3213,7 @@ void CastOperation::CheckCStyleCast() {
!CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) {
Self.Diag(SrcExpr.get()->getBeginLoc(),
diag::err_typecheck_incompatible_ownership)
<< SrcType << DestType << Sema::AA_Casting
<< SrcType << DestType << AssignmentAction::Casting
<< SrcExpr.get()->getSourceRange();
return;
}
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4880,7 +4880,8 @@ bool Sema::BuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs,
if (Arg->isTypeDependent())
return false;

ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy, AA_Passing);
ExprResult Res = PerformImplicitConversion(Arg, Context.IntTy,
AssignmentAction::Passing);

if (Res.isInvalid())
return true;
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10871,7 +10871,8 @@ bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) {
ExprResult This =
ActOnCXXThis(OperatorDelete->getParamDecl(0)->getLocation());
assert(!This.isInvalid() && "couldn't form 'this' expr in dtor?");
This = PerformImplicitConversion(This.get(), ParamType, AA_Passing);
This = PerformImplicitConversion(This.get(), ParamType,
AssignmentAction::Passing);
if (This.isInvalid()) {
// FIXME: Register this as a context note so that it comes out
// in the right order.
Expand Down
32 changes: 16 additions & 16 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9586,7 +9586,7 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &CallerRHS,
QualType RHSType = RHS.get()->getType();
if (Diagnose) {
RHS = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(),
AA_Assigning);
AssignmentAction::Assigning);
} else {
ImplicitConversionSequence ICS =
TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(),
Expand All @@ -9598,7 +9598,7 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &CallerRHS,
if (ICS.isFailure())
return Incompatible;
RHS = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(),
ICS, AA_Assigning);
ICS, AssignmentAction::Assigning);
}
if (RHS.isInvalid())
return Incompatible;
Expand Down Expand Up @@ -13660,8 +13660,8 @@ QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS,
ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType);
}

if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType,
RHS.get(), AA_Assigning))
if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, RHS.get(),
AssignmentAction::Assigning))
return QualType();

CheckForNullPointerDereference(*this, LHSExpr);
Expand Down Expand Up @@ -16669,7 +16669,7 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
MayHaveConvFixit = true;
break;
case IncompatiblePointer:
if (Action == AA_Passing_CFAudited) {
if (Action == AssignmentAction::Passing_CFAudited) {
DiagKind = diag::err_arc_typecheck_convert_incompatible_pointer;
} else if (getLangOpts().CPlusPlus) {
DiagKind = diag::err_typecheck_convert_incompatible_pointer;
Expand Down Expand Up @@ -16823,19 +16823,19 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,

QualType FirstType, SecondType;
switch (Action) {
case AA_Assigning:
case AA_Initializing:
case AssignmentAction::Assigning:
case AssignmentAction::Initializing:
// The destination type comes first.
FirstType = DstType;
SecondType = SrcType;
break;

case AA_Returning:
case AA_Passing:
case AA_Passing_CFAudited:
case AA_Converting:
case AA_Sending:
case AA_Casting:
case AssignmentAction::Returning:
case AssignmentAction::Passing:
case AssignmentAction::Passing_CFAudited:
case AssignmentAction::Converting:
case AssignmentAction::Sending:
case AssignmentAction::Casting:
// The source type comes first.
FirstType = SrcType;
SecondType = DstType;
Expand All @@ -16844,8 +16844,8 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,

PartialDiagnostic FDiag = PDiag(DiagKind);
AssignmentAction ActionForDiag = Action;
if (Action == AA_Passing_CFAudited)
ActionForDiag = AA_Passing;
if (Action == AssignmentAction::Passing_CFAudited)
ActionForDiag = AssignmentAction::Passing;

FDiag << FirstType << SecondType << ActionForDiag
<< SrcExpr->getSourceRange();
Expand Down Expand Up @@ -16885,7 +16885,7 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
if (CheckInferredResultType)
ObjC().EmitRelatedResultTypeNote(SrcExpr);

if (Action == AA_Returning && ConvTy == IncompatiblePointer)
if (Action == AssignmentAction::Returning && ConvTy == IncompatiblePointer)
ObjC().EmitRelatedResultTypeNoteForReturn(DstType);

if (Complained)
Expand Down
47 changes: 24 additions & 23 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2199,8 +2199,8 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
if (getLangOpts().CPlusPlus14) {
assert(Context.getTargetInfo().getIntWidth() && "Builtin type of size 0?");

ConvertedSize = PerformImplicitConversion(*ArraySize, Context.getSizeType(),
AA_Converting);
ConvertedSize = PerformImplicitConversion(
*ArraySize, Context.getSizeType(), AssignmentAction::Converting);

if (!ConvertedSize.isInvalid() &&
(*ArraySize)->getType()->getAs<RecordType>())
Expand Down Expand Up @@ -3851,7 +3851,8 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
Context.getQualifiedType(Pointee.getUnqualifiedType(), Qs));
Ex = ImpCastExprToType(Ex.get(), Unqual, CK_NoOp);
}
Ex = PerformImplicitConversion(Ex.get(), ParamType, AA_Passing);
Ex = PerformImplicitConversion(Ex.get(), ParamType,
AssignmentAction::Passing);
if (Ex.isInvalid())
return ExprError();
}
Expand Down Expand Up @@ -4256,10 +4257,9 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
}
// Watch out for ellipsis conversion.
if (!ICS.UserDefined.EllipsisConversion) {
ExprResult Res =
PerformImplicitConversion(From, BeforeToType,
ICS.UserDefined.Before, AA_Converting,
CCK);
ExprResult Res = PerformImplicitConversion(
From, BeforeToType, ICS.UserDefined.Before,
AssignmentAction::Converting, CCK);
if (Res.isInvalid())
return ExprError();
From = Res.get();
Expand All @@ -4282,7 +4282,7 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
return From;

return PerformImplicitConversion(From, ToType, ICS.UserDefined.After,
AA_Converting, CCK);
AssignmentAction::Converting, CCK);
}

case ImplicitConversionSequence::AmbiguousConversion:
Expand Down Expand Up @@ -4451,19 +4451,19 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
// target entity shall allow at least the exceptions allowed by the
// source value in the assignment or initialization.
switch (Action) {
case AA_Assigning:
case AA_Initializing:
case AssignmentAction::Assigning:
case AssignmentAction::Initializing:
// Note, function argument passing and returning are initialization.
case AA_Passing:
case AA_Returning:
case AA_Sending:
case AA_Passing_CFAudited:
case AssignmentAction::Passing:
case AssignmentAction::Returning:
case AssignmentAction::Sending:
case AssignmentAction::Passing_CFAudited:
if (CheckExceptionSpecCompatibility(From, ToType))
return ExprError();
break;

case AA_Casting:
case AA_Converting:
case AssignmentAction::Casting:
case AssignmentAction::Converting:
// Casts and implicit conversions are not initialization, so are not
// checked for exception specification mismatches.
break;
Expand Down Expand Up @@ -4577,9 +4577,10 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,

case ICK_Writeback_Conversion:
case ICK_Pointer_Conversion: {
if (SCS.IncompatibleObjC && Action != AA_Casting) {
if (SCS.IncompatibleObjC && Action != AssignmentAction::Casting) {
// Diagnose incompatible Objective-C conversions
if (Action == AA_Initializing || Action == AA_Assigning)
if (Action == AssignmentAction::Initializing ||
Action == AssignmentAction::Assigning)
Diag(From->getBeginLoc(),
diag::ext_typecheck_convert_incompatible_pointer)
<< ToType << From->getType() << Action << From->getSourceRange()
Expand All @@ -4596,12 +4597,12 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
} else if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() &&
!ObjC().CheckObjCARCUnavailableWeakConversion(ToType,
From->getType())) {
if (Action == AA_Initializing)
if (Action == AssignmentAction::Initializing)
Diag(From->getBeginLoc(), diag::err_arc_weak_unavailable_assign);
else
Diag(From->getBeginLoc(), diag::err_arc_convesion_of_weak_unavailable)
<< (Action == AA_Casting) << From->getType() << ToType
<< From->getSourceRange();
<< (Action == AssignmentAction::Casting) << From->getType()
<< ToType << From->getSourceRange();
}

// Defer address space conversion to the third conversion.
Expand Down Expand Up @@ -6666,14 +6667,14 @@ static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS
// We found a match. Perform the conversions on the arguments and move on.
ExprResult LHSRes = Self.PerformImplicitConversion(
LHS.get(), Best->BuiltinParamTypes[0], Best->Conversions[0],
Sema::AA_Converting);
AssignmentAction::Converting);
if (LHSRes.isInvalid())
break;
LHS = LHSRes;

ExprResult RHSRes = Self.PerformImplicitConversion(
RHS.get(), Best->BuiltinParamTypes[1], Best->Conversions[1],
Sema::AA_Converting);
AssignmentAction::Converting);
if (RHSRes.isInvalid())
break;
RHS = RHSRes;
Expand Down
23 changes: 12 additions & 11 deletions clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6799,43 +6799,44 @@ InitializationSequence::~InitializationSequence() {
//===----------------------------------------------------------------------===//
// Perform initialization
//===----------------------------------------------------------------------===//
static Sema::AssignmentAction
getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) {
static AssignmentAction getAssignmentAction(const InitializedEntity &Entity,
bool Diagnose = false) {
switch(Entity.getKind()) {
case InitializedEntity::EK_Variable:
case InitializedEntity::EK_New:
case InitializedEntity::EK_Exception:
case InitializedEntity::EK_Base:
case InitializedEntity::EK_Delegating:
return Sema::AA_Initializing;
return AssignmentAction::Initializing;

case InitializedEntity::EK_Parameter:
if (Entity.getDecl() &&
isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
return Sema::AA_Sending;
return AssignmentAction::Sending;

return Sema::AA_Passing;
return AssignmentAction::Passing;

case InitializedEntity::EK_Parameter_CF_Audited:
if (Entity.getDecl() &&
isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
return Sema::AA_Sending;
return AssignmentAction::Sending;

return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited;
return !Diagnose ? AssignmentAction::Passing
: AssignmentAction::Passing_CFAudited;

case InitializedEntity::EK_Result:
case InitializedEntity::EK_StmtExprResult: // FIXME: Not quite right.
return Sema::AA_Returning;
return AssignmentAction::Returning;

case InitializedEntity::EK_Temporary:
case InitializedEntity::EK_RelatedResult:
// FIXME: Can we tell apart casting vs. converting?
return Sema::AA_Casting;
return AssignmentAction::Casting;

case InitializedEntity::EK_TemplateParameter:
// This is really initialization, but refer to it as conversion for
// consistency with CheckConvertedConstantExpression.
return Sema::AA_Converting;
return AssignmentAction::Converting;

case InitializedEntity::EK_Member:
case InitializedEntity::EK_ParenAggInitMember:
Expand All @@ -6847,7 +6848,7 @@ getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) {
case InitializedEntity::EK_LambdaToBlockConversionBlockElement:
case InitializedEntity::EK_LambdaCapture:
case InitializedEntity::EK_CompoundLiteralInit:
return Sema::AA_Initializing;
return AssignmentAction::Initializing;
}

llvm_unreachable("Invalid EntityKind!");
Expand Down
Loading

0 comments on commit ff04c5b

Please sign in to comment.