[cfe-commits] r129066 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/Sema.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaExprCXX.cpp

Abramo Bagnara abramo.bagnara at gmail.com
Thu Apr 7 02:26:19 PDT 2011


Author: abramo
Date: Thu Apr  7 04:26:19 2011
New Revision: 129066

URL: http://llvm.org/viewvc/llvm-project?rev=129066&view=rev
Log:
In C++ the argument of logical not should always be bool. Added missing implicit cast for scalars.

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=129066&r1=129065&r2=129066&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Thu Apr  7 04:26:19 2011
@@ -4749,6 +4749,10 @@
                          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.

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=129066&r1=129065&r2=129066&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Thu Apr  7 04:26:19 2011
@@ -235,6 +235,21 @@
   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 :

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=129066&r1=129065&r2=129066&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Apr  7 04:26:19 2011
@@ -8633,8 +8633,14 @@
     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();

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=129066&r1=129065&r2=129066&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Thu Apr  7 04:26:19 2011
@@ -2183,21 +2183,11 @@
     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;





More information about the cfe-commits mailing list