How to retrieve a custom annotation in an ASTVisitor?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to retrieve a custom annotation in an ASTVisitor?

Sumner, Brian via cfe-dev

Hi everyone,

I attach in my ASTConsumer for certain functions an annotation like this:

bool HandleTopLevelDecl(DeclGroupRef DR) override {

    ...

    functionDecl->addAttr(AnnotateAttr::CreateImplicit(FD->getASTContext(), "SomeAttribute"));

    ...

}

But how can I later retrieve this annotation in my visitor?

Any hint is welcome ;-)

Thanks


Marcel





_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to retrieve a custom annotation in an ASTVisitor?

Sumner, Brian via cfe-dev
On Wed, Aug 2, 2017 at 12:04 PM, Marcel Schaible via cfe-dev
<[hidden email]> wrote:

> Hi everyone,
>
> I attach in my ASTConsumer for certain functions an annotation like this:
>
> bool HandleTopLevelDecl(DeclGroupRef DR) override {
>
>     ...
>
>     functionDecl->addAttr(AnnotateAttr::CreateImplicit(FD->getASTContext(),
> "SomeAttribute"));
>
>     ...
>
> }
>
> But how can I later retrieve this annotation in my visitor?

You can use functionDecl->hasAttr<AnnotateAttr>() to test for the
presence of the attribute, or const auto *A =
functionDecl->getAttr<AnnotateAttr>() to get the attribute AST node
from the declaration.

HTH!

~Aaron

>
> Any hint is welcome ;-)
>
> Thanks
>
>
> Marcel
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to retrieve a custom annotation in an ASTVisitor?

Sumner, Brian via cfe-dev
Thanks Aaron, works for one annotation.

If I have multiple annotations per function declaration how can I
iterate over them or can I use something like funcdecl->getAttr("SomeAttr")?

Cheers

Marcel

Am 02.08.2017 um 18:16 schrieb Aaron Ballman:

> On Wed, Aug 2, 2017 at 12:04 PM, Marcel Schaible via cfe-dev
> <[hidden email]> wrote:
>> Hi everyone,
>>
>> I attach in my ASTConsumer for certain functions an annotation like this:
>>
>> bool HandleTopLevelDecl(DeclGroupRef DR) override {
>>
>>      ...
>>
>>      functionDecl->addAttr(AnnotateAttr::CreateImplicit(FD->getASTContext(),
>> "SomeAttribute"));
>>
>>      ...
>>
>> }
>>
>> But how can I later retrieve this annotation in my visitor?
> You can use functionDecl->hasAttr<AnnotateAttr>() to test for the
> presence of the attribute, or const auto *A =
> functionDecl->getAttr<AnnotateAttr>() to get the attribute AST node
> from the declaration.
>
> HTH!
>
> ~Aaron
>
>> Any hint is welcome ;-)
>>
>> Thanks
>>
>>
>> Marcel
>>
>>
>>
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> [hidden email]
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to retrieve a custom annotation in an ASTVisitor?

Sumner, Brian via cfe-dev
On Wed, Aug 2, 2017 at 12:43 PM, Marcel Schaible
<[hidden email]> wrote:
> Thanks Aaron, works for one annotation.
>
> If I have multiple annotations per function declaration how can I iterate
> over them or can I use something like funcdecl->getAttr("SomeAttr")?

You can use the specific_attrs<> function to get a range, like:

for (const auto *A : functionDecl->specific_attrs<AnnotateAttr>()) {

}

(There is also specific_attr_begin<>() and specific_attr_end<>() if
you want iterators instead of a range.)

~Aaron
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Loading...