This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Add -Wpointer-to-enum-cast and -Wvoid-pointer-to-enum-cast
ClosedPublic

Authored by nathanchance on Mar 6 2020, 9:24 AM.

Details

Summary

GCC does not warn on casts from pointers to enumerators, while clang
currently does: https://godbolt.org/z/3DFDVG

This causes a bunch of extra warnings in the Linux kernel, where
certain structs contain a void pointer to avoid using a gigantic
union for all of the various types of driver data, such as
versions.

Add a diagnostic that allows certain projects like the kernel to
disable the warning just for enums, which allows those projects to
keep full compatibility with GCC but keeps the intention of treating
casts to integers and enumerators the same by default so that other
projects have the opportunity to catch issues not noticed before (or
follow suite and disable the warning).

Link: https://github.com/ClangBuiltLinux/linux/issues/887

Diff Detail

Event Timeline

nathanchance created this revision.Mar 6 2020, 9:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 6 2020, 9:24 AM

Thanks, Verified that this fixes the kernel warnings in my local builds with https://gist.github.com/nathanchance/767cccf4d093c1342e1994083518815e!

xbolva00 added inline comments.
clang/include/clang/Basic/DiagnosticSemaKinds.td
3672

You can reuse warning text:

warn_pointer_to_int_cast.Text

I think..

nathanchance edited the summary of this revision. (Show Details)
nathanchance marked 2 inline comments as done.
nathanchance added inline comments.
clang/include/clang/Basic/DiagnosticSemaKinds.td
3672

Thanks, that is indeed cleaner!

rjmccall accepted this revision.Mar 7 2020, 1:55 PM
This revision is now accepted and ready to land.Mar 7 2020, 1:55 PM
nathanchance marked an inline comment as done.Mar 7 2020, 2:08 PM

Thanks for the review and accepting. I do not have commit rights, would you mind doing that on my behalf?

MaskRay added a subscriber: MaskRay.Mar 7 2020, 4:43 PM
MaskRay added inline comments.
clang/lib/Sema/SemaCast.cpp
2783

A ternary operator is better here. I'll fix it.

This revision was automatically updated to reflect the committed changes.