Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* Opinion about -Wtautological-compare
@ 2017-10-28  3:24 Simon Marchi
  2017-10-30  9:35 ` Yao Qi
  0 siblings, 1 reply; 9+ messages in thread
From: Simon Marchi @ 2017-10-28  3:24 UTC (permalink / raw)
  To: GDB Patches

Hi all,

When building with Clang, I see warnings like these in multiple files:

/home/emaisin/src/binutils-gdb/gdb/arm-tdep.c:10013:13: error: comparison of 0 <= unsigned expression is always true [-Werror,-Wtautological-compare]
      if (0 <= insn_op1 && 3 >= insn_op1)
          ~ ^  ~~~~~~~~
/home/emaisin/src/binutils-gdb/gdb/arm-tdep.c:11722:20: error: comparison of unsigned expression >= 0 is always true [-Werror,-Wtautological-compare]
      else if (opB >= 0 && opB <= 2)
               ~~~ ^  ~

They are saying that because the variable is unsigned, the comparison
"variable >= 0" will always be true.  What do you think we should do with
this?

1) Remove the comparisons and keep the warning:

  - if (0 <= insn_op1 && 3 >= insn_op1)
  + if (3 >= insn_op1)

2) Leave the comparisons, but put them in comments and keep the warning:

  - if (0 <= insn_op1 && 3 >= insn_op1)
  + if (/* 0 <= insn_op1 && */ 3 >= insn_op1)

3) Make a util function "in_inclusive_range" and use it (and keep the warning).  Something like:

static bool
in_inclusive_range (unsigned int value, unsigned int low, unsigned int high)
{
  return value >= low && value <= high;
}

and

  - if (0 <= insn_op1 && 3 >= insn_op1)
  + if (in_inclusive_range (insn_op1, 0, 3))


4) Leave the code as-is and remove the warning (add -Wno-tautological-compare to the build).

Personally, I think the warning is useful and can reveal bugs, so I'd like to keep
it.  I lean towards 2 or 3, because they help convey the idea that we check if the
value is within a range.  If you are following with the architecture manual on the
side, it will probably show the same range (0-3) for those bits, so it helps if the
code does the same.

Simon


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-10-30 18:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-28  3:24 Opinion about -Wtautological-compare Simon Marchi
2017-10-30  9:35 ` Yao Qi
2017-10-30 12:49   ` John Baldwin
2017-10-30 14:27     ` Simon Marchi
2017-10-30 14:33     ` [PATCH] Introduce in_inclusive_range, fix -Wtautological-compare warnings Simon Marchi
2017-10-30 15:57       ` John Baldwin
2017-10-30 16:01         ` Simon Marchi
2017-10-30 16:05           ` John Baldwin
2017-10-30 18:29             ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox