[PATCH] D39149: [libc++] Prevent tautological comparisons

Shoaib Meenai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 20 16:29:58 PDT 2017


smeenai created this revision.

Tip-of-tree clang produces `-Wtautological-constant-compare` for
tautological constant comparisons, and these pieces of code will trigger
that diagnostic when `int` and `long` have the same size (for example,
when compiling for a 32-bit target, or for Windows 64-bit).

I personally find the diagnostic to be pretty unhelpful when dealing
with templates, since my change is essentially manually writing out the
code the compiler would have (or at least should have) produced anyway.
An alternative would be to just disable the diagnostic around these
regions instead of manually avoiding the comparisons.


https://reviews.llvm.org/D39149

Files:
  include/istream
  src/string.cpp


Index: src/string.cpp
===================================================================
--- src/string.cpp
+++ src/string.cpp
@@ -88,8 +88,10 @@
 {
     // Use long as no Standard string to integer exists.
     long r = as_integer_helper<long>( func, s, idx, base, strtol );
+#if __SIZEOF_INT__ != __SIZEOF_LONG__
     if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
         throw_from_string_out_of_range(func);
+#endif
     return static_cast<int>(r);
 }
 
@@ -133,8 +135,10 @@
 {
     // Use long as no Stantard string to integer exists.
     long r = as_integer_helper<long>( func, s, idx, base, wcstol );
+#if __SIZEOF_INT__ != __SIZEOF_LONG__
     if (r < numeric_limits<int>::min() || numeric_limits<int>::max() < r)
         throw_from_string_out_of_range(func);
+#endif
     return static_cast<int>(r);
 }
 
Index: include/istream
===================================================================
--- include/istream
+++ include/istream
@@ -711,6 +711,7 @@
             ios_base::iostate __err = ios_base::goodbit;
             long __temp;
             use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __temp);
+#if __SIZEOF_INT__ != __SIZEOF_LONG__
             if (__temp < numeric_limits<int>::min())
             {
                 __err |= ios_base::failbit;
@@ -723,6 +724,9 @@
             }
             else
                 __n = static_cast<int>(__temp);
+#else
+            __n = static_cast<int>(__temp);
+#endif
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39149.119723.patch
Type: text/x-patch
Size: 1560 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171020/2a3d630c/attachment-0001.bin>


More information about the cfe-commits mailing list