Index: include/__config
===================================================================
--- include/__config	(revision 189354)
+++ include/__config	(working copy)
@@ -421,7 +421,7 @@
 #define _ALIGNAS(x) __declspec(align(x))
 #define _LIBCPP_HAS_NO_VARIADICS
 
-#define _NOEXCEPT throw()
+#define _NOEXCEPT throw ()
 #define _NOEXCEPT_(x)
 
 #define _LIBCPP_BEGIN_NAMESPACE_STD namespace std {
Index: include/algorithm
===================================================================
--- include/algorithm	(revision 189354)
+++ include/algorithm	(working copy)
@@ -631,6 +631,9 @@
 #if defined(__IBMCPP__)
 #include "support/ibm/support.h"
 #endif
+#if defined(_LIBCPP_MSVCRT)
+#include "support/win32/support.h" // __builtin
+#endif
 
 #include <__undef_min_max>
 
Index: include/cmath
===================================================================
--- include/cmath	(revision 189354)
+++ include/cmath	(working copy)
@@ -767,7 +767,7 @@
 #endif
 
 template <class _A1>
-inline _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY
 typename enable_if<is_integral<_A1>::value, double>::type
 cos(_A1 __x) _NOEXCEPT {return cos((double)__x);}
 
@@ -1680,11 +1680,8 @@
 using ::tgammal;
 using ::truncl;
 #endif // !_LIBCPP_MSVCRT
-
-#else 
 using ::lgamma;
 using ::lgammaf;
 #endif // __sun__
 _LIBCPP_END_NAMESPACE_STD
-
 #endif  // _LIBCPP_CMATH
Index: include/cstdio
===================================================================
--- include/cstdio	(revision 189354)
+++ include/cstdio	(working copy)
@@ -135,7 +135,9 @@
 using ::fscanf;
 using ::printf;
 using ::scanf;
+#ifndef _LIBCPP_MSVCRT
 using ::snprintf;
+#endif
 using ::sprintf;
 using ::sscanf;
 #ifndef _LIBCPP_MSVCRT
Index: include/cstdlib
===================================================================
--- include/cstdlib	(revision 189354)
+++ include/cstdlib	(working copy)
@@ -85,7 +85,7 @@
 #include <__config>
 #include <stdlib.h>
 #ifdef _LIBCPP_MSVCRT
-#include "support/win32/locale_win32.h"
+#include "support/win32/support.h" // _Exit etc.
 #endif // _LIBCPP_MSVCRT
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Index: include/support/win32/limits_win32.h
===================================================================
--- include/support/win32/limits_win32.h	(revision 189354)
+++ include/support/win32/limits_win32.h	(working copy)
@@ -11,17 +11,19 @@
 #ifndef _LIBCPP_SUPPORT_WIN32_LIMITS_WIN32_H
 #define _LIBCPP_SUPPORT_WIN32_LIMITS_WIN32_H
 
+#if !defined(_MSC_VER) || defined(__clang__)
+#pragma GCC system_header
+#endif
+
 #if !defined(_LIBCPP_MSVCRT)
 #error "This header complements Microsoft's C Runtime library, and should not be included otherwise."
 #else
 
-#ifndef NOMINMAX
-#define NOMINMAX
-#endif
-#include <windows.h> // ymath.h works correctly
+// These are built-in's for clang etc. Provide them for the genuine ms compiler.
+#if defined(_LIBCPP_MSVC)
+#include <float.h> // FLT_ etc.
+#include <limits.h> // CHAR_BIT
 
-#include <float.h> // limit constants
-
 #define __FLT_MANT_DIG__   FLT_MANT_DIG
 #define __FLT_DIG__        FLT_DIG
 #define __FLT_RADIX__      FLT_RADIX
@@ -60,7 +62,7 @@
 #define __LDBL_EPSILON__    LDBL_EPSILON
 // predefined by MinGW GCC
 #define __LDBL_DENORM_MIN__ 3.64519953188247460253e-4951L
-
+#define __CHAR_BIT__        CHAR_BIT
 // __builtin replacements/workarounds
 #include <math.h> // HUGE_VAL
 #include <ymath.h> // internal MSVC header providing the needed functionality
@@ -74,6 +76,6 @@
 #define __builtin_nansf(__dummy) _FSnan._Float
 #define __builtin_nansl(__dummy) _LSnan._Long_double
 
+#endif // _LIBCPP_MSVC
 #endif // _LIBCPP_MSVCRT
-
 #endif // _LIBCPP_SUPPORT_WIN32_LIMITS_WIN32_H
Index: include/support/win32/locale_win32.h
===================================================================
--- include/support/win32/locale_win32.h	(revision 189354)
+++ include/support/win32/locale_win32.h	(working copy)
@@ -11,10 +11,16 @@
 #ifndef _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
 #define _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
 
+#if !defined(_MSC_VER) || defined(__clang__)
+#pragma GCC system_header
+#endif
+
+#if !defined(_LIBCPP_MSVCRT)
+#error "This header complements Microsoft's C Runtime library, and should not be included otherwise."
+#else
 // ctype mask table defined in msvcrt.dll
 extern "C" unsigned short  __declspec(dllimport) _ctype[];
 
-#include "support/win32/support.h"
 #include <memory>
 #include <xlocinfo.h> // _locale_t
 #define locale_t _locale_t
@@ -30,23 +36,23 @@
                      | LC_MONETARY_MASK \
                      | LC_NUMERIC_MASK \
                      | LC_TIME_MASK )
-#define freelocale _free_locale
+inline void freelocale(locale_t __l) { _free_locale(__l); }
 // FIXME: base currently unused. Needs manual work to construct the new locale
 locale_t newlocale( int mask, const char * locale, locale_t base );
 locale_t uselocale( locale_t newloc );
 lconv *localeconv_l( locale_t loc );
-size_t mbrlen_l( const char *__restrict__ s, size_t n,
-                 mbstate_t *__restrict__ ps, locale_t loc);
-size_t mbsrtowcs_l( wchar_t *__restrict__ dst, const char **__restrict__ src,
-                    size_t len, mbstate_t *__restrict__ ps, locale_t loc );
-size_t wcrtomb_l( char *__restrict__ s, wchar_t wc, mbstate_t *__restrict__ ps,
+size_t mbrlen_l( const char *__restrict s, size_t n,
+                 mbstate_t *__restrict ps, locale_t loc);
+size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
+                    size_t len, mbstate_t *__restrict ps, locale_t loc );
+size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps,
                   locale_t loc);
-size_t mbrtowc_l( wchar_t *__restrict__ pwc, const char *__restrict__ s,
-                  size_t n, mbstate_t *__restrict__ ps, locale_t loc);
-size_t mbsnrtowcs_l( wchar_t *__restrict__ dst, const char **__restrict__ src,
-                     size_t nms, size_t len, mbstate_t *__restrict__ ps, locale_t loc);
-size_t wcsnrtombs_l( char *__restrict__ dst, const wchar_t **__restrict__ src,
-                     size_t nwc, size_t len, mbstate_t *__restrict__ ps, locale_t loc);
+size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s,
+                  size_t n, mbstate_t *__restrict ps, locale_t loc);
+size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
+                     size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc);
+size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src,
+                     size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc);
 wint_t btowc_l( int c, locale_t loc );
 int wctob_l( wint_t c, locale_t loc );
 typedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
@@ -59,7 +65,6 @@
 }
 
 // the *_l functions are prefixed on Windows, only available for msvcr80+, VS2005+
-#include <stdio.h>
 #define mbtowc_l _mbtowc_l
 #define strtoll_l _strtoi64_l
 #define strtoull_l _strtoui64_l
@@ -70,7 +75,7 @@
 int
 islower_l(int c, _locale_t loc)
 {
- return _islower_l((int)c, loc);
+    return _islower_l((int)c, loc);
 }
 
 inline _LIBCPP_INLINE_VISIBILITY
@@ -77,7 +82,7 @@
 int
 isupper_l(int c, _locale_t loc)
 {
- return _isupper_l((int)c, loc);
+    return _isupper_l((int)c, loc);
 }
 
 #define isdigit_l _isdigit_l
@@ -126,4 +131,5 @@
 inline int iswblank( wint_t c, locale_t /*loc*/ )
 { return ( c == L' ' || c == L'\t' ); }
 #endif // _MSC_VER
+#endif // _LIBCPP_MSVCRT
 #endif // _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
Index: include/support/win32/math_win32.h
===================================================================
--- include/support/win32/math_win32.h	(revision 189354)
+++ include/support/win32/math_win32.h	(working copy)
@@ -11,11 +11,16 @@
 #ifndef _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H
 #define _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H
 
+#if !defined(_MSC_VER) || defined(__clang__)
+#pragma GCC system_header
+#endif
+
 #if !defined(_LIBCPP_MSVCRT)
 #error "This header complements Microsoft's C Runtime library, and should not be included otherwise."
 #else
 
-#include <math.h>
+#if defined(_LIBCPP_MSVC)
+#include <float.h>
 
 typedef float float_t;
 typedef double double_t;
@@ -72,6 +77,7 @@
 {
     return isnan(x) || isnan(y);
 }
+
 _LIBCPP_ALWAYS_INLINE bool signbit( double num )
 {
     switch(_fpclass(num))
@@ -91,6 +97,7 @@
     }
     return false;
 }
+
 _LIBCPP_ALWAYS_INLINE float copysignf( float x, float y )
 {
     return (signbit (x) != signbit (y) ? - x : x);
@@ -103,11 +110,11 @@
 {
     return ::_copysignl(x,y);
 }
+
 _LIBCPP_ALWAYS_INLINE int fpclassify( double num )
 {
     return _fpclass(num);
 }
-
+#endif // _LIBCPP_MSVC
 #endif // _LIBCPP_MSVCRT
-
 #endif // _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H
Index: include/support/win32/support.h
===================================================================
--- include/support/win32/support.h	(revision 189354)
+++ include/support/win32/support.h	(working copy)
@@ -15,10 +15,24 @@
    Functions and constants used in libc++ that are missing from the Windows C library.
   */
 
-#include <cwchar>  // mbstate_t
+#if !defined(_MSC_VER) || defined(__clang__)
+#pragma GCC system_header
+#endif
+
+#if !defined(_LIBCPP_MSVCRT)
+#error "This header complements Microsoft's C Runtime library, and should not be included otherwise."
+#else
+
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+#include <windows.h> // DWORD etc.
+#include <intrin.h> // BitScanForward64 (requires 64 bit compiler)
+#if defined(_LIBCPP_MSVC)
+#include <xlocinfo.h> // _Stold etc.
+#endif
+#include <wchar.h>  // mbstate_t
 #include <cstdarg> // va_ macros
-#define swprintf _snwprintf
-#define vswprintf _vsnwprintf
 
 extern "C" {
 
@@ -30,9 +44,11 @@
                    size_t nwc, size_t len, mbstate_t *__restrict ps );
 }
 
-#if defined(_LIBCPP_MSVCRT)
+// MSVC doesn't have these.
+#if defined(_LIBCPP_MSVC)
+#define swprintf _snwprintf
+#define vswprintf _vsnwprintf
 #define snprintf _snprintf
-#include <xlocinfo.h>
 #define atoll _atoi64
 #define strtoll _strtoi64
 #define strtoull _strtoui64
@@ -44,12 +60,8 @@
 { return _Stod(nptr, endptr, 0); }
 _LIBCPP_ALWAYS_INLINE long double strtold( const char *nptr, char **endptr )
 { return _Stold(nptr, endptr, 0); }
+inline void _Exit( int status ) { _exit(status); }
 
-#define _Exit _exit
-
-#ifndef __clang__ // MSVC-based Clang also defines _MSC_VER
-#include <intrin.h>
-
 _LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) {
    static const unsigned int m1 = 0x55555555; //binary: 0101...
    static const unsigned int m2 = 0x33333333; //binary: 00110011..
@@ -58,7 +70,7 @@
    x -= (x >> 1) & m1;             //put count of each 2 bits into those 2 bits
    x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
    x = (x + (x >> 4)) & m4;        //put count of each 8 bits into those 8 bits
-   return (x * h01) >> 24;  //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24)
+   return static_cast<int>((x * h01) >> 24);  //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24)
 }
 
 _LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) {
@@ -105,10 +117,10 @@
 _LIBCPP_ALWAYS_INLINE int __builtin_clzll( unsigned long long x )
 {
     DWORD r = 0;
-    _BitScanForward64(&r, x);
+    _BitScanForward64(&r, x); // Note intrinsic only available on 64 bit compiler.
     return static_cast<int>(r);
 }
-#endif // !__clang__
+#endif // _LIBCPP_MSVC
 #endif // _LIBCPP_MSVCRT
+#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H
 
-#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H
