[cfe-commits] Patch: Add warning for undefined reinterpret_cast behavior

John McCall rjmccall at apple.com
Mon Apr 11 10:15:07 PDT 2011


On Apr 11, 2011, at 9:44 AM, Chandler Carruth wrote:
> I share John and Doug's concern over false positive rates. However, I'd like to evaluate that by getting a conservative version of this in, trying it on some code, and evaluating the fallout. If this isn't useful we can drop it.

As long as it's off by default and enabled by some extremely specific
flag like -Wpedantic-aliasing.  I am not willing to consider a diagnostic
that's on by default unless it's policing the relaxed aliasing rules that
Clang uses, where l-values that "obviously" alias are allowed to alias
regardless of type.

> However, I think we could do a couple of things more to avoid false-positives:
> 
> 1) Only warn on reference reinterpret casts when the result is in an lvalue-to-rvalue cast to avoid this false positive:
> 
> float f = 0.0f;
> int *x = &reinterpret_cast<int&>(f);

That's probably reasonable.  I'd actually be okay with warning on this
in general if you can come up with a small set of "universal-ish" types that
you think it's reasonable to cast something through, like maybe void& and
void*.

Oh, and of course you can't warn about reinterpret_casts to/from
references/pointers to char types.

> 2) Don't warn for reinterpret casts of tag types.

Probably a good first iteration.

John.



More information about the cfe-commits mailing list