This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C PATCH] c_incomplete_type_error TLC


This patch cleans up c_incomplete_type_error a bit: today, we should prefer
using %qT over %<%s %E%> (the code is largely intact since 1992), and second,
we should print the type if we can.  The "invalid use of incomplete typedef"
error wasn't tested at all, so I'm also adding a test to exercise that code
path.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-04-29  Marek Polacek  <polacek@redhat.com>

	* c-typeck.c (c_incomplete_type_error): Refactor to use %qT.  Print
	the type of a decl.

	* gcc.dg/incomplete-typedef-1.c: New test.

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index c58e918..cc4acf7 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -229,15 +229,13 @@ require_complete_type (tree value)
 void
 c_incomplete_type_error (const_tree value, const_tree type)
 {
-  const char *type_code_string;
-
   /* Avoid duplicate error message.  */
   if (TREE_CODE (type) == ERROR_MARK)
     return;
 
   if (value != 0 && (TREE_CODE (value) == VAR_DECL
 		     || TREE_CODE (value) == PARM_DECL))
-    error ("%qD has an incomplete type", value);
+    error ("%qD has an incomplete type %qT", value, type);
   else
     {
     retry:
@@ -246,15 +244,8 @@ c_incomplete_type_error (const_tree value, const_tree type)
       switch (TREE_CODE (type))
 	{
 	case RECORD_TYPE:
-	  type_code_string = "struct";
-	  break;
-
 	case UNION_TYPE:
-	  type_code_string = "union";
-	  break;
-
 	case ENUMERAL_TYPE:
-	  type_code_string = "enum";
 	  break;
 
 	case VOID_TYPE:
@@ -280,11 +271,10 @@ c_incomplete_type_error (const_tree value, const_tree type)
 	}
 
       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
-	error ("invalid use of undefined type %<%s %E%>",
-	       type_code_string, TYPE_NAME (type));
+	error ("invalid use of undefined type %qT", type);
       else
 	/* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
-	error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
+	error ("invalid use of incomplete typedef %qT", type);
     }
 }
 
diff --git gcc/testsuite/gcc.dg/incomplete-typedef-1.c gcc/testsuite/gcc.dg/incomplete-typedef-1.c
index e69de29..622bf65 100644
--- gcc/testsuite/gcc.dg/incomplete-typedef-1.c
+++ gcc/testsuite/gcc.dg/incomplete-typedef-1.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+typedef struct S TS;
+typedef union U TU;
+
+void
+foo (void)
+{
+  (TS) { }; /* { dg-error "invalid use of incomplete typedef" } */
+  (TU) { }; /* { dg-error "invalid use of incomplete typedef" } */
+}

	Marek


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]