This is an archive of the discontinued LLVM Phabricator instance.

Remove va_start diagnostic false positive with enumerations
ClosedPublic

Authored by aaron.ballman on Aug 26 2016, 7:34 AM.

Details

Summary

r267338 improved the diagnostic checking for undefined behavior with va_start(), but it had some false positives regarding enumerations. The underlying type for an enumeration in C is either char, signed int, or unsigned int. In the case the underlying type is chosen to be char (such as when passing -fshort-enums or using attribute((packed)) on the enum declaration), the enumeration can result in undefined behavior. However, when the underlying type is signed int or unsigned int (or long long as an extension), there is no undefined behavior because the types are compatible. This patch silences diagnostics for the latter while retaining the diagnostics for the former.

This patch addresses PR29140.

Diff Detail

Event Timeline

aaron.ballman retitled this revision from to Remove va_start diagnostic false positive with enumerations.
aaron.ballman updated this object.
aaron.ballman added a subscriber: cfe-commits.
dim added a subscriber: dim.Aug 28 2016, 12:03 PM

This works for me. I had two test cases from the FreeBSD source tree which resulted in warnings, e.g.:

cddl/contrib/opensolaris/lib/libnvpair/libnvpair.c:388:15: error: passing an object that undergoes default argument promotion to 'va_start' has undefined behavior
      [-Werror,-Wvarargs]
        va_start(ap, which);
                     ^
cddl/contrib/opensolaris/lib/libnvpair/libnvpair.c:382:66: note: parameter of type 'enum nvlist_prtctl_fmt' is declared here
nvlist_prtctl_dofmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which, ...)
                                                                 ^
1 error generated.

This fix resolves both of them.

emaste added a subscriber: emaste.Aug 29 2016, 7:26 AM
dim added a comment.Sep 14 2016, 2:35 PM

FWIW, I have already imported this fix into FreeBSD two weeks ago: https://svnweb.freebsd.org/changeset/base/304960
It has been working fine for us.

aaron.ballman accepted this revision.Sep 15 2016, 7:09 AM
aaron.ballman added a reviewer: aaron.ballman.
This revision is now accepted and ready to land.Sep 15 2016, 7:09 AM
aaron.ballman closed this revision.Sep 15 2016, 7:10 AM

Commit in r281609

I see this has been reverted in r281612, but I can no longer access the build log serving as a reason linked in the message: https://www.mail-archive.com/cfe-commits@lists.llvm.org/msg35386.html
We have a few false-positive warnings that (I think) would be silenced by this change. I'm just wondering if something like this, if not this, could be included anyway? Not critical of course, it just would be nice.