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

John McCall rjmccall at apple.com
Wed Apr 27 16:32:46 PDT 2011


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.



More information about the cfe-commits mailing list