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:

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”?

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!

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

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] [https://clang.llvm.org/doxygen/DirectIvarAssignment_8cpp_source.html](https://clang.llvm.org/doxygen/DirectIvarAssignment_8cpp_source.html)

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

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

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

This code should do what you want