@@ -236,6 +236,7 @@ int Wptr_subtraction_blows = 0;
int Wreturn_void = 0;
int Wshadow = 0;
int Wsizeof_bool = 0;
+int Wtautological_compare = 0;
int Wtransparent_union = 0;
int Wtypesign = 0;
int Wundef = 0;
@@ -471,6 +472,7 @@ static const struct warning {
{ "return-void", &Wreturn_void },
{ "shadow", &Wshadow },
{ "sizeof-bool", &Wsizeof_bool },
+ { "tautological-compare", &Wtautological_compare },
{ "transparent-union", &Wtransparent_union },
{ "typesign", &Wtypesign },
{ "undef", &Wundef },
@@ -122,6 +122,7 @@ extern int Wptr_subtraction_blows;
extern int Wreturn_void;
extern int Wshadow;
extern int Wsizeof_bool;
+extern int Wtautological_compare;
extern int Wtransparent_union;
extern int Wtypesign;
extern int Wundef;
@@ -494,6 +494,8 @@ static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg)
case OP_SET_NE:
case OP_SET_LT: case OP_SET_GT:
case OP_SET_B: case OP_SET_A:
+ if (Wtautological_compare)
+ warning(insn->pos, "self-comparison always evaluates to false");
case OP_SUB:
case OP_XOR:
return replace_with_pseudo(insn, value_pseudo(0));
@@ -501,6 +503,8 @@ static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg)
case OP_SET_EQ:
case OP_SET_LE: case OP_SET_GE:
case OP_SET_BE: case OP_SET_AE:
+ if (Wtautological_compare)
+ warning(insn->pos, "self-comparison always evaluates to true");
return replace_with_pseudo(insn, value_pseudo(1));
case OP_AND:
new file mode 100644
@@ -0,0 +1,35 @@
+typedef unsigned int u32;
+
+int seq(int a) { return a == a; }
+int sne(int a) { return a != a; }
+int slt(int a) { return a < a; }
+int sgt(int a) { return a > a; }
+int sle(int a) { return a <= a; }
+int sge(int a) { return a >= a; }
+
+u32 ueq(u32 a) { return a == a; }
+u32 une(u32 a) { return a != a; }
+u32 ult(u32 a) { return a < a; }
+u32 ugt(u32 a) { return a > a; }
+u32 ule(u32 a) { return a <= a; }
+u32 uge(u32 a) { return a >= a; }
+
+/*
+ * check-name: tautological-compare
+ * check-command: sparse -Wno-decl -Wtautological-compare $file
+ *
+ * check-error-start
+tautological-compare.c:3:30: warning: self-comparison always evaluates to true
+tautological-compare.c:4:30: warning: self-comparison always evaluates to false
+tautological-compare.c:5:29: warning: self-comparison always evaluates to false
+tautological-compare.c:6:29: warning: self-comparison always evaluates to false
+tautological-compare.c:7:30: warning: self-comparison always evaluates to true
+tautological-compare.c:8:30: warning: self-comparison always evaluates to true
+tautological-compare.c:10:30: warning: self-comparison always evaluates to true
+tautological-compare.c:11:30: warning: self-comparison always evaluates to false
+tautological-compare.c:12:29: warning: self-comparison always evaluates to false
+tautological-compare.c:13:29: warning: self-comparison always evaluates to false
+tautological-compare.c:14:30: warning: self-comparison always evaluates to true
+tautological-compare.c:15:30: warning: self-comparison always evaluates to true
+ * check-error-end
+ */
Now that we optimize away expressions like 'x == x' or 'x < x', also add a warning for it (but disabled by default). Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> --- lib.c | 2 ++ lib.h | 1 + simplify.c | 4 ++++ validation/tautological-compare.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 validation/tautological-compare.c