[cfe-commits] r99452 - in /cfe/trunk: lib/Sema/SemaCXXCast.cpp test/SemaCXX/static-cast.cpp

Douglas Gregor dgregor at apple.com
Wed Mar 24 16:38:29 PDT 2010


Author: dgregor
Date: Wed Mar 24 18:38:29 2010
New Revision: 99452

URL: http://llvm.org/viewvc/llvm-project?rev=99452&view=rev
Log:
Switch static_cast from the old reference-initialization code (via
CheckReferenceInit) over to the new initialization code
(InitializationSequence), which is better-tested and doesn't require
us to compute the entire conversion sequence twice.

Modified:
    cfe/trunk/lib/Sema/SemaCXXCast.cpp
    cfe/trunk/test/SemaCXX/static-cast.cpp

Modified: cfe/trunk/lib/Sema/SemaCXXCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCXXCast.cpp?rev=99452&r1=99451&r2=99452&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCXXCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCXXCast.cpp Wed Mar 24 18:38:29 2010
@@ -913,27 +913,24 @@
     // At this point of CheckStaticCast, if the destination is a reference,
     // this has to work. There is no other way that works.
     // On the other hand, if we're checking a C-style cast, we've still got
-    // the reinterpret_cast way. So in C-style mode, we first try the call
-    // with an ICS to suppress errors.
-    if (CStyle) {
-      ImplicitConversionSequence ICS;
-      if(Self.CheckReferenceInit(SrcExpr, DestType, OpRange.getBegin(),
-                                 /*SuppressUserConversions=*/false,
-                                 /*AllowExplicit=*/false, /*ForceRValue=*/false,
-                                 &ICS))
-        return TC_NotApplicable;
+    // the reinterpret_cast way.
+    InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
+    InitializationKind InitKind = InitializationKind::CreateCast(OpRange, 
+                                                                 CStyle);    
+    InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
+    if (InitSeq.getKind() == InitializationSequence::FailedSequence && CStyle)
+      return TC_NotApplicable;
+    
+    Sema::OwningExprResult Result
+      = InitSeq.Perform(Self, Entity, InitKind,
+                        Action::MultiExprArg(Self, (void**)&SrcExpr, 1));
+    if (Result.isInvalid()) {
+      msg = 0;
+      return TC_Failed;
     }
-    // Now we're committed either way.
-    if(!Self.CheckReferenceInit(SrcExpr, DestType, OpRange.getBegin(),
-                                /*SuppressUserConversions=*/false,
-                                /*AllowExplicit=*/false,
-                                /*ForceRValue=*/false, 0,
-                                /*IgnoreBaseAccess=*/CStyle))
-      return TC_Success;
-
-    // We already got an error message.
-    msg = 0;
-    return TC_Failed;
+    
+    SrcExpr = Result.takeAs<Expr>();
+    return TC_Success;
   }
 
   if (DestType->isRecordType()) {

Modified: cfe/trunk/test/SemaCXX/static-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/static-cast.cpp?rev=99452&r1=99451&r2=99452&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/static-cast.cpp (original)
+++ cfe/trunk/test/SemaCXX/static-cast.cpp Wed Mar 24 18:38:29 2010
@@ -60,7 +60,7 @@
   (void)static_cast<A*>((H*)0); // expected-error {{ambiguous conversion}}
   (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}}
   (void)static_cast<A**>((B**)0); // expected-error {{static_cast from 'B **' to 'A **' is not allowed}}
-  (void)static_cast<char&>(i); // expected-error {{non-const lvalue reference to type 'char' cannot be initialized with a value of type 'int'}}
+  (void)static_cast<char&>(i); // expected-error {{non-const lvalue reference to type 'char' cannot bind to a value of unrelated type 'int'}}
 }
 
 // Anything to void
@@ -91,7 +91,7 @@
   (void)static_cast<H*>((A*)0); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n    struct A -> struct B -> struct G1 -> struct H\n    struct A -> struct B -> struct G2 -> struct H}}
   (void)static_cast<H&>(*((A*)0)); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n    struct A -> struct B -> struct G1 -> struct H\n    struct A -> struct B -> struct G2 -> struct H}}
   (void)static_cast<E*>((B*)0); // expected-error {{static_cast from 'B *' to 'E *' is not allowed}}
-  (void)static_cast<E&>(*((B*)0)); // expected-error {{non-const lvalue reference to type 'E' cannot be initialized with a value of type 'B'}}
+  (void)static_cast<E&>(*((B*)0)); // expected-error {{non-const lvalue reference to type 'E' cannot bind to a value of unrelated type 'B'}}
 
   // TODO: Test inaccessible base in context where it's accessible, i.e.
   // member function and friend.





More information about the cfe-commits mailing list