r183394 - Disallow reinterpret_cast from pointer to bool on Windows

Hans Wennborg hans at hanshq.net
Thu Jun 6 02:16:37 PDT 2013


Author: hans
Date: Thu Jun  6 04:16:36 2013
New Revision: 183394

URL: http://llvm.org/viewvc/llvm-project?rev=183394&view=rev
Log:
Disallow reinterpret_cast from pointer to bool on Windows

This became allowed by accident in r131201, but triggers an assert.
That patch added an exception to allow conversion from pointers to
narrow integral types for MSVC compatibility. However, a pointer can
already be converted to bool in a civilized manner; allowing conversion
via reinterpret_cast is a bad idea.

Fixes PR16222.

Modified:
    cfe/trunk/lib/Sema/SemaCast.cpp
    cfe/trunk/test/Sema/MicrosoftExtensions.c
    cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp

Modified: cfe/trunk/lib/Sema/SemaCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=183394&r1=183393&r2=183394&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCast.cpp Thu Jun  6 04:16:36 2013
@@ -1813,10 +1813,12 @@ static TryCastResult TryReinterpretCast(
     assert(srcIsPtr && "One type must be a pointer");
     // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
     //   type large enough to hold it; except in Microsoft mode, where the
-    //   integral type size doesn't matter.
+    //   integral type size doesn't matter (except we don't allow bool).
+    bool MicrosoftException = Self.getLangOpts().MicrosoftExt &&
+                              !DestType->isBooleanType();
     if ((Self.Context.getTypeSize(SrcType) >
          Self.Context.getTypeSize(DestType)) &&
-         !Self.getLangOpts().MicrosoftExt) {
+         !MicrosoftException) {
       msg = diag::err_bad_reinterpret_cast_small_int;
       return TC_Failed;
     }

Modified: cfe/trunk/test/Sema/MicrosoftExtensions.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/MicrosoftExtensions.c?rev=183394&r1=183393&r2=183394&view=diff
==============================================================================
--- cfe/trunk/test/Sema/MicrosoftExtensions.c (original)
+++ cfe/trunk/test/Sema/MicrosoftExtensions.c Thu Jun  6 04:16:36 2013
@@ -76,6 +76,9 @@ void pointer_to_integral_type_conv(char*
    short sh = (short)ptr;
    ch = (char)ptr;
    sh = (short)ptr;
+
+   // This is valid ISO C.
+   _Bool b = (_Bool)ptr;
 }
 
 

Modified: cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp?rev=183394&r1=183393&r2=183394&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp (original)
+++ cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp Thu Jun  6 04:16:36 2013
@@ -167,8 +167,14 @@ void pointer_to_integral_type_conv(char*
    short sh = (short)ptr;
    ch = (char)ptr;
    sh = (short)ptr;
-} 
 
+   // These are valid C++.
+   bool b = (bool)ptr;
+   b = static_cast<bool>(ptr);
+
+   // This is bad.
+   b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
+}
 
 namespace friend_as_a_forward_decl {
 





More information about the cfe-commits mailing list