r312792 - [Sema] Put tautological comparison of unsigned and zero into it's own flag
Roman Lebedev via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 8 06:56:45 PDT 2017
Author: lebedevri
Date: Fri Sep 8 06:56:45 2017
New Revision: 312792
URL: http://llvm.org/viewvc/llvm-project?rev=312792&view=rev
Log:
[Sema] Put tautological comparison of unsigned and zero into it's own flag
Summary:
As requested by Sam McCall.
```
$ /build/llvm-build-Clang-release/./bin/clang -c /build/clang/test/Sema/outof-range-constant-compare.c /build/clang/test/Sema/outof-range-constant-compare.c:40:11: warning: comparison of unsigned expression < 0 is always false [-Wtautological-unsigned-zero-compare]
if (a < 0x0000000000000000UL) // expected-warning {{comparison of unsigned expression < 0 is always false}}
~ ^ ~~~~~~~~~~~~~~~~~~~~
```
Reviewers: sammccall, bkramer, djasper, rsmith, rjmccall, aaron.ballman
Reviewed By: sammccall, aaron.ballman
Subscribers: aaron.ballman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D37620
Added:
cfe/trunk/test/Sema/tautological-unsigned-zero-compare.c
Modified:
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=312792&r1=312791&r2=312792&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Fri Sep 8 06:56:45 2017
@@ -427,12 +427,14 @@ def StringCompare : DiagGroup<"string-co
def StringPlusInt : DiagGroup<"string-plus-int">;
def StringPlusChar : DiagGroup<"string-plus-char">;
def StrncatSize : DiagGroup<"strncat-size">;
+def TautologicalUnsignedZeroCompare : DiagGroup<"tautological-unsigned-zero-compare">;
def TautologicalOutOfRangeCompare : DiagGroup<"tautological-constant-out-of-range-compare">;
def TautologicalPointerCompare : DiagGroup<"tautological-pointer-compare">;
def TautologicalOverlapCompare : DiagGroup<"tautological-overlap-compare">;
def TautologicalUndefinedCompare : DiagGroup<"tautological-undefined-compare">;
def TautologicalCompare : DiagGroup<"tautological-compare",
- [TautologicalOutOfRangeCompare,
+ [TautologicalUnsignedZeroCompare,
+ TautologicalOutOfRangeCompare,
TautologicalPointerCompare,
TautologicalOverlapCompare,
TautologicalUndefinedCompare]>;
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=312792&r1=312791&r2=312792&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Sep 8 06:56:45 2017
@@ -5898,14 +5898,14 @@ def warn_mixed_sign_comparison : Warning
InGroup<SignCompare>, DefaultIgnore;
def warn_lunsigned_always_true_comparison : Warning<
"comparison of unsigned%select{| enum}2 expression %0 is always %1">,
- InGroup<TautologicalCompare>;
+ InGroup<TautologicalUnsignedZeroCompare>;
def warn_out_of_range_compare : Warning<
"comparison of %select{constant %0|true|false}1 with "
"%select{expression of type %2|boolean expression}3 is always "
"%select{false|true}4">, InGroup<TautologicalOutOfRangeCompare>;
def warn_runsigned_always_true_comparison : Warning<
"comparison of %0 unsigned%select{| enum}2 expression is always %1">,
- InGroup<TautologicalCompare>;
+ InGroup<TautologicalUnsignedZeroCompare>;
def warn_comparison_of_mixed_enum_types : Warning<
"comparison of two values with different enumeration types"
"%diff{ ($ and $)|}0,1">,
Added: cfe/trunk/test/Sema/tautological-unsigned-zero-compare.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/tautological-unsigned-zero-compare.c?rev=312792&view=auto
==============================================================================
--- cfe/trunk/test/Sema/tautological-unsigned-zero-compare.c (added)
+++ cfe/trunk/test/Sema/tautological-unsigned-zero-compare.c Fri Sep 8 06:56:45 2017
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -fsyntax-only -DTEST -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wno-tautological-unsigned-zero-compare -verify %s
+
+unsigned value(void);
+
+int main() {
+ unsigned un = value();
+
+#ifdef TEST
+ if (un < 0) // expected-warning {{comparison of unsigned expression < 0 is always false}}
+ return 0;
+ if (un >= 0) // expected-warning {{comparison of unsigned expression >= 0 is always true}}
+ return 0;
+ if (0 <= un) // expected-warning {{comparison of 0 <= unsigned expression is always true}}
+ return 0;
+ if (0 > un) // expected-warning {{comparison of 0 > unsigned expression is always false}}
+ return 0;
+ if (un < 0U) // expected-warning {{comparison of unsigned expression < 0 is always false}}
+ return 0;
+ if (un >= 0U) // expected-warning {{comparison of unsigned expression >= 0 is always true}}
+ return 0;
+ if (0U <= un) // expected-warning {{comparison of 0 <= unsigned expression is always true}}
+ return 0;
+ if (0U > un) // expected-warning {{comparison of 0 > unsigned expression is always false}}
+ return 0;
+#else
+// expected-no-diagnostics
+ if (un < 0)
+ return 0;
+ if (un >= 0)
+ return 0;
+ if (0 <= un)
+ return 0;
+ if (0 > un)
+ return 0;
+ if (un < 0U)
+ return 0;
+ if (un >= 0U)
+ return 0;
+ if (0U <= un)
+ return 0;
+ if (0U > un)
+ return 0;
+#endif
+
+ return 1;
+}
More information about the cfe-commits
mailing list