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]

Re: Non-template-friend warning.


On Feb 14, 2001, Jacek Generowicz <jmg@ecs.soton.ac.uk> wrote:

> Is there a way of signalling that, in a particular case, the
> non-template nature of the friend is intended?

Not at this time.  But we could probably remove a number of false
positives.  For one, we currently print a warning even for:

template <class T>
class foo {
  friend int bar(int);
};

because uses_the scope of foo<T>::bar(int) refers to T.  Here's a patch
that fixes this.  Ok to install, if it doesn't introduce any
regressions?  Mainline and branch?

Index: gcc/cp/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* friend.c (do_friend): Don't take the nested [template] class
	into account when deciding whether to warn about the friend
	function not referring to a template function.

Index: gcc/cp/friend.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/friend.c,v
retrieving revision 1.64
diff -u -p -r1.64 friend.c
--- gcc/cp/friend.c 2001/01/12 09:13:15 1.64
+++ gcc/cp/friend.c 2001/02/15 03:11:17
@@ -380,6 +380,14 @@ do_friend (ctype, declarator, decl, parm
 
       if (! DECL_USE_TEMPLATE (decl))
 	{
+	  /* We must check whether the decl refers to template
+	     arguments before push_template_decl_real adds a
+	     reference to the containing template class.  */
+	  int warn = (warn_nontemplate_friend
+		      && ! funcdef_flag && ! is_friend_template
+		      && current_template_parms
+		      && uses_template_parms (decl));
+
 	  /* We can call pushdecl here, because the TREE_CHAIN of this
 	     FUNCTION_DECL is not needed for other purposes.  Don't do
 	     this for a template instantiation.  However, we don't
@@ -393,9 +401,7 @@ do_friend (ctype, declarator, decl, parm
 	  else 
 	    decl = push_template_decl_real (decl, /*is_friend=*/1); 
 
-	  if (warn_nontemplate_friend
-	      && ! funcdef_flag && ! is_friend_template
-	      && current_template_parms && uses_template_parms (decl))
+	  if (warn)
 	    {
 	      static int explained;
 	      cp_warning ("friend declaration `%#D' declares a non-template function", decl);
Index: gcc/testsuite/g++.old-deja/g++.oliva/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* friend1.C: New test.

Index: gcc/testsuite/g++.old-deja/g++.oliva/friend1.C
===================================================================
RCS file: friend1.C
diff -N friend1.C
--- /dev/null	Tue May  5 13:32:27 1998
+++ gcc/testsuite/g++.old-deja/g++.oliva/friend1.C Wed Feb 14 19:11:17 2001
@@ -0,0 +1,12 @@
+// Build don't link:
+
+// Copyright (C) 2001 Free Software Foundation
+
+// by Alexandre Oliva <aoliva@redhat.com>
+
+// We shouldn't warn about bar referring to a non-template in this case.
+
+template <typename T>
+class foo {
+  friend int bar(int);
+};

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

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