Bug 9393 - [3.3 regression] Anonymous namespaces and compiling the same file twice
Summary: [3.3 regression] Anonymous namespaces and compiling the same file twice
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 3.3.1
Assignee: Geoff Keating
URL:
Keywords:
: 14160 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-01-21 23:06 UTC by Wolfgang Bangerth
Modified: 2004-02-16 15:43 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2003-06-17 23:01:48


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wolfgang Bangerth 2003-01-21 23:06:00 UTC
Hm, this counters what anonymous namespaces are all about:
if I have the following file, and compile it twice (once
with D defined, once without), then I get a linker error
that elements of the anonymous namespaces are defined twice:
------------------------
namespace {
  struct S {
      void f();
  };
  void S::f () {}
}

#if D
  void foo () { S s; }
#else
  int main () { S s; }
#endif
--------------------------
With present 3.4 CVS, I get the following:

g/a> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc -DD -o x1.o
g/a> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc -o x2.o
g/a> /home/bangerth/bin/gcc-3.4-pre/bin/c++ x?.o -o a.out
x2.o: In function `(anonymous namespace)::S::f()':
x2.o(.text+0x0): multiple definition of `(anonymous namespace)::S::f()'
x1.o(.text+0x0): first defined here
collect2: ld returned 1 exit status


On the other hand, it worked with 2.95:

g/a> c++ -c x.cc -DD -o x1.o
g/a> c++ -c x.cc -o x2.o
g/a> c++ x?.o -o a.out
g/a>

It also works with 3.0.4, but is already broken on the
3.2 branch. In any case, it's a regression. Too bad,
anonymous namespaces are a useful feature...

When digging a little deeper, it is of course clear what
happens: until 3.0.4, the name of a symbol from an anonymous
namespace differs when the file is compiled twice (even with
the exact same flags). This is no longer the case since
3.2.2. A simpler testcase is thus the following: take
---------------------
namespace {
  int i;
}
---------------------
and compile it twice and look at the symbol names. I get

g/a> /home/bangerth/bin/gcc-3.0.4/bin/c++ -c x.cc -o x2.o
g/a> nm x2.o
00000000 B _ZN21_GLOBAL__N_x.ccc18R7c1iE

g/a> /home/bangerth/bin/gcc-3.0.4/bin/c++ -c x.cc -o x2.o
g/a> nm x2.o
00000000 B _ZN21_GLOBAL__N_x.ccQqZgxc1iE

which is different, but

g/a> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc -o x2.o
g/a> nm x2.o
00000000 B _ZN21_GLOBAL__N_x.ccI4JKib1iE

g/a> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc -o x2.o
g/a> nm x2.o
00000000 B _ZN21_GLOBAL__N_x.ccI4JKib1iE

which is the same twice.

W.

Release:
unknown

Environment:
gcc 3.4
Comment 1 Christian Ehrhardt 2003-02-01 23:25:44 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed with 3.2 and 3.4. The current behaviour violates 7.3.1.1[1].
    This worked with 2.95.3, i.e. this is a regression.
Comment 2 INCORRECT EMAIL ADDRESS! 2003-04-10 16:49:01 UTC
From: Geoffrey Keating <geoffk@apple.com>
To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org, bangerth@ices.utexas.edu,
   gcc-prs@gcc.gnu.org
Cc:  
Subject: Re: c++/9393: [3.2/3.3/3.4 regression] Anonymous namespaces and compiling the same file twice
Date: Thu, 10 Apr 2003 16:49:01 -0700

 A better testcase is:
 
 #include <iostream>
 namespace {
    struct S {
        S();
    };
    S::S () { std::cout << "One more call" << std::endl; }
    S local_s;
 }
 
 (you can insert anything you like in the constructor).  You can have as 
 many of these as you like in your program.
 
 One additional problem with the existing behaviour is that you get 
 "determinism" only if inode numbers and modification times don't 
 change.  This means that (a) it's not reproducible; users normally 
 can't control inode numbers; and (b) it's fragile, because both of 
 these can quietly vary, especially on network file systems.
 

Comment 3 Wolfgang Bangerth 2003-04-10 19:22:02 UTC
From: Wolfgang Bangerth <bangerth@ices.utexas.edu>
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: c++/9393
Date: Thu, 10 Apr 2003 19:22:02 -0500 (CDT)

 Quite some discussion about this PR can now also be found in the thread 
 starting here:
     http://gcc.gnu.org/ml/gcc/2003-04/msg00479.html
 
 -------------------------------------------------------------------------
 Wolfgang Bangerth              email:            bangerth@ices.utexas.edu
                                www: http://www.ices.utexas.edu/~bangerth/
 
 

Comment 4 Geoff Keating 2003-04-12 02:16:46 UTC
From: geoffk@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/9393
Date: 12 Apr 2003 02:16:46 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	geoffk@gcc.gnu.org	2003-04-12 02:16:46
 
 Modified files:
 	gcc            : ChangeLog config.in configure configure.in 
 	                 flags.h toplev.c tree.c tree.h 
 	gcc/config/alpha: t-crtfm 
 	gcc/doc        : invoke.texi 
 
 Log message:
 	PR c++/9393
 	* doc/invoke.texi (Debugging Options): Document -frandom-seed.
 	* configure.in: Check for gettimeofday.
 	* tree.c (flag_random_seed): Define.
 	(default_flag_random_seed): New.
 	(append_random_chars): Use flag_random_seed rather than trying
 	to acquire randomness here.
 	* tree.h (default_flag_random_seed): Declare.
 	* toplev.c (display_help): Add -frandom-seed and -fstack-limit-*
 	descriptions.
 	(decode_f_option): Handle -frandom-seed.
 	(print_switch_values): Call default_flag_random_seed.
 	* flags.h (flag_random_seed): Declare.
 	* configure: Regenerate.
 	* config.in: Regenerate.
 	* config/alpha/t-crtfm: Use -frandom-seed.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.17407&r2=1.17408
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config.in.diff?cvsroot=gcc&r1=1.154&r2=1.155
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/configure.diff?cvsroot=gcc&r1=1.672&r2=1.673
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/configure.in.diff?cvsroot=gcc&r1=1.657&r2=1.658
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/flags.h.diff?cvsroot=gcc&r1=1.104&r2=1.105
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/toplev.c.diff?cvsroot=gcc&r1=1.740&r2=1.741
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree.c.diff?cvsroot=gcc&r1=1.294&r2=1.295
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree.h.diff?cvsroot=gcc&r1=1.390&r2=1.391
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/alpha/t-crtfm.diff?cvsroot=gcc&r1=1.1&r2=1.2
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/doc/invoke.texi.diff?cvsroot=gcc&r1=1.263&r2=1.264
Comment 5 Andrew Pinski 2003-06-01 23:15:37 UTC
Should this be fixed for 3.3.x?
Comment 6 Wolfgang Bangerth 2003-06-02 15:00:38 UTC
Geoff, since Andrew reminded me of that: I contacted Mark right before the
3.3 release about this one and he prefered to pass this one over. Would
you mind resubmitting your patch for inclusion in 3.3.1? Presently should
be a good time for that, I think it was just to late in the 3.3.0 release
process last time.

Thanks
  W.
Comment 7 CVS Commits 2003-06-20 21:17:06 UTC
Subject: Bug 9393

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	geoffk@gcc.gnu.org	2003-06-20 21:17:03

Modified files:
	gcc/config/alpha: t-crtfm 
	gcc/doc        : invoke.texi 

Log message:
	PR c++/9393
	* doc/invoke.texi (Debugging Options): Document -frandom-seed.
	* configure.in: Check for gettimeofday.
	* tree.c (flag_random_seed): Define.
	(default_flag_random_seed): New.
	(append_random_chars): Use flag_random_seed rather than trying
	to acquire randomness here.
	* tree.h (default_flag_random_seed): Declare.
	* toplev.c (display_help): Add -frandom-seed and -fstack-limit-*
	descriptions.
	(decode_f_option): Handle -frandom-seed.
	(print_switch_values): Call default_flag_random_seed.
	* flags.h (flag_random_seed): Declare.
	* configure: Regenerate.
	* config.in: Regenerate.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/alpha/t-crtfm.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.1&r2=1.1.50.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/doc/invoke.texi.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.209.2.31&r2=1.209.2.32

Comment 8 CVS Commits 2003-06-20 21:18:46 UTC
Subject: Bug 9393

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	geoffk@gcc.gnu.org	2003-06-20 21:18:43

Modified files:
	gcc            : ChangeLog config.in configure configure.in 
	                 flags.h toplev.c tree.c tree.h 

Log message:
	PR c++/9393
	* doc/invoke.texi (Debugging Options): Document -frandom-seed.
	* configure.in: Check for gettimeofday.
	* tree.c (flag_random_seed): Define.
	(default_flag_random_seed): New.
	(append_random_chars): Use flag_random_seed rather than trying
	to acquire randomness here.
	* tree.h (default_flag_random_seed): Declare.
	* toplev.c (display_help): Add -frandom-seed and -fstack-limit-*
	descriptions.
	(decode_f_option): Handle -frandom-seed.
	(print_switch_values): Call default_flag_random_seed.
	* flags.h (flag_random_seed): Declare.
	* configure: Regenerate.
	* config.in: Regenerate.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.599&r2=1.16114.2.600
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config.in.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.146.2.2&r2=1.146.2.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/configure.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.641.2.12&r2=1.641.2.13
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/configure.in.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.627.2.11&r2=1.627.2.12
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/flags.h.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.93.4.2&r2=1.93.4.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/toplev.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.690.2.19&r2=1.690.2.20
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.282.2.1&r2=1.282.2.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree.h.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.367.2.5&r2=1.367.2.6

Comment 9 Geoff Keating 2003-06-20 21:22:07 UTC
Patch committed.
Comment 10 Andrew Pinski 2004-02-16 15:43:27 UTC
*** Bug 14160 has been marked as a duplicate of this bug. ***