2

clang::ASTContext内部にオブジェクトを含む独自の再帰 AST ビジター クラスがあります。

class Visitor : public clang::RecursiveASTVisitor<Visitor> {
 public:
  Visitor(const clang::ASTContext& context) : context_(context) {}

 private:
  const clang::ASTContext& context_;
};

次に、たとえば、すべての名前空間宣言にアクセスして、それらの名前を色付けします。

bool VisitNamespaceDecl(clang::NamespaceDecl* decl) {
  const auto& sm = context_.getSourceManager();
  auto loc = decl->getLocStart();

  if (!sm.isInMainFile(loc)) {
    return true;
  }

  if (!sm.isMacroBodyExpansion(loc)) {
    auto line = sm.getSpellingLineNumber(loc);
    auto column = sm.getSpellingColumnNumber(loc);
    // Colorify the "namespace" keyword itself.

    // TODO: how to get location of the name token after "namespace" keyword?
  }

  return true;
}

それはほんの一例です。トークンなしで名前空間の名前の場所を取得する方法があるとしても、よりグローバルなアプローチに興味があります。


宣言ソース範囲内のトークンに「ジャンプ」してトラバースする方法はありますか?

4

0 に答える 0