From: Argyrios Kyrtzidis Date: Fri, 22 Apr 2011 23:57:57 +0000 (+0000) Subject: Don't allow reinterpret_cast to reference of vector element and property expression... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bb29d1ba8b0895e3923c724f49845636f35b4bde;p=clang Don't allow reinterpret_cast to reference of vector element and property expression. Thanks goes to Eli Friedman! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130036 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 6e130b1797..910536ab7a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2879,8 +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">; +def err_bad_reinterpret_cast_reference : Error< + "reinterpret_cast of a %0 to %1 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 29475539d7..743fdd2566 100644 --- a/lib/Sema/SemaCXXCast.cpp +++ b/lib/Sema/SemaCXXCast.cpp @@ -1327,9 +1327,18 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, // 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; + const char *inappropriate = 0; + switch (SrcExpr.get()->getObjectKind()) { + default: break; + case OK_BitField: inappropriate = "bit-field"; break; + case OK_VectorComponent: inappropriate = "vector element"; break; + case OK_ObjCProperty: inappropriate = "property expression"; break; + } + if (inappropriate) { + Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference) + << inappropriate << DestType + << OpRange << SrcExpr.get()->getSourceRange(); + msg = 0; SrcExpr = ExprError(); return TC_NotApplicable; } diff --git a/test/SemaCXX/reinterpret-cast.cpp b/test/SemaCXX/reinterpret-cast.cpp index 2ec45b37cd..f5262496c4 100644 --- a/test/SemaCXX/reinterpret-cast.cpp +++ b/test/SemaCXX/reinterpret-cast.cpp @@ -111,5 +111,8 @@ void const_arrays() { 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}} + int *y = &reinterpret_cast(x.a); // expected-error {{not allowed}} + + __attribute((ext_vector_type(4))) typedef float v4; + float& w(v4 &a) { return reinterpret_cast(a[1]); } // expected-error {{not allowed}} }