Saleem Abdulrasool:  avoid hardcoding buffer lengths.

llvm-svn: 171169
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 80a11413bb95bd214a2e452ac29d9120b2cff27b
diff --git a/src/hash.cpp b/src/hash.cpp
index d785f1a..a013500 100644
--- a/src/hash.cpp
+++ b/src/hash.cpp
@@ -12,7 +12,6 @@
 #include "stdexcept"
 #include "type_traits"
 
-#pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
 
 _LIBCPP_BEGIN_NAMESPACE_STD
diff --git a/src/locale.cpp b/src/locale.cpp
index e75f27f..34afe8c 100644
--- a/src/locale.cpp
+++ b/src/locale.cpp
@@ -81,6 +81,24 @@
     return *(T*)&buf;
 }
 
+template <typename T, size_t N>
+_LIBCPP_ALWAYS_INLINE
+_LIBCPP_CONSTEXPR
+size_t
+countof(const T (&)[N])
+{
+    return N;
+}
+
+template <typename T>
+_LIBCPP_ALWAYS_INLINE
+_LIBCPP_CONSTEXPR
+size_t
+countof(const T * const begin, const T * const end)
+{
+    return static_cast<size_t>(end - begin);
+}
+
 }
 
 const locale::category locale::none;
@@ -4597,7 +4615,7 @@
     char f[3] = {0};
     f[0] = '%';
     f[1] = fmt;
-    size_t n = strftime_l(buf, 100, f, &t, __loc_);
+    size_t n = strftime_l(buf, countof(buf), f, &t, __loc_);
     char* bb = buf;
     char* be = buf + n;
     string result;
@@ -4743,15 +4761,15 @@
     char f[3] = {0};
     f[0] = '%';
     f[1] = fmt;
-    strftime_l(buf, 100, f, &t, __loc_);
+    strftime_l(buf, countof(buf), f, &t, __loc_);
     wchar_t wbuf[100];
     wchar_t* wbb = wbuf;
     mbstate_t mb = {0};
     const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
 #else
-    size_t j = __mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = __mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -4889,26 +4907,26 @@
     for (int i = 0; i < 7; ++i)
     {
         t.tm_wday = i;
-        strftime_l(buf, 100, "%A", &t, __loc_);
+        strftime_l(buf, countof(buf), "%A", &t, __loc_);
         __weeks_[i] = buf;
-        strftime_l(buf, 100, "%a", &t, __loc_);
+        strftime_l(buf, countof(buf), "%a", &t, __loc_);
         __weeks_[i+7] = buf;
     }
     // __months_
     for (int i = 0; i < 12; ++i)
     {
         t.tm_mon = i;
-        strftime_l(buf, 100, "%B", &t, __loc_);
+        strftime_l(buf, countof(buf), "%B", &t, __loc_);
         __months_[i] = buf;
-        strftime_l(buf, 100, "%b", &t, __loc_);
+        strftime_l(buf, countof(buf), "%b", &t, __loc_);
         __months_[i+12] = buf;
     }
     // __am_pm_
     t.tm_hour = 1;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     __am_pm_[0] = buf;
     t.tm_hour = 13;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     __am_pm_[1] = buf;
     __c_ = __analyze('c', ct);
     __r_ = __analyze('r', ct);
@@ -4929,25 +4947,25 @@
     for (int i = 0; i < 7; ++i)
     {
         t.tm_wday = i;
-        strftime_l(buf, 100, "%A", &t, __loc_);
+        strftime_l(buf, countof(buf), "%A", &t, __loc_);
         mb = mbstate_t();
         const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
         __weeks_[i].assign(wbuf, wbe);
-        strftime_l(buf, 100, "%a", &t, __loc_);
+        strftime_l(buf, countof(buf), "%a", &t, __loc_);
         mb = mbstate_t();
         bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -4958,25 +4976,25 @@
     for (int i = 0; i < 12; ++i)
     {
         t.tm_mon = i;
-        strftime_l(buf, 100, "%B", &t, __loc_);
+        strftime_l(buf, countof(buf), "%B", &t, __loc_);
         mb = mbstate_t();
         const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
         __months_[i].assign(wbuf, wbe);
-        strftime_l(buf, 100, "%b", &t, __loc_);
+        strftime_l(buf, countof(buf), "%b", &t, __loc_);
         mb = mbstate_t();
         bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -4985,26 +5003,26 @@
     }
     // __am_pm_
     t.tm_hour = 1;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     mb = mbstate_t();
     const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-    size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
     wbe = wbuf + j;
     __am_pm_[0].assign(wbuf, wbe);
     t.tm_hour = 13;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     mb = mbstate_t();
     bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-    j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5267,7 +5285,7 @@
     char fmt[] = {'%', __fmt, __mod, 0};
     if (__mod != 0)
         swap(fmt[1], fmt[2]);
-    size_t n = strftime_l(__nb, static_cast<size_t>(__ne-__nb), fmt, __tm, __loc_);
+    size_t n = strftime_l(__nb, countof(__nb, __ne), fmt, __tm, __loc_);
     __ne = __nb + n;
 }
 
@@ -5281,9 +5299,9 @@
     mbstate_t mb = {0};
     const char* __nb = __nar;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
+    size_t j = mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
 #else
-    size_t j = __mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
+    size_t j = __mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5806,9 +5824,9 @@
     mbstate_t mb = {0};
     const char* bb = lc->currency_symbol;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-    size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5825,9 +5843,9 @@
         mb = mbstate_t();
         bb = lc->positive_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -5841,9 +5859,9 @@
         mb = mbstate_t();
         bb = lc->negative_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -5889,9 +5907,9 @@
     mbstate_t mb = {0};
     const char* bb = lc->int_curr_symbol;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-    size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5912,9 +5930,9 @@
         mb = mbstate_t();
         bb = lc->positive_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -5932,9 +5950,9 @@
         mb = mbstate_t();
         bb = lc->negative_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");