diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -164,9 +164,9 @@ void CheckNoDeref(Sema &S, const QualType FromType, const QualType ToType, SourceLocation OpLoc) { - if (const auto *PtrType = dyn_cast(FromType)) { + if (const auto *PtrType = FromType->getAs()) { if (PtrType->getPointeeType()->hasAttr(attr::NoDeref)) { - if (const auto *DestType = dyn_cast(ToType)) { + if (const auto *DestType = ToType->getAs()) { if (!DestType->getPointeeType()->hasAttr(attr::NoDeref)) { S.Diag(OpLoc, diag::warn_noderef_to_dereferenceable_pointer); } @@ -1032,6 +1032,8 @@ /// like this: /// char *bytes = reinterpret_cast\(int_ptr); void CastOperation::CheckReinterpretCast() { + CheckNoDerefRAII NoderefCheck(*this); + if (ValueKind == VK_RValue && !isPlaceholder(BuiltinType::Overload)) SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get()); else @@ -2489,6 +2491,7 @@ void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle, bool ListInitialization) { assert(Self.getLangOpts().CPlusPlus); + CheckNoDerefRAII NoderefCheck(*this); // Handle placeholders. if (isPlaceholder()) { diff --git a/clang/test/Frontend/noderef.cpp b/clang/test/Frontend/noderef.cpp --- a/clang/test/Frontend/noderef.cpp +++ b/clang/test/Frontend/noderef.cpp @@ -120,20 +120,18 @@ void cast_from_void_ptr(NODEREF void *x) { int *a = static_cast(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} - // Allow regular C-style casts and C-style through reinterpret_casts to be holes - int *b = reinterpret_cast(x); - int *c = (int *)x; + int *b = reinterpret_cast(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} + int *c = (int *)x; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} } void conversion_sequences() { NODEREF int *x; int *x2 = x; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} int *x3 = static_cast(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} - int *x4 = reinterpret_cast(x); // Functional cast - This is exactly equivalent to a C-style cast. typedef int *INT_PTR; - int *x5 = INT_PTR(x); + int *x5 = INT_PTR(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} NODEREF Child *child; Child *child2 = dynamic_cast(child); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}