This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Add -Wtype-limits to -Wextra for GCC compatibility
AbandonedPublic

Authored by xgupta on Jan 28 2023, 8:51 PM.

Details

Summary

GCC added the -Wtype-limits warning group to -Wextra around
GCC 4.4 and the group has some very helpful extra warnings
like tautological comparison type limit warnings
(comparingan unsigned int to see if it's positive, etc).

Fix https://github.com/llvm/llvm-project/issues/58375

Diff Detail

Event Timeline

xgupta created this revision.Jan 28 2023, 8:51 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 28 2023, 8:51 PM
xgupta requested review of this revision.Jan 28 2023, 8:51 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 28 2023, 8:51 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added a reviewer: Restricted Project.Jan 30 2023, 4:51 AM

Heh, I literally started working on this on Friday afternoon. :-D

There's a failing CI case that needs to be addressed: Sema/tautological-constant-compare.c -- IIRC, fixing that should suffice for test coverage. This should also have a release note.

Also, I'm adding clang-vendors not because I think this is a potentially *breaking* change, but because it could be potentially disruptive due to adding to -Wextra when we've not really changed that much in recent history.

xgupta updated this revision to Diff 493477.Jan 30 2023, 7:48 PM

address comment

thesamesam accepted this revision.Jan 30 2023, 7:53 PM
thesamesam added a subscriber: thesamesam.

I both like it and can't foresee any issues from the vendor side.

This revision is now accepted and ready to land.Jan 30 2023, 7:53 PM
xgupta updated this revision to Diff 493479.Jan 30 2023, 7:57 PM

update release note

Heh, I literally started working on this on Friday afternoon. :-D

Yeah, I saw someone asking about it on discord on Sunday. so thought to look.

aaron.ballman added inline comments.Jan 31 2023, 5:53 AM
clang/docs/ReleaseNotes.rst
45–46

I'd move this into bug fixes as I don't think this is enough of a potential disruption to warrant putting it here. If we get user feedback about surprises, we might move it back to here later.

clang/test/Sema/tautological-constant-compare.c
7–9

I'd change this the other way; this way we demonstrate that -Wextra is what enabled -Wtype-limits.

xgupta updated this revision to Diff 493649.Jan 31 2023, 9:38 AM

address comments

aaron.ballman added inline comments.Jan 31 2023, 11:15 AM
clang/test/Sema/tautological-constant-compare.c
7–9

Ooops, I'm sorry, I forgot about this edit. :-( Once you make that change on the RUN lines, you'll remove all the additional warnings you added.

xgupta updated this revision to Diff 493810.Jan 31 2023, 7:21 PM

address comment

aaron.ballman accepted this revision.Feb 1 2023, 5:31 AM

LGTM, thank you!

Btw, if you change:

Fix https://github.com/llvm/llvm-project/issues/58375

into:

Fixes #58375

in the commit message, it will automatically close the github issue and associate it with this commit.

nikic added a subscriber: nikic.Feb 1 2023, 5:32 AM

@aaron.ballman It works with the URL as well. The URL is preferred, because that way people don't have to reconstruct it when seeing the issue reference on phabricator, where it does not get auto-linked.

@aaron.ballman It works with the URL as well. The URL is preferred, because that way people don't have to reconstruct it when seeing the issue reference on phabricator, where it does not get auto-linked.

Oh, that's really good to know, thank you!

This revision was landed with ongoing or failed builds.Feb 1 2023, 6:00 AM
This revision was automatically updated to reflect the committed changes.
xgupta added a comment.Feb 1 2023, 6:02 AM

LGTM, thank you!

Thank you for reviewing and helping it to commit.

chapuni added a subscriber: chapuni.Feb 1 2023, 3:56 PM

This discovers a warning in https://reviews.llvm.org/rGa68d4b11465f5b3326be1dd820f59fac275b7581
I think its checking is valid if size_t is uint32_t (eg. -m32)

Could you guys teach me what the right fix for it? I don't know canonical fixes for it.

hctim added a subscriber: hctim.Feb 1 2023, 4:19 PM

Hey, looks like this broke the x86_64-linux sanitizer buildbot: https://lab.llvm.org/buildbot/#/builders/37/builds/19910

You can reproduce the bot using the instructions at https://github.com/google/sanitizers/wiki/SanitizerBotReproduceBuild (substitute buildbot_fast.sh for buildbot_cmake.sh to get the right one, but I don't think using the official buildscript is necessary. This looks like a 2-stage compile is not currently -Wextra -Werror clean:

/b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/ProfileData/InstrProfReader.cpp:177:41: error: result of comparison 'uint64_t' (aka 'unsigned long') > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]
  if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint64_t>::max())
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/ProfileData/InstrProfReader.cpp:227:41: error: result of comparison 'uint64_t' (aka 'unsigned long') > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]
  if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint64_t>::max())
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I tried to whack-a-mole those two bugs (see my two commits on https://lab.llvm.org/buildbot/#/builders/37/builds/19926), but there's more latent bugs hanging around.

Probably worth reverting this and the warnings need to be fixed inside clang/llvm first, then we can make it the default.

xgupta added a comment.Feb 1 2023, 4:32 PM

Hey, looks like this broke the x86_64-linux sanitizer buildbot: https://lab.llvm.org/buildbot/#/builders/37/builds/19910

You can reproduce the bot using the instructions at https://github.com/google/sanitizers/wiki/SanitizerBotReproduceBuild (substitute buildbot_fast.sh for buildbot_cmake.sh to get the right one, but I don't think using the official buildscript is necessary. This looks like a 2-stage compile is not currently -Wextra -Werror clean:

/b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/ProfileData/InstrProfReader.cpp:177:41: error: result of comparison 'uint64_t' (aka 'unsigned long') > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]
  if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint64_t>::max())
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/ProfileData/InstrProfReader.cpp:227:41: error: result of comparison 'uint64_t' (aka 'unsigned long') > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]
  if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint64_t>::max())
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I tried to whack-a-mole those two bugs (see my two commits on https://lab.llvm.org/buildbot/#/builders/37/builds/19926), but there's more latent bugs hanging around.

Probably worth reverting this and the warnings need to be fixed inside clang/llvm first, then we can make it the default.

Sure, revert this for time being. will check later today.

hans added a subscriber: hans.Feb 2 2023, 2:39 AM

This discovers a warning in https://reviews.llvm.org/rGa68d4b11465f5b3326be1dd820f59fac275b7581
I think its checking is valid if size_t is uint32_t (eg. -m32)

Could you guys teach me what the right fix for it? I don't know canonical fixes for it.

+1 we have some code like this too and I'm not sure what's the best way to rewrite it to pacify this warning.

This discovers a warning in https://reviews.llvm.org/rGa68d4b11465f5b3326be1dd820f59fac275b7581
I think its checking is valid if size_t is uint32_t (eg. -m32)

Could you guys teach me what the right fix for it? I don't know canonical fixes for it.

This might be pointing out an issue with our existing -Wtype-limits implementation, as the way I would correct that still gets diagnosed: https://godbolt.org/z/sY9PodKbf The only time the comparison is tautological is when size_t has less width than uint64_t, but the extra test for that scenario doesn't silence the diagnostic. Even when I try really hard: https://godbolt.org/z/PbWcTYaz1

haowei added a subscriber: haowei.Feb 7 2023, 2:45 PM

There are also cases that this clang type-limit check will warn when checking if a enum type is larger than 0. For example, we are seeing warnings on ICU source code (https://github.com/unicode-org/icu/blob/8d5a97ae0f49f6974372736ca67db15c37522f6f/icu4c/source/i18n/displayoptions.cpp#L79) after this patch was landed.

U_CAPI const char * U_EXPORT2
udispopt_getGrammaticalCaseIdentifier(UDisplayOptionsGrammaticalCase grammaticalCase) {
    if (grammaticalCase >= 0 && grammaticalCase < UPRV_LENGTHOF(grammaticalCaseIds)) {
        return grammaticalCaseIds[grammaticalCase];
    }

    return grammaticalCaseIds[0];
}

grammaticalCase is a enum. And whether enum type is unsigned or not does not seem to be strictly defined in C/C++ standard, it ups to the compiler to decide how enum type should be defined. In that case, this check is legit and shouldn't trigger an warning.

xgupta reopened this revision.Feb 11 2023, 12:35 AM
This revision is now accepted and ready to land.Feb 11 2023, 12:35 AM
xgupta abandoned this revision.Mon, Dec 4, 11:46 PM

We will attempt this after some time on GitHub.