[cfe-commits] r81151 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaCXXCast.cpp test/SemaCXX/static-cast-complete-type.cpp

Anders Carlsson andersca at mac.com
Mon Sep 7 11:25:50 PDT 2009


Author: andersca
Date: Mon Sep  7 13:25:47 2009
New Revision: 81151

URL: http://llvm.org/viewvc/llvm-project?rev=81151&view=rev
Log:
Check that the destination type of a static_cast expression is a complete type.

Added:
    cfe/trunk/test/SemaCXX/static-cast-complete-type.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaCXXCast.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=81151&r1=81150&r2=81151&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Sep  7 13:25:47 2009
@@ -1515,6 +1515,7 @@
   "cannot cast from type %1 to pointer type %2">;
 def err_bad_static_cast_member_pointer_nonmp : Error<
   "cannot cast from type %1 to member pointer type %2">;
+def err_bad_static_cast_incomplete : Error<"%0 is an incomplete type">;
 
 // These messages don't adhere to the pattern.
 // FIXME: Display the path somehow better.

Modified: cfe/trunk/lib/Sema/SemaCXXCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCXXCast.cpp?rev=81151&r1=81150&r2=81151&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaCXXCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCXXCast.cpp Mon Sep  7 13:25:47 2009
@@ -759,6 +759,14 @@
                       bool CStyle, const SourceRange &OpRange, unsigned &msg,
                       CXXMethodDecl *&ConversionDecl)
 {
+  if (DestType->isRecordType()) {
+    if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
+                                 diag::err_bad_dynamic_cast_incomplete)) {
+      msg = 0;
+      return TC_Failed;
+    }
+  }
+  
   if (DestType->isReferenceType()) {
     // At this point of CheckStaticCast, if the destination is a reference,
     // this has to work. There is no other way that works.

Added: cfe/trunk/test/SemaCXX/static-cast-complete-type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/static-cast-complete-type.cpp?rev=81151&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/static-cast-complete-type.cpp (added)
+++ cfe/trunk/test/SemaCXX/static-cast-complete-type.cpp Mon Sep  7 13:25:47 2009
@@ -0,0 +1,13 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+template<typename T> struct S {
+  S(int);
+};
+
+struct T; // expected-note{{forward declaration of 'struct T'}}
+
+void f() {
+  S<int> s0 = static_cast<S<int> >(0);
+  S<void*> s1 = static_cast<S<void*> >(00);
+
+  (void)static_cast<T>(10); // expected-error{{'struct T' is an incomplete type}}
+}





More information about the cfe-commits mailing list