[cfe-commits] r108630 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/cast-incomplete.c

Eli Friedman eli.friedman at gmail.com
Sat Jul 17 13:43:49 PDT 2010


Author: efriedma
Date: Sat Jul 17 15:43:49 2010
New Revision: 108630

URL: http://llvm.org/viewvc/llvm-project?rev=108630&view=rev
Log:
Check for casts to an incomplete type in C.  Improves diagnostics for cast to
incomplete union (PR5692) and incomplete enum, and fixes obscure
accepts-invalid on cast to incomplete struct.


Added:
    cfe/trunk/test/Sema/cast-incomplete.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=108630&r1=108629&r2=108630&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sat Jul 17 15:43:49 2010
@@ -2650,6 +2650,8 @@
   "used type %0 where arithmetic or pointer type is required">;
 def ext_typecheck_cond_one_void : Extension<
   "C99 forbids conditional expressions with only one void side">;
+def err_typecheck_cast_to_incomplete : Error<
+  "cast to incomplete type %0">;
 def ext_typecheck_cast_nonscalar : Extension<
   "C99 forbids casting nonscalar type %0 to the same type">;
 def ext_typecheck_cast_to_union : Extension<"C99 forbids casts to union type">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=108630&r1=108629&r2=108630&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Jul 17 15:43:49 2010
@@ -3891,6 +3891,10 @@
     return false;
   }
 
+  if (RequireCompleteType(TyR.getBegin(), castType,
+                          diag::err_typecheck_cast_to_incomplete))
+    return true;
+
   if (!castType->isScalarType() && !castType->isVectorType()) {
     if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) &&
         (castType->isStructureType() || castType->isUnionType())) {

Added: cfe/trunk/test/Sema/cast-incomplete.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/cast-incomplete.c?rev=108630&view=auto
==============================================================================
--- cfe/trunk/test/Sema/cast-incomplete.c (added)
+++ cfe/trunk/test/Sema/cast-incomplete.c Sat Jul 17 15:43:49 2010
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+// PR5692
+
+enum x;            // expected-note   {{forward declaration}}
+extern struct y a; // expected-note   {{forward declaration}}
+extern union z b;  // expected-note 2 {{forward declaration}}
+
+void foo() {
+  (enum x)1;   // expected-error {{cast to incomplete type}}
+  (struct y)a; // expected-error {{cast to incomplete type}}
+  (union z)b;  // expected-error {{cast to incomplete type}}
+  (union z)1;  // expected-error {{cast to incomplete type}}
+}
+





More information about the cfe-commits mailing list