Skip to content

Commit

Permalink
[Sema] Re-land: Diagnose tautological comparison with type's min/max …
Browse files Browse the repository at this point in the history
…values

The first attempt, rL315614 was reverted because one libcxx
test broke, and i did not know at the time how to deal with it.

Summary:
Currently, clang only diagnoses completely out-of-range comparisons (e.g. `char` and constant `300`),
and comparisons of unsigned and `0`. But gcc also does diagnose the comparisons with the
`std::numeric_limits<>::max()` / `std::numeric_limits<>::min()` so to speak

Finally Fixes https://bugs.llvm.org/show_bug.cgi?id=34147
Continuation of https://reviews.llvm.org/D37565

Reviewers: rjmccall, rsmith, aaron.ballman

Reviewed By: rsmith

Subscribers: rtrieu, jroelofs, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D38101

llvm-svn: 315875
  • Loading branch information
LebedevRI committed Oct 15, 2017
1 parent ac9309a commit 6de129e
Show file tree
Hide file tree
Showing 9 changed files with 1,032 additions and 297 deletions.
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -78,6 +78,10 @@ Improvements to Clang's diagnostics
when the signed integer is coerced to an unsigned type for the comparison.
``-Wsign-compare`` was adjusted not to warn in this case.

- ``-Wtautological-constant-compare`` is a new warning that warns on
tautological comparisons between integer variable of the type ``T`` and the
largest/smallest possible integer constant of that same type.

- ``-Wnull-pointer-arithmetic`` now warns about performing pointer arithmetic
on a null pointer. Such pointer arithmetic has an undefined behavior if the
offset is nonzero. It also now warns about arithmetic on a null pointer
Expand Down
8 changes: 5 additions & 3 deletions clang/include/clang/Basic/DiagnosticGroups.td
Expand Up @@ -432,13 +432,15 @@ def StrncatSize : DiagGroup<"strncat-size">;
def TautologicalUnsignedZeroCompare : DiagGroup<"tautological-unsigned-zero-compare">;
def TautologicalUnsignedEnumZeroCompare : DiagGroup<"tautological-unsigned-enum-zero-compare">;
def TautologicalOutOfRangeCompare : DiagGroup<"tautological-constant-out-of-range-compare">;
def TautologicalConstantCompare : DiagGroup<"tautological-constant-compare",
[TautologicalUnsignedZeroCompare,
TautologicalUnsignedEnumZeroCompare,
TautologicalOutOfRangeCompare]>;
def TautologicalPointerCompare : DiagGroup<"tautological-pointer-compare">;
def TautologicalOverlapCompare : DiagGroup<"tautological-overlap-compare">;
def TautologicalUndefinedCompare : DiagGroup<"tautological-undefined-compare">;
def TautologicalCompare : DiagGroup<"tautological-compare",
[TautologicalUnsignedZeroCompare,
TautologicalUnsignedEnumZeroCompare,
TautologicalOutOfRangeCompare,
[TautologicalConstantCompare,
TautologicalPointerCompare,
TautologicalOverlapCompare,
TautologicalUndefinedCompare]>;
Expand Down
20 changes: 10 additions & 10 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -5938,18 +5938,18 @@ def note_typecheck_assign_const : Note<
"member function %q1 is declared const here|"
"%select{|nested }1data member %2 declared const here}0">;

def warn_lunsigned_always_true_comparison : Warning<
"comparison of unsigned expression %0 is always %select{false|true}1">,
def warn_unsigned_always_true_comparison : Warning<
"comparison of %select{%3|unsigned expression}0 %2 "
"%select{unsigned expression|%3}0 is always %select{false|true}4">,
InGroup<TautologicalUnsignedZeroCompare>;
def warn_runsigned_always_true_comparison : Warning<
"comparison of %0 unsigned expression is always %select{false|true}1">,
InGroup<TautologicalUnsignedZeroCompare>;
def warn_lunsigned_enum_always_true_comparison : Warning<
"comparison of unsigned enum expression %0 is always %select{false|true}1">,
InGroup<TautologicalUnsignedEnumZeroCompare>;
def warn_runsigned_enum_always_true_comparison : Warning<
"comparison of %0 unsigned enum expression is always %select{false|true}1">,
def warn_unsigned_enum_always_true_comparison : Warning<
"comparison of %select{%3|unsigned enum expression}0 %2 "
"%select{unsigned enum expression|%3}0 is always %select{false|true}4">,
InGroup<TautologicalUnsignedEnumZeroCompare>;
def warn_tautological_constant_compare : Warning<
"comparison %select{%3|%1}0 %2 "
"%select{%1|%3}0 is always %select{false|true}4">,
InGroup<TautologicalConstantCompare>;

def warn_mixed_sign_comparison : Warning<
"comparison of integers of different signs: %0 and %1">,
Expand Down

0 comments on commit 6de129e

Please sign in to comment.