[cfe-commits] r124319 - in /cfe/trunk: lib/Sema/SemaCXXCast.cpp test/CXX/expr/expr.cast/p4-0x.cpp

Douglas Gregor dgregor at apple.com
Wed Jan 26 13:04:07 PST 2011


Author: dgregor
Date: Wed Jan 26 15:04:06 2011
New Revision: 124319

URL: http://llvm.org/viewvc/llvm-project?rev=124319&view=rev
Log:
Handle C-style casts to rvalue reference types that cast away constness.

Added:
    cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp   (with props)
Modified:
    cfe/trunk/lib/Sema/SemaCXXCast.cpp

Modified: cfe/trunk/lib/Sema/SemaCXXCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCXXCast.cpp?rev=124319&r1=124318&r2=124319&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCXXCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCXXCast.cpp Wed Jan 26 15:04:06 2011
@@ -78,7 +78,8 @@
 // %1: Source Type
 // %2: Destination Type
 static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr,
-                                           QualType DestType, CastKind &Kind,
+                                           QualType DestType, bool CStyle,
+                                           CastKind &Kind,
                                            CXXCastPath &BasePath,
                                            unsigned &msg);
 static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
@@ -583,7 +584,8 @@
   // C++0x [expr.static.cast]p3: 
   //   A glvalue of type "cv1 T1" can be cast to type "rvalue reference to cv2
   //   T2" if "cv2 T2" is reference-compatible with "cv1 T1".
-  tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, Kind, BasePath, msg);
+  tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, CStyle, Kind, BasePath, 
+                              msg);
   if (tcr != TC_NotApplicable)
     return tcr;
 
@@ -695,7 +697,8 @@
 /// Tests whether a conversion according to N2844 is valid.
 TryCastResult
 TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
-                      CastKind &Kind, CXXCastPath &BasePath, unsigned &msg) {
+                      bool CStyle, CastKind &Kind, CXXCastPath &BasePath, 
+                      unsigned &msg) {
   // C++0x [expr.static.cast]p3:
   //   A glvalue of type "cv1 T1" can be cast to type "rvalue reference to 
   //   cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
@@ -711,8 +714,15 @@
   // FIXME: Should allow casting away constness if CStyle.
   bool DerivedToBase;
   bool ObjCConversion;
+  QualType FromType = SrcExpr->getType();
+  QualType ToType = R->getPointeeType();
+  if (CStyle) {
+    FromType = FromType.getUnqualifiedType();
+    ToType = ToType.getUnqualifiedType();
+  }
+  
   if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
-                                        R->getPointeeType(), SrcExpr->getType(),
+                                        ToType, FromType,
                                         DerivedToBase, ObjCConversion) <
         Sema::Ref_Compatible_With_Added_Qualification) {
     msg = diag::err_bad_lvalue_to_rvalue_cast;

Added: cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp?rev=124319&view=auto
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp (added)
+++ cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp Wed Jan 26 15:04:06 2011
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
+
+struct X { };
+struct Y : X { };
+
+void test_lvalue_to_rvalue_drop_cvquals(const X &x, const Y &y, const int &i) {
+  (void)(X&&)x;
+  (void)(int&&)i;
+  (void)(X&&)y;
+  (void)(Y&&)x;
+}

Propchange: cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CXX/expr/expr.cast/p4-0x.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list