[cfe-commits] [PATCH] Extend lexical scope of attributes to include function parameters.
Douglas Gregor
dgregor at apple.com
Wed Sep 7 10:44:42 PDT 2011
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
More information about the cfe-commits
mailing list