From 83c07b5e50207458445262ed10b79aa6b2d2d0ce Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 21 Apr 2014 18:09:46 -0700 Subject: [PATCH] Fix WCHAR_MAX, WCHAR_MIN, WINT_MAX, and WINT_MIN. GCC tells us everything we need to know. clang does its usual half-assed job. Change-Id: Id4d664529b10345274602768cd564d3df717e931 --- libc/include/machine/wchar_limits.h | 42 +++++++++++++++++++++++++++++ libc/include/stdint.h | 19 ++++++------- libc/include/wchar.h | 8 ++---- tests/wchar_test.cpp | 4 +++ 4 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 libc/include/machine/wchar_limits.h diff --git a/libc/include/machine/wchar_limits.h b/libc/include/machine/wchar_limits.h new file mode 100644 index 000000000..94cbd7ec5 --- /dev/null +++ b/libc/include/machine/wchar_limits.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _MACHINE_WCHAR_LIMITS_H_ +#define _MACHINE_WCHAR_LIMITS_H_ + +/* Both GCC and clang define __WCHAR_MAX__. */ +#define WCHAR_MAX __WCHAR_MAX__ + +/* As of 3.4, clang still doesn't define __WCHAR_MIN__. */ +#if defined(__WCHAR_UNSIGNED__) +# define WCHAR_MIN L'\0' +#else +# define WCHAR_MIN (-(WCHAR_MAX) - 1) +#endif + +#endif /* _MACHINE_WCHAR_LIMITS_H_ */ diff --git a/libc/include/stdint.h b/libc/include/stdint.h index 1f3d003d1..f34843c09 100644 --- a/libc/include/stdint.h +++ b/libc/include/stdint.h @@ -30,6 +30,7 @@ #define _STDINT_H #include +#include typedef __signed char __int8_t; typedef unsigned char __uint8_t; @@ -86,7 +87,7 @@ typedef uint8_t uint_fast8_t; typedef int64_t int_fast64_t; typedef uint64_t uint_fast64_t; -#ifdef __LP64__ +#if defined(__LP64__) typedef int64_t int_fast16_t; typedef uint64_t uint_fast16_t; typedef int64_t int_fast32_t; @@ -135,7 +136,7 @@ typedef int64_t intmax_t; #define INTMAX_C(c) INT64_C(c) #define UINTMAX_C(c) UINT64_C(c) -#ifdef __LP64__ +#if defined(__LP64__) # define INT64_C(c) c ## L # define UINT64_C(c) c ## UL # define INTPTR_C(c) INT64_C(c) @@ -200,15 +201,15 @@ typedef int64_t intmax_t; #define SIG_ATOMIC_MAX INT32_MAX #define SIG_ATOMIC_MIN INT32_MIN -#ifndef WCHAR_MAX /* These might also have been defined by . */ -# define WCHAR_MAX INT32_MAX -# define WCHAR_MIN INT32_MIN +#if defined(__WINT_UNSIGNED__) +# define WINT_MAX UINT32_MAX +# define WINT_MIN UINT32_MIN +#else +# define WINT_MAX INT32_MAX +# define WINT_MIN INT32_MIN #endif -#define WINT_MAX INT32_MAX -#define WINT_MIN INT32_MIN - -#ifdef __LP64__ +#if defined(__LP64__) # define INTPTR_MIN INT64_MIN # define INTPTR_MAX INT64_MAX # define UINTPTR_MAX UINT64_MAX diff --git a/libc/include/wchar.h b/libc/include/wchar.h index 04818b953..0029a65be 100644 --- a/libc/include/wchar.h +++ b/libc/include/wchar.h @@ -34,7 +34,8 @@ #include #include #include -#include + +#include __BEGIN_DECLS @@ -58,11 +59,6 @@ typedef enum { WC_TYPE_MAX } wctype_t; -#ifndef WCHAR_MAX -#define WCHAR_MAX INT_MAX -#define WCHAR_MIN INT_MIN -#endif - #define WEOF ((wint_t)(-1)) extern wint_t btowc(int); diff --git a/tests/wchar_test.cpp b/tests/wchar_test.cpp index d5d27ed75..be451c1b4 100644 --- a/tests/wchar_test.cpp +++ b/tests/wchar_test.cpp @@ -155,3 +155,7 @@ TEST(wchar, wcstombs_wcrtombs) { bytes[3] = 0; EXPECT_STREQ("hix", bytes); } + +TEST(wchar, limits) { + ASSERT_LT(WCHAR_MIN, WCHAR_MAX); +}