Clang:检索公共方法

时间:2014-07-03 23:02:20

标签: c++ clang llvm-clang

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

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

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

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

const SourceLocation last_public_method_location = get_last_public_method_loc(decl);

有关编写此功能的任何见解? method_end()指向方法定义的末尾,因此它没有我希望的那样有用。

1 个答案:

答案 0 :(得分:2)

这可能是您可以尝试的。我有一个变量,它应该存储您显式定义的最后一个公共方法的名称,这意味着它不会被编译器自动添加。与methodDecl(isPublic(), unless(isImplicit()))匹配器匹配的每个函数都将存储在lastPublicMethodName字符串变量中,该变量最后将包含上次访问的方法。

我强烈建议将此网站放在所有AST匹配器的后袋中:http://clang.llvm.org/docs/LibASTMatchersReference.html。此外,如果您想了解特定匹配器的工作原理,tools/clang/unitests/ASTMatchers/ASTMatchersTest.cpp是一种宝贵的资源。

这是我解析的代码:

<强> /tmp/Foo.hpp:

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工具的程序代码:

<强> LLVM /工具/铛/工具/额外/ 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;
}

我希望,这对你有帮助。

相关问题