Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@ericsson.com>
To: GDB Patches <gdb-patches@sourceware.org>
Subject: Opinion about -Wtautological-compare
Date: Sat, 28 Oct 2017 03:24:00 -0000	[thread overview]
Message-ID: <f64bd46a-acb0-2031-d408-a331245e1fd5@ericsson.com> (raw)

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


             reply	other threads:[~2017-10-28  3:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-28  3:24 Simon Marchi [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f64bd46a-acb0-2031-d408-a331245e1fd5@ericsson.com \
    --to=simon.marchi@ericsson.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox