[PATCH] Fix PR c/6343 (was: Re: GCC 3.1 Prerelease)

Franz Sirl Franz.Sirl-kernel@lauterbach.com
Thu Apr 25 12:57:00 GMT 2002


On Wednesday 24 April 2002 22:01, Jason Merrill wrote:
> >>>>> "Franz" == Franz Sirl <Franz.Sirl-kernel@lauterbach.com> writes:
> >
> > Hmm, it seems to make more sense for the warning check too, with
> > TREE_USED changed to TREE_SYMBOL_REFERENCED the c++ regression
> > g++.old-deja/g++.jason/template39.C went away along with a bunch of
> > regressions in the libstdc++ testsuite, except one:
> >
> > FAIL: 26_numerics/complex_inserters_extractors.cc (test for excess
> > errors) Excess errors:
> > /home/fsirl/TC/gcc/BUILD/obj-gcc31-ppc/ppc-linux/libstdc++-v3/include/bit
> >s/basic_string.h: In instantiation of `const size_t
> > std::basic_string<char, gnu_char_traits, std::allocator<char> >::npos':
> > /home/fsirl/TC/gcc/BUILD/gcc-3.1/libstdc++-v3/testsuite/26_numerics/compl
> >ex_inserters_extractors.cc:109: instantiated from here
> > /home/fsirl/TC/gcc/BUILD/obj-gcc31-ppc/ppc-linux/libstdc++-v3/include/bit
> >s/basic_string.h:217: warning: weak declaration of `const size_t
> > std::basic_string<char, gnu_char_traits, std::allocator<char> >::npos'
> > after first use may result in unspecified behaviour
> >
> > Can a c++ expert tell me if this warning makes sense in this testcase or
> > does the warning check need still more refinement?
>
> The latter.  We don't want to warn about the C++ frontend's internal
> trickery with DECL_WEAK.

What kind of trickery? Can I detect that in declare_weak?

> I'd prefer to omit the warning entirely on the branch.

Well, I prefer overzealous warnings over no warning at all in the same way I 
prefer ICEs over miscompiled code :-). Besides that extra warning, I'm really 
satisfied now with my patch otherwise and unless you think it's overly 
complicated to get rid of it, I would like to fix it even for the branch.

Attached the patch I successfully bootstrapped and regtested (minus the 
additional libstdc++ fail) on powerpc-linux-gnu and x86-linux-gnu.

Franz.

	PR c/6343
	* c-decl.c (duplicate_decls): Use declare_weak to merge weak status.
	* varasm.c (declare_weak): Make sure we don't give an error on VAR_DECLs.
	Warn about potential miscompilations.
	Mark RTL with SYMBOL_REF_WEAK.

cp:
	* decl.c (duplicate_decls): Use declare_weak to merge weak status.

-------------- next part --------------
Index: gcc/c-decl.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.300.2.5
diff -u -p -r1.300.2.5 c-decl.c
--- gcc/c-decl.c	28 Mar 2002 18:49:57 -0000	1.300.2.5
+++ gcc/c-decl.c	23 Apr 2002 21:41:28 -0000
@@ -1955,7 +1955,11 @@ duplicate_decls (newdecl, olddecl, diffe
     }
 
   /* Merge the storage class information.  */
-  DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
+  if (DECL_WEAK (newdecl) && !DECL_WEAK (olddecl))
+    declare_weak (olddecl);
+  if (!DECL_WEAK (newdecl) && DECL_WEAK (olddecl))
+    declare_weak (newdecl);
+
   /* For functions, static overrides non-static.  */
   if (TREE_CODE (newdecl) == FUNCTION_DECL)
     {
Index: gcc/varasm.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/varasm.c,v
retrieving revision 1.250.2.7
diff -u -p -r1.250.2.7 varasm.c
--- gcc/varasm.c	25 Mar 2002 00:54:26 -0000	1.250.2.7
+++ gcc/varasm.c	23 Apr 2002 21:41:28 -0000
@@ -4998,17 +4998,25 @@ declare_weak (decl)
 {
   if (! TREE_PUBLIC (decl))
     error_with_decl (decl, "weak declaration of `%s' must be public");
-  else if (TREE_ASM_WRITTEN (decl))
+  else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
     error_with_decl (decl, "weak declaration of `%s' must precede definition");
   else if (SUPPORTS_WEAK)
     {
       if (! DECL_WEAK (decl))
 	weak_decls = tree_cons (NULL, decl, weak_decls);
+      if (TREE_CODE (decl) != FUNCTION_DECL && TREE_USED (decl))
+	warning_with_decl (decl, "weak declaration of `%s' after first use may result in unspecified behaviour");
     }
   else
     warning_with_decl (decl, "weak declaration of `%s' not supported");
 
   DECL_WEAK (decl) = 1;
+
+  if (DECL_RTL_SET_P (decl)
+      && GET_CODE (DECL_RTL (decl)) == MEM
+      && XEXP (DECL_RTL (decl), 0)
+      && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
+    SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
 }
 
 /* Emit any pending weak declarations.  */
Index: gcc/cp/decl.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.866.2.23
diff -u -p -r1.866.2.23 decl.c
--- gcc/cp/decl.c	16 Apr 2002 03:15:54 -0000	1.866.2.23
+++ gcc/cp/decl.c	23 Apr 2002 21:41:28 -0000
@@ -3645,7 +3645,11 @@ duplicate_decls (newdecl, olddecl)
     }
 
   /* Merge the storage class information.  */
-  DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
+  if (DECL_WEAK (newdecl) && !DECL_WEAK (olddecl))
+    declare_weak (olddecl);
+  if (!DECL_WEAK (newdecl) && DECL_WEAK (olddecl))
+    declare_weak (newdecl);
+
   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);


More information about the Gcc mailing list