Patch for bug with #pragma weak and declaration in inner scope

Joseph S. Myers joseph@codesourcery.com
Wed Apr 6 13:42:00 GMT 2005


These patches fix a #pragma weak bug: #pragma weak before a
declaration should still apply even if the declaration is not at file
scope.  This is a regression relative to 3.0 and 2.95.

For C++, #pragma weak before a declaration presently needs to use the
mangled name; the test accordingly uses one although it would probably
make sense to follow something closer to the Sun specification
<http://docs.sun.com/source/817-0926/Pragmas_App.html>: either
identifiers or strings can be used in C++, identifiers for a function
which has already been declared, strings with mangled names.

I chose the conservative approach of only removing the file scope
checks for declarations which are not function definitions, not the
separate checks for function definitions, although it might be safe to
remove it for function definitions as well.

The first patch is relative to csl-sol210-3_4-branch; committed there
after testing on i386-pc-solaris2.10.1.  The second patch is for
mainline and 4.0 branch; bootstrapped with no regressions, both
mainline and 4.0 branch, on i386-pc-solaris2.10.1; OK to commit?

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    joseph@codesourcery.com (CodeSourcery mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

diff -u -p -r1.1.2.13 ChangeLog.csl
--- GCC/ChangeLog.csl	19 Mar 2005 00:46:15 -0000	1.1.2.13
+++ GCC/ChangeLog.csl	5 Apr 2005 18:31:00 -0000
@@ -1,3 +1,12 @@
+2005-04-06  Joseph Myers  <joseph@codesourcery.com>
+
+	* gcc/c-decl.c (finish_decl): Apply pending #pragma weak
+	regardless of scope.
+	* gcc/cp/decl.c (start_decl): Apply pending #pragma weak
+	regardless of scope.
+	* gcc/testsuite/gcc.dg/weak/weak-12.c,
+	gcc/testsuite/g++.dg/ext/weak2.C: New tests.
+
 2005-03-18  Joseph Myers  <joseph@codesourcery.com>
 
 	* gcc/config.gcc: Consistently use solaris2.1[0-9]* instead of
diff -u -p -r1.470.4.16.2.1 c-decl.c
--- GCC/gcc/c-decl.c	23 Sep 2004 07:27:04 -0000	1.470.4.16.2.1
+++ GCC/gcc/c-decl.c	5 Apr 2005 18:25:16 -0000
@@ -2840,8 +2840,7 @@ finish_decl (tree decl, tree init, tree 
     }
 
   /* If #pragma weak was used, mark the decl weak now.  */
-  if (current_scope == global_scope)
-    maybe_apply_pragma_weak (decl);
+  maybe_apply_pragma_weak (decl);
 
   /* Output the assembler code and/or RTL code for variables and functions,
      unless the type is an undefined structure or union.
diff -u -p -r1.1174.2.24.2.1 decl.c
--- GCC/gcc/cp/decl.c	9 Nov 2004 01:50:28 -0000	1.1174.2.24.2.1
+++ GCC/gcc/cp/decl.c	5 Apr 2005 18:27:22 -0000
@@ -3765,8 +3765,7 @@ start_decl (tree declarator, 
   cplus_decl_attributes (&decl, attributes, 0);
 
   /* If #pragma weak was used, mark the decl weak now.  */
-  if (global_scope_p (current_binding_level))
-    maybe_apply_pragma_weak (decl);
+  maybe_apply_pragma_weak (decl);
 
   if (TREE_CODE (decl) == FUNCTION_DECL
       && DECL_DECLARED_INLINE_P (decl)
diff -rupN GCC.orig/gcc/testsuite/g++.dg/ext/weak2.C GCC/gcc/testsuite/g++.dg/ext/weak2.C
--- GCC.orig/gcc/testsuite/g++.dg/ext/weak2.C	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/g++.dg/ext/weak2.C	2005-04-05 18:11:17.000000000 +0000
@@ -0,0 +1,17 @@
+// Test for #pragma weak with declaration not at file scope. 
+// { dg-do compile }
+// { dg-require-weak "" }
+// { dg-options "" }
+
+// { dg-final { scan-assembler "weak\[^ \t\]*\[ \t\]_?_Z3foov" } }
+
+#pragma weak _Z3foov
+
+int
+main (void)
+{
+  extern int foo (void);
+  if (&foo)
+    return foo ();
+  return 0;
+}
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/weak/weak-12.c GCC/gcc/testsuite/gcc.dg/weak/weak-12.c
--- GCC.orig/gcc/testsuite/gcc.dg/weak/weak-12.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/weak/weak-12.c	2005-04-05 18:09:59.000000000 +0000
@@ -0,0 +1,17 @@
+/* Test for #pragma weak with declaration not at file scope.  */
+/* { dg-do compile } */
+/* { dg-require-weak "" } */
+/* { dg-options "" } */
+
+/* { dg-final { scan-assembler "weak\[^ \t\]*\[ \t\]_?foo" } } */
+
+#pragma weak foo
+
+int
+main (void)
+{
+  extern int foo (void);
+  if (&foo)
+    return foo ();
+  return 0;
+}

2005-04-06  Joseph S. Myers  <joseph@codesourcery.com>

	* c-decl.c (finish_decl): Apply pending #pragma weak regardless of
	scope.

cp:
2005-04-06  Joseph S. Myers  <joseph@codesourcery.com>

	* decl.c (start_decl): Apply pending #pragma weak regardless of
	scope.

testsuite:
2005-04-06  Joseph S. Myers  <joseph@codesourcery.com>

	* gcc.dg/weak/weak-12.c, g++.dg/ext/weak2.C: New tests.

diff -rupN GCC.orig/gcc/c-decl.c GCC/gcc/c-decl.c
--- GCC.orig/gcc/c-decl.c	2005-04-01 16:25:45.000000000 +0000
+++ GCC/gcc/c-decl.c	2005-04-05 18:34:49.000000000 +0000
@@ -3272,8 +3272,7 @@ finish_decl (tree decl, tree init, tree 
     }
 
   /* If #pragma weak was used, mark the decl weak now.  */
-  if (current_scope == file_scope)
-    maybe_apply_pragma_weak (decl);
+  maybe_apply_pragma_weak (decl);
 
   /* If this is a variable definition, determine its ELF visibility.  */
   if (TREE_CODE (decl) == VAR_DECL 
diff -rupN GCC.orig/gcc/cp/decl.c GCC/gcc/cp/decl.c
--- GCC.orig/gcc/cp/decl.c	2005-03-30 13:55:11.000000000 +0000
+++ GCC/gcc/cp/decl.c	2005-04-05 18:34:59.000000000 +0000
@@ -3654,8 +3654,7 @@ start_decl (const cp_declarator *declara
   cplus_decl_attributes (&decl, attributes, 0);
 
   /* If #pragma weak was used, mark the decl weak now.  */
-  if (global_scope_p (current_binding_level))
-    maybe_apply_pragma_weak (decl);
+  maybe_apply_pragma_weak (decl);
 
   if (TREE_CODE (decl) == FUNCTION_DECL
       && DECL_DECLARED_INLINE_P (decl)
diff -rupN GCC.orig/gcc/testsuite/g++.dg/ext/weak2.C GCC/gcc/testsuite/g++.dg/ext/weak2.C
--- GCC.orig/gcc/testsuite/g++.dg/ext/weak2.C	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/g++.dg/ext/weak2.C	2005-04-05 18:11:17.000000000 +0000
@@ -0,0 +1,17 @@
+// Test for #pragma weak with declaration not at file scope. 
+// { dg-do compile }
+// { dg-require-weak "" }
+// { dg-options "" }
+
+// { dg-final { scan-assembler "weak\[^ \t\]*\[ \t\]_?_Z3foov" } }
+
+#pragma weak _Z3foov
+
+int
+main (void)
+{
+  extern int foo (void);
+  if (&foo)
+    return foo ();
+  return 0;
+}
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/weak/weak-12.c GCC/gcc/testsuite/gcc.dg/weak/weak-12.c
--- GCC.orig/gcc/testsuite/gcc.dg/weak/weak-12.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/weak/weak-12.c	2005-04-05 18:09:59.000000000 +0000
@@ -0,0 +1,17 @@
+/* Test for #pragma weak with declaration not at file scope.  */
+/* { dg-do compile } */
+/* { dg-require-weak "" } */
+/* { dg-options "" } */
+
+/* { dg-final { scan-assembler "weak\[^ \t\]*\[ \t\]_?foo" } } */
+
+#pragma weak foo
+
+int
+main (void)
+{
+  extern int foo (void);
+  if (&foo)
+    return foo ();
+  return 0;
+}



More information about the Gcc-patches mailing list