How to match clang::attr::Annotate with specific value?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to match clang::attr::Annotate with specific value?

David Chisnall via cfe-dev
For example I have a struct:
struct [[clang::annotate("example")]] ExampleStruct {
int i;
float f;
double d;
int* ip;
uint32_t ii;
};
Currently I use the matcher like this:
clang::ast_matchers::cxxRecordDecl(clang::ast_matchers::hasDefinition(),
clang::ast_matchers::hasAttr(clang::attr::Annotate)).bind("example_struct");

Can I go further by making the matcher check if the annotate is "example"?

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

Re: How to match clang::attr::Annotate with specific value?

David Chisnall via cfe-dev
Hello,

Yes of course you can do that, you can first interrogate Decl::hasAttrs [1], and if that’s true you can get the AttrVec [2].
I would suggest to wrapp this in a custom matcher.

Hope this helps! 

[2] https://clang.llvm.org/doxygen/classclang_1_1Decl.html#a59463d45ab90b696a368b58d23eb49aa

On 5 Jul 2020, at 11:25, Yafei Liu via cfe-dev <[hidden email]> wrote:

For example I have a struct:
struct [[clang::annotate("example")]] ExampleStruct {
int i;
float f;
double d;
int* ip;
uint32_t ii;
};
Currently I use the matcher like this:
clang::ast_matchers::cxxRecordDecl(clang::ast_matchers::hasDefinition(),
clang::ast_matchers::hasAttr(clang::attr::Annotate)).bind("example_struct");

Can I go further by making the matcher check if the annotate is "example"?
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


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

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to match clang::attr::Annotate with specific value?

David Chisnall via cfe-dev
Some more info about the answer from my previous reply if you want a more direct approach you can do something like:
 
157 static bool isAnnotatedToAllowDirectAssignment(const Decl *D) {
158  for (const auto *Ann : D->specific_attrs<AnnotateAttr>())
159  if (Ann->getAnnotation() ==
160  "objc_allow_direct_instance_variable_assignment")
161  return true;
162  return false;
163 }
```
Of course wrapping this in a custom matcher and modified according to your needs, the example was take from `DirectlvarAssignment Checker` [1].


On 5 Jul 2020, at 11:39, Andi-Bogdan Postelnicu <[hidden email]> wrote:

Hello,

Yes of course you can do that, you can first interrogate Decl::hasAttrs [1], and if that’s true you can get the AttrVec [2].
I would suggest to wrapp this in a custom matcher.

Hope this helps! 

[2] https://clang.llvm.org/doxygen/classclang_1_1Decl.html#a59463d45ab90b696a368b58d23eb49aa

On 5 Jul 2020, at 11:25, Yafei Liu via cfe-dev <[hidden email]> wrote:

For example I have a struct:
struct [[clang::annotate("example")]] ExampleStruct {
int i;
float f;
double d;
int* ip;
uint32_t ii;
};
Currently I use the matcher like this:
clang::ast_matchers::cxxRecordDecl(clang::ast_matchers::hasDefinition(),
clang::ast_matchers::hasAttr(clang::attr::Annotate)).bind("example_struct");

Can I go further by making the matcher check if the annotate is "example"?
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



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

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to match clang::attr::Annotate with specific value?

David Chisnall via cfe-dev
Hi Andi-Bogdan, do you mean I should do the checking in the MatchCallback, as long as this cannot be done in a matcher?

On Sun, Jul 5, 2020 at 4:50 PM Andi-Bogdan Postelnicu <[hidden email]> wrote:
Some more info about the answer from my previous reply if you want a more direct approach you can do something like:
 
157 static bool isAnnotatedToAllowDirectAssignment(const Decl *D) {
158  for (const auto *Ann : D->specific_attrs<AnnotateAttr>())
159  if (Ann->getAnnotation() ==
160  "objc_allow_direct_instance_variable_assignment")
161  return true;
162  return false;
163 }
```
Of course wrapping this in a custom matcher and modified according to your needs, the example was take from `DirectlvarAssignment Checker` [1].


On 5 Jul 2020, at 11:39, Andi-Bogdan Postelnicu <[hidden email]> wrote:

Hello,

Yes of course you can do that, you can first interrogate Decl::hasAttrs [1], and if that’s true you can get the AttrVec [2].
I would suggest to wrapp this in a custom matcher.

Hope this helps! 

[2] https://clang.llvm.org/doxygen/classclang_1_1Decl.html#a59463d45ab90b696a368b58d23eb49aa

On 5 Jul 2020, at 11:25, Yafei Liu via cfe-dev <[hidden email]> wrote:

For example I have a struct:
struct [[clang::annotate("example")]] ExampleStruct {
int i;
float f;
double d;
int* ip;
uint32_t ii;
};
Currently I use the matcher like this:
clang::ast_matchers::cxxRecordDecl(clang::ast_matchers::hasDefinition(),
clang::ast_matchers::hasAttr(clang::attr::Annotate)).bind("example_struct");

Can I go further by making the matcher check if the annotate is "example"?
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



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

Re: How to match clang::attr::Annotate with specific value?

David Chisnall via cfe-dev
No, I wasn’t saying this, I was saying you can build a custom matcher like the examples from the Mozilla repo [1].

[1] https://searchfox.org/mozilla-central/source/build/clang-plugin/CustomMatchers.h#22

On 5 Jul 2020, at 11:57, Yafei Liu <[hidden email]> wrote:

Hi Andi-Bogdan, do you mean I should do the checking in the MatchCallback, as long as this cannot be done in a matcher?


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

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to match clang::attr::Annotate with specific value?

David Chisnall via cfe-dev
oh, I got it, I'll investigate, thank you.

On Sun, Jul 5, 2020 at 5:00 PM Andi-Bogdan Postelnicu <[hidden email]> wrote:
No, I wasn’t saying this, I was saying you can build a custom matcher like the examples from the Mozilla repo [1].

[1] https://searchfox.org/mozilla-central/source/build/clang-plugin/CustomMatchers.h#22

On 5 Jul 2020, at 11:57, Yafei Liu <[hidden email]> wrote:

Hi Andi-Bogdan, do you mean I should do the checking in the MatchCallback, as long as this cannot be done in a matcher?


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

Re: How to match clang::attr::Annotate with specific value?

David Chisnall via cfe-dev
In reply to this post by David Chisnall via cfe-dev
This code should do what you want
-----------------------------------------------------------------------
using namespace clang;

// General case
AST_MATCHER_P(Decl, hasAnnotatedMessage, std::string, Message) {
  return llvm::any_of(Node.specific_attrs<AnnotateAttr>(),
                      [&](const AnnotateAttr *Annotate) {
                        return Annotate->getAnnotation() == Message;
                      });
}

// Your specific case
AST_MATCHER(Decl, hasExampleAnnotation) {
  return llvm::any_of(Node.specific_attrs<AnnotateAttr>(),
                      [](const AnnotateAttr *Annotate) {
                        return Annotate->getAnnotation() == "example";
                      });
}

-----------------------------------------------------------------------

Hope this helps,
Nathan James

On Sun, 2020-07-05 at 16:25 +0800, Yafei Liu via cfe-dev wrote:

> For example I have a struct:
> struct [[clang::annotate("example")]] ExampleStruct {
>   int i;
>   float f;
>   double d;
>   int* ip;
>   uint32_t ii;
> };
> Currently I use the matcher like this:
> clang::ast_matchers::cxxRecordDecl(clang::ast_matchers::hasDefinition
> (),
>     clang::ast_matchers::hasAttr(clang::attr::Annotate)).bind("exampl
> e_struct");
>
> Can I go further by making the matcher check if the annotate is
> "example"?
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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