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?
On Mon, Apr 11, 2011 at 10:13 AM, John McCall <[email protected]> wrote:
> 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.
>
Index: test/SemaCXX/reinterpret-cast.cpp
===================================================================
--- test/SemaCXX/reinterpret-cast.cpp (revision 130335)
+++ test/SemaCXX/reinterpret-cast.cpp (working copy)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -ffreestanding %s
+// RUN: %clang_cc1 -fsyntax-only -verify -ffreestanding -Wundefined-reinterpret-cast %s
#include <stdint.h>
@@ -116,3 +116,53 @@
__attribute((ext_vector_type(4))) typedef float v4;
float& w(v4 &a) { return reinterpret_cast<float&>(a[1]); } // expected-error {{not allowed}}
}
+
+void dereference_reinterpret_cast() {
+ struct A {};
+ class B {};
+ A a, aa;
+ B b, bb;
+ long x, xx;
+ double z, zz;
+ char c, cc;
+ unsigned char uc;
+ void* v_ptr;
+ z = reinterpret_cast<double&>(x); // expected-warning {{reinterpret_cast from 'long' to 'double &' has undefined behavior}}
+ c = reinterpret_cast<char&>(x);
+ z = *reinterpret_cast<double*>(&x); // expected-warning {{dereference of type 'double *' that was reinterpret_cast from type 'long *' has undefined behavior}}
+ c = *reinterpret_cast<char*>(&x);
+
+ a = reinterpret_cast<A&>(b);
+ a = *reinterpret_cast<A*>(&b);
+ a = reinterpret_cast<A&>(aa);
+ a = *reinterpret_cast<A*>(&aa);
+ b = reinterpret_cast<B&>(bb);
+ b = *reinterpret_cast<B*>(&bb);
+ x = reinterpret_cast<long&>(xx);
+ x = *reinterpret_cast<long*>(&xx);
+ z = reinterpret_cast<double&>(zz);
+ z = *reinterpret_cast<double*>(&zz);
+ c = reinterpret_cast<char&>(cc);
+ c = *reinterpret_cast<char*>(&cc);
+
+ // Casting to and from chars are allowable
+ a = reinterpret_cast<A&>(cc);
+ a = *reinterpret_cast<A*>(&cc);
+ b = reinterpret_cast<B&>(cc);
+ b = *reinterpret_cast<B*>(&cc);
+ x = reinterpret_cast<long&>(cc);
+ x = *reinterpret_cast<long*>(&cc);
+ z = reinterpret_cast<double&>(cc);
+ z = *reinterpret_cast<double*>(&cc);
+ c = reinterpret_cast<char&>(xx);
+ c = *reinterpret_cast<char*>(&xx);
+ c = reinterpret_cast<char&>(zz);
+ c = *reinterpret_cast<char*>(&zz);
+
+ // Casting from void pointer.
+ a = *reinterpret_cast<A*>(v_ptr);
+ b = *reinterpret_cast<B*>(v_ptr);
+ x = *reinterpret_cast<long*>(v_ptr);
+ z = *reinterpret_cast<double*>(v_ptr);
+ c = *reinterpret_cast<char*>(v_ptr);
+}
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td (revision 130335)
+++ include/clang/Basic/DiagnosticSemaKinds.td (working copy)
@@ -2637,6 +2637,10 @@
"indirection of non-volatile null pointer will be deleted, not trap">, InGroup<NullDereference>;
def note_indirection_through_null : Note<
"consider using __builtin_trap() or qualifying pointer with 'volatile'">;
+def warn_pointer_indirection_from_incompatible_type : Warning<
+ "dereference of type %0 that was reinterpret_cast from type %1 has undefined "
+ "behavior.">,
+ InGroup<DiagGroup<"undefined-reinterpret-cast">>, DefaultIgnore;
def err_assignment_requires_nonfragile_object : Error<
"cannot assign to class object in non-fragile ABI (%0 invalid)">;
@@ -2900,6 +2904,9 @@
def err_bad_static_cast_incomplete : Error<"%0 is an incomplete type">;
def err_bad_reinterpret_cast_reference : Error<
"reinterpret_cast of a %0 to %1 needs its address which is not allowed">;
+def warn_undefined_reinterpret_cast : Warning<
+ "reinterpret_cast from %0 to %1 has undefined behavior.">,
+ InGroup<DiagGroup<"undefined-reinterpret-cast">>, DefaultIgnore;
// These messages don't adhere to the pattern.
// FIXME: Display the path somehow better.
Index: include/clang/Sema/Sema.h
===================================================================
--- include/clang/Sema/Sema.h (revision 130335)
+++ include/clang/Sema/Sema.h (working copy)
@@ -2574,6 +2574,10 @@
ParsedType ObjectType,
bool EnteringContext);
+ // Checks that reinterpret casts don't have undefined behavior.
+ // Returns false when the reinterpret cast results in undefined behavior.
+ bool CompatibleReinterpretCast(QualType SrcType, QualType DestPointeeType);
+
/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
ExprResult ActOnCXXNamedCast(SourceLocation OpLoc,
tok::TokenKind Kind,
Index: lib/Sema/SemaCXXCast.cpp
===================================================================
--- lib/Sema/SemaCXXCast.cpp (revision 130335)
+++ lib/Sema/SemaCXXCast.cpp (working copy)
@@ -1288,6 +1288,27 @@
return TC_Success;
}
+// 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,
+ QualType DestType) {
+ // Cast is compatible if the types are the same.
+ if (Context.hasSameUnqualifiedType(DestType, SrcType)) {
+ return true;
+ }
+ // or one of the types is a char or void type
+ if (DestType->isAnyCharacterType() || DestType->isVoidType() ||
+ SrcType->isAnyCharacterType() || SrcType->isVoidType()) {
+ return true;
+ }
+ // or one of the types is a tag type.
+ if (isa<TagType>(SrcType) && isa<TagType>(DestType)) {
+ return true;
+ }
+ return false;
+}
static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType, bool CStyle,
@@ -1324,6 +1345,13 @@
return TC_NotApplicable;
}
+ QualType DestPointeeType = DestTypeTmp->getPointeeType();
+
+ if (!CStyle && !Self.CompatibleReinterpretCast(SrcType, DestPointeeType)) {
+ Self.Diag(OpRange.getBegin(), diag::warn_undefined_reinterpret_cast)
+ << SrcType << DestType << OpRange;
+ }
+
// C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
// same effect as the conversion *reinterpret_cast<T*>(&x) with the
// built-in & and * operators.
@@ -1345,7 +1373,7 @@
}
// This code does this transformation for the checked types.
- DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
+ DestType = Self.Context.getPointerType(DestPointeeType);
SrcType = Self.Context.getPointerType(SrcType);
IsLValueCast = true;
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp (revision 130335)
+++ lib/Sema/SemaExpr.cpp (working copy)
@@ -8393,7 +8393,18 @@
Op = ConvResult.take();
QualType OpTy = Op->getType();
QualType Result;
-
+
+ if (S.getLangOptions().CPlusPlus && isa<CXXReinterpretCastExpr>(Op)) {
+ QualType OpOrigType = Op->IgnoreParenCasts()->getType();
+ if (OpOrigType->isPointerType() && OpTy->isPointerType()) {
+ if (!S.CompatibleReinterpretCast(OpOrigType->getPointeeType(),
+ OpTy->getPointeeType())) {
+ S.Diag(OpLoc, diag::warn_pointer_indirection_from_incompatible_type)
+ << OpTy << OpOrigType << Op->getSourceRange();
+ }
+ }
+ }
+
// Note that per both C89 and C99, indirection is always legal, even if OpTy
// is an incomplete type or void. It would be possible to warn about
// dereferencing a void pointer, but it's completely well-defined, and such a
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits