Implement access checking for the "delete" operator. Fixes PR9050,

from Alex Miller!

llvm-svn: 124663
This commit is contained in:
Douglas Gregor 2011-02-01 15:50:11 +00:00
parent 1732850158
commit fa7781384e
5 changed files with 41 additions and 6 deletions

View File

@ -564,6 +564,9 @@ def err_access_field: Error<
def err_access_ctor_field :
Error<"field of type %1 has %select{private|protected}2 constructor">,
AccessControl;
def err_access_dtor : Error<
"calling a %select{private|protected}1 destructor of class %0">,
AccessControl;
def err_access_dtor_base :
Error<"base class %0 has %select{private|protected}1 destructor">,
AccessControl;

View File

@ -1687,7 +1687,15 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
MarkDeclarationReferenced(StartLoc, OperatorDelete);
// FIXME: Check access and ambiguity of operator delete and destructor.
// Check access and ambiguity of operator delete and destructor.
if (const RecordType *RT = PointeeElem->getAs<RecordType>()) {
CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
if (CXXDestructorDecl *Dtor = LookupDestructor(RD)) {
CheckDestructorAccess(Ex->getExprLoc(), Dtor,
PDiag(diag::err_access_dtor) << PointeeElem);
}
}
}
return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm,

View File

@ -89,6 +89,7 @@ namespace test2 {
namespace test3 {
class A {
public:
~A();
};

View File

@ -24,11 +24,23 @@ void f0(T2_A *a) { T2_C x; x.f0(a); }
class T2_A { };
// An alternate version of the same.
//
// FIXME: Revisit this case when we have access control.
class T3_A;
template<typename T>
struct T3_B { void f0(T *a) { delete a; } };
struct T3_C { T3_B<T3_A> x; void f0(T3_A *a) { x.f0(a); } };
struct T3_B {
void f0(T *a) {
delete a; // expected-error{{calling a private destructor of class 'T3_A'}}
}
};
struct T3_C {
T3_B<T3_A> x;
void f0(T3_A *a) {
x.f0(a); // expected-note{{in instantiation of member function 'T3_B<T3_A>::f0' requested here}}
}
};
void f0(T3_A *a) { T3_C x; x.f0(a); }
class T3_A { private: ~T3_A(); };
class T3_A {
private:
~T3_A(); // expected-note{{declared private here}}
};

View File

@ -234,6 +234,17 @@ void f(X14 *x14a, X14 *x14b) {
delete x14a;
}
class X15 {
private:
X15(); // expected-note {{declared private here}}
~X15(); // expected-note {{declared private here}}
};
void f(X15* x) {
new X15(); // expected-error {{calling a private constructor}}
delete x; // expected-error {{calling a private destructor}}
}
namespace PR5918 { // Look for template operator new overloads.
struct S { template<typename T> static void* operator new(size_t, T); };
void test() {