[cfe-commits] r130024 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaCXXCast.cpp test/SemaCXX/reinterpret-cast.cpp

Eli Friedman eli.friedman at gmail.com
Fri Apr 22 15:44:51 PDT 2011


On Fri, Apr 22, 2011 at 3:31 PM, Argyrios Kyrtzidis <akyrtzi at gmail.com> wrote:
> Author: akirtzidis
> Date: Fri Apr 22 17:31:13 2011
> New Revision: 130024
>
> URL: http://llvm.org/viewvc/llvm-project?rev=130024&view=rev
> Log:
> reinterpret_cast to reference of a bit-field is not allowed.
>
> Fixes rdar://9202628 & http://llvm.org/PR9564.
>
> Modified:
>    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>    cfe/trunk/lib/Sema/SemaCXXCast.cpp
>    cfe/trunk/test/SemaCXX/reinterpret-cast.cpp
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=130024&r1=130023&r2=130024&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Apr 22 17:31:13 2011
> @@ -2879,6 +2879,8 @@
>   "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 err_bad_reinterpret_cast_bitfield : Error<
> +  "reinterpret_cast of a bit-field to %2 needs its address which is not allowed">;
>
>  // These messages don't adhere to the pattern.
>  // FIXME: Display the path somehow better.
>
> Modified: cfe/trunk/lib/Sema/SemaCXXCast.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCXXCast.cpp?rev=130024&r1=130023&r2=130024&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaCXXCast.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaCXXCast.cpp Fri Apr 22 17:31:13 2011
> @@ -1326,6 +1326,13 @@
>     // 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.
> +
> +    // Cannot get address of a bitfield.
> +    if (SrcExpr.get()->getObjectKind() == OK_BitField) {
> +      msg = diag::err_bad_reinterpret_cast_bitfield;
> +      return TC_NotApplicable;
> +    }

I think you need to handle OK_VectorComponent and OK_ObjCProperty as
well; AFAICT, this doesn't handle, for example, the following related
case:
 __attribute((ext_vector_type(4))) typedef float v4;
float& x(v4 &a) { return reinterpret_cast<float&>(a[1]); }

-Eli




More information about the cfe-commits mailing list