diff --git a/clang/include/clang/Sema/Initialization.h b/clang/include/clang/Sema/Initialization.h --- a/clang/include/clang/Sema/Initialization.h +++ b/clang/include/clang/Sema/Initialization.h @@ -689,6 +689,9 @@ return Context >= IC_StaticCast; } + /// Determine whether this initialization is a static cast. + bool isStaticCast() const { return Context == IC_StaticCast; } + /// Determine whether this initialization is a C-style cast. bool isCStyleOrFunctionalCast() const { return Context >= IC_CStyleCast; 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 @@ -229,6 +229,18 @@ unsigned &msg, CastKind &Kind); +static void CheckNoderef(Sema &S, const QualType FromType, + const QualType ToType, SourceLocation OpLoc) { + if (const auto *PtrType = dyn_cast(FromType)) { + if (PtrType->getPointeeType()->hasAttr(attr::NoDeref)) { + if (const auto *DestType = dyn_cast(ToType)) { + if (!DestType->getPointeeType()->hasAttr(attr::NoDeref)) { + S.Diag(OpLoc, diag::warn_noderef_to_dereferenceable_pointer); + } + } + } + } +} /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's. ExprResult @@ -249,6 +261,8 @@ CheckExtraCXXDefaultArguments(D); } + CheckNoderef(*this, E->getType(), TInfo->getType(), OpLoc); + return BuildCXXNamedCast(OpLoc, Kind, TInfo, E, SourceRange(LAngleBracketLoc, RAngleBracketLoc), SourceRange(LParenLoc, RParenLoc)); @@ -2948,6 +2962,8 @@ // -Wcast-qual DiagnoseCastQual(Op.Self, Op.SrcExpr, Op.DestType); + CheckNoderef(*this, Op.SrcExpr.get()->getType(), Op.ResultType, LPLoc); + return Op.complete(CStyleCastExpr::Create(Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(), &Op.BasePath, CastTypeInfo, LPLoc, RPLoc)); diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -8179,9 +8179,13 @@ if (const auto *ToPtrType = Step->Type->getAs()) { if (FromPtrType->getPointeeType()->hasAttr(attr::NoDeref) && !ToPtrType->getPointeeType()->hasAttr(attr::NoDeref)) { - S.Diag(CurInit.get()->getExprLoc(), - diag::warn_noderef_to_dereferenceable_pointer) - << CurInit.get()->getSourceRange(); + // Do not check static casts here because they are checked earlier + // in Sema::ActOnCXXNamedCast() + if (!Kind.isStaticCast()) { + S.Diag(CurInit.get()->getExprLoc(), + diag::warn_noderef_to_dereferenceable_pointer) + << CurInit.get()->getSourceRange(); + } } } } diff --git a/clang/test/Frontend/noderef.c b/clang/test/Frontend/noderef.c --- a/clang/test/Frontend/noderef.c +++ b/clang/test/Frontend/noderef.c @@ -207,3 +207,7 @@ do {} while (*p); // expected-warning{{dereferencing p; was declared with a 'noderef' type}} return *p; // expected-warning{{dereferencing p; was declared with a 'noderef' type}} } + +int *implicit_cast(NODEREF int *x) { + return (int *)x; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +} 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 @@ -80,12 +80,28 @@ int member; int NODEREF *member2; int NODEREF member3; // expected-warning{{'noderef' can only be used on an array or pointer type}} + int *member4; + + int func() { return member; } + virtual int func_virt() { return member; } + + A(NODEREF int *x) : member4(x) {} // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} }; +class Child : public A {}; + void MemberPointer() { int NODEREF A::*var = &A::member; // expected-warning{{'noderef' can only be used on an array or pointer type}} } +int MethodCall(NODEREF A *a) { // expected-note{{a declared here}} + return a->func(); // expected-warning{{dereferencing a; was declared with a 'noderef' type}} +} + +int ChildCall(NODEREF Child *child) { // expected-note{{child declared here}} + return child->func(); // expected-warning{{dereferencing child; was declared with a 'noderef' type}} +} + template class B { Ty NODEREF *member; @@ -100,3 +116,37 @@ int NODEREF *glob_ptr; // expected-note{{glob_ptr declared here}} int glob_int = *glob_ptr; // expected-warning{{dereferencing glob_ptr; was declared with a 'noderef' type}} + +void cast_from_void_ptr(NODEREF void *x) { + int *a = static_cast(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} + 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}} +} + +int *static_cast_from_same_ptr_type(NODEREF int *x) { + return static_cast(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +} + +A *dynamic_cast_up(NODEREF Child *child) { + return dynamic_cast(child); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +} + +Child *dynamic_cast_down(NODEREF A *a) { + return dynamic_cast(a); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +} + +A *dynamic_cast_side(NODEREF A *a) { + return dynamic_cast(a); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +} + +void *dynamic_cast_to_void_ptr(NODEREF A *a) { + return dynamic_cast(a); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +} + +int *const_cast_check(NODEREF const int *x) { + return const_cast(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +} + +const int *const_cast_check(NODEREF int *x) { + return const_cast(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}} +}