r320212 - Remove creation of out-of-bounds value of enumeration type (resulting in UB).

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 8 15:29:59 PST 2017


Author: rsmith
Date: Fri Dec  8 15:29:59 2017
New Revision: 320212

URL: http://llvm.org/viewvc/llvm-project?rev=320212&view=rev
Log:
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.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/AST/OperationKinds.h
    cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=320212&r1=320211&r2=320212&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Fri Dec  8 15:29:59 2017
@@ -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());

Modified: cfe/trunk/include/clang/AST/OperationKinds.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/OperationKinds.h?rev=320212&r1=320211&r2=320212&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/OperationKinds.h (original)
+++ cfe/trunk/include/clang/AST/OperationKinds.h Fri Dec  8 15:29:59 2017
@@ -23,8 +23,6 @@ enum CastKind {
 #include "clang/AST/OperationKinds.def"
 };
 
-static const CastKind CK_Invalid = static_cast<CastKind>(-1);
-
 enum BinaryOperatorKind {
 #define BINARY_OPERATION(Name, Spelling) BO_##Name,
 #include "clang/AST/OperationKinds.def"

Modified: cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h?rev=320212&r1=320211&r2=320212&view=diff
==============================================================================
--- cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h (original)
+++ cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h Fri Dec  8 15:29:59 2017
@@ -122,21 +122,20 @@ template <> struct ArgTypeTraits<unsigne
 
 template <> struct ArgTypeTraits<attr::Kind> {
 private:
-  static attr::Kind getAttrKind(llvm::StringRef AttrKind) {
-    return llvm::StringSwitch<attr::Kind>(AttrKind)
+  static Optional<attr::Kind> getAttrKind(llvm::StringRef AttrKind) {
+    return llvm::StringSwitch<Optional<attr::Kind>>(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<CastKind> {
 private:
-  static CastKind getCastKind(llvm::StringRef AttrKind) {
-    return llvm::StringSwitch<CastKind>(AttrKind)
+  static Optional<CastKind> getCastKind(llvm::StringRef AttrKind) {
+    return llvm::StringSwitch<Optional<CastKind>>(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() {

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=320212&r1=320211&r2=320212&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Fri Dec  8 15:29:59 2017
@@ -529,7 +529,7 @@ CastKind Sema::ScalarTypeToBooleanCastKi
   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.

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=320212&r1=320211&r2=320212&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Dec  8 15:29:59 2017
@@ -7511,7 +7511,7 @@ Sema::CheckAssignmentConstraints(SourceL
   // 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::CheckTransparentUnionArgumentConst
       }
     }
 
-    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(Q
     }
   }
   
-  CastKind Kind = CK_Invalid;
+  CastKind Kind;
   Sema::AssignConvertType result =
     CheckAssignmentConstraints(LHSType, RHS, Kind, ConvertRHS);
 
@@ -8114,7 +8114,7 @@ static bool tryVectorConvertAndSplat(Sem
                                      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(Sem
 
   // 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);
   }

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=320212&r1=320211&r2=320212&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Fri Dec  8 15:29:59 2017
@@ -3840,7 +3840,7 @@ Sema::PerformImplicitConversion(Expr *Fr
           << 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 *Fr
   }
 
   case ICK_Pointer_Member: {
-    CastKind Kind = CK_Invalid;
+    CastKind Kind;
     CXXCastPath BasePath;
     if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle))
       return ExprError();




More information about the cfe-commits mailing list