]> granicus.if.org Git - clang/commitdiff
In C++ the argument of logical not should always be bool. Added missing implicit...
authorAbramo Bagnara <abramo.bagnara@gmail.com>
Thu, 7 Apr 2011 09:26:19 +0000 (09:26 +0000)
committerAbramo Bagnara <abramo.bagnara@gmail.com>
Thu, 7 Apr 2011 09:26:19 +0000 (09:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129066 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Sema/Sema.h
lib/Sema/Sema.cpp
lib/Sema/SemaExpr.cpp
lib/Sema/SemaExprCXX.cpp

index 6adf844c97e10c71f315d6c59bc8d1bde0466d7f..356ffed6941427710eb890bb7231e520c0a67e8d 100644 (file)
@@ -4749,6 +4749,10 @@ public:
                          ExprValueKind VK = VK_RValue,
                          const CXXCastPath *BasePath = 0);
 
+  /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
+  /// to the conversion from scalar type ScalarTy to the Boolean type.
+  static CastKind ScalarTypeToBooleanCastKind(QualType ScalarTy);
+
   /// IgnoredValueConversions - Given that an expression's result is
   /// syntactically ignored, perform any conversions that are
   /// required.
index 0846845e26dcca025b453a49e9de810fa0a5850a..0e783018ece3a16b109486f88b46d4845cb3920f 100644 (file)
@@ -235,6 +235,21 @@ void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
   Expr = ImplicitCastExpr::Create(Context, Ty, Kind, Expr, BasePath, VK);
 }
 
+/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
+/// to the conversion from scalar type ScalarTy to the Boolean type.
+CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
+  switch (ScalarTy->getScalarTypeKind()) {
+  case Type::STK_Bool: return CK_NoOp;
+  case Type::STK_Pointer: return CK_PointerToBoolean;
+  case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
+  case Type::STK_Integral: return CK_IntegralToBoolean;
+  case Type::STK_Floating: return CK_FloatingToBoolean;
+  case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
+  case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
+  }
+  return CK_Invalid;
+}
+
 ExprValueKind Sema::CastCategory(Expr *E) {
   Expr::Classification Classification = E->Classify(Context);
   return Classification.isRValue() ? VK_RValue :
index b62b2eeff1da50c6a8937f9b63a876ca8344b2ab..a822c45b90d79c7ba35961a787f09d46ead3d407 100644 (file)
@@ -8633,8 +8633,14 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
     resultType = Input->getType();
     if (resultType->isDependentType())
       break;
-    if (resultType->isScalarType()) { // C99 6.5.3.3p1
-      // ok, fallthrough
+    if (resultType->isScalarType()) {
+      // C99 6.5.3.3p1: ok, fallthrough;
+      if (Context.getLangOptions().CPlusPlus) {
+        // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9:
+        // operand contextually converted to bool.
+        ImpCastExprToType(Input, Context.BoolTy,
+                          ScalarTypeToBooleanCastKind(resultType));
+      }
     } else if (resultType->isPlaceholderType()) {
       ExprResult PR = CheckPlaceholderExpr(Input, OpLoc);
       if (PR.isInvalid()) return ExprError();
index 8c81acc9d1576eb072982d1ee607dc1ff5d1e7ab..3d38b511ecb603e09d3489ee09ca64400c9b8f19 100644 (file)
@@ -2183,21 +2183,11 @@ Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
     ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath);
     break;
   }
-  case ICK_Boolean_Conversion: {
-    CastKind Kind = CK_Invalid;
-    switch (FromType->getScalarTypeKind()) {
-    case Type::STK_Pointer: Kind = CK_PointerToBoolean; break;
-    case Type::STK_MemberPointer: Kind = CK_MemberPointerToBoolean; break;
-    case Type::STK_Bool: llvm_unreachable("bool -> bool conversion?");
-    case Type::STK_Integral: Kind = CK_IntegralToBoolean; break;
-    case Type::STK_Floating: Kind = CK_FloatingToBoolean; break;
-    case Type::STK_IntegralComplex: Kind = CK_IntegralComplexToBoolean; break;
-    case Type::STK_FloatingComplex: Kind = CK_FloatingComplexToBoolean; break;
-    }
 
-    ImpCastExprToType(From, Context.BoolTy, Kind);
+  case ICK_Boolean_Conversion:
+    ImpCastExprToType(From, Context.BoolTy,
+                      ScalarTypeToBooleanCastKind(FromType));
     break;
-  }
 
   case ICK_Derived_To_Base: {
     CXXCastPath BasePath;