Bug 8880 - __WCHAR_TYPE__ macro incorrectly set to "long int" with -fshort-wchar
Summary: __WCHAR_TYPE__ macro incorrectly set to "long int" with -fshort-wchar
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: preprocessor (show other bugs)
Version: 3.2.1
: P3 normal
Target Milestone: ---
Assignee: Neil Booth
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-12-09 12:26 UTC by cheinan
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description cheinan 2002-12-09 12:26:01 UTC
The __WCHAR_TYPE__ macro is not set correctly when the -fshort-wchar command line option is specified.  In gcc 3.0.3 the macro would be defined to "unsigned short," but in 3.2.1 it remains set to "long int."  The wchar_t type itself is correct.

The compiler also emits the diagnostic:
<command line>:1:1: warning: "__WCHAR_TYPE__" redefined
<command line>:1:1: warning: this is the location of the previous definition

Release:
gcc version 3.2.1

Environment:
Sun Solaris 8 Sparc
liza:~/test> g++ -v
Reading specs from /opt/exp/full/lib/gcc-lib/sparc-sun-solaris2.8/3.2.1/specs
Configured with: ../gcc-3.2.1/configure --prefix=/opt/exp/full --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --disable-nls --enable-languages=c,c++
Thread model: posix
gcc version 3.2.1

How-To-Repeat:
liza:~/test> cat empty.cpp
int main()
{
        return 0;
}
liza:~/test> g++ -E -dM -fshort-wchar empty.cpp
<command line>:1:1: warning: "__WCHAR_TYPE__" redefined
<command line>:1:1: warning: this is the location of the previous definition
#define __EXTENSIONS__ 1
#define __HAVE_BUILTIN_SETJMP__ 1
#define _XOPEN_SOURCE 500
#define __sparc 1
#define sun 1
#define __unix__ 1
#define unix 1
#define __SIZE_TYPE__ unsigned int
#define __GNUC_PATCHLEVEL__ 1
#define sparc 1
#define __unix 1
#define __PRAGMA_REDEFINE_EXTNAME 1
#define __USER_LABEL_PREFIX__ 
#define __STDC_HOSTED__ 1
#define _LARGEFILE64_SOURCE 1
#define __EXCEPTIONS 1
#define __GXX_WEAK__ 1
#define __WCHAR_TYPE__ long int
#define _LARGEFILE_SOURCE 1
#define __WINT_TYPE__ long int
#define __GNUC__ 3
#define __sun__ 1
#define __SVR4 1
#define __GCC_NEW_VARARGS__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __svr4__ 1
#define __GNUG__ 3
#define __sparc__ 1
#define __GXX_ABI_VERSION 102
#define __GNUC_MINOR__ 2
#define __PTRDIFF_TYPE__ int
#define __sun 1
#define __REGISTER_PREFIX__ 
#define __NO_INLINE__ 1
#define __VERSION__ "3.2.1"

Alternative illustration:
> cat wcharmacro.cpp
#include <iostream>

int main()
{
        std::cout << "sizeof(__WCHAR_TYPE__) = " << sizeof(__WCHAR_TYPE__) << std::endl;
        std::cout << "sizeof(wchar_t) = " << sizeof(wchar_t) << std::endl;

        return 0;
}

> g++ wcharmacro.cpp -fshort-wchar -owcm -Wl,-R/opt/exp/full/lib
<command line>:1:1: warning: "__WCHAR_TYPE__" redefined
<command line>:1:1: warning: this is the location of the previous definition
> ./wcm
sizeof(__WCHAR_TYPE__) = 4
sizeof(wchar_t) = 2
Comment 1 cheinan 2002-12-09 12:26:01 UTC
Fix:
Workaround:  Add a -D__WCHAR_TYPE__=whatever after the -fshort-wchar on the command line.

Better fix:  Add a macro that defines to a string that tells whether the -fshort-wchar is present.
Comment 2 Neil Booth 2002-12-10 00:19:37 UTC
From: Neil Booth <neil@daikokuya.co.uk>
To: cheinan@primus.com
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: preprocessor/8880: __WCHAR_TYPE__ macro incorrectly set to "long int" with -fshort-wchar
Date: Tue, 10 Dec 2002 00:19:37 +0000

 cheinan@primus.com wrote:-
 
 > The __WCHAR_TYPE__ macro is not set correctly when the -fshort-wchar command line option is specified.  In gcc 3.0.3 the macro would be defined to "unsigned short," but in 3.2.1 it remains set to "long int."  The wchar_t type itself is correct.
 > 
 > The compiler also emits the diagnostic:
 > <command line>:1:1: warning: "__WCHAR_TYPE__" redefined
 > <command line>:1:1: warning: this is the location of the previous definition
 > >How-To-Repeat:
 > liza:~/test> cat empty.cpp
 > int main()
 > {
 >         return 0;
 > }
 
 Would you please confirm this issue does not arise in CVS gcc (pre-3.3)?
 
 Neil.
Comment 3 Neil Booth 2002-12-11 13:11:40 UTC
Responsible-Changed-From-To: unassigned->neil
Responsible-Changed-Why: Mine.
Comment 4 Neil Booth 2002-12-11 13:11:40 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Patch in progress.
Comment 5 Neil Booth 2002-12-11 21:13:15 UTC
From: Neil Booth <neil@daikokuya.co.uk>
To: cheinan@primus.com
Cc: gcc-gnats@gcc.gnu.org, gcc-patches@gcc.gnu.org, gcc-bugs@gcc.gnu.org
Subject: Re: preprocessor/8880: __WCHAR_TYPE__ macro incorrectly set to "long int" with -fshort-wchar
Date: Wed, 11 Dec 2002 21:13:15 +0000

 Please confirm this patch fixes it (a kludge around the kludge that is
 predefined macros before 3.3).
 
 Neil.
 
 	PR preprocessor/8880
 	* gcc.c (cpp_unique_options): Handle -fshort-wchar later so it
 	overrides any target CPU specs.
 
 Index: gcc.c
 ===================================================================
 RCS file: /cvs/gcc/gcc/gcc/gcc.c,v
 retrieving revision 1.296.2.12.4.4
 diff -u -p -r1.296.2.12.4.4 gcc.c
 --- gcc.c	14 Oct 2002 21:55:26 -0000	1.296.2.12.4.4
 +++ gcc.c	11 Dec 2002 21:11:52 -0000
 @@ -688,10 +688,10 @@ static const char *cpp_unique_options =
   %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
   %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
   %{fno-inline|O0|!O*:-D__NO_INLINE__} %{ffast-math:-D__FAST_MATH__}\
 - %{fshort-wchar:-U__WCHAR_TYPE__ -D__WCHAR_TYPE__=short\\ unsigned\\ int}\
   %{ffreestanding:-D__STDC_HOSTED__=0} %{fno-hosted:-D__STDC_HOSTED__=0}\
   %{!ffreestanding:%{!fno-hosted:-D__STDC_HOSTED__=1}} %{remap}\
   %{g3:-dD} %{H} %C %{D*&U*&A*} %{i*} %Z %i\
 + %{fshort-wchar:-U__WCHAR_TYPE__ -D__WCHAR_TYPE__=short\\ unsigned\\ int}\
   %{E|M|MM:%W{o*}}";
  
  /* This contains cpp options which are common with cc1_options and are passed
Comment 6 Neil Booth 2003-01-01 08:24:43 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: No feedback; I'm assuming the patch fixes it and I've checked it in.
Comment 7 Neil Booth 2003-01-01 16:24:29 UTC
From: neil@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: preprocessor/8880
Date: 1 Jan 2003 16:24:29 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	neil@gcc.gnu.org	2003-01-01 08:24:29
 
 Modified files:
 	gcc            : ChangeLog gcc.c 
 
 Log message:
 	PR preprocessor/8880
 	* gcc.c (cpp_unique_options): Handle -fshort-wchar later so it
 	overrides any target CPU specs.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.13152.2.657.2.168&r2=1.13152.2.657.2.169
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/gcc.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.296.2.12.4.4&r2=1.296.2.12.4.5