使用ASTMatcher获取至少具有两个相同访问说明符的类 我需要在C++代码中捕获案例,当类中有两个或更多类似的访问说明符时。 假设有两个类 class A{ public: int b; public: int a; } class B{ public: int a; }

使用ASTMatcher获取至少具有两个相同访问说明符的类 我需要在C++代码中捕获案例,当类中有两个或更多类似的访问说明符时。 假设有两个类 class A{ public: int b; public: int a; } class B{ public: int a; },c++,clang,abstract-syntax-tree,clang-query,C++,Clang,Abstract Syntax Tree,Clang Query,如何将类A(因为它有两个“public”)而不是类B与ASTMatcher匹配?此匹配器获取“public”声明: accessSpecDecl( isPublic(), hasAncestor(cxxRecordDecl().bind("crd"))).bind("asd") 在回调类中,您可以跟踪匹配器对给定结构声明的命中次数,例如使用std::map: struct report\u public:public MatchCallback{ 使用map\u t=std::map;

如何将类A(因为它有两个“public”)而不是类B与ASTMatcher匹配?

此匹配器获取“public”声明:

accessSpecDecl(
  isPublic(),
  hasAncestor(cxxRecordDecl().bind("crd"))).bind("asd")
在回调类中,您可以跟踪匹配器对给定结构声明的命中次数,例如使用
std::map

struct report\u public:public MatchCallback{
使用map\u t=std::map;
使用map\u it=map\u t::迭代器;
地图计数;
无效运行(匹配结果常量和结果){
AccessSpecDecl const*asd=result.Nodes.getNodeAs(“asd”);
CXXRecordDecl常量*crd=结果.Nodes.getNodeAs(“crd”);
如果(asd和crd){
string const struct_name=crd->getnameastring();
map\u it=count.find(结构名称);
如果(it!=count.end())count[struct_name]++;
else计数[struct_name]=1;
}
else{/*错误处理*/}
返回;
}//运行
}; // 公开报道

好的,据我所知,仅使用Matcher是不可能做到这一点的?我不知道如何说“大于这些节点中的一个”。
struct report_public : public MatchCallback{
  using map_t = std::map<string,int>;
  using map_it = map_t::iterator;

  map_t count;

  void run(MatchResult const & result){
    AccessSpecDecl const * asd = result.Nodes.getNodeAs<AccessSpecDecl>("asd");
    CXXRecordDecl const * crd = result.Nodes.getNodeAs<CXXRecordDecl>("crd");
    if(asd && crd){
      string const struct_name = crd->getNameAsString();
      map_it it = count.find(struct_name);
      if(it != count.end()) count[struct_name]++;
      else count[struct_name] = 1;
    }
    else { /* error handling */}
    return;
  } // run
}; // report_public