git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/2] fix clang -Wtautological-compare with unsigned enum

From
John Keeping <john@keeping.me.uk>
Date
Jan 17, 2013, 11:00 UTC
Message-ID
<20130117110008.GD4574@serenity.lan>
In-Reply-To
<CALWbr2wk+78zxGKCo-hCOwMuMOzdGspYvMu7PA6o0OYM3Y3m4A@mail.gmail.com>
On Thu, Jan 17, 2013 at 11:32:39AM +0100, Antoine Pelisse wrote:
> John, could you confirm that you trigger the -Wtautological-compare
> warning with your version of clang ?

Yes, the warning is still there with both 3.2 and a recent trunk build but this patch squelches it.

> And that this patch makes clang compilation warning-free (with the
> very latest clang) ?

There is one remaining warning on pu which hasn't been discussed in this thread as far as I can see. I'll send a patch shortly.

There's also a warning that triggers with clang 3.2 but not clang trunk, which I think is a legitimate warning - perhaps someone who understands integer type promotion better than me can explain why the code is OK (patch->score is declared as 'int'):

builtin/apply.c:1044:47: warning: comparison of constant 18446744073709551615
    with expression of type 'int' is always false
    [-Wtautological-constant-out-of-range-compare]
        if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~
Show 53 quoted lines
> On Wed, Jan 16, 2013 at 11:47 PM, Antoine Pelisse <apelisse@gmail.com> wrote:
> > Create a GREP_HEADER_FIELD_MIN so we can check that the field value is
> > sane and silent the clang warning.
> >
> > Clang warning happens because the enum is unsigned (this is
> > implementation-defined, and there is no negative fields) and the check
> > is then tautological.
> >
> > Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
> > ---
> > I tried to consider discussion [1] and this [2] discussion on clang's list
> >
> > With these two patches and the patch from Max Horne, I'm finally able to
> > compile with CC=clang CFLAGS=-Werror.
> >
> >  [1]: http://thread.gmane.org/gmane.comp.version-control.git/184908
> >  [2]: http://clang-developers.42468.n3.nabble.com/Possibly-invalid-enum-tautology-warning-td3233140.html
> >
> >  grep.c | 3 ++-
> >  grep.h | 3 ++-
> >  2 files changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/grep.c b/grep.c
> > index 4bd1b8b..bb548ca 100644
> > --- a/grep.c
> > +++ b/grep.c
> > @@ -625,7 +625,8 @@ static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
> >         for (p = opt->header_list; p; p = p->next) {
> >                 if (p->token != GREP_PATTERN_HEAD)
> >                         die("bug: a non-header pattern in grep header list.");
> > -               if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
> > +               if (p->field < GREP_HEADER_FIELD_MIN ||
> > +                   GREP_HEADER_FIELD_MAX <= p->field)
> >                         die("bug: unknown header field %d", p->field);
> >                 compile_regexp(p, opt);
> >         }
> > diff --git a/grep.h b/grep.h
> > index 8fc854f..e4a1df5 100644
> > --- a/grep.h
> > +++ b/grep.h
> > @@ -28,7 +28,8 @@ enum grep_context {
> >  };
> >
> >  enum grep_header_field {
> > -       GREP_HEADER_AUTHOR = 0,
> > +       GREP_HEADER_FIELD_MIN = 0,
> > +       GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
> >         GREP_HEADER_COMMITTER,
> >         GREP_HEADER_REFLOG,
> >
> > --
> > 1.8.1.1.435.g20d29be.dirty
> >
Previous: Antoine PelisseNext: John Keeping
Message 21 of 35 in “fix some clang warnings”
  1. fix some clang warningsMax Horn, Jan 16, 2013
  2. Jeff KingJan 16, 2013
  3. Junio C HamanoJan 16, 2013
  4. Antoine PelisseJan 16, 2013
  5. John KeepingJan 16, 2013
  6. Max HornJan 16, 2013
  7. Jeff KingJan 16, 2013
  8. Jeff KingJan 16, 2013
  9. Jeff KingJan 16, 2013
  10. John KeepingJan 16, 2013
  11. Jeff KingJan 16, 2013
  12. Antoine PelisseJan 16, 2013
  13. John KeepingJan 16, 2013
  14. Jeff KingJan 16, 2013
  15. John KeepingJan 16, 2013
  16. John KeepingJan 17, 2013
  17. 1/2 fix clang -Wconstant-conversion with bit fieldsAntoine Pelisse, Jan 16, 2013
  18. 2/2 fix clang -Wtautological-compare with unsigned enumAntoine Pelisse, Jan 16, 2013
  19. Antoine PelisseJan 16, 2013
  20. Antoine PelisseJan 17, 2013
  21. John KeepingJan 17, 2013
  22. combine-diff: suppress a clang warningJohn Keeping, Jan 17, 2013
  23. Linus TorvaldsJan 17, 2013
  24. Antoine PelisseJan 17, 2013
  25. John KeepingJan 17, 2013
  26. Phil HordJan 18, 2013
  27. Linus TorvaldsJan 18, 2013
  28. John KeepingJan 16, 2013
  29. Antoine PelisseJan 16, 2013
  30. Antoine PelisseJan 16, 2013
  31. Junio C HamanoJan 16, 2013
  32. Junio C HamanoJan 16, 2013
  33. Tomas CarneckyJan 16, 2013
  34. Matthieu MoyJan 16, 2013
  35. Miles BaderFeb 1, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.