[cfe-commits] r66087 - in /cfe/trunk: Driver/PrintParserCallbacks.cpp include/clang/Parse/Action.h lib/Parse/ParseExpr.cpp lib/Parse/ParseObjc.cpp lib/Sema/Sema.h lib/Sema/SemaExpr.cpp test/SemaObjC/ivar-access-tests.m

steve naroff snaroff at apple.com
Wed Mar 4 14:46:47 PST 2009


Hey Fariborz,

Thanks for the follow-up on this.

It's too bad the solution below isn't more localized (to Sema). From  
my perspective, the Parser/Actions interface shouldn't have to change  
for this bug fix.

snaroff

On Mar 4, 2009, at 5:30 PM, Fariborz Jahanian wrote:

> Author: fjahanian
> Date: Wed Mar  4 16:30:12 2009
> New Revision: 66087
>
> URL: http://llvm.org/viewvc/llvm-project?rev=66087&view=rev
> Log:
> Implemented access check for ivars accessed inside
> c-style functions declared inside objc @implementations.
>
>
> Modified:
>    cfe/trunk/Driver/PrintParserCallbacks.cpp
>    cfe/trunk/include/clang/Parse/Action.h
>    cfe/trunk/lib/Parse/ParseExpr.cpp
>    cfe/trunk/lib/Parse/ParseObjc.cpp
>    cfe/trunk/lib/Sema/Sema.h
>    cfe/trunk/lib/Sema/SemaExpr.cpp
>    cfe/trunk/test/SemaObjC/ivar-access-tests.m
>
> Modified: cfe/trunk/Driver/PrintParserCallbacks.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/PrintParserCallbacks.cpp?rev=66087&r1=66086&r2=66087&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/Driver/PrintParserCallbacks.cpp (original)
> +++ cfe/trunk/Driver/PrintParserCallbacks.cpp Wed Mar  4 16:30:12 2009
> @@ -517,7 +517,8 @@
>                                                       SourceLocation  
> OpLoc,
>                                                       tok::TokenKind  
> OpKind,
>                                                       SourceLocation  
> MemberLoc,
> -                                                       
> IdentifierInfo &Member) {
> +                                                       
> IdentifierInfo &Member,
> +                                                      DeclTy  
> *ImplDecl) {
>       llvm::cout << __FUNCTION__ << "\n";
>       return ExprEmpty();
>     }
>
> Modified: cfe/trunk/include/clang/Parse/Action.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=66087&r1=66086&r2=66087&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/include/clang/Parse/Action.h (original)
> +++ cfe/trunk/include/clang/Parse/Action.h Wed Mar  4 16:30:12 2009
> @@ -642,7 +642,8 @@
>                                                     SourceLocation  
> OpLoc,
>                                                     tok::TokenKind  
> OpKind,
>                                                     SourceLocation  
> MemberLoc,
> -                                                    IdentifierInfo  
> &Member) {
> +                                                    IdentifierInfo  
> &Member,
> +                                                    DeclTy  
> *ObjCImpDecl) {
>     return ExprEmpty();
>   }
>
>
> Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=66087&r1=66086&r2=66087&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
> +++ cfe/trunk/lib/Parse/ParseExpr.cpp Wed Mar  4 16:30:12 2009
> @@ -852,7 +852,8 @@
>       if (!LHS.isInvalid()) {
>         LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS),  
> OpLoc,
>                                                OpKind,  
> Tok.getLocation(),
> -                                                
> *Tok.getIdentifierInfo());
> +                                                
> *Tok.getIdentifierInfo(),
> +                                               ObjCImpDecl);
>       }
>       ConsumeToken();
>       break;
>
> Modified: cfe/trunk/lib/Parse/ParseObjc.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseObjc.cpp?rev=66087&r1=66086&r2=66087&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/Parse/ParseObjc.cpp (original)
> +++ cfe/trunk/lib/Parse/ParseObjc.cpp Wed Mar  4 16:30:12 2009
> @@ -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:
>
> Modified: cfe/trunk/lib/Sema/Sema.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=66087&r1=66086&r2=66087&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/Sema/Sema.h (original)
> +++ cfe/trunk/lib/Sema/Sema.h Wed Mar  4 16:30:12 2009
> @@ -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,
>
> Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=66087&r1=66086&r2=66087&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Mar  4 16:30:12 2009
> @@ -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
>
> Modified: cfe/trunk/test/SemaObjC/ivar-access-tests.m
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/ivar-access-tests.m?rev=66087&r1=66086&r2=66087&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/test/SemaObjC/ivar-access-tests.m (original)
> +++ cfe/trunk/test/SemaObjC/ivar-access-tests.m Wed Mar  4 16:30:12  
> 2009
> @@ -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;
> }
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list