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

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:


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


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!


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())
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].

[1] [](

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

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


oh, I got it, I’ll investigate, thank you.

This code should do what you want