From: Argyrios Kyrtzidis Date: Fri, 22 Apr 2011 22:31:13 +0000 (+0000) Subject: reinterpret_cast to reference of a bit-field is not allowed. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b464a5b18916b467ed884d07f9e34295d39cec0a;p=clang reinterpret_cast to reference of a bit-field is not allowed. Fixes rdar://9202628 & http://llvm.org/PR9564. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130024 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index dbc30c0dfb..6e130b1797 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2879,6 +2879,8 @@ def err_bad_cxx_cast_member_pointer_size : Error< "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. diff --git a/lib/Sema/SemaCXXCast.cpp b/lib/Sema/SemaCXXCast.cpp index a0d1514bd0..29475539d7 100644 --- a/lib/Sema/SemaCXXCast.cpp +++ b/lib/Sema/SemaCXXCast.cpp @@ -1326,6 +1326,13 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, // C++ 5.2.10p10: [...] a reference cast reinterpret_cast(x) has the // same effect as the conversion *reinterpret_cast(&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; + } + // This code does this transformation for the checked types. DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType()); SrcType = Self.Context.getPointerType(SrcType); diff --git a/test/SemaCXX/reinterpret-cast.cpp b/test/SemaCXX/reinterpret-cast.cpp index 9e5211cb1b..2ec45b37cd 100644 --- a/test/SemaCXX/reinterpret-cast.cpp +++ b/test/SemaCXX/reinterpret-cast.cpp @@ -108,3 +108,8 @@ void const_arrays() { (void)reinterpret_cast(s); // expected-error {{reinterpret_cast from 'const STRING *' (aka 'char const (*)[10]') to 'char *' casts away qualifiers}} (void)reinterpret_cast(c); } + +namespace PR9564 { + struct a { int a : 10; }; a x; + int *y = &reinterpret_cast(x.a); // expected-error {{reinterpret_cast of a bit-field to 'int &' needs its address which is not allowed}} +}