Revising the patch per your suggestions:
1) Make this warning off by default
2) Removing unrelated whitespace changes
3) Replace underscores with hyphens in diagnostic group name

On Sun, Mar 27, 2011 at 3:13 AM, John McCall <[email protected]> wrote:

> On Mar 27, 2011, at 2:44 AM, Douglas Gregor wrote:
> > On Mar 22, 2011, at 10:24 PM, Richard Trieu wrote:
> >> Add a warning for when reinterpret_cast leads to undefined behavior.
>  Also updated reinterpret_cast tests and turned off this warning for tests
> that don't need it.
> >
> > I'm not totally convinced that this warning is useful. The issue is that
> we're not really invoking undefined behavior by performing the cast, or even
> dereferencing the result of that cast, so long as the memory we're pointing
> to actually has an object of the right type. However, that's not what this
> warning is checking: this warning is checking whether the declared type
> before the cast, and the type we're casting to, are compatible.
> >
> > Overall, I expect that this warning will have a fairly high
> false-positive rate, since programmers tend to use reinterpret_cast in very
> specific ways where they know far more about the type of the object behind
> the pointer/reference than the type system says. To really catch problems
> with reinterpret_cast, we're likely to need some stronger analysis (e.g.,
> something in the static analyzer) that checks that we aren't referring to
> the same lvalue in different places with different, incompatible types.
>
> This isn't just an abstract point — we use reinterpret_cast in various
> Clang headers to convert up and down class hierarchies that we don't want to
> require the inclusion of headers for.  In addition, while reinterpreting a
> pointer to a double object as an int* and accessing it is technically
> undefined behavior because of aliasing rules, it's actually such an
> important idiom that a major design goal of the LLVM aliasing optimization
> has been to make sure that "sufficiently obvious" code is not broken by
> strict aliasing.
>
> I think that even if this warning were doing the more sophisticated
> analysis Doug has in mind, it would still be inappropriate for it to be
> enabled by default.
>
> Also, your patch has unrelated whitespace changes;  please remove these.
>
> John.
Index: test/SemaCXX/reinterpret-cast.cpp
===================================================================
--- test/SemaCXX/reinterpret-cast.cpp	(revision 128089)
+++ 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>
 
@@ -108,3 +108,54 @@
   (void)reinterpret_cast<char *>(s); // expected-error {{reinterpret_cast from 'const STRING *' (aka 'char const (*)[10]') to 'char *' casts away constness}}
   (void)reinterpret_cast<const STRING *>(c);
 }
+
+void dereference_reinterpret_cast() {
+  struct A {};
+  struct 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);  // expected-warning {{reinterpret_cast from 'B' to 'A &' has undefined behavior}}
+  a = *reinterpret_cast<A*>(&b);  // expected-warning {{dereference of type 'A *' that was reinterpret_cast from type 'B *' has undefined behavior}}
+
+  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 128089)
+++ include/clang/Basic/DiagnosticSemaKinds.td	(working copy)
@@ -2562,6 +2562,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)">;
@@ -2817,6 +2821,9 @@
   "cannot %select{||reinterpret_cast||C-style cast|}0 from member pointer "
   "type %1 to member pointer type %2 of different size">;
 def err_bad_static_cast_incomplete : Error<"%0 is an incomplete type">;
+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 128089)
+++ include/clang/Sema/Sema.h	(working copy)
@@ -2409,6 +2409,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 128089)
+++ lib/Sema/SemaCXXCast.cpp	(working copy)
@@ -1249,9 +1249,22 @@
   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) {
+  if (Context.hasSameUnqualifiedType(DestType, SrcType)) {
+    return true;
+  }
+  if (DestType->isAnyCharacterType() || DestType->isVoidType() ||
+      SrcType->isAnyCharacterType() || SrcType->isVoidType()) {
+    return true;
+  }
+  return false;
+}
 
-
-
 static TryCastResult TryReinterpretCast(Sema &Self, Expr *&SrcExpr,
                                         QualType DestType, bool CStyle,
                                         const SourceRange &OpRange,
@@ -1287,11 +1300,18 @@
       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.
     // 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 128089)
+++ lib/Sema/SemaExpr.cpp	(working copy)
@@ -7892,7 +7892,18 @@
   S.UsualUnaryConversions(Op);
   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

Reply via email to