diff mbox

C++ PATCH to use the same anonymous namespace name on all targets

Message ID 514A7B8D.4000601@redhat.com
State New
Headers show

Commit Message

Jason Merrill March 21, 2013, 3:16 a.m. UTC
Previous attempts to build the compiler as C++ had problems with stage 
comparison due to anonymous namespaces having random names.  This was 
necessary in the past because of string comparison of type_info names on 
targets that didn't support unification of the nodes for address 
comparison, but when we switched to doing string comparison on GNU/Linux 
as well, we changed it so that we do address comparison for types in the 
anonymous namespace, so we don't need a randomized name anymore.

Tested x86_64-pc-linux-gnu, applying to trunk.

Comments

Gabriel Dos Reis March 21, 2013, 5:41 a.m. UTC | #1
Jason Merrill <jason@redhat.com> writes:

| Previous attempts to build the compiler as C++ had problems with stage
| comparison due to anonymous namespaces having random names.  This was
| necessary in the past because of string comparison of type_info names
| on targets that didn't support unification of the nodes for address
| comparison, but when we switched to doing string comparison on
| GNU/Linux as well, we changed it so that we do address comparison for
| types in the anonymous namespace, so we don't need a randomized name
| anymore.

Excellent!  
Many thanks,

-- Gaby
diff mbox

Patch

commit 95fed08c94d20b03994248b5ffa3e5d41a4aff7d
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Mar 19 10:11:18 2013 -0400

    	* name-lookup.c (get_anonymous_namespace_name): Never use
    	get_file_function_name.

diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index 2a47331..7084a53 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -69,14 +69,12 @@  get_anonymous_namespace_name (void)
 {
   if (!anonymous_namespace_name)
     {
-      /* The anonymous namespace has to have a unique name
-	 if typeinfo objects are being compared by name.  */
-      if (! flag_weak || ! SUPPORTS_ONE_ONLY)
-       anonymous_namespace_name = get_file_function_name ("N");
-      else
-       /* The demangler expects anonymous namespaces to be called
-          something starting with '_GLOBAL__N_'.  */
-       anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
+      /* We used to use get_file_function_name here, but that isn't
+	 necessary now that anonymous namespace typeinfos
+	 are !TREE_PUBLIC, and thus compared by address.  */
+      /* The demangler expects anonymous namespaces to be called
+	 something starting with '_GLOBAL__N_'.  */
+      anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
     }
   return anonymous_namespace_name;
 }
diff --git a/gcc/testsuite/g++.dg/eh/anon1.C b/gcc/testsuite/g++.dg/eh/anon1.C
new file mode 100644
index 0000000..2a5ef4b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/anon1.C
@@ -0,0 +1,26 @@ 
+// Test that the anonymous namespace isn't mangled with random characters,
+// but also doesn't get mixed up with an anonymous namespace in another
+// translation unit.
+
+// { dg-do run }
+// { dg-additional-sources "anon1a.cc" }
+
+namespace {
+  struct A
+  {
+    virtual void f();
+  };
+
+  void A::f() { }
+}
+
+extern void g();
+
+int main()
+{
+  try {
+    try {
+      g();
+    } catch (A) { __builtin_abort(); }
+  } catch (...) { }
+}
diff --git a/gcc/testsuite/g++.dg/eh/anon1a.cc b/gcc/testsuite/g++.dg/eh/anon1a.cc
new file mode 100644
index 0000000..ba161ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/anon1a.cc
@@ -0,0 +1,10 @@ 
+namespace {
+  struct A
+  {
+    virtual void f();
+  };
+
+  void A::f() { }
+}
+
+void g() { throw A(); }
diff --git a/gcc/testsuite/g++.dg/eh/anon2.C b/gcc/testsuite/g++.dg/eh/anon2.C
new file mode 100644
index 0000000..9ff9ee3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/anon2.C
@@ -0,0 +1,24 @@ 
+// Test that the anonymous namespace isn't mangled with random characters,
+// but also doesn't get mixed up with an anonymous namespace in another
+// translation unit.
+// { dg-final { scan-assembler "\\*N12_GLOBAL__N_11AE" } }
+
+namespace {
+  struct A
+  {
+    virtual void f();
+  };
+
+  void A::f() { }
+}
+
+extern void g();
+
+int main()
+{
+  try {
+    try {
+      g();
+    } catch (A) { __builtin_abort(); }
+  } catch (...) { }
+}