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]
Other format: [Raw text]

C++ PATCH for c++/29365


This patch extends Seongbae Park's earlier patch to avoid warning about uses of types from the anonymous namespace if we're in the main input file to cover template instantiations as well.

Tested x86_64-pc-linux-gnu, applied to trunk and 4.2 (as this is a regression).
2007-08-20  Jason Merrill  <jason@redhat.com>

	PR c++/29365
	* pt.c (outermost_tinst_level): New function.
	* lex.c (in_main_input_context): New function.
	* decl2.c (constrain_class_visibility): Use it to avoid warning
	about uses of the anonymous namespace in the main input file.

Index: cp/cp-tree.h
===================================================================
*** cp/cp-tree.h	(revision 127709)
--- cp/cp-tree.h	(working copy)
*************** extern void yyerror				(const char *);
*** 4374,4379 ****
--- 4374,4380 ----
  extern void yyhook				(int);
  extern bool cxx_init				(void);
  extern void cxx_finish				(void);
+ extern bool in_main_input_context		(void);
  
  /* in method.c */
  extern void init_method				(void);
*************** extern tree build_non_dependent_args		(t
*** 4465,4470 ****
--- 4466,4472 ----
  extern bool reregister_specialization		(tree, tree, tree);
  extern tree fold_non_dependent_expr		(tree);
  extern bool explicit_class_specialization_p     (tree);
+ extern tree outermost_tinst_level		(void);
  
  /* in repo.c */
  extern void init_repo				(void);
Index: cp/pt.c
===================================================================
*** cp/pt.c	(revision 127709)
--- cp/pt.c	(working copy)
*************** reopen_tinst_level (tree level)
*** 6045,6050 ****
--- 6045,6059 ----
    pop_tinst_level ();
  }
  
+ /* Returns the TINST_LEVEL which gives the original instantiation
+    context.  */
+ 
+ tree
+ outermost_tinst_level (void)
+ {
+   return tree_last (current_tinst_level);
+ }
+ 
  /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
     vector of template arguments, as for tsubst.
  
Index: cp/lex.c
===================================================================
*** cp/lex.c	(revision 127709)
--- cp/lex.c	(working copy)
*************** make_aggr_type (enum tree_code code)
*** 849,851 ****
--- 849,866 ----
  
    return t;
  }
+ 
+ /* Returns true if we are currently in the main source file, or in a
+    template instantiation started from the main source file.  */
+ 
+ bool
+ in_main_input_context (void)
+ {
+   tree tl = outermost_tinst_level();
+ 
+   if (tl)
+     return strcmp (main_input_filename,
+ 		   LOCATION_FILE (TINST_LOCATION (tl))) == 0;
+   else
+     return strcmp (main_input_filename, input_filename) == 0;
+ }
Index: cp/decl2.c
===================================================================
*** cp/decl2.c	(revision 127709)
--- cp/decl2.c	(working copy)
*************** constrain_class_visibility (tree type)
*** 1854,1861 ****
  
  	if (subvis == VISIBILITY_ANON)
  	  {
! 	    if (strcmp (main_input_filename,
! 	                DECL_SOURCE_FILE (TYPE_MAIN_DECL (ftype))))
  	      warning (0, "\
  %qT has a field %qD whose type uses the anonymous namespace",
  		       type, t);
--- 1854,1860 ----
  
  	if (subvis == VISIBILITY_ANON)
  	  {
! 	    if (!in_main_input_context ())
  	      warning (0, "\
  %qT has a field %qD whose type uses the anonymous namespace",
  		       type, t);
*************** constrain_class_visibility (tree type)
*** 1875,1882 ****
  
        if (subvis == VISIBILITY_ANON)
          {
! 	  if (strcmp (main_input_filename,
! 	              DECL_SOURCE_FILE (TYPE_MAIN_DECL (TREE_TYPE (t)))))
  	    warning (0, "\
  %qT has a base %qT whose type uses the anonymous namespace",
  		     type, TREE_TYPE (t));
--- 1874,1880 ----
  
        if (subvis == VISIBILITY_ANON)
          {
! 	  if (!in_main_input_context())
  	    warning (0, "\
  %qT has a base %qT whose type uses the anonymous namespace",
  		     type, TREE_TYPE (t));
Index: testsuite/g++.dg/warn/anonymous-namespace-3.C
===================================================================
*** testsuite/g++.dg/warn/anonymous-namespace-3.C	(revision 0)
--- testsuite/g++.dg/warn/anonymous-namespace-3.C	(revision 0)
***************
*** 0 ****
--- 1,13 ----
+ // Test for the warning of exposing types from an anonymous namespace
+ // { dg-do compile }
+ //
+ #include <memory>
+ #include "anonymous-namespace-3.h"
+ 
+ struct B { std::auto_ptr<A> p; };
+ 
+ #line 10 "foo.C"
+ struct C
+ { // { dg-warning "uses the anonymous namespace" }
+   std::auto_ptr<A> p;
+ };
Index: testsuite/g++.dg/warn/anonymous-namespace-3.h
===================================================================
*** testsuite/g++.dg/warn/anonymous-namespace-3.h	(revision 0)
--- testsuite/g++.dg/warn/anonymous-namespace-3.h	(revision 0)
***************
*** 0 ****
--- 1 ----
+ namespace { struct A; }
Index: testsuite/g++.dg/warn/anonymous-namespace-1.C
===================================================================
*** testsuite/g++.dg/warn/anonymous-namespace-1.C	(revision 127709)
--- testsuite/g++.dg/warn/anonymous-namespace-1.C	(working copy)
*************** class foobar1
*** 13,17 ****
--- 13,18 ----
    good g;
  };
  
+ #line 17 "foo.C"
  class foobar : public bad { }; // { dg-warning "uses the anonymous namespace" }
  class foobar2 { bad b; }; // { dg-warning "uses the anonymous namespace" }
Index: testsuite/g++.dg/warn/anonymous-namespace-2.C
===================================================================
*** testsuite/g++.dg/warn/anonymous-namespace-2.C	(revision 127709)
--- testsuite/g++.dg/warn/anonymous-namespace-2.C	(working copy)
*************** namespace {
*** 10,29 ****
  struct g1 {
      good * A;
  };
- struct b1 { // { dg-warning "uses the anonymous namespace" }
-     bad * B;
- };
- 
  struct g2 {
      good * A[1];
  };
- struct b2 { // { dg-warning "uses the anonymous namespace" }
-     bad * B[1];
- };
- 
  struct g3 {
      good (*A)[1];
  };
  struct b3 { // { dg-warning "uses the anonymous namespace" }
      bad (*B)[1];
  };
--- 10,29 ----
  struct g1 {
      good * A;
  };
  struct g2 {
      good * A[1];
  };
  struct g3 {
      good (*A)[1];
  };
+ 
+ #line 21 "foo.C"
+ struct b1 { // { dg-warning "uses the anonymous namespace" }
+     bad * B;
+ };
+ struct b2 { // { dg-warning "uses the anonymous namespace" }
+     bad * B[1];
+ };
  struct b3 { // { dg-warning "uses the anonymous namespace" }
      bad (*B)[1];
  };

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