summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authorRoman Lebedev <lebedev.ri@gmail.com>2017-09-19 21:11:35 +0000
committerRoman Lebedev <lebedev.ri@gmail.com>2017-09-19 21:11:35 +0000
commitc9c9748d99e663cb98663f7fbc6ac0b30bd8fa16 (patch)
treece21fbffe94832d4e00135015a5b48839cda7961 /clang/lib/Sema/SemaChecking.cpp
parent8cc8b63b0656b857eb9e9ce773529f29594d2ac1 (diff)
downloadbcm5719-llvm-c9c9748d99e663cb98663f7fbc6ac0b30bd8fa16.tar.gz
bcm5719-llvm-c9c9748d99e663cb98663f7fbc6ac0b30bd8fa16.zip
[Sema] Move some stuff into -Wtautological-unsigned-enum-zero-compare
Summary: As requested by Sam McCall: > Enums (not new I guess). Typical case: if (enum < 0 || enum > MAX) > The warning strongly suggests that the enum < 0 check has no effect > (for enums with nonnegative ranges). > Clang doesn't seem to optimize such checks out though, and they seem > likely to catch bugs in some cases. Yes, only if there's UB elsewhere, > but I assume not optimizing out these checks indicates a deliberate > decision to stay somewhat compatible with a technically-incorrect > mental model. > If this is the case, should we move these to a > -Wtautological-compare-enum subcategory? Reviewers: rjmccall, rsmith, aaron.ballman, sammccall, bkramer, djasper Reviewed By: aaron.ballman Subscribers: jroelofs, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D37629 llvm-svn: 313677
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp42
1 files changed, 25 insertions, 17 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 212fe65c76f..6544cb923b1 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8583,7 +8583,7 @@ bool CheckTautologicalComparisonWithZero(Sema &S, BinaryOperator *E) {
// bool values are handled by DiagnoseOutOfRangeComparison().
- BinaryOperatorKind op = E->getOpcode();
+ BinaryOperatorKind Op = E->getOpcode();
if (E->isValueDependent())
return false;
@@ -8592,22 +8592,30 @@ bool CheckTautologicalComparisonWithZero(Sema &S, BinaryOperator *E) {
bool Match = true;
- if (op == BO_LT && isNonBooleanUnsignedValue(LHS) && IsZero(S, RHS)) {
- S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
- << "< 0" << "false" << HasEnumType(LHS)
- << LHS->getSourceRange() << RHS->getSourceRange();
- } else if (op == BO_GE && isNonBooleanUnsignedValue(LHS) && IsZero(S, RHS)) {
- S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
- << ">= 0" << "true" << HasEnumType(LHS)
- << LHS->getSourceRange() << RHS->getSourceRange();
- } else if (op == BO_GT && isNonBooleanUnsignedValue(RHS) && IsZero(S, LHS)) {
- S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
- << "0 >" << "false" << HasEnumType(RHS)
- << LHS->getSourceRange() << RHS->getSourceRange();
- } else if (op == BO_LE && isNonBooleanUnsignedValue(RHS) && IsZero(S, LHS)) {
- S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
- << "0 <=" << "true" << HasEnumType(RHS)
- << LHS->getSourceRange() << RHS->getSourceRange();
+ if (Op == BO_LT && isNonBooleanUnsignedValue(LHS) && IsZero(S, RHS)) {
+ S.Diag(E->getOperatorLoc(),
+ HasEnumType(LHS) ? diag::warn_lunsigned_enum_always_true_comparison
+ : diag::warn_lunsigned_always_true_comparison)
+ << "< 0"
+ << "false" << LHS->getSourceRange() << RHS->getSourceRange();
+ } else if (Op == BO_GE && isNonBooleanUnsignedValue(LHS) && IsZero(S, RHS)) {
+ S.Diag(E->getOperatorLoc(),
+ HasEnumType(LHS) ? diag::warn_lunsigned_enum_always_true_comparison
+ : diag::warn_lunsigned_always_true_comparison)
+ << ">= 0"
+ << "true" << LHS->getSourceRange() << RHS->getSourceRange();
+ } else if (Op == BO_GT && isNonBooleanUnsignedValue(RHS) && IsZero(S, LHS)) {
+ S.Diag(E->getOperatorLoc(),
+ HasEnumType(RHS) ? diag::warn_runsigned_enum_always_true_comparison
+ : diag::warn_runsigned_always_true_comparison)
+ << "0 >"
+ << "false" << LHS->getSourceRange() << RHS->getSourceRange();
+ } else if (Op == BO_LE && isNonBooleanUnsignedValue(RHS) && IsZero(S, LHS)) {
+ S.Diag(E->getOperatorLoc(),
+ HasEnumType(RHS) ? diag::warn_runsigned_enum_always_true_comparison
+ : diag::warn_runsigned_always_true_comparison)
+ << "0 <="
+ << "true" << LHS->getSourceRange() << RHS->getSourceRange();
} else
Match = false;
OpenPOWER on IntegriCloud