For non-floating point types, added check for expressions of the form
"x == x" and "x != x". We emit a warning for these since they always evaluate to a constant value and often indicate a logical error. Added test case for this check. llvm-svn: 43450
This commit is contained in:
parent
d45e055f2c
commit
e451eae8d7
|
@ -1218,6 +1218,13 @@ inline QualType Sema::CheckCompareOperands( // C99 6.5.8
|
|||
QualType lType = lex->getType();
|
||||
QualType rType = rex->getType();
|
||||
|
||||
if (!lType->isFloatingType()) {
|
||||
if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
|
||||
if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
|
||||
if (DRL->getDecl() == DRR->getDecl())
|
||||
Diag(loc, diag::warn_selfcomparison);
|
||||
}
|
||||
|
||||
if (isRelational) {
|
||||
if (lType->isRealType() && rType->isRealType())
|
||||
return Context.IntTy;
|
||||
|
|
|
@ -824,6 +824,11 @@ DIAG(warn_ret_stack_ref, WARNING,
|
|||
DIAG(warn_floatingpoint_eq, WARNING,
|
||||
"comparing floating point with == or != is unsafe")
|
||||
|
||||
// CHECK: for non-floating point, expressions of the form x == x or x != x
|
||||
// should result in a warning, since these always evaluate to a constant.
|
||||
DIAG(warn_selfcomparison,WARNING,
|
||||
"self-comparison always results in a constant value.")
|
||||
|
||||
// CHECK: stores to variables that are no longer live (dead stores)
|
||||
DIAG(warn_dead_store, WARNING, "value stored to variable is never used")
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
// RUN: clang -fsyntax-only -verify %s
|
||||
|
||||
int foo(int x) {
|
||||
return x == x; // expected-warning {{self-comparison always results}}
|
||||
}
|
||||
|
||||
int foo2(int x) {
|
||||
return (x) != (((x))); // expected-warning {{self-comparison always results}}
|
||||
}
|
||||
|
||||
int bar(float x) {
|
||||
return x == x; // no-warning
|
||||
}
|
||||
|
||||
int bar2(float x) {
|
||||
return x != x; // no-warning
|
||||
}
|
Loading…
Reference in New Issue