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
diff --git a/libc/include/machine/wchar_limits.h b/libc/include/machine/wchar_limits.h
new file mode 100644
index 0000000..94cbd7e
--- /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 1f3d003..f34843c 100644
--- a/libc/include/stdint.h
+++ b/libc/include/stdint.h
@@ -30,6 +30,7 @@
 #define _STDINT_H
 
 #include <stddef.h>
+#include <machine/wchar_limits.h>
 
 typedef __signed char __int8_t;
 typedef unsigned char __uint8_t;
@@ -86,7 +87,7 @@
 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 @@
 #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 @@
 #define SIG_ATOMIC_MAX   INT32_MAX
 #define SIG_ATOMIC_MIN   INT32_MIN
 
-#ifndef WCHAR_MAX /* These might also have been defined by <wchar.h>. */
-#  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 04818b9..0029a65 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -34,7 +34,8 @@
 #include <stdarg.h>
 #include <stddef.h>
 #include <time.h>
-#include <malloc.h>
+
+#include <machine/wchar_limits.h>
 
 __BEGIN_DECLS
 
@@ -58,11 +59,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);
diff --git a/tests/wchar_test.cpp b/tests/wchar_test.cpp
index d5d27ed..be451c1 100644
--- a/tests/wchar_test.cpp
+++ b/tests/wchar_test.cpp
@@ -155,3 +155,7 @@
   bytes[3] = 0;
   EXPECT_STREQ("hix", bytes);
 }
+
+TEST(wchar, limits) {
+  ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
+}