Bug 22856 - glibc-2.2 code for htonl/etc won't compile with -O outside of a function body
Summary: glibc-2.2 code for htonl/etc won't compile with -O outside of a function body
Keywords:
Status: CLOSED UPSTREAM
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 7.0
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2000-12-26 22:08 UTC by j. alan eldridge
Modified: 2007-04-18 16:30 UTC (History)
1 user (show)

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2004-10-05 22:54:07 UTC
Embargoed:


Attachments (Terms of Use)

Description j. alan eldridge 2000-12-26 22:08:57 UTC
ok, this is kind of messy.

first, we have /usr/include/netinet/in.h, which does:

#ifdef __OPTIMIZE__
[ ... ]
#  if __BYTE_ORDER == __LITTLE_ENDIAN
#   define ntohl(x)	__bswap_32 (x)
#   define ntohs(x)	__bswap_16 (x)
#   define htonl(x)	__bswap_32 (x)
#   define htons(x)	__bswap_16 (x)
#  endif
[ ... ]
#endif

then, in /usr/include/bits/byteswap.h, we get this:

#  define __bswap_32(x) \
     (__extension__							      \
      ({ register unsigned int __v;					      \
	 if (__builtin_constant_p (x))					      \
	   __v = __bswap_constant_32 (x);				      \
	 else								      \
	   __asm__ __volatile__ ("bswap %0"				      \
				 : "=r" (__v)				      \
				 : "0" ((unsigned int) (x)));		      \
	 __v; }))

that's the background.

take a small piece of c++ code that assigns a global scope variable using
htonl() and "houston, we have a problem."

[alane@wozzle /tmp]$ cat t.cc
#include <netinet/in.h>

uint32_t noaddr = htonl(INADDR_NONE);
[alane@wozzle /tmp]$ g++ -c t.cc
[alane@wozzle /tmp]$ g++ -O -c t.cc
t.cc:3: braced-group within expression allowed only inside a function
t.cc:3: ISO C++ forbids declaration of `__v' with no type
t.cc:3: parse error before `}'
[alane@wozzle /tmp]$ 

yes, i know this is *really* obscure. and it could just as well be taken as
a bug in glibc. feel free to recategorize as such if that is warranted.

problem is, it's just *so* weird, and the error message from compiling a
perfectly reasonable piece of code is totally martian. i don't know what
right fix is. i just put parens around the htonl call so it wouldn't macro
expand. fsck. [sigh]

Comment 1 Jakub Jelinek 2000-12-27 11:05:53 UTC
I see, but I think also older g++ (e.g. egcs 1.1.2 or gcc 2.95.2) did this and
likewise older glibc (like 2.1.x).
I see possible 4 solutions for this, am just mailing libc-hackers to discuss
which way to go.

Comment 2 Richard Henderson 2004-10-01 21:15:29 UTC
G++ will accept this for gcc 4.0, but no prior versions.  I suspect this
largely due to the re-implementation of statement expressions for
tree-ssa.

Comment 3 Richard Henderson 2004-10-05 22:54:07 UTC
Pushed upstream as a feature request to
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17854


Note You need to log in before you can comment on or make changes to this bug.