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]

enforce [basic.link] for static variables


C++ doesn't allow variables with static linkage to have a type that
doesn't have linkage, and it's a mandatory diagnostic.  We have this
for functions already, so I made the compiler treat variables just
like functions for this case.  Variables with C linkage are exempted,
just like functions; this should avoid problems with non-C++-aware
headers.  People whose code is broken can use -fpermissive to turn the
error into a warning (just like every other time the C++ compiler
becomes stricter).

Bootstrapped & tested on powerpc-darwin.  I'll wait a bit in case
people have objections.

-- 
- Geoffrey Keating <geoffk@apple.com>

===File ~/patches/gcc-c++-vartypelinkage.patch==============
Index: cp/ChangeLog
2004-03-03  Geoffrey Keating  <geoffk@apple.com>

	* decl.c (grokfndecl): Update old incorrect comment.
	(grokvardecl): Diagnose C++ variables of type with no linkage.

Index: testsuite/ChangeLog
2004-03-03  Geoffrey Keating  <geoffk@apple.com>

	* g++.old-deja/g++.other/linkage1.C: Expect errors about
	global variables of a type with no linkage.
	* g++.old-deja/g++.other/qual1.C: Name class for 'action'.
	* g++.old-deja/g++.mike/misc13.C: Name enum for 'want'.
	* g++.old-deja/g++.bugs/900210_01.C: Name enum for 'ep'.
	* g++.old-deja/g++.bugs/900210_02.C: Likewise.
	* g++.old-deja/g++.bugs/900210_03.C: Likewise.
	* g++.old-deja/g++.brendan/bit-fields2.C: Name structure for 's'.
	* g++.old-deja/g++.brendan/init10.C: Name structure for 'a'.
	* g++.dg/warn/deprecated.C: Name enum Color.
	* g++.dg/overload/VLA.C: Name structure for 'b'.
	* g++.dg/lookup/anon2.C: Expect diagnostic about type linkage.

Index: cp/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1190
diff -u -p -u -p -r1.1190 decl.c
--- cp/decl.c	1 Mar 2004 06:23:38 -0000	1.1190
+++ cp/decl.c	4 Mar 2004 00:39:09 -0000
@@ -5544,8 +5544,7 @@ grokfndecl (tree ctype, 
     }
 
   /* Members of anonymous types and local classes have no linkage; make
-     them internal.  */
-  /* FIXME what if it gets a name from typedef?  */
+     them internal.  If a typedef is made later, this will be changed.  */
   if (ctype && (TYPE_ANONYMOUS_P (ctype)
 		|| decl_function_context (TYPE_MAIN_DECL (ctype))))
     publicp = 0;
@@ -5880,7 +5879,19 @@ grokvardecl (tree type,
       if (t)
 	{
 	  if (TYPE_ANONYMOUS_P (t))
-	    /* Ignore for now; `enum { foo } e' is pretty common.  */;
+	    {
+	      if (DECL_EXTERN_C_P (decl))
+		/* Allow this; it's pretty common in C.  */;
+	      else
+		{
+		  pedwarn ("non-local variable `%#D' uses anonymous type",
+			   decl);
+		  if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
+		    cp_pedwarn_at ("\
+`%#D' does not refer to the unqualified type, so it is not used for linkage",
+				   TYPE_NAME (t));
+		}
+	    }
 	  else
 	    pedwarn ("non-local variable `%#D' uses local type `%T'",
 			decl, t);
Index: testsuite/g++.dg/lookup/anon2.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/lookup/anon2.C,v
retrieving revision 1.1
diff -u -p -u -p -r1.1 anon2.C
--- testsuite/g++.dg/lookup/anon2.C	29 Nov 2002 22:08:36 -0000	1.1
+++ testsuite/g++.dg/lookup/anon2.C	4 Mar 2004 00:39:11 -0000
@@ -1,6 +1,6 @@
 // { dg-do compile }
 // { dg-options "" }
 
-class { int i; } a; // { dg-error "private" }
+class { int i; } a; // { dg-error "private|anonymous type" }
 void foo() { a.i; } // { dg-error "context" }
 
Index: testsuite/g++.dg/overload/VLA.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/overload/VLA.C,v
retrieving revision 1.1
diff -u -p -u -p -r1.1 VLA.C
--- testsuite/g++.dg/overload/VLA.C	7 Sep 2003 03:43:15 -0000	1.1
+++ testsuite/g++.dg/overload/VLA.C	4 Mar 2004 00:39:11 -0000
@@ -6,7 +6,7 @@
 
 // { dg-do compile }
 
-struct {
+struct S {
   int (*p)[];
 } B;
 
Index: testsuite/g++.dg/warn/deprecated.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/warn/deprecated.C,v
retrieving revision 1.1
diff -u -p -u -p -r1.1 deprecated.C
--- testsuite/g++.dg/warn/deprecated.C	15 Mar 2002 11:07:04 -0000	1.1
+++ testsuite/g++.dg/warn/deprecated.C	4 Mar 2004 00:39:12 -0000
@@ -20,7 +20,7 @@ INT2 f4(void) { return 0; }		/* { dg-war
 int f5(INT2 x);				/* { dg-warning "`INT2' is deprecated" "" } */
 int f6(INT2 x) __attribute__ ((__deprecated__));
 
-typedef enum {red, green, blue} Color __attribute__((deprecated));
+typedef enum Color {red, green, blue} Color __attribute__((deprecated));
 
 int g1;
 int g2 __attribute__ ((deprecated));
Index: testsuite/g++.old-deja/g++.brendan/bit-fields2.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.brendan/bit-fields2.C,v
retrieving revision 1.5
diff -u -p -u -p -r1.5 bit-fields2.C
--- testsuite/g++.old-deja/g++.brendan/bit-fields2.C	1 May 2003 02:02:34 -0000	1.5
+++ testsuite/g++.old-deja/g++.brendan/bit-fields2.C	4 Mar 2004 00:39:12 -0000
@@ -1,6 +1,6 @@
 // { dg-do assemble  }
 // GROUPS passed bit-fields
-	struct {
+	struct S {
 	    char c;
 	    int i:8;
 	} s;
Index: testsuite/g++.old-deja/g++.brendan/init10.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.brendan/init10.C,v
retrieving revision 1.3
diff -u -p -u -p -r1.3 init10.C
--- testsuite/g++.old-deja/g++.brendan/init10.C	1 May 2003 02:02:35 -0000	1.3
+++ testsuite/g++.old-deja/g++.brendan/init10.C	4 Mar 2004 00:39:12 -0000
@@ -1,3 +1,3 @@
 // { dg-do assemble  }
 // GROUPS passed initialization
-struct { int :0; } a;
+struct S { int :0; } a;
Index: testsuite/g++.old-deja/g++.bugs/900210_01.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.bugs/900210_01.C,v
retrieving revision 1.3
diff -u -p -u -p -r1.3 900210_01.C
--- testsuite/g++.old-deja/g++.bugs/900210_01.C	1 May 2003 02:02:37 -0000	1.3
+++ testsuite/g++.old-deja/g++.bugs/900210_01.C	4 Mar 2004 00:39:12 -0000
@@ -15,7 +15,7 @@ char c;
 float f;
 double d;
 long double ld;
-enum {enum_value_0} e;
+enum E {enum_value_0} e;
 
 signed int si;
 signed long sl;
@@ -30,8 +30,8 @@ unsigned char uc;
 void* vp;
 char* cp;
 int* ip;
-enum {enum_value_1} * ep;
-struct { int member; } * sp;
+enum E2 {enum_value_1} * ep;
+struct S { int member; } * sp;
 void (*fp) (void);
 
 void global_function ()
Index: testsuite/g++.old-deja/g++.bugs/900210_02.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.bugs/900210_02.C,v
retrieving revision 1.3
diff -u -p -u -p -r1.3 900210_02.C
--- testsuite/g++.old-deja/g++.bugs/900210_02.C	1 May 2003 02:02:37 -0000	1.3
+++ testsuite/g++.old-deja/g++.bugs/900210_02.C	4 Mar 2004 00:39:12 -0000
@@ -16,7 +16,7 @@ char c;
 float f;
 double d;
 long double ld;
-enum {enum_value_0} e;
+enum E {enum_value_0} e;
 
 signed int si;
 signed long sl;
@@ -31,8 +31,8 @@ unsigned char uc;
 void* vp;
 char* cp;
 int* ip;
-enum {enum_value_1} * ep;
-struct { int member; } * sp;
+enum E2 {enum_value_1} * ep;
+struct S { int member; } * sp;
 void (*fp) (void);
 
 void global_function ()
Index: testsuite/g++.old-deja/g++.bugs/900210_03.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.bugs/900210_03.C,v
retrieving revision 1.3
diff -u -p -u -p -r1.3 900210_03.C
--- testsuite/g++.old-deja/g++.bugs/900210_03.C	1 May 2003 02:02:37 -0000	1.3
+++ testsuite/g++.old-deja/g++.bugs/900210_03.C	4 Mar 2004 00:39:12 -0000
@@ -11,8 +11,8 @@
 void* vp;
 char* cp;
 int* ip;
-enum {enum_value_1} * ep;
-struct { int member; } * sp;
+enum E {enum_value_1} * ep;
+struct S { int member; } * sp;
 void (*fp) (void);
 
 void global_function ()
Index: testsuite/g++.old-deja/g++.mike/misc13.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.mike/misc13.C,v
retrieving revision 1.6
diff -u -p -u -p -r1.6 misc13.C
--- testsuite/g++.old-deja/g++.mike/misc13.C	1 May 2003 02:02:45 -0000	1.6
+++ testsuite/g++.old-deja/g++.mike/misc13.C	4 Mar 2004 00:39:12 -0000
@@ -1,7 +1,7 @@
 // { dg-do run  }
 // GROUPS passed vtable
 extern "C" int printf (const char *, ...);
-enum { vf_request, vf_event } want;
+enum E { vf_request, vf_event } want;
 
 int errs = 0;
 
Index: testsuite/g++.old-deja/g++.other/linkage1.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.other/linkage1.C,v
retrieving revision 1.3
diff -u -p -u -p -r1.3 linkage1.C
--- testsuite/g++.old-deja/g++.other/linkage1.C	1 May 2003 02:02:50 -0000	1.3
+++ testsuite/g++.old-deja/g++.other/linkage1.C	4 Mar 2004 00:39:13 -0000
@@ -3,13 +3,13 @@ typedef struct {
   int i;
 } *p;
 
-void f (p) { }			// { dg-error "" } function uses anonymous type
-p q;
+void f (p) { }			// { dg-error "uses anonymous type" }
+p q;				// { dg-error "uses anonymous type" } 
 
 int main()
 {
-  extern p j;
+  extern p j;			// { dg-error "uses anonymous type" }
   struct A { int j; };
-  extern A a;			// { dg-error "" } extern uses local type
-  extern void f (A);		// { dg-error "" } extern uses local type
+  extern A a;			// { dg-error "uses local type" }
+  extern void f (A);		// { dg-error "uses local type" }
 }
Index: testsuite/g++.old-deja/g++.other/qual1.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.other/qual1.C,v
retrieving revision 1.5
diff -u -p -u -p -r1.5 qual1.C
--- testsuite/g++.old-deja/g++.other/qual1.C	1 May 2003 02:02:50 -0000	1.5
+++ testsuite/g++.old-deja/g++.other/qual1.C	4 Mar 2004 00:39:13 -0000
@@ -6,7 +6,7 @@
 
 typedef const char *(func_type)();
 
-class
+class C
 {
 public:
   func_type *Function;
============================================================


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