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] Diagnose static data members in anon ns which are used, but weren't defined (PR c++/34094)


Hi!

Before the visibility changes for anon namespaces if a static data member
wasn't defined (just declared), but used, there would be a link time
failure.  But as constrain_visibility (decl, VISIBILITY_ANON) makes
now the static data members !TREE_PUBLIC and DECL_NOR_REALLY_EXTERN,
even when they are not defined g++ emits them in .bss.

The patch below adds a diagnostic for this.

I'm not sure whether the && DECL_INITIAL (decl) == NULL_TREE should
be there, given that [class.static.data]/4 says that the definition should
be present even for const qualified static data members with initializers
in the class, but removing that line breaks a bunch of testcases in the
testsuite.

Regtested on x86_64-linux, ok for trunk?

2007-11-20  Jakub Jelinek  <jakub@redhat.com>

	PR c++/34094
	* decl2.c (cp_write_global_declarations): Issue error about static
	data members in anonymous namespace which are declared and used,
	but not defined.

	* g++.dg/ext/visibility/anon7.C: New test.

--- gcc/cp/decl2.c.jj	2007-11-06 09:15:15.000000000 +0100
+++ gcc/cp/decl2.c	2007-11-20 19:10:36.000000000 +0100
@@ -3365,7 +3365,16 @@ cp_write_global_declarations (void)
 	  /* If this static data member is needed, provide it to the
 	     back end.  */
 	  if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
-	    DECL_EXTERNAL (decl) = 0;
+	    {
+	      if (TREE_CODE (decl) == VAR_DECL
+		  && DECL_NONLOCAL (decl)
+		  && DECL_CLASS_SCOPE_P (decl)
+		  && DECL_INITIAL (decl) == NULL_TREE
+		  && type_visibility (DECL_CONTEXT (decl)) == VISIBILITY_ANON)
+		error ("%Jstatic data member %qD used, but not defined",
+		       decl, decl);
+	      DECL_EXTERNAL (decl) = 0;
+	    }
 	}
       if (VEC_length (tree, pending_statics) != 0
 	  && wrapup_global_declarations (VEC_address (tree, pending_statics),
--- gcc/testsuite/g++.dg/ext/visibility/anon7.C.jj	2007-11-20 19:03:19.000000000 +0100
+++ gcc/testsuite/g++.dg/ext/visibility/anon7.C	2007-11-20 19:12:40.000000000 +0100
@@ -0,0 +1,23 @@
+// PR c++/34094
+// { dg-do compile }
+
+namespace
+{
+  struct A {
+    static int bar ();
+    static int i;		// { dg-error "used, but not defined" }
+    static int j;
+    static int k;
+    static int l;
+    static const int m = 16;
+    static const int n = 17;
+  };
+  int A::j = 4;
+  int A::k;
+  const int A::m;
+}
+
+int foo (void)
+{
+  return A::i + A::j + A::k + A::m + A::n + A::bar ();
+}

	Jakub


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