[Bug preprocessor/23479] Implement binary constants with a "0b" prefix

Joerg Wunsch j@uriah.heep.sax.de
Mon Mar 12 21:16:00 GMT 2007


As Tom Tromey wrote:

> Joerg> Which I'm hereby doing.  I updated the patch to the output of
> Joerg> "svn diff" as of yesterday, and reassured everything works fine
> Joerg> with the SVN trunk still.

> There's no ChangeLog entry.  That is required for a patch to go in.

Sorry for that.  Here's the ChangeLog entries:

libcpp/ChangeLog

2007-02-09 Joerg Wunsch <j.gnu@uriah.heep.sax.de>

	PR preprocessor/23479
	* expr.c: Implement 0b-prefixed binary integer constants.
	* include/cpplib.h: Add CPP_N_BINARY, to be used for 0b-
	prefixed binary integer constants.

gcc/ChangeLog

2007-02-09 Joerg Wunsch <j.gnuuriah.heep.sax.de>

	PR preprocessor/23479
	* doc/extend.texi: Document the 0b-prefixed binary
	integer constant extension.

gcc/testsuite/ChangeLog

2007-02-09 Joerg Wunsch <j.gnuuriah.heep.sax.de>

	PR preprocessor/23479
	* testsuite/gcc.dg/binary-constants-1.c: Add test suites for
	the 0b-prefixed binary integer constants.
	* testsuite/gcc.dg/binary-constants-2.c: (Ditto.)
	* testsuite/gcc.dg/binary-constants-3.c: (Ditto.)
	* testsuite/gcc.dg/binary-constants-4.c: (Ditto.)

-- 
cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)
-------------- next part --------------
Index: gcc/doc/extend.texi
===================================================================
--- gcc/doc/extend.texi	(revision 121711)
+++ gcc/doc/extend.texi	(working copy)
@@ -81,6 +81,7 @@
 * Pragmas::             Pragmas accepted by GCC.
 * Unnamed Fields::      Unnamed struct/union fields within structs/unions.
 * Thread-Local::        Per-thread variables.
+* Binary constants::    Binary constants using the @samp{0b} prefix.
 @end menu
 
 @node Statement Exprs
@@ -10495,6 +10496,28 @@
 @end quotation
 @end itemize
 
+@node Binary constants
+@section Binary constants using the @samp{0b} prefix
+@cindex Binary constants using the @samp{0b} prefix
+
+Integer constants can be written as binary constants, consisting of a
+sequence of @samp{0} and @samp{1} digits, prefixed by @samp{0b} or
+@samp{0B}.  This is particularly useful in environments that operate a
+lot on the bit-level (like microcontrollers).
+
+The following statements are identical:
+
+@smallexample
+i =       42;
+i =     0x2a;
+i =      052;
+i = 0b101010;
+@end smallexample
+
+The type of these constants follows the same rules as for octal or
+hexadecimal integer constants, so suffixes like @samp{L} or @samp{UL}
+can be applied.
+
 @node C++ Extensions
 @chapter Extensions to the C++ Language
 @cindex extensions, C++ language
Index: gcc/testsuite/gcc.dg/binary-constants-1.c
===================================================================
--- gcc/testsuite/gcc.dg/binary-constants-1.c	(revision 0)
+++ gcc/testsuite/gcc.dg/binary-constants-1.c	(revision 0)
@@ -0,0 +1,312 @@
+/* Test for binary integer constants.  */
+
+/* Derived from: c99-intconst-1.c, bye Joseph Myers <jsm28@cam.ac.uk>.  */
+/* Origin: Joerg Wunsch <j.gnu@uriah.heep.sax.de>.  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99" } */
+
+#include <limits.h>
+
+/* Assertion that constant C is of type T.  */
+#define ASSERT_CONST_TYPE(C, T)			\
+	do {					\
+	  typedef T type;			\
+	  typedef type **typepp;		\
+	  typedef __typeof__((C)) ctype;	\
+	  typedef ctype **ctypepp;		\
+	  typepp x = 0;				\
+	  ctypepp y = 0;			\
+	  x = y;				\
+	  y = x;				\
+	} while (0)
+
+/* (T *) if E is zero, (void *) otherwise.  */
+#define type_if_not(T, E) __typeof__(0 ? (T *)0 : (void *)(E))
+
+/* (T *) if E is nonzero, (void *) otherwise.  */
+#define type_if(T, E) type_if_not(T, !(E))
+
+/* Combine pointer types, all but one (void *).  */
+#define type_comb2(T1, T2) __typeof__(0 ? (T1)0 : (T2)0)
+#define type_comb3(T1, T2, T3) type_comb2(T1, type_comb2(T2, T3))
+#define type_comb4(T1, T2, T3, T4)				\
+	type_comb2(T1, type_comb2(T2, type_comb2(T3, T4)))
+#define type_comb6(T1, T2, T3, T4, T5, T6)				   \
+	type_comb2(T1,							   \
+		   type_comb2(T2,					   \
+			      type_comb2(T3,				   \
+					 type_comb2(T4,			   \
+						    type_comb2(T5, T6)))))
+
+/* (T1 *) if E1, otherwise (T2 *) if E2.  */
+#define first_of2p(T1, E1, T2, E2) type_comb2(type_if(T1, (E1)),	   \
+					     type_if(T2, (!(E1) && (E2))))
+/* (T1 *) if E1, otherwise (T2 *) if E2, otherwise (T3 *) if E3.  */
+#define first_of3p(T1, E1, T2, E2, T3, E3)			\
+	type_comb3(type_if(T1, (E1)),				\
+		   type_if(T2, (!(E1) && (E2))),		\
+		   type_if(T3, (!(E1) && !(E2) && (E3))))
+/* (T1 *) if E1, otherwise (T2 *) if E2, otherwise (T3 *) if E3, otherwise
+   (T4 *) if E4.  */
+#define first_of4p(T1, E1, T2, E2, T3, E3, T4, E4)			\
+	type_comb4(type_if(T1, (E1)),					\
+		   type_if(T2, (!(E1) && (E2))),			\
+		   type_if(T3, (!(E1) && !(E2) && (E3))),		\
+		   type_if(T4, (!(E1) && !(E2) && !(E3) && (E4))))
+/* (T1 *) if E1, otherwise (T2 *) if E2, otherwise (T3 *) if E3, otherwise
+   (T4 *) if E4, otherwise (T5 *) if E5, otherwise (T6 *) if E6.  */
+#define first_of6p(T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6)	    \
+	type_comb6(type_if(T1, (E1)),					    \
+		   type_if(T2, (!(E1) && (E2))),			    \
+		   type_if(T3, (!(E1) && !(E2) && (E3))),		    \
+		   type_if(T4, (!(E1) && !(E2) && !(E3) && (E4))),	    \
+		   type_if(T5, (!(E1) && !(E2) && !(E3) && !(E4) && (E5))), \
+		   type_if(T6, (!(E1) && !(E2) && !(E3)			    \
+				&& !(E4) && !(E5) && (E6))))
+
+/* Likewise, but return the original type rather than a pointer type.  */
+#define first_of2(T1, E1, T2, E2)			\
+	__typeof__(*((first_of2p(T1, (E1), T2, (E2)))0))
+#define first_of3(T1, E1, T2, E2, T3, E3)				\
+	__typeof__(*((first_of3p(T1, (E1), T2, (E2), T3, (E3)))0))
+#define first_of4(T1, E1, T2, E2, T3, E3, T4, E4)			    \
+	__typeof__(*((first_of4p(T1, (E1), T2, (E2), T3, (E3), T4, (E4)))0))
+#define first_of6(T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6)	\
+	__typeof__(*((first_of6p(T1, (E1), T2, (E2), T3, (E3),		\
+				 T4, (E4), T5, (E5), T6, (E6)))0))
+
+/* Types of constants according to the C99 rules.  */
+#define C99_UNSUF_TYPE(C)					\
+	first_of6(int, (C) <= INT_MAX,				\
+		  unsigned int, (C) <= UINT_MAX,		\
+		  long int, (C) <= LONG_MAX,			\
+		  unsigned long int, (C) <= ULONG_MAX,		\
+		  long long int, (C) <= LLONG_MAX,		\
+		  unsigned long long int, (C) <= ULLONG_MAX)
+#define C99_SUFu_TYPE(C)					\
+	first_of3(unsigned int, (C) <= UINT_MAX,		\
+		  unsigned long int, (C) <= ULONG_MAX,		\
+		  unsigned long long int, (C) <= ULLONG_MAX)
+#define C99_SUFl_TYPE(C)					\
+	first_of4(long int, (C) <= LONG_MAX,			\
+		  unsigned long int, (C) <= ULONG_MAX,		\
+		  long long int, (C) <= LLONG_MAX,		\
+		  unsigned long long int, (C) <= ULLONG_MAX)
+#define C99_SUFul_TYPE(C)					\
+	first_of2(unsigned long int, (C) <= ULONG_MAX,		\
+		  unsigned long long int, (C) <= ULLONG_MAX)
+#define C99_SUFll_TYPE(C)					\
+	first_of2(long long int, (C) <= LLONG_MAX,		\
+		  unsigned long long int, (C) <= ULLONG_MAX)
+
+/* Checks that constants have correct type.  */
+#define CHECK_UNSUF_TYPE(C)				\
+	ASSERT_CONST_TYPE((C), C99_UNSUF_TYPE((C)))
+#define CHECK_SUFu_TYPE(C) ASSERT_CONST_TYPE((C), C99_SUFu_TYPE((C)))
+#define CHECK_SUFl_TYPE(C)				\
+	ASSERT_CONST_TYPE((C), C99_SUFl_TYPE((C)))
+#define CHECK_SUFul_TYPE(C) ASSERT_CONST_TYPE((C), C99_SUFul_TYPE((C)))
+#define CHECK_SUFll_TYPE(C)				\
+	ASSERT_CONST_TYPE((C), C99_SUFll_TYPE((C)))
+#define CHECK_SUFull_TYPE(C) ASSERT_CONST_TYPE((C), unsigned long long int)
+
+/* Check an octal or hexadecimal value, with all suffixes.  */
+#define CHECK_CONST(C)                  	\
+	CHECK_UNSUF_TYPE(C);                    \
+	CHECK_SUFu_TYPE(C##u);			\
+	CHECK_SUFu_TYPE(C##U);			\
+	CHECK_SUFl_TYPE(C##l);                  \
+	CHECK_SUFl_TYPE(C##L);                  \
+	CHECK_SUFul_TYPE(C##ul);		\
+	CHECK_SUFul_TYPE(C##uL);		\
+	CHECK_SUFul_TYPE(C##Ul);		\
+	CHECK_SUFul_TYPE(C##UL);		\
+	CHECK_SUFll_TYPE(C##ll);		\
+	CHECK_SUFll_TYPE(C##LL);		\
+	CHECK_SUFull_TYPE(C##ull);		\
+	CHECK_SUFull_TYPE(C##uLL);		\
+	CHECK_SUFull_TYPE(C##Ull);		\
+	CHECK_SUFull_TYPE(C##ULL);
+
+#define CHECK_BIN_CONST(C)			\
+	CHECK_CONST(0b##C);                     \
+	CHECK_CONST(0B##C);
+
+/* True iff "long long" is at least B bits.  This presumes that (B-2)/3 is at
+   most 63.  */
+#define LLONG_AT_LEAST(B)			\
+	(LLONG_MAX >> ((B)-2)/3 >> ((B)-2)/3	\
+	 >> ((B)-2 - ((B)-2)/3 - ((B)-2)/3))
+
+#define LLONG_HAS_BITS(B) (LLONG_AT_LEAST((B)) && !LLONG_AT_LEAST((B) + 1))
+
+#define FOO 0b1101
+#if !FOO
+# error "preprocessor does not accept binary constants"
+#endif
+
+void
+foo (void)
+{
+  /* Check all 2^n and 2^n - 1 up to 2^72 - 1.  */
+  CHECK_BIN_CONST(1);
+  CHECK_BIN_CONST(10);
+  CHECK_BIN_CONST(11);
+  CHECK_BIN_CONST(100);
+  CHECK_BIN_CONST(111);
+  CHECK_BIN_CONST(1000);
+  CHECK_BIN_CONST(1111);
+  CHECK_BIN_CONST(10000);
+  CHECK_BIN_CONST(11111);
+  CHECK_BIN_CONST(100000);
+  CHECK_BIN_CONST(111111);
+  CHECK_BIN_CONST(1000000);
+  CHECK_BIN_CONST(1111111);
+  CHECK_BIN_CONST(10000000);
+  CHECK_BIN_CONST(11111111);
+  CHECK_BIN_CONST(100000000);
+  CHECK_BIN_CONST(111111111);
+  CHECK_BIN_CONST(1000000000);
+  CHECK_BIN_CONST(1111111111);
+  CHECK_BIN_CONST(10000000000);
+  CHECK_BIN_CONST(11111111111);
+  CHECK_BIN_CONST(100000000000);
+  CHECK_BIN_CONST(111111111111);
+  CHECK_BIN_CONST(1000000000000);
+  CHECK_BIN_CONST(1111111111111);
+  CHECK_BIN_CONST(10000000000000);
+  CHECK_BIN_CONST(11111111111111);
+  CHECK_BIN_CONST(100000000000000);
+  CHECK_BIN_CONST(111111111111111);
+  CHECK_BIN_CONST(1000000000000000);
+  CHECK_BIN_CONST(1111111111111111);
+  CHECK_BIN_CONST(10000000000000000);
+  CHECK_BIN_CONST(11111111111111111);
+  CHECK_BIN_CONST(100000000000000000);
+  CHECK_BIN_CONST(111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111);
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111111);
+#if LLONG_AT_LEAST(65)
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111111);
+#endif
+#if LLONG_AT_LEAST(66)
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111111);
+#endif
+#if LLONG_AT_LEAST(67)
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111111111);
+#endif
+#if LLONG_AT_LEAST(68)
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111111111);
+#endif
+#if LLONG_AT_LEAST(69)
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111111111);
+#endif
+#if LLONG_AT_LEAST(70)
+  CHECK_BIN_CONST(1000000000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(1111111111111111111111111111111111111111111111111111111111111111111111);
+#endif
+#if LLONG_AT_LEAST(71)
+  CHECK_BIN_CONST(10000000000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(11111111111111111111111111111111111111111111111111111111111111111111111);
+#endif
+#if LLONG_AT_LEAST(72)
+  CHECK_BIN_CONST(100000000000000000000000000000000000000000000000000000000000000000000000);
+  CHECK_BIN_CONST(111111111111111111111111111111111111111111111111111111111111111111111111);
+#endif
+}
+
Index: gcc/testsuite/gcc.dg/binary-constants-3.c
===================================================================
--- gcc/testsuite/gcc.dg/binary-constants-3.c	(revision 0)
+++ gcc/testsuite/gcc.dg/binary-constants-3.c	(revision 0)
@@ -0,0 +1,16 @@
+/* Test for binary integer constants: -pedantic-errors.  */
+
+/* Origin: Joerg Wunsch <j.gnu@uriah.heep.sax.de>.  */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+#define FOO 0b1101
+
+int
+foo (void)
+{
+#if FOO /* { dg-error "binary constants are a GCC extension" } */
+  return 23;
+#endif
+  return 0b1101; /* { dg-error "binary constants are a GCC extension" } */
+}
Index: gcc/testsuite/gcc.dg/binary-constants-2.c
===================================================================
--- gcc/testsuite/gcc.dg/binary-constants-2.c	(revision 0)
+++ gcc/testsuite/gcc.dg/binary-constants-2.c	(revision 0)
@@ -0,0 +1,16 @@
+/* Test for binary integer constants: -pedantic warnings.  */
+
+/* Origin: Joerg Wunsch <j.gnu@uriah.heep.sax.de>.  */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic" } */
+
+#define FOO 0b1101
+
+int
+foo (void)
+{
+#if FOO /* { dg-warning "binary constants are a GCC extension" } */
+  return 23;
+#endif
+  return 0b1101; /* { dg-warning "binary constants are a GCC extension" } */
+}
Index: gcc/testsuite/gcc.dg/binary-constants-4.c
===================================================================
--- gcc/testsuite/gcc.dg/binary-constants-4.c	(revision 0)
+++ gcc/testsuite/gcc.dg/binary-constants-4.c	(revision 0)
@@ -0,0 +1,18 @@
+/* Test for binary integer constants: random errors.  */
+
+/* Origin: Joerg Wunsch <j.gnu@uriah.heep.sax.de>.  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99" } */
+
+void
+foo(void)
+{
+  double d;
+  int i;
+
+  d = 0b1101;
+  d = 0b1101p1; /* { dg-error "invalid suffix \"p1\" on integer constant" } */
+  d = 0x1101p1;
+  i = 0b3011;   /* { dg-error "invalid suffix \"b3011\" on integer constant" } */
+  i = 0b113;    /* { dg-error "invalid digit \"3\" in binary constant" } */
+}
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp/include/cpplib.h	(revision 121711)
+++ libcpp/include/cpplib.h	(working copy)
@@ -744,6 +744,7 @@
 #define CPP_N_DECIMAL	0x0100
 #define CPP_N_HEX	0x0200
 #define CPP_N_OCTAL	0x0400
+#define CPP_N_BINARY	0x0800
 
 #define CPP_N_UNSIGNED	0x1000	/* Properties.  */
 #define CPP_N_IMAGINARY	0x2000
Index: libcpp/expr.c
===================================================================
--- libcpp/expr.c	(revision 121711)
+++ libcpp/expr.c	(working copy)
@@ -182,6 +182,11 @@
 	  radix = 16;
 	  str++;
 	}
+      else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
+	{
+	  radix = 2;
+	  str++;
+	}
     }
 
   /* Now scan for a well-formed integer or float.  */
@@ -220,10 +225,22 @@
     radix = 10;
 
   if (max_digit >= radix)
-    SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
+    {
+      if (radix == 2)
+	SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
+      else
+	SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
+    }
 
   if (float_flag != NOT_FLOAT)
     {
+      if (radix == 2)
+	{
+	  cpp_error (pfile, CPP_DL_ERROR,
+		     "invalid prefix \"0b\" for floating constant");
+	  return CPP_N_INVALID;
+	}
+
       if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
 	cpp_error (pfile, CPP_DL_PEDWARN,
 		   "use of C99 hexadecimal floating constant");
@@ -308,11 +325,16 @@
   if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
     cpp_error (pfile, CPP_DL_PEDWARN,
 	       "imaginary constants are a GCC extension");
+  if (radix == 2 && CPP_PEDANTIC (pfile))
+    cpp_error (pfile, CPP_DL_PEDWARN,
+	       "binary constants are a GCC extension");
 
   if (radix == 10)
     result |= CPP_N_DECIMAL;
   else if (radix == 16)
     result |= CPP_N_HEX;
+  else if (radix == 2)
+    result |= CPP_N_BINARY;
   else
     result |= CPP_N_OCTAL;
 
@@ -363,6 +385,11 @@
 	  base = 16;
 	  p += 2;
 	}
+      else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
+	{
+	  base = 2;
+	  p += 2;
+	}
 
       /* We can add a digit to numbers strictly less than this without
 	 needing the precision and slowness of double integers.  */
@@ -418,12 +445,25 @@
 append_digit (cpp_num num, int digit, int base, size_t precision)
 {
   cpp_num result;
-  unsigned int shift = 3 + (base == 16);
+  unsigned int shift;
   bool overflow;
   cpp_num_part add_high, add_low;
 
-  /* Multiply by 8 or 16.  Catching this overflow here means we don't
+  /* Multiply by 2, 8 or 16.  Catching this overflow here means we don't
      need to worry about add_high overflowing.  */
+  switch (base)
+    {
+    case 2:
+      shift = 1;
+      break;
+
+    case 16:
+      shift = 4;
+      break;
+
+    default:
+      shift = 3;
+    }
   overflow = !!(num.high >> (PART_PRECISION - shift));
   result.high = num.high << shift;
   result.low = num.low << shift;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20070312/809f0c88/attachment.sig>


More information about the Gcc-patches mailing list