Subject: lib/11766: is not -Wold-style-cast clean
To: None <gnats-bugs@gnats.netbsd.org>
From: None <dave@dtsp.co.nz>
List: netbsd-bugs
Date: 12/18/2000 03:07:12
>Number:         11766
>Category:       lib
>Synopsis:       <assert.h> is not -Wold-style-cast clean
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 18 03:07:00 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     David Sainty
>Release:        Recent NetBSD-current
>Organization:
Dynamic Technology Services and Products Ltd (NZ)
>Environment:
System: NetBSD tequila.dave.dtsp.co.nz 1.5K NetBSD 1.5K (TEQUILA) #1: Mon Dec 11 23:13:44 NZDT 2000 dave@tequila.dave.dtsp.co.nz:/vol/tequila/userB/u2/NetBSD-current/src/sys/arch/i386/compile/TEQUILA i386


>Description:
	The g++ warning -Wold-style-cast is very useful when porting C code to
	C++.  Some of the NetBSD headers generate this warning, however.

>How-To-Repeat:
	echo "#include <assert.h>
	main() { assert(!\"error\"); }" >| t.c

	g++ -Wold-style-cast t.c
	t.c: In function `int main()':
	t.c:2: warning: use of old-style cast

>Fix:
	The following patches implement a suggested fix.  They define a new
	macro __static_cast(), which can be used in either C or C++ code with
	the correct effect.  This can also be used elsewhere where this is an
	issue, if this suggestion is taken on-board.

--- src/sys/sys/cdefs.h.orig	Thu Oct  5 20:31:56 2000
+++ src/sys/sys/cdefs.h	Mon Dec 18 23:42:36 2000
@@ -70,9 +70,11 @@
 #if defined(__cplusplus)
 #define	__BEGIN_DECLS	extern "C" {
 #define	__END_DECLS	};
+#define	__static_cast(x,y)	static_cast<x>(y)
 #else
 #define	__BEGIN_DECLS
 #define	__END_DECLS
+#define	__static_cast(x,y)	(x)y
 #endif
 
 /*
--- src/include/assert.h.orig	Sun Aug 13 15:07:46 2000
+++ src/include/assert.h	Mon Dec 18 23:44:41 2000
@@ -50,8 +50,8 @@
 
 #ifdef NDEBUG
 # ifndef lint
-#  define assert(e)	((void)0)
-#  define _assert(e)	((void)0)
+#  define assert(e)	(__static_cast(void,0))
+#  define _assert(e)	(__static_cast(void,0))
 # else /* !lint */
 #  define assert(e)
 #  define _assert(e)
@@ -59,24 +59,24 @@
 #else /* !NDEBUG */
 # define _assert(e)	assert(e)
 # if __STDC__
-#  define assert(e) 	((e) ? (void)0 : __assert(__FILE__, __LINE__, #e))
+#  define assert(e) 	((e) ? __static_cast(void,0) : __assert(__FILE__, __LINE__, #e))
 # else	/* PCC */
-#  define assert(e)	((e) ? (void)0 : __assert(__FILE__, __LINE__, "e"))
+#  define assert(e)	((e) ? __static_cast(void,0) : __assert(__FILE__, __LINE__, "e"))
 # endif /* !__STDC__ */
 #endif /* NDEBUG */
 
 #undef _DIAGASSERT
 #if !defined(_DIAGNOSTIC)
 # if !defined(lint)
-#  define _DIAGASSERT(e) ((void)0)
+#  define _DIAGASSERT(e) __static_cast(void,0)
 # else /* !lint */
 #  define _DIAGASSERT(e)
 # endif /* lint */
 #else /* _DIAGNOSTIC */
 # if __STDC__
-#  define _DIAGASSERT(e) ((e) ? (void)0 : __diagassert(__FILE__, __LINE__, #e))
+#  define _DIAGASSERT(e) ((e) ? __static_cast(void,0) : __diagassert(__FILE__, __LINE__, #e))
 # else	/* !__STDC__ */
-#  define _DIAGASSERT(e) ((e) ? (void)0 : __diagassert(__FILE__, __LINE__, "e"))
+#  define _DIAGASSERT(e) ((e) ? __static_cast(void,0) : __diagassert(__FILE__, __LINE__, "e"))
 # endif
 #endif /* _DIAGNOSTIC */
 
>Release-Note:
>Audit-Trail:
>Unformatted: