Merge "ndk: Fix WCHAR_MIN / WCHAR_MAX definitions."
diff --git a/ndk/platforms/android-3/include/stdint.h b/ndk/platforms/android-3/include/stdint.h
index 54ea026..44613d5 100644
--- a/ndk/platforms/android-3/include/stdint.h
+++ b/ndk/platforms/android-3/include/stdint.h
@@ -234,11 +234,7 @@
/* Limits of wchar_t. */
#ifdef __STDINT_LIMITS
- /* Also possibly defined in <wchar.h> */
-# ifndef WCHAR_MIN
-# define WCHAR_MIN INT32_MIN
-# define WCHAR_MAX INT32_MAX
-# endif
+#include <sys/_wchar_limits.h>
#endif
/* Limits of wint_t. */
diff --git a/ndk/platforms/android-3/include/sys/_wchar_limits.h b/ndk/platforms/android-3/include/sys/_wchar_limits.h
new file mode 100644
index 0000000..644792f
--- /dev/null
+++ b/ndk/platforms/android-3/include/sys/_wchar_limits.h
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2013 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 _SYS__WCHAR_LIMITS_H
+#define _SYS__WCHAR_LIMITS_H
+
+#include <android/api-level.h>
+
+/* WCHAR_MIN / WCHAR_MAX can be defined by <stdint.h> or <wchar.h>.
+ * Due to historical reasons, their definition is a bit complex.
+ *
+ * - In NDK r8e and older, all definitions of WCHAR_MIN and WCHAR_MAX
+ * where 32-bit signed values (with one exception described below),
+ * despite the fact that wchar_t is 'unsigned' on ARM.
+ * See http://b.android.com/57749
+ *
+ * This is no longer the case, unless you define _WCHAR_IS_ALWAYS_SIGNED
+ * at compile time to restore the old (broken) behaviour. This doesn't
+ * affect other CPU ABIs.
+ *
+ * - Before API level 9, on ARM, wchar_t was typedef to 'char' when
+ * compiling C (not C++). Also, the definitions of WCHAR_MIN and
+ * WCHAR_MAX differed between <stdint.h> and <wchar.h>:
+ *
+ * <stdint.h> conditionally defined them to INT32_MIN / INT32_MAX.
+ * <wchar.h> conditionally defined them to 0 and 255 instead.
+ *
+ * <stdint.h> would only define WCHAR_MIN and WCHAR_MAX when:
+ * - Compiling C sources.
+ * - Compiling C++ sources with __STDC_LIMIT_MACROS being defined.
+ *
+ * <wchar.h> always ends up including <stdint.h> indirectly. This
+ * means that:
+ *
+ * - When compiling C sources, WCHAR_MIN / WCHAR_MAX were always
+ * defined as INT32_MIN / INT32_MAX.
+ *
+ * - When compiling C++ sources with __STDC_LIMIT_MACROS defined,
+ * they were always defined to INT32_MIN / INT32_MAX
+ *
+ * - When compiling C++ sources without __STDC_LIMIT_MACROS defined,
+ * they were defined by <wchar.h> as 0 and 255, respectively.
+ *
+ * Keep in mind that this was ARM-specific, only for API level < 9.
+ *
+ * If _WCHAR_IS_8BIT is defined, the same broken behaviour will
+ * be restored. See http://b.android.com/57267
+ */
+#if !defined(WCHAR_MIN)
+
+# if defined(_WCHAR_IS_8BIT) && defined(__arm__) && __ANDROID_API__ < 9
+# if defined(__cplusplus) && !defined(__STDC_LIMIT_MACROS)
+# define WCHAR_MIN 0
+# define WCHAR_MAX 255
+# else
+# define WCHAR_MIN (-2147483647 - 1)
+# define WCHAR_MAX (2147483647)
+# endif
+# elif defined(_WCHAR_IS_ALWAYS_SIGNED)
+# define WCHAR_MIN (-2147483647 - 1)
+# define WCHAR_MAX (2147483647)
+# else
+ /* Otherwise, the value is derived from the toolchain configuration.
+ * to avoid putting explicit CPU checks in this header. */
+# ifndef __WCHAR_MAX__
+# error "__WCHAR_MAX__ is not defined. Check your toolchain!"
+# endif
+ /* Clang does define __WCHAR_MAX__, but not __WCHAR_MIN__ */
+# ifndef __WCHAR_MIN__
+# if __WCHAR_MAX__ == 4294967295
+# define __WCHAR_MIN__ (0U)
+# elif __WCHAR_MAX__ == 2147483647
+# define __WCHAR_MIN__ (-2147483647 - 1)
+# else
+# error "Invalid __WCHAR_MAX__ value. Check your toolchain!"
+# endif
+# endif /* !__WCHAR_MIN__ */
+# define WCHAR_MIN __WCHAR_MIN__
+# define WCHAR_MAX __WCHAR_MAX__
+# endif /* !_WCHAR_IS_ALWAYS_SIGNED */
+
+#endif /* !WCHAR_MIN */
+
+#endif /* _SYS__WCHAR_LIMITS_H */
diff --git a/ndk/platforms/android-3/include/wchar.h b/ndk/platforms/android-3/include/wchar.h
index 836ad30..575da4d 100644
--- a/ndk/platforms/android-3/include/wchar.h
+++ b/ndk/platforms/android-3/include/wchar.h
@@ -40,6 +40,7 @@
#include <malloc.h>
#include <stddef.h>
+#include <sys/_wchar_limits.h>
/* IMPORTANT: Any code that relies on wide character support is essentially
* non-portable and/or broken. the only reason this header exist
@@ -70,36 +71,10 @@
WC_TYPE_MAX
} wctype_t;
-/* TECHNICAL NOTE: This is tricky!
- *
- * Due to the following inclusion chain:
- * <wchar.h> -> <stdio.h> -> <sys/types.h> -> <stdint.h>
- *
- * WCHAR_MIN / WCHAR_MAX will already be defined to INT32_MIN / INT32_MAX
- * when reaching this line in the following cases:
- * - Compiling C source code.
- * - Compiling C++ source code AND having __STDC_LIMIT_MACROS defined.
- *
- * When _WCHAR_IS_8BIT is defined, it should emulate the old behaviour.
- * which was to (conditionally) set the values to 0 and 255, respectively.
- */
-#ifndef WCHAR_MAX
-# ifdef _WCHAR_IS_8BIT
-# define WCHAR_MAX 255
-# define WCHAR_MIN 0
-# else
-/* Same values as INT32_MIN/INT32_MAX, without including <stdint.h> */
-# define WCHAR_MAX (2147483647)
-# define WCHAR_MIN (-1-2147483647)
-# endif
-#endif
-
-/* Similarly, WEOF used to be defined as simply -1, which is
+/* WEOF used to be defined as simply -1, which is
* invalid (the standard mandates that the expression must have wint_t
- * type). There is no difference in C, but there is one in C++!!
- *
- * Revert to the old broken behaviour is _WCHAR_IS_8BIT is defined.
- */
+ * type). Revert to the old broken behaviour is _WCHAR_IS_8BIT is defined.
+ * See http://b.android.com/57267 */
#ifdef _WCHAR_IS_8BIT
#define WEOF (-1)
#else
diff --git a/ndk/platforms/android-8/include/wchar.h b/ndk/platforms/android-8/include/wchar.h
index e67b0be..453b6d5 100644
--- a/ndk/platforms/android-8/include/wchar.h
+++ b/ndk/platforms/android-8/include/wchar.h
@@ -40,6 +40,7 @@
#include <malloc.h>
#include <stddef.h>
+#include <sys/_wchar_limits.h>
/* IMPORTANT: Any code that relies on wide character support is essentially
* non-portable and/or broken. the only reason this header exist
@@ -70,36 +71,15 @@
WC_TYPE_MAX
} wctype_t;
-/* TECHNICAL NOTE: This is tricky!
- *
- * Due to the following inclusion chain:
- * <wchar.h> -> <stdio.h> -> <sys/types.h> -> <stdint.h>
- *
- * WCHAR_MIN / WCHAR_MAX will already be defined to INT32_MIN / INT32_MAX
- * when reaching this line in the following cases:
- * - Compiling C source code.
- * - Compiling C++ source code AND having __STDC_LIMIT_MACROS defined.
- *
- * When _WCHAR_IS_8BIT is defined, it should emulate the old behaviour.
- * which was to set the values to 0 and 255, respectively.
- */
-#ifndef WCHAR_MAX
-# ifdef _WCHAR_IS_8BIT
-# define WCHAR_MAX 255
-# define WCHAR_MIN 0
-# else
-/* Same values as INT32_MIN/INT32_MAX, without including <stdint.h> */
-# define WCHAR_MAX (2147483647)
-# define WCHAR_MIN (-1-2147483647)
-# endif
-#endif
-
-/* Similarly, WEOF used to be defined as simply -1, which is
+/* WEOF used to be defined as simply -1, which is
* invalid (the standard mandates that the expression must have wint_t
- * type). There is no difference in C, but there is one in C++!!
- *
- * Revert to the old broken behaviour is _WCHAR_IS_8BIT is defined.
- */
+ * type). Revert to the old broken behaviour is _WCHAR_IS_8BIT is defined.
+ * See http://b.android.com/57267 */
+#ifdef _WCHAR_IS_8BIT
+#define WEOF (-1)
+#else
+#define WEOF ((wint_t)-1)
+#endif
#ifdef _WCHAR_IS_8BIT
#define WEOF (-1)
#else
diff --git a/ndk/platforms/android-9/include/wchar.h b/ndk/platforms/android-9/include/wchar.h
index 437f52b..806e3d1 100644
--- a/ndk/platforms/android-9/include/wchar.h
+++ b/ndk/platforms/android-9/include/wchar.h
@@ -41,13 +41,7 @@
#include <malloc.h>
#include <stddef.h>
-
-/* IMPORTANT: Any code that relies on wide character support is essentially
- * non-portable and/or broken. the only reason this header exist
- * is because I'm really a nice guy. However, I'm not nice enough
- * to provide you with a real implementation. instead wchar_t == char
- * and all wc functions are stubs to their "normal" equivalent...
- */
+#include <sys/_wchar_limits.h>
__BEGIN_DECLS
@@ -71,11 +65,6 @@
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);