在已经匹配了一些东西之后,有什么方法可以与 libclang 匹配者"restart matching"?

Is there any way to "restart matching" with libclang matchers after already having matched something?

本文关键字:方法 libclang restart 什么 matching 之后      更新时间:2023-10-16
class __attribute__((annotate("some_important_string"))) Foo {
public:
    void do_something();
};

我想找到具有特定注释的类中的所有公共方法。我设置了一个匹配器来获取带有注释的类:

Matcher.addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()),
                                 hasAttr(attr::Annotate)).bind("class")

,但似乎没有任何方法来匹配注释的确切字符串。

因此,在我的MatchCallback::run方法中,我通过获取属性dyn_castAnnotateAttr并检查注释字符串ref:

来检查属性字符串
auto &attrs = decl->getAttrs();
...
auto attribute_attr = dyn_cast<AnnotateAttr>(attr);
auto string_ref = attribute_attr->getAnnotation();

,但现在我更愿意做匹配,而不是AST树遍历。我的理解是匹配器比树遍历函数更稳定,而且它们似乎也更直接。

所以有任何方法回到使用匹配器后过滤类的注释字符串?

我目前基于以下代码进行过滤,它工作正常,但这不是我想要的方式:

        if (method->getAccess() != AS_public) {
            if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**%s is not public, skippingn", indentation.c_str(), method_name.c_str());
            return;
        }
        if (method->isOverloadedOperator()) {
            if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping overloaded operator %sn", indentation.c_str(), method_name.c_str());
            return;
        }
        if (dyn_cast<CXXConstructorDecl>(method)) {
            if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping constructor %sn", indentation.c_str(), method_name.c_str());
            return;
        }
        if (dyn_cast<CXXDestructorDecl>(method)) {
            if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping destructor %sn", indentation.c_str(), method_name.c_str());
            return;
        }
        if (method->isPure()) {
            assert(method->isVirtual());
            if (PRINT_SKIPPED_EXPORT_REASONS) printf("%s**skipping pure virtual %sn", indentation.c_str(), method_name.c_str());
            return;
        }

我不确定Clang AST Matchers API目前(截至3.9)是否支持重新启动匹配过程。据我所知,MatchFinder包括匹配节点MatchFinder的方法。match,并匹配整个翻译单元MatchFinder。而不是从提供的节点递归地匹配。

解决这个问题的一个方法是简单地让你的匹配器更复杂,并在一个地方嵌入你想要做的所有匹配(如下)。

#include <string>
#include <iostream>
#include "clang/AST/AST.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;
static llvm::cl::OptionCategory ToolingSampleCategory("Matcher Sample");
class Handler : public MatchFinder::MatchCallback {
public:
  Handler() 
  {}
  void run(const MatchFinder::MatchResult &Result) override {
    if(const CXXRecordDecl* klass = Result.Nodes.getNodeAs<CXXRecordDecl>("klass"))
    {
      for(auto it : klass->attrs())
      {
        Attr& attr = (*it);
        auto annotatedAttr = dyn_cast<AnnotateAttr>(&attr);
        if((annotatedAttr == nullptr) || (std::string(annotatedAttr->getAnnotation()) != "meh"))
        {
            return;
        }
      }
    }
    if(const CXXMethodDecl* mthd = Result.Nodes.getNodeAs<CXXMethodDecl>("mthd"))
    {
      std::cout << mthd->getNameInfo().getAsString() << std::endl;
    }
  }
};
int main(int argc, const char **argv) {
  CommonOptionsParser op(argc, argv, ToolingSampleCategory);
  RefactoringTool Tool(op.getCompilations(), op.getSourcePathList());
  Handler handler;
  MatchFinder finder;
  finder.addMatcher(
          cxxRecordDecl(
              hasAttr(attr::Annotate),
              forEach(
                  cxxMethodDecl(
                      isPublic(),
                      unless(isPure()),
                      unless(cxxConstructorDecl()),
                      unless(cxxDestructorDecl())).bind("mthd")
              )).bind("klass"), &handler);
  Tool.run(newFrontendActionFactory(&finder).get());
  return 0;
}