Clang:检索公共方法 [英] Clang: Retrieving public methods

查看:331
本文介绍了Clang:检索公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个函数,它将使用Clang LibTooling库返回指向最后定义的公共方法的指针。

I want to define a function that will return a pointer to the last defined public method using the Clang LibTooling library.

目前我有一个 CXXRecordDecl 指针 * decl 和以下行来获取第一个方法的源位置。

Currently I have a CXXRecordDecl pointer *decl and the following line to get the source location of the first method.

const SourceLocation method_begin_location = decl->method_begin()->getLocation();

理想情况下,我想用一个函数替换它,以获取最后定义的公共方法的位置,如果没有方法,公共声明的开始位置如下。

Ideally, I want to replace this with a function to get the location of the last defined public method or the location of the beginning of the public declaration if there are no methods as follows.

const SourceLocation last_public_method_location = get_last_public_method_loc(decl);

有关编写此函数的任何见解吗? method_end()指向超过方法定义的末尾,因此它不像我所希望的那样有用。

Any insights into writing this function? method_end() points past the end of the method definitions so it is not as helpful as I was hoping.

推荐答案

这可能是你可以尝试。我有一个变量,它将存储您明确定义的最后一个公共方法的名称,意味着它不是由编译器自动添加的。匹配 methodDecl(isPublic(),unless(isImplicit()))的每个函数将存储在 lastPublicMethodName

This might be something you could try. I have one variable, that shall store the name of the last public method that you explicitly defined, meaning that it wasn't added by the compiler automatically. Each function that matches the methodDecl(isPublic(), unless(isImplicit())) matcher will be stored in the lastPublicMethodName string variable which in the end will contain the last visited method.

我可以强烈推荐将这个网站放在你的后面口袋里,所有的AST匹配器: http://clang.llvm.org/docs/LibASTMatchersReference.html 。此外,如果您想了解特定匹配器的工作原理, tools / clang / unitests / ASTMatchers / ASTMatchersTest.cpp 是一个有价值的资源。

I can highly recommend keeping this site in your back pocket for all the AST matchers: http://clang.llvm.org/docs/LibASTMatchersReference.html. Also, tools/clang/unitests/ASTMatchers/ASTMatchersTest.cpp is a valuable resource if you want to find out how a particular matcher works.

这是我解析的代码:

/tmp/Foo.hpp: b

class Foo { 
    public:
        virtual ~Foo() {}
        virtual void somePublicFunction1() = 0;
        virtual void somePublicFunction2() = 0;
        virtual void somePublicFunction3() = 0;
        virtual void somePublicFunction4() = 0;
        virtual void somePublicFunction5() = 0;
    private:
        virtual void somePrivateFunction1() = 0;
        virtual void somePrivateFunction2() = 0;
        virtual void somePrivateFunction3() = 0;
        virtual void somePrivateFunction4() = 0;
        virtual void somePrivateFunction5() = 0;
};

这是Clang工具的程序代码:

Here's the program code of my Clang tool:

llvm / tools / clang / tools / extra / mytool / MyTool.cpp

// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <iostream>

using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
using namespace llvm;

// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory MyToolCategory("my-tool options");

// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);

// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...");

DeclarationMatcher methodMatcher = methodDecl(isPublic(),unless(isImplicit())).bind("methods");

class MethodPrinter : public MatchFinder::MatchCallback {
public :
  virtual void run(const MatchFinder::MatchResult &Result) {
    if (const CXXMethodDecl *md = Result.Nodes.getNodeAs<clang::CXXMethodDecl>("methods")) {    
      lastPublicMethodName = md->getNameAsString();
    }
  }

  std::string lastPublicMethodName;
};

int main(int argc, const char **argv) {
  CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  ClangTool Tool(OptionsParser.getCompilations(),
                 OptionsParser.getSourcePathList());

  MethodPrinter Printer;
  MatchFinder Finder;
  Finder.addMatcher(methodMatcher, &Printer);

  Tool.run(newFrontendActionFactory(&Finder).get());

  std::cout << "The last public method is: " << Printer.lastPublicMethodName << std::endl;
  return 0;
}



我希望这有助于你。

I hope, this helps you.

这篇关于Clang:检索公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆