Skip to content

Commit e11b17a

Browse files
committed
[clang][NFC] Refactor Sema::CheckedConversionKind
Convert it to scoped enum, and move it to namespace scope to enable forward declarations.
1 parent bc3620d commit e11b17a

File tree

8 files changed

+119
-104
lines changed

8 files changed

+119
-104
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,20 @@ enum class CXXSpecialMemberKind {
437437
Invalid
438438
};
439439

440+
/// The kind of conversion being performed.
441+
enum class CheckedConversionKind {
442+
/// An implicit conversion.
443+
Implicit,
444+
/// A C-style cast.
445+
CStyleCast,
446+
/// A functional-style cast.
447+
FunctionalCast,
448+
/// A cast other than a C-style cast.
449+
OtherCast,
450+
/// A conversion for an operand of a builtin overloaded operator.
451+
ForBuiltinOverloadedOp
452+
};
453+
440454
/// Sema - This implements semantic analysis and AST building for C.
441455
/// \nosubgrouping
442456
class Sema final : public SemaBase {
@@ -700,28 +714,27 @@ class Sema final : public SemaBase {
700714
void checkTypeSupport(QualType Ty, SourceLocation Loc,
701715
ValueDecl *D = nullptr);
702716

703-
/// The kind of conversion being performed.
704-
enum CheckedConversionKind {
705-
/// An implicit conversion.
706-
CCK_ImplicitConversion,
707-
/// A C-style cast.
708-
CCK_CStyleCast,
709-
/// A functional-style cast.
710-
CCK_FunctionalCast,
711-
/// A cast other than a C-style cast.
712-
CCK_OtherCast,
713-
/// A conversion for an operand of a builtin overloaded operator.
714-
CCK_ForBuiltinOverloadedOp
715-
};
717+
// /// The kind of conversion being performed.
718+
// enum CheckedConversionKind {
719+
// /// An implicit conversion.
720+
// CCK_ImplicitConversion,
721+
// /// A C-style cast.
722+
// CCK_CStyleCast,
723+
// /// A functional-style cast.
724+
// CCK_FunctionalCast,
725+
// /// A cast other than a C-style cast.
726+
// CCK_OtherCast,
727+
// /// A conversion for an operand of a builtin overloaded operator.
728+
// CCK_ForBuiltinOverloadedOp
729+
// };
716730

717731
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit
718732
/// cast. If there is already an implicit cast, merge into the existing one.
719733
/// If isLvalue, the result of the cast is an lvalue.
720-
ExprResult
721-
ImpCastExprToType(Expr *E, QualType Type, CastKind CK,
722-
ExprValueKind VK = VK_PRValue,
723-
const CXXCastPath *BasePath = nullptr,
724-
CheckedConversionKind CCK = CCK_ImplicitConversion);
734+
ExprResult ImpCastExprToType(
735+
Expr *E, QualType Type, CastKind CK, ExprValueKind VK = VK_PRValue,
736+
const CXXCastPath *BasePath = nullptr,
737+
CheckedConversionKind CCK = CheckedConversionKind::Implicit);
725738

726739
/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
727740
/// to the conversion from scalar type ScalarTy to the Boolean type.
@@ -1781,8 +1794,9 @@ class Sema final : public SemaBase {
17811794

17821795
public:
17831796
static bool isCast(CheckedConversionKind CCK) {
1784-
return CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast ||
1785-
CCK == CCK_OtherCast;
1797+
return CCK == CheckedConversionKind::CStyleCast ||
1798+
CCK == CheckedConversionKind::FunctionalCast ||
1799+
CCK == CheckedConversionKind::OtherCast;
17861800
}
17871801

17881802
/// ActOnCXXNamedCast - Parse
@@ -6739,11 +6753,10 @@ class Sema final : public SemaBase {
67396753

67406754
bool IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType);
67416755

6742-
ExprResult
6743-
PerformImplicitConversion(Expr *From, QualType ToType,
6744-
const ImplicitConversionSequence &ICS,
6745-
AssignmentAction Action,
6746-
CheckedConversionKind CCK = CCK_ImplicitConversion);
6756+
ExprResult PerformImplicitConversion(
6757+
Expr *From, QualType ToType, const ImplicitConversionSequence &ICS,
6758+
AssignmentAction Action,
6759+
CheckedConversionKind CCK = CheckedConversionKind::Implicit);
67476760
ExprResult PerformImplicitConversion(Expr *From, QualType ToType,
67486761
const StandardConversionSequence &SCS,
67496762
AssignmentAction Action,
@@ -7064,7 +7077,7 @@ class Sema final : public SemaBase {
70647077

70657078
ExprResult PerformQualificationConversion(
70667079
Expr *E, QualType Ty, ExprValueKind VK = VK_PRValue,
7067-
CheckedConversionKind CCK = CCK_ImplicitConversion);
7080+
CheckedConversionKind CCK = CheckedConversionKind::Implicit);
70687081

70697082
bool CanPerformCopyInitialization(const InitializedEntity &Entity,
70707083
ExprResult Init);

clang/lib/Sema/SemaCast.cpp

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ namespace {
155155
Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange);
156156
}
157157

158-
void checkObjCConversion(Sema::CheckedConversionKind CCK) {
158+
void checkObjCConversion(CheckedConversionKind CCK) {
159159
assert(Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers());
160160

161161
Expr *src = SrcExpr.get();
@@ -248,18 +248,14 @@ static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExp
248248
CastKind &Kind,
249249
CXXCastPath &BasePath);
250250

251-
static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr,
252-
QualType DestType,
253-
Sema::CheckedConversionKind CCK,
254-
SourceRange OpRange,
255-
unsigned &msg, CastKind &Kind,
256-
bool ListInitialization);
251+
static TryCastResult
252+
TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
253+
CheckedConversionKind CCK, SourceRange OpRange,
254+
unsigned &msg, CastKind &Kind, bool ListInitialization);
257255
static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
258-
QualType DestType,
259-
Sema::CheckedConversionKind CCK,
260-
SourceRange OpRange,
261-
unsigned &msg, CastKind &Kind,
262-
CXXCastPath &BasePath,
256+
QualType DestType, CheckedConversionKind CCK,
257+
SourceRange OpRange, unsigned &msg,
258+
CastKind &Kind, CXXCastPath &BasePath,
263259
bool ListInitialization);
264260
static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr,
265261
QualType DestType, bool CStyle,
@@ -1223,7 +1219,7 @@ void CastOperation::CheckReinterpretCast() {
12231219

12241220
if (isValidCast(tcr)) {
12251221
if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
1226-
checkObjCConversion(Sema::CCK_OtherCast);
1222+
checkObjCConversion(CheckedConversionKind::OtherCast);
12271223
DiagnoseReinterpretUpDownCast(Self, SrcExpr.get(), DestType, OpRange);
12281224

12291225
if (unsigned DiagID = checkCastFunctionType(Self, SrcExpr, DestType))
@@ -1274,9 +1270,9 @@ void CastOperation::CheckStaticCast() {
12741270
}
12751271

12761272
unsigned msg = diag::err_bad_cxx_cast_generic;
1277-
TryCastResult tcr
1278-
= TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg,
1279-
Kind, BasePath, /*ListInitialization=*/false);
1273+
TryCastResult tcr =
1274+
TryStaticCast(Self, SrcExpr, DestType, CheckedConversionKind::OtherCast,
1275+
OpRange, msg, Kind, BasePath, /*ListInitialization=*/false);
12801276
if (tcr != TC_Success && msg != 0) {
12811277
if (SrcExpr.isInvalid())
12821278
return;
@@ -1296,7 +1292,7 @@ void CastOperation::CheckStaticCast() {
12961292
if (Kind == CK_BitCast)
12971293
checkCastAlign();
12981294
if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
1299-
checkObjCConversion(Sema::CCK_OtherCast);
1295+
checkObjCConversion(CheckedConversionKind::OtherCast);
13001296
} else {
13011297
SrcExpr = ExprError();
13021298
}
@@ -1317,14 +1313,13 @@ static bool IsAddressSpaceConversion(QualType SrcType, QualType DestType) {
13171313
/// possible. If @p CStyle, ignore access restrictions on hierarchy casting
13181314
/// and casting away constness.
13191315
static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
1320-
QualType DestType,
1321-
Sema::CheckedConversionKind CCK,
1316+
QualType DestType, CheckedConversionKind CCK,
13221317
SourceRange OpRange, unsigned &msg,
13231318
CastKind &Kind, CXXCastPath &BasePath,
13241319
bool ListInitialization) {
13251320
// Determine whether we have the semantics of a C-style cast.
1326-
bool CStyle
1327-
= (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
1321+
bool CStyle = (CCK == CheckedConversionKind::CStyleCast ||
1322+
CCK == CheckedConversionKind::FunctionalCast);
13281323

13291324
// The order the tests is not entirely arbitrary. There is one conversion
13301325
// that can be handled in two different ways. Given:
@@ -1884,11 +1879,11 @@ TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType,
18841879
///
18851880
/// An expression e can be explicitly converted to a type T using a
18861881
/// @c static_cast if the declaration "T t(e);" is well-formed [...].
1887-
TryCastResult
1888-
TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
1889-
Sema::CheckedConversionKind CCK,
1890-
SourceRange OpRange, unsigned &msg,
1891-
CastKind &Kind, bool ListInitialization) {
1882+
TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr,
1883+
QualType DestType,
1884+
CheckedConversionKind CCK,
1885+
SourceRange OpRange, unsigned &msg,
1886+
CastKind &Kind, bool ListInitialization) {
18921887
if (DestType->isRecordType()) {
18931888
if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
18941889
diag::err_bad_cast_incomplete) ||
@@ -1900,13 +1895,14 @@ TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
19001895
}
19011896

19021897
InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
1903-
InitializationKind InitKind
1904-
= (CCK == Sema::CCK_CStyleCast)
1905-
? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange,
1906-
ListInitialization)
1907-
: (CCK == Sema::CCK_FunctionalCast)
1908-
? InitializationKind::CreateFunctionalCast(OpRange, ListInitialization)
1909-
: InitializationKind::CreateCast(OpRange);
1898+
InitializationKind InitKind =
1899+
(CCK == CheckedConversionKind::CStyleCast)
1900+
? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange,
1901+
ListInitialization)
1902+
: (CCK == CheckedConversionKind::FunctionalCast)
1903+
? InitializationKind::CreateFunctionalCast(OpRange,
1904+
ListInitialization)
1905+
: InitializationKind::CreateCast(OpRange);
19101906
Expr *SrcExprRaw = SrcExpr.get();
19111907
// FIXME: Per DR242, we should check for an implicit conversion sequence
19121908
// or for a constructor that could be invoked by direct-initialization
@@ -1918,8 +1914,8 @@ TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
19181914
// There is no other way that works.
19191915
// On the other hand, if we're checking a C-style cast, we've still got
19201916
// the reinterpret_cast way.
1921-
bool CStyle
1922-
= (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
1917+
bool CStyle = (CCK == CheckedConversionKind::CStyleCast ||
1918+
CCK == CheckedConversionKind::FunctionalCast);
19231919
if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType()))
19241920
return TC_NotApplicable;
19251921

@@ -2814,8 +2810,9 @@ void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
28142810
if (isValidCast(tcr))
28152811
Kind = CK_NoOp;
28162812

2817-
Sema::CheckedConversionKind CCK =
2818-
FunctionalStyle ? Sema::CCK_FunctionalCast : Sema::CCK_CStyleCast;
2813+
CheckedConversionKind CCK = FunctionalStyle
2814+
? CheckedConversionKind::FunctionalCast
2815+
: CheckedConversionKind::CStyleCast;
28192816
if (tcr == TC_NotApplicable) {
28202817
tcr = TryAddressSpaceCast(Self, SrcExpr, DestType, /*CStyle*/ true, msg,
28212818
Kind);
@@ -3201,7 +3198,7 @@ void CastOperation::CheckCStyleCast() {
32013198

32023199
// ARC imposes extra restrictions on casts.
32033200
if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) {
3204-
checkObjCConversion(Sema::CCK_CStyleCast);
3201+
checkObjCConversion(CheckedConversionKind::CStyleCast);
32053202
if (SrcExpr.isInvalid())
32063203
return;
32073204

clang/lib/Sema/SemaExpr.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10177,8 +10177,9 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &CallerRHS,
1017710177
// diagnostics and just checking for errors, e.g., during overload
1017810178
// resolution, return Incompatible to indicate the failure.
1017910179
if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() &&
10180-
CheckObjCConversion(SourceRange(), Ty, E, CCK_ImplicitConversion,
10181-
Diagnose, DiagnoseCFAudited) != ACR_okay) {
10180+
CheckObjCConversion(SourceRange(), Ty, E,
10181+
CheckedConversionKind::Implicit, Diagnose,
10182+
DiagnoseCFAudited) != ACR_okay) {
1018210183
if (!Diagnose)
1018310184
return Incompatible;
1018410185
}
@@ -12899,14 +12900,15 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS,
1289912900
Expr *E = LHS.get();
1290012901
if (getLangOpts().ObjCAutoRefCount)
1290112902
CheckObjCConversion(SourceRange(), RHSType, E,
12902-
CCK_ImplicitConversion);
12903+
CheckedConversionKind::Implicit);
1290312904
LHS = ImpCastExprToType(E, RHSType,
1290412905
RPT ? CK_BitCast :CK_CPointerToObjCPointerCast);
1290512906
}
1290612907
else {
1290712908
Expr *E = RHS.get();
1290812909
if (getLangOpts().ObjCAutoRefCount)
12909-
CheckObjCConversion(SourceRange(), LHSType, E, CCK_ImplicitConversion,
12910+
CheckObjCConversion(SourceRange(), LHSType, E,
12911+
CheckedConversionKind::Implicit,
1291012912
/*Diagnose=*/true,
1291112913
/*DiagnoseCFAudited=*/false, Opc);
1291212914
RHS = ImpCastExprToType(E, LHSType,

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,7 +4250,8 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
42504250
AssignmentAction Action,
42514251
CheckedConversionKind CCK) {
42524252
// C++ [over.match.oper]p7: [...] operands of class type are converted [...]
4253-
if (CCK == CCK_ForBuiltinOverloadedOp && !From->getType()->isRecordType())
4253+
if (CCK == CheckedConversionKind::ForBuiltinOverloadedOp &&
4254+
!From->getType()->isRecordType())
42544255
return From;
42554256

42564257
switch (ICS.getKind()) {
@@ -4311,7 +4312,7 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
43114312
// C++ [over.match.oper]p7:
43124313
// [...] the second standard conversion sequence of a user-defined
43134314
// conversion sequence is not applied.
4314-
if (CCK == CCK_ForBuiltinOverloadedOp)
4315+
if (CCK == CheckedConversionKind::ForBuiltinOverloadedOp)
43154316
return From;
43164317

43174318
return PerformImplicitConversion(From, ToType, ICS.UserDefined.After,
@@ -4352,7 +4353,8 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
43524353
const StandardConversionSequence& SCS,
43534354
AssignmentAction Action,
43544355
CheckedConversionKind CCK) {
4355-
bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast);
4356+
bool CStyle = (CCK == CheckedConversionKind::CStyleCast ||
4357+
CCK == CheckedConversionKind::FunctionalCast);
43564358

43574359
// Overall FIXME: we are recomputing too many types here and doing far too
43584360
// much extra work. What this means is that we need to keep track of more

0 commit comments

Comments
 (0)