Bug 15090 - [3.4 Regression] -fno-for-scope vs. libstdc++
Summary: [3.4 Regression] -fno-for-scope vs. libstdc++
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 3.4.1
Assignee: Benjamin Kosnik
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-04-23 09:16 UTC by Simon Marshall
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
locale_facets.h patch (498 bytes, patch)
2004-04-23 14:49 UTC, Simon Marshall
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Marshall 2004-04-23 09:16:02 UTC
With foo.cpp:

#include <iostream>
int main () {
 return 0;
}

compiled with:

g++ -fno-for-scope foo.cpp

results in:

In file included from /home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/bits/basic_ios.h:44,
                 from /home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/ios:51,
                 from /home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/ostream:45,
                 from /home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/iostream:45,
                 from foo.cpp:1:
/home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/bits/locale_facets.h: In member 
function `void std::ctype<char>::_M_widen_init() const':
/home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/bits/locale_facets.h:1164: error: 
redeclaration of `size_t __i'
/home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/bits/locale_facets.h:1158: error: 
`size_t __i' previously declared here
/home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/bits/locale_facets.h: In member 
function `void std::ctype<char>::_M_narrow_init() const':
/home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/bits/locale_facets.h:1185: error: 
redeclaration of `size_t __i'
/home/marshals/slash/usr/local/lib/gcc/i686-pc-linux-
gnu/3.4.0/../../../../include/c++/3.4.0/bits/locale_facets.h:1178: error: 
`size_t __i' previously declared here

The reason is that ctype<char>::_M_widen_init() and _M_narrow_init() declare a 
variable (__i) in 2 for scopes.  A simple fix is to use different variable 
names for the loops:

*** /home/marshals/slash/usr/local/include/c++/3.4.0/bits/locale_facets.h-3.4.0 
2004-04-23 09:33:23.000000000 +0100
--- /home/marshals/slash/usr/local/include/c++/3.4.0/bits/locale_facets.h-fix   
2004-04-23 09:54:36.000000000 +0100
***************
*** 1161,1168 ****
  
        _M_widen_ok = 1;
        // Set _M_widen_ok to 2 if memcpy can't be used.
!       for (size_t __i = 0; __i < sizeof(_M_widen); ++__i)
!         if (__tmp[__i] != _M_widen[__i])
            {
              _M_widen_ok = 2;
              break;
--- 1161,1168 ----
  
        _M_widen_ok = 1;
        // Set _M_widen_ok to 2 if memcpy can't be used.
!       for (size_t __j = 0; __j < sizeof(_M_widen); ++__j)
!         if (__tmp[__j] != _M_widen[__j])
            {
              _M_widen_ok = 2;
              break;
***************
*** 1182,1192 ****
        // Check if any default values were created.  Do this by
        // renarrowing with a different default value and comparing.
        bool __consecutive = true;
!       for (size_t __i = 0; __i < sizeof(_M_narrow); ++__i)
!         if (!_M_narrow[__i])
            {
              char __c;
!             do_narrow(__tmp + __i, __tmp + __i + 1, 1, &__c);
              if (__c == 1)
                {
                  __consecutive = false;
--- 1182,1192 ----
        // Check if any default values were created.  Do this by
        // renarrowing with a different default value and comparing.
        bool __consecutive = true;
!       for (size_t __j = 0; __j < sizeof(_M_narrow); ++__j)
!         if (!_M_narrow[__j])
            {
              char __c;
!             do_narrow(__tmp + __j, __tmp + __j + 1, 1, &__c);
              if (__c == 1)
                {
                  __consecutive = false;
Comment 1 Paolo Carlini 2004-04-23 11:30:44 UTC
Humpf! I'm not sure we really want the library to be consistent with all the
switches for old-style non-standard C++! Anyway, this specific issue
seems so trivial to be worthy...

Can you possibly check that there are no other occurrence around? Some may be
also under config/.

In any case, please prepare your patch from the gcc-3.4.0 sources, not from
the install directory: just 'cp -ar libstdc++-v3 libstdc++-v3-orig', modify
libstdc++-v3, then do a recursive 'diff -prN libstdc++-v3-orig libstdc++-v3'.

Thanks!
Comment 2 Simon Marshall 2004-04-23 14:49:35 UTC
Created attachment 6152 [details]
locale_facets.h patch

Our application uses string, set, map, vector and some iostream and algorithm
and builds OK with just the patch to locale_facets.h.  I will attach the diff
at requested.
Comment 3 Wolfgang Bangerth 2004-04-23 15:59:01 UTC
While it may be worth fixing this one instance, I believe we should 
not be bothered with this. The for-scoping rules have been in place 
for many many years, and it is really time for applications to 
fix their bugs. In particular, since the bugs this is about are so 
easy to fix: just move the declaration of the loop variable out of 
the for-header. 
 
I for one would not be unhappy if the -fno-for-scope thing would 
simply go away... 
 
W. 
Comment 4 Simon Marshall 2004-04-23 16:22:32 UTC
I can understand gcc maintainers not fixing these sorts of issues themselves 
even when -fno-for-scope exists, but I hope contribution patches such as mine 
will be applied while it does exist.

I don't like us having to use old for-scoping for our own application, but I 
certainly don't view them as "bugs" in our application - it's just an 
unfortunate consequence of our situation.
Comment 5 Wolfgang Bangerth 2004-04-23 16:46:49 UTC
As I said: in this case I'm all for fixing it. Nevertheless, I maintain 
my position that your code does have bugs, because it is not conforming 
to the C++ standard. For the sake of long-term maintainability and 
portability, you may want to fix it. 
 
W. 
Comment 6 Paolo Carlini 2004-04-29 10:39:23 UTC
... probably -fno-for-scope will go away...
Comment 7 Benjamin Kosnik 2004-05-13 15:56:23 UTC
This is fixed, easily. In the future, please fix this up for the whole shebang.

Ie

make CXXFLAGS="-g -O2 -fno-for-scope" WERROR="-Werror" all

should work.

-benjamin
Comment 8 GCC Commits 2004-05-13 16:29:48 UTC
Subject: Bug 15090

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	bkoz@gcc.gnu.org	2004-05-13 16:29:40

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/config/locale/gnu: ctype_members.cc 
	                                numeric_members.cc 
	libstdc++-v3/include/bits: locale_facets.h 
	libstdc++-v3/include/debug: safe_iterator.tcc safe_sequence.h 
	libstdc++-v3/src: debug.cc locale.cc locale_init.cc 
	                  localename.cc 
	libstdc++-v3/testsuite: testsuite_abi.cc testsuite_hooks.cc 

Log message:
	2004-05-13  Simon Marshall <simon.marshall@misys.com>
	Benjamin Kosnik  <bkoz@redhat.com>
	
	PR libstdc++/15090
	* include/bits/locale_facets.h: Fix for -fno-for-scope.
	* include/debug/safe_sequence.h: Same.
	* include/debug/safe_iterator.tcc: Same.
	* src/debug.cc: Same.
	* src/locale.cc: Same.
	* src/locale_init.cc: Same.
	* src/localename.cc: Same.
	* config/locale/gnu/ctype_members.cc: Same.
	* config/locale/gnu/numeric_members.cc: Same.
	* testsuite/testsuite_abi.cc: Same.
	* testsuite/testsuite_hooks.cc: Same.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&r1=1.2472&r2=1.2473
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/config/locale/gnu/ctype_members.cc.diff?cvsroot=gcc&r1=1.17&r2=1.18
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/config/locale/gnu/numeric_members.cc.diff?cvsroot=gcc&r1=1.17&r2=1.18
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/locale_facets.h.diff?cvsroot=gcc&r1=1.93&r2=1.94
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/debug/safe_iterator.tcc.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/debug/safe_sequence.h.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/debug.cc.diff?cvsroot=gcc&r1=1.5&r2=1.6
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/locale.cc.diff?cvsroot=gcc&r1=1.108&r2=1.109
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/locale_init.cc.diff?cvsroot=gcc&r1=1.11&r2=1.12
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/localename.cc.diff?cvsroot=gcc&r1=1.53&r2=1.54
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/testsuite_abi.cc.diff?cvsroot=gcc&r1=1.1&r2=1.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/testsuite_hooks.cc.diff?cvsroot=gcc&r1=1.21&r2=1.22

Comment 9 GCC Commits 2004-05-15 21:18:06 UTC
Subject: Bug 15090

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	bkoz@gcc.gnu.org	2004-05-15 21:18:00

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/config/locale/gnu: ctype_members.cc 
	                                numeric_members.cc 
	libstdc++-v3/include/bits: locale_facets.h 
	libstdc++-v3/include/debug: safe_iterator.tcc safe_sequence.h 
	libstdc++-v3/src: debug.cc locale.cc locale_init.cc 
	                  localename.cc 
	libstdc++-v3/testsuite: testsuite_abi.cc testsuite_hooks.cc 

Log message:
	2004-05-15  Simon Marshall <simon.marshall@misys.com>
	Benjamin Kosnik  <bkoz@redhat.com>
	
	PR libstdc++/15090
	* include/bits/locale_facets.h: Fix for -fno-for-scope.
	* include/debug/safe_sequence.h: Same.
	* include/debug/safe_iterator.tcc: Same.
	* src/debug.cc: Same.
	* src/locale.cc: Same.
	* src/locale_init.cc: Same.
	* src/localename.cc: Same.
	* config/locale/gnu/ctype_members.cc: Same.
	* config/locale/gnu/numeric_members.cc: Same.
	* testsuite/testsuite_abi.cc: Same.
	* testsuite/testsuite_hooks.cc: Same.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.2224.2.102&r2=1.2224.2.103
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/config/locale/gnu/ctype_members.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.16.4.1&r2=1.16.4.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/config/locale/gnu/numeric_members.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.13.2.2&r2=1.13.2.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/locale_facets.h.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.85.2.4&r2=1.85.2.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/debug/safe_iterator.tcc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.2&r2=1.2.8.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/debug/safe_sequence.h.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.2&r2=1.2.8.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/debug.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3.10.1&r2=1.3.10.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/locale.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.103.4.3&r2=1.103.4.4
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/locale_init.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.6.4.2&r2=1.6.4.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/localename.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.51.4.1&r2=1.51.4.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/testsuite_abi.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.2.2.1&r2=1.2.2.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/testsuite_hooks.cc.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.19.2.2&r2=1.19.2.3

Comment 10 Benjamin Kosnik 2004-05-18 01:20:52 UTC
Fixed.