[cfe-dev] How to match clang::attr::Annotate with specific value?

Nathan James via cfe-dev cfe-dev at lists.llvm.org
Sun Jul 5 02:42:58 PDT 2020


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
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list