[cfe-commits] [PATCH] Extend lexical scope of attributes to include function parameters.

Delesley Hutchins delesley at google.com
Wed Sep 7 12:32:36 PDT 2011


Thanks for the comment!  Here's an updated version:

  http://codereview.appspot.com/4978056/

  -DeLesley

On Wed, Sep 7, 2011 at 10:44 AM, Douglas Gregor <dgregor at apple.com> wrote:
>
> On Sep 2, 2011, at 11:42 AM, Delesley Hutchins wrote:
>
>> This patch extends the lexical scope of attributes to include method
>> parameters.  The main motivation for this patch is to implement thread
>> safety attributes, such as the following:
>>
>> class Foo {
>>  int foo(Foo *f) __attribute__((exclusive_locks_required(f->mu))) { }
>>
>>  Mutex mu;
>> };
>>
>> The change in scope is only enabled for attributes that are tagged as
>> being late parsed, using the previous late parsing patch; this
>> currently means that it is enabled only for thread-safety attributes.
>> It also works only on methods and not top-level functions, since
>> top-level functions do not currently use late parsing.
>>
>>  http://codereview.appspot.com/4959055/
>
>
> Okay. One structural comment on this patch:
>
> @@ -759,8 +759,29 @@ void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
>   ParsedAttributes Attrs(AttrFactory);
>   SourceLocation endLoc;
>
> +  // For function attributes, enter the function scope
> +  FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(LA.D);
> +  ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope, FD);
> +
> +  if (FD) {
> +    Actions.PushDeclContext(Actions.CurScope, FD);
> +    for (unsigned P = 0, NumParams = FD->getNumParams(); P < NumParams; ++P) {
> +      ParmVarDecl *Param = FD->getParamDecl(P);
> +      // If the parameter has an identifier, then add it to the scope
> +      if (Param->getIdentifier()) {
> +        Actions.CurScope->AddDecl(Param);
> +        Actions.IdResolver.AddDecl(Param);
> +      }
> +    }
> +  }
> +
>   ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
>
> +  if (FD) {
> +    Actions.PopDeclContext();
> +    FnScope.Exit();  // Pop scope, and remove Decls from IdResolver
> +  }
> +
>   // Late parsed attributes must be attached to Decls by hand.  If the
>   // LA.D is not set, then this was not done properly.
>   assert(LA.D);
> diff --git a/test/SemaCXX/warn-thread-safety-parsing.cpp b/test/SemaCXX/warn-thread-safety-parsing.cpp
> index 5063c64..4e5327d 100644
> --- a/test/SemaCXX/warn-thread-safety-parsing.cpp
> +++ b/test/SemaCXX/warn-thread-safety-parsing.cpp
> @@ -1219,3 +1219,27 @@ struct Foomgoper {
>  };
>
> We try to avoid looking into the AST within the parser. Please add Sema actions or re-use existing actions (such as ActOnStartDelayedMemberDeclarations) that perform the work of injecting parameter names into the context. The parser should merely parse, and call Sema to update the AST and/or lookup tables.
>
>        - Doug
>



-- 
DeLesley Hutchins | Software Engineer | delesley at google.com | 505-206-0315
-------------- next part --------------
A non-text attachment was scrubbed...
Name: function_parameter_parsing3.patch
Type: text/x-patch
Size: 4214 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110907/e8954f54/attachment.bin>


More information about the cfe-commits mailing list