Fix for 10591, make variables in anonymous namespace static

Geoffrey Keating gkeating@apple.com
Thu Dec 4 21:19:00 GMT 2003


I was working through my Apple mailbox (I'm up to June of this year!)
and noticed that I never dealt with PR 10591.  This fixes it.  I'll
wait a little bit so that people can comment on the patch.

Bootstrapped & tested on powerpc-darwin.

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

===File ~/patches/gcc-10591.patch===========================
2003-12-04  Geoffrey Keating  <geoffk@apple.com>

	PR 10591
	* name-lookup.c (push_namespace): Always use the same name
	for the anonymous namespace.
	* decl.c (grokvardecl): Make declarations in the anonymous
	namespace not be visible outside this module.

2003-12-04  Geoffrey Keating  <geoffk@apple.com>

	* g++.dg/pch/anonymous-1.C: New file.
	* g++.dg/pch/anonymous-1.Hs: New file.

Index: cp/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1157
diff -u -p -u -p -r1.1157 decl.c
--- cp/decl.c	22 Nov 2003 06:49:18 -0000	1.1157
+++ cp/decl.c	4 Dec 2003 21:13:54 -0000
@@ -5840,6 +5840,19 @@ grokvardecl (tree type,
 	}
     }
 
+  /* In the anonymous namespace, declarations are treated as 'static'.  */
+  {
+    tree ctx;
+    for (ctx = DECL_CONTEXT (decl); ctx; ctx = DECL_CONTEXT (ctx))
+      if (TREE_CODE (ctx) != NAMESPACE_DECL)
+	break;
+      else if (DECL_NAME (ctx) == anonymous_namespace_name)
+	{
+	  TREE_PUBLIC (decl) = 0;
+	  break;
+	}
+  }
+  
   return decl;
 }
 
Index: cp/name-lookup.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/name-lookup.c,v
retrieving revision 1.22
diff -u -p -u -p -r1.22 name-lookup.c
--- cp/name-lookup.c	15 Nov 2003 19:34:57 -0000	1.22
+++ cp/name-lookup.c	4 Dec 2003 21:13:54 -0000
@@ -2974,8 +2974,8 @@ current_decl_namespace (void)
   return result;
 }
 
-/* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
-   select a name that is unique to this compilation unit.  */
+/* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then
+   the anonymous namespace is used.  */
 
 void
 push_namespace (tree name)
@@ -2994,13 +2994,11 @@ push_namespace (tree name)
 
   if (!name)
     {
-      /* The name of anonymous namespace is unique for the translation
-         unit.  */
       if (!anonymous_namespace_name)
-        anonymous_namespace_name = get_file_function_name ('N');
+        anonymous_namespace_name = get_identifier ("_GLOBAL__N_");
       name = anonymous_namespace_name;
       d = IDENTIFIER_NAMESPACE_VALUE (name);
       if (d)
Index: testsuite/g++.dg/pch/anonymous-1.C
===================================================================
RCS file: testsuite/g++.dg/pch/anonymous-1.C
diff -N testsuite/g++.dg/pch/anonymous-1.C
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/g++.dg/pch/anonymous-1.C	4 Dec 2003 21:13:57 -0000
@@ -0,0 +1,11 @@
+#include "anonymous-1.H"
+
+namespace {
+  int y, z;
+};
+
+int bar(void)
+{
+  return foo() + z;
+}
+
Index: testsuite/g++.dg/pch/anonymous-1.Hs
===================================================================
RCS file: testsuite/g++.dg/pch/anonymous-1.Hs
diff -N testsuite/g++.dg/pch/anonymous-1.Hs
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/g++.dg/pch/anonymous-1.Hs	4 Dec 2003 21:13:57 -0000
@@ -0,0 +1,5 @@
+namespace {
+  int x;
+
+  int foo(void) { return x; }
+}
============================================================



More information about the Gcc-patches mailing list