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

Richard Trieu rtrieu at google.com
Wed Apr 27 18:07:15 PDT 2011


Is there a way that Clang can check if the warning is turned off and skip
the expensive checking?
And did you mean the list in [basic.lval]p15?  [basic.lval]p10 doesn't have
a list.

On Wed, Apr 27, 2011 at 4:32 PM, John McCall <rjmccall at apple.com> wrote:

> On Apr 27, 2011, at 4:18 PM, Richard Trieu wrote:
> > Added a new check so that casting between tag types doesn't give the
> warning.  Casting to/from chars and voids won't throw the warning either.
>  Anything else needed?
>
> +// Returns false if the reinterpret_cast has undefined behavior.
> +// SrcType = A, DestType = B
> +// *reinterpret_cast<B*>(&A)
> +// reinterpret_cast<&B>(A)
> +bool Sema::CompatibleReinterpretCast(QualType SrcType,
>
> You have punctuation backwards in the comment.  Also, this is a predicate
> and should be named like one, e.g. "isCompatibleReinterpretCast".
>
> If the intent of this warning is to warn about violations of the aliasing
> rules
> in [basic.lval]p10, you really need to white-list all those cases.  That's
> going
> to be kindof expensive to check on every reinterpret_cast for a warning
> that's disabled by default.
>
> +  if (S.getLangOptions().CPlusPlus && isa<CXXReinterpretCastExpr>(Op)) {
> +    QualType OpOrigType = Op->IgnoreParenCasts()->getType();
> +    if (OpOrigType->isPointerType() && OpTy->isPointerType()) {
>
> Unconditionally checking for CXXReinterpretCastExpr is very likely
> faster than checking for the language option.
>
> Also, you should use getAs<PointerType>() instead of
> isPointerType() / getPointeeType().
>
> John.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110427/1df28467/attachment.html>


More information about the cfe-commits mailing list