Implement a __WCHAR_UNSIGNED__ macro and use it to include WCHAR_MIN and
WCHAR_MAX in limits.h, thus solving the problem where the system header
thinks it knows better.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135455 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/InitPreprocessor.cpp b/lib/Frontend/InitPreprocessor.cpp
index 9428cd5..77a1b3f 100644
--- a/lib/Frontend/InitPreprocessor.cpp
+++ b/lib/Frontend/InitPreprocessor.cpp
@@ -554,6 +554,9 @@
   if (!TargetInfo::isTypeSigned(TI.getWIntType()))
     Builder.defineMacro("__WINT_UNSIGNED__");
 
+  if (!TargetInfo::isTypeSigned(TI.getWCharType()))
+    Builder.defineMacro("__WCHAR_UNSIGNED__");
+
   // Define exact-width integer types for stdint.h
   Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__",
                       "char");
diff --git a/lib/Headers/limits.h b/lib/Headers/limits.h
index ecd09a4..6683a2d 100644
--- a/lib/Headers/limits.h
+++ b/lib/Headers/limits.h
@@ -52,6 +52,8 @@
 #undef  LONG_MIN
 #undef  LONG_MAX
 #undef  ULONG_MAX
+#undef  WCHAR_MIN
+#undef  WCHAR_MAX
 
 #undef  CHAR_BIT
 #undef  CHAR_MIN
@@ -62,6 +64,7 @@
 #define SHRT_MAX  __SHRT_MAX__
 #define INT_MAX   __INT_MAX__
 #define LONG_MAX  __LONG_MAX__
+#define WCHAR_MAX __WCHAR_MAX__
 
 #define SCHAR_MIN (-__SCHAR_MAX__-1)
 #define SHRT_MIN  (-__SHRT_MAX__ -1)
@@ -87,6 +90,17 @@
 #define CHAR_MAX __SCHAR_MAX__
 #endif
 
+/* C++ or C99: Added wchar_t */
+#if defined(__cplusplus) || __STDC_VERSION__ >= 199901
+
+#ifdef __WCHAR_UNSIGNED__
+#define WCHAR_MIN 0
+#else
+#define WCHAR_MIN (-__WCHAR_MAX__-1)
+#endif
+
+#endif
+
 /* C99 5.2.4.2.1: Added long long. */
 #if __STDC_VERSION__ >= 199901