Implemented access check for ivars accessed inside
c-style functions declared inside objc @implementations.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66087 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/PrintParserCallbacks.cpp b/Driver/PrintParserCallbacks.cpp
index 0a6d09a..7600378 100644
--- a/Driver/PrintParserCallbacks.cpp
+++ b/Driver/PrintParserCallbacks.cpp
@@ -517,7 +517,8 @@
SourceLocation OpLoc,
tok::TokenKind OpKind,
SourceLocation MemberLoc,
- IdentifierInfo &Member) {
+ IdentifierInfo &Member,
+ DeclTy *ImplDecl) {
llvm::cout << __FUNCTION__ << "\n";
return ExprEmpty();
}
diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h
index ab87eb3..c5a1628 100644
--- a/include/clang/Parse/Action.h
+++ b/include/clang/Parse/Action.h
@@ -642,7 +642,8 @@
SourceLocation OpLoc,
tok::TokenKind OpKind,
SourceLocation MemberLoc,
- IdentifierInfo &Member) {
+ IdentifierInfo &Member,
+ DeclTy *ObjCImpDecl) {
return ExprEmpty();
}
diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp
index 6c6842e..4a81d30 100644
--- a/lib/Parse/ParseExpr.cpp
+++ b/lib/Parse/ParseExpr.cpp
@@ -852,7 +852,8 @@
if (!LHS.isInvalid()) {
LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
OpKind, Tok.getLocation(),
- *Tok.getIdentifierInfo());
+ *Tok.getIdentifierInfo(),
+ ObjCImpDecl);
}
ConsumeToken();
break;
diff --git a/lib/Parse/ParseObjc.cpp b/lib/Parse/ParseObjc.cpp
index 3637d14..1cbc0e7 100644
--- a/lib/Parse/ParseObjc.cpp
+++ b/lib/Parse/ParseObjc.cpp
@@ -1082,12 +1082,15 @@
Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
assert(Tok.isObjCAtKeyword(tok::objc_end) &&
"ParseObjCAtEndDeclaration(): Expected @end");
+ DeclTy *Result = ObjCImpDecl;
ConsumeToken(); // the "end" identifier
- if (ObjCImpDecl)
+ if (ObjCImpDecl) {
Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
+ ObjCImpDecl = 0;
+ }
else
Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
- return ObjCImpDecl;
+ return Result;
}
/// compatibility-alias-decl:
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index f68ef92..31a8c38 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -1158,7 +1158,8 @@
SourceLocation OpLoc,
tok::TokenKind OpKind,
SourceLocation MemberLoc,
- IdentifierInfo &Member);
+ IdentifierInfo &Member,
+ DeclTy *ImplDecl=0);
bool ConvertArgumentsForCall(CallExpr *Call, Expr *Fn,
FunctionDecl *FDecl,
const FunctionProtoType *Proto,
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index f9d89fd..f71f82d 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -1666,16 +1666,11 @@
return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]);
}
-ObjCImplementationDecl *getCurImplementationDecl(DeclContext *DC) {
- while (DC && !isa<ObjCImplementationDecl>(DC))
- DC = DC->getParent();
- return dyn_cast_or_null<ObjCImplementationDecl>(DC);
-}
-
Action::OwningExprResult
Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc,
tok::TokenKind OpKind, SourceLocation MemberLoc,
- IdentifierInfo &Member) {
+ IdentifierInfo &Member,
+ DeclTy *ObjCImpDecl) {
Expr *BaseExpr = static_cast<Expr *>(Base.release());
assert(BaseExpr && "no record expression");
@@ -1803,17 +1798,24 @@
ObjCInterfaceDecl *ClassOfMethodDecl = 0;
if (ObjCMethodDecl *MD = getCurMethodDecl())
ClassOfMethodDecl = MD->getClassInterface();
- else if (FunctionDecl *FD = getCurFunctionDecl()) {
- // FIXME: This isn't working yet. Will discuss with Fariborz.
- // FIXME: Should be ObjCImplDecl, so categories can work.
- // Need to fiddle with castToDeclContext/castFromDeclContext.
- ObjCImplementationDecl *ImpDecl = getCurImplementationDecl(FD);
- if (ImpDecl)
- ClassOfMethodDecl = ImpDecl->getClassInterface();
+ else if (ObjCImpDecl && getCurFunctionDecl()) {
+ // Case of a c-function declared inside an objc implementation.
+ // FIXME: For a c-style function nested inside an objc implementation
+ // class, there is no implementation context available, so we pass down
+ // the context as argument to this routine. Ideally, this context need
+ // be passed down in the AST node and somehow calculated from the AST
+ // for a function decl.
+ Decl *ImplDecl = static_cast<Decl *>(ObjCImpDecl);
+ if (ObjCImplementationDecl *IMPD =
+ dyn_cast<ObjCImplementationDecl>(ImplDecl))
+ ClassOfMethodDecl = IMPD->getClassInterface();
+ else if (ObjCCategoryImplDecl* CatImplClass =
+ dyn_cast<ObjCCategoryImplDecl>(ImplDecl))
+ ClassOfMethodDecl = CatImplClass->getClassInterface();
}
- if (IV->getAccessControl() == ObjCIvarDecl::Private) {
+ if (IV->getAccessControl() == ObjCIvarDecl::Private) {
if (ClassDeclared != IFTy->getDecl() ||
- (ClassOfMethodDecl && (ClassOfMethodDecl != ClassDeclared)))
+ ClassOfMethodDecl != ClassDeclared)
Diag(MemberLoc, diag::error_private_ivar_access) << IV->getDeclName();
}
// @protected
diff --git a/test/SemaObjC/ivar-access-tests.m b/test/SemaObjC/ivar-access-tests.m
index 7f5580d..ad8f603 100644
--- a/test/SemaObjC/ivar-access-tests.m
+++ b/test/SemaObjC/ivar-access-tests.m
@@ -73,7 +73,7 @@
{
MySuperClass *s = 0;
int access;
- access = s->private; // FIXME: {{instance variable 'private' is private}}
+ access = s->private; // expected-error {{instance variable 'private' is private}}
access = s->protected; // expected-error {{instance variable 'protected' is protected}}
return 0;
}