From: Richard Smith Date: Fri, 8 Dec 2017 23:29:59 +0000 (+0000) Subject: Remove creation of out-of-bounds value of enumeration type (resulting in UB). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6afb63d7c872f5de9402d6d65a6567d1d3a76b0d;p=clang Remove creation of out-of-bounds value of enumeration type (resulting in UB). Also remove unnecessary initialization of out-parameters with this value, so that MSan is able to catch errors appropriately. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@320212 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index bef2339e8e..b4c49eaf52 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -2742,7 +2742,6 @@ protected: ty->containsUnexpandedParameterPack()) || (op && op->containsUnexpandedParameterPack()))), Op(op) { - assert(kind != CK_Invalid && "creating cast with invalid cast kind"); CastExprBits.Kind = kind; setBasePathSize(BasePathSize); assert(CastConsistency()); diff --git a/include/clang/AST/OperationKinds.h b/include/clang/AST/OperationKinds.h index 00f060fe9e..e3832689d6 100644 --- a/include/clang/AST/OperationKinds.h +++ b/include/clang/AST/OperationKinds.h @@ -23,8 +23,6 @@ enum CastKind { #include "clang/AST/OperationKinds.def" }; -static const CastKind CK_Invalid = static_cast(-1); - enum BinaryOperatorKind { #define BINARY_OPERATION(Name, Spelling) BO_##Name, #include "clang/AST/OperationKinds.def" diff --git a/lib/ASTMatchers/Dynamic/Marshallers.h b/lib/ASTMatchers/Dynamic/Marshallers.h index d5626b2218..af90e2c7ec 100644 --- a/lib/ASTMatchers/Dynamic/Marshallers.h +++ b/lib/ASTMatchers/Dynamic/Marshallers.h @@ -122,21 +122,20 @@ template <> struct ArgTypeTraits { template <> struct ArgTypeTraits { private: - static attr::Kind getAttrKind(llvm::StringRef AttrKind) { - return llvm::StringSwitch(AttrKind) + static Optional getAttrKind(llvm::StringRef AttrKind) { + return llvm::StringSwitch>(AttrKind) #define ATTR(X) .Case("attr::" #X, attr:: X) #include "clang/Basic/AttrList.inc" - .Default(attr::Kind(-1)); + .Default(llvm::None); } public: static bool is(const VariantValue &Value) { - return Value.isString() && - getAttrKind(Value.getString()) != attr::Kind(-1); + return Value.isString() && getAttrKind(Value.getString()); } static attr::Kind get(const VariantValue &Value) { - return getAttrKind(Value.getString()); + return *getAttrKind(Value.getString()); } static ArgKind getKind() { @@ -146,21 +145,20 @@ public: template <> struct ArgTypeTraits { private: - static CastKind getCastKind(llvm::StringRef AttrKind) { - return llvm::StringSwitch(AttrKind) + static Optional getCastKind(llvm::StringRef AttrKind) { + return llvm::StringSwitch>(AttrKind) #define CAST_OPERATION(Name) .Case( #Name, CK_##Name) #include "clang/AST/OperationKinds.def" - .Default(CK_Invalid); + .Default(llvm::None); } public: static bool is(const VariantValue &Value) { - return Value.isString() && - getCastKind(Value.getString()) != CK_Invalid; + return Value.isString() && getCastKind(Value.getString()); } static CastKind get(const VariantValue &Value) { - return getCastKind(Value.getString()); + return *getCastKind(Value.getString()); } static ArgKind getKind() { diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index 31400e1de8..4e57e5ef81 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -529,7 +529,7 @@ CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) { case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean; case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean; } - return CK_Invalid; + llvm_unreachable("unknown scalar type kind"); } /// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector. diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 7ff533b92c..3bd88c7176 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -7511,7 +7511,7 @@ Sema::CheckAssignmentConstraints(SourceLocation Loc, // usually happen on valid code. OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); ExprResult RHSPtr = &RHSExpr; - CastKind K = CK_Invalid; + CastKind K; return CheckAssignmentConstraints(LHSType, RHSPtr, K, /*ConvertRHS=*/false); } @@ -7903,7 +7903,7 @@ Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, } } - CastKind Kind = CK_Invalid; + CastKind Kind; if (CheckAssignmentConstraints(it->getType(), RHS, Kind) == Compatible) { RHS = ImpCastExprToType(RHS.get(), it->getType(), Kind); @@ -8019,7 +8019,7 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &CallerRHS, } } - CastKind Kind = CK_Invalid; + CastKind Kind; Sema::AssignConvertType result = CheckAssignmentConstraints(LHSType, RHS, Kind, ConvertRHS); @@ -8114,7 +8114,7 @@ static bool tryVectorConvertAndSplat(Sema &S, ExprResult *scalar, unsigned &DiagID) { // The conversion to apply to the scalar before splatting it, // if necessary. - CastKind scalarCast = CK_Invalid; + CastKind scalarCast = CK_NoOp; if (vectorEltTy->isIntegralType(S.Context)) { if (S.getLangOpts().OpenCL && (scalarTy->isRealFloatingType() || @@ -8145,7 +8145,7 @@ static bool tryVectorConvertAndSplat(Sema &S, ExprResult *scalar, // Adjust scalar if desired. if (scalar) { - if (scalarCast != CK_Invalid) + if (scalarCast != CK_NoOp) *scalar = S.ImpCastExprToType(scalar->get(), vectorEltTy, scalarCast); *scalar = S.ImpCastExprToType(scalar->get(), vectorTy, CK_VectorSplat); } diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index e4634d96d1..cd0d715741 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -3840,7 +3840,7 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType, << From->getSourceRange(); } - CastKind Kind = CK_Invalid; + CastKind Kind; CXXCastPath BasePath; if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) return ExprError(); @@ -3860,7 +3860,7 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType, } case ICK_Pointer_Member: { - CastKind Kind = CK_Invalid; + CastKind Kind; CXXCastPath BasePath; if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) return ExprError();