[PATCH] D155457: [clang] Skip tautological comparison if the comparison involves the 'size_t' type

Shivam Gupta via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 27 05:49:30 PDT 2023


xgupta updated this revision to Diff 544728.
xgupta added a comment.

updated code as per the comment of @cor3ntin


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155457/new/

https://reviews.llvm.org/D155457

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/type-limit-compare.cpp
  clang/test/SemaCXX/bool-compare.cpp


Index: clang/test/SemaCXX/bool-compare.cpp
===================================================================
--- clang/test/SemaCXX/bool-compare.cpp
+++ clang/test/SemaCXX/bool-compare.cpp
@@ -8,13 +8,13 @@
   if(b > true)    {} // expected-warning {{comparison of true with expression of type 'bool' is always false}}
   if(b < true)    {} // no warning
   if(b >= true)   {} // no warning
-  if(b <= true)   {} // expected-warning {{comparison of true with expression of type 'bool' is always true}}
+  if(b <= true)   {} // expected-warning {{result of comparison of true with expression of type 'bool' is always true}}
   if(b == true)   {} // no warning
   if(b != true)   {} // no warning
 
   if(b > false)   {} // no warning
-  if(b < false)   {} // expected-warning {{comparison of false with expression of type 'bool' is always false}}
-  if(b >= false)  {} // expected-warning {{comparison of false with expression of type 'bool' is always true}}
+  if(b < false)   {} // expected-warning {{result of comparison of false with expression of type 'bool' is always false}}
+  if(b >= false)  {} // expected-warning {{result of comparison of false with expression of type 'bool' is always true}}
   if(b <= false)  {} // no warning
   if(b == false)  {} // no warning
   if(b != false)  {} // no warning
Index: clang/test/Sema/type-limit-compare.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/type-limit-compare.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 %s -fsyntax-only -Wtautological-type-limit-compare -verify
+
+// expected-no-diagnostics
+#if defined(_WIN32)
+typedef unsigned long long uint64_t;
+#else
+typedef unsigned long uint64_t;
+#endif
+
+namespace std {
+using size_t = decltype(sizeof(0));
+} // namespace std
+
+bool func(uint64_t Size) {
+  if (sizeof(std::size_t) < sizeof(uint64_t) &&
+     Size > (uint64_t)(__SIZE_MAX__))
+    return false;
+  return true;
+}
+
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -13804,6 +13804,25 @@
   if (InRange && IsEnumConstOrFromMacro(S, Constant))
     return false;
 
+  // Don't warn if the comparison involves integral or floating-point types with
+  // the same canonical types.
+  QualType LHSCanonical = Constant->getType().getCanonicalType();
+  QualType RHSCanonical = Other->getType().getCanonicalType();
+  if (TautologicalTypeCompare &&
+      (LHSCanonical->isIntegralOrEnumerationType() ||
+       LHSCanonical->isFloatingType()) &&
+      S.Context.hasSameType(LHSCanonical, RHSCanonical) &&
+      !S.Context.hasSameType(Constant->getType(), Other->getType())) {
+    return false;
+  }
+
+  // Don't warn if the comparison involves the 'size_t' type.
+  QualType SizeT = S.Context.getSizeType();
+  if (S.Context.hasSameType(Constant->getType().getCanonicalType(), SizeT) &&
+      S.Context.hasSameType(Other->getType().getCanonicalType(), SizeT)) {
+    return false;
+  }
+
   // A comparison of an unsigned bit-field against 0 is really a type problem,
   // even though at the type level the bit-field might promote to 'signed int'.
   if (Other->refersToBitField() && InRange && Value == 0 &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155457.544728.patch
Type: text/x-patch
Size: 3277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230727/dc2ffdb2/attachment-0001.bin>


More information about the cfe-commits mailing list