[cfe-commits] r149664 - in /cfe/trunk: lib/Sema/SemaCast.cpp test/SemaCXX/reinterpret-cast.cpp

Chad Rosier mcrosier at apple.com
Thu Feb 2 18:54:37 PST 2012


Author: mcrosier
Date: Thu Feb  2 20:54:37 2012
New Revision: 149664

URL: http://llvm.org/viewvc/llvm-project?rev=149664&view=rev
Log:
C++ 5.2.10p2 has a note that mentions that, subject to all other restrictions,
a cast to the same type is allowed so long as it does not cast away constness.

Fix for PR11747. Patch by Aaron Ballman. Reviewed by Eli.

Modified:
    cfe/trunk/lib/Sema/SemaCast.cpp
    cfe/trunk/test/SemaCXX/reinterpret-cast.cpp

Modified: cfe/trunk/lib/Sema/SemaCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=149664&r1=149663&r2=149664&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCast.cpp Thu Feb  2 20:54:37 2012
@@ -1616,7 +1616,27 @@
     
     return TC_Failed;
   }
-  
+
+  if (SrcType == DestType) {
+    // C++ 5.2.10p2 has a note that mentions that, subject to all other
+    // restrictions, a cast to the same type is allowed so long as it does not
+    // cast away constness. In C++98, the intent was not entirely clear here, 
+    // since all other paragraphs explicitly forbid casts to the same type.
+    // C++11 clarifies this case with p2.
+    //
+    // The only allowed types are: integral, enumeration, pointer, or 
+    // pointer-to-member types.  We also won't restrict Obj-C pointers either.
+    Kind = CK_NoOp;
+    TryCastResult Result = TC_NotApplicable;
+    if (SrcType->isIntegralOrEnumerationType() ||
+        SrcType->isAnyPointerType() ||
+        SrcType->isMemberPointerType() ||
+        SrcType->isBlockPointerType()) {
+      Result = TC_Success;
+    }
+    return Result;
+  }
+
   bool destIsPtr = DestType->isAnyPointerType() ||
                    DestType->isBlockPointerType();
   bool srcIsPtr = SrcType->isAnyPointerType() ||
@@ -1627,17 +1647,6 @@
     return TC_NotApplicable;
   }
 
-  if (SrcType == DestType) {
-    // C++ 5.2.10p2 has a note that mentions that, subject to all other
-    // restrictions, a cast to the same type is allowed. The intent is not
-    // entirely clear here, since all other paragraphs explicitly forbid casts
-    // to the same type. However, the behavior of compilers is pretty consistent
-    // on this point: allow same-type conversion if the involved types are
-    // pointers, disallow otherwise.
-    Kind = CK_NoOp;
-    return TC_Success;
-  }
-
   if (DestType->isIntegralType(Self.Context)) {
     assert(srcIsPtr && "One type must be a pointer");
     // C++ 5.2.10p4: A pointer can be explicitly converted to any integral

Modified: cfe/trunk/test/SemaCXX/reinterpret-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/reinterpret-cast.cpp?rev=149664&r1=149663&r2=149664&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/reinterpret-cast.cpp (original)
+++ cfe/trunk/test/SemaCXX/reinterpret-cast.cpp Thu Feb  2 20:54:37 2012
@@ -9,13 +9,27 @@
 // Test the conversion to self.
 void self_conversion()
 {
-  // T*->T* is allowed, T->T in general not.
+  // T->T is allowed per [expr.reinterpret.cast]p2 so long as it doesn't
+  // cast away constness, and is integral, enumeration, pointer or 
+  // pointer-to-member.
   int i = 0;
-  (void)reinterpret_cast<int>(i); // expected-error {{reinterpret_cast from 'int' to 'int' is not allowed}}
-  structure s;
-  (void)reinterpret_cast<structure>(s); // expected-error {{reinterpret_cast from 'structure' to 'structure' is not allowed}}
+  (void)reinterpret_cast<int>(i);
+
+  test e = testval;
+  (void)reinterpret_cast<test>(e);
+
+  // T*->T* is allowed
   int *pi = 0;
   (void)reinterpret_cast<int*>(pi);
+
+  const int structure::*psi = 0;
+  (void)reinterpret_cast<const int structure::*>(psi);
+
+  structure s;
+  (void)reinterpret_cast<structure>(s); // expected-error {{reinterpret_cast from 'structure' to 'structure' is not allowed}}
+
+  float f = 0.0f;
+  (void)reinterpret_cast<float>(f); // expected-error {{reinterpret_cast from 'float' to 'float' is not allowed}}
 }
 
 // Test conversion between pointer and integral types, as in /3 and /4.





More information about the cfe-commits mailing list