【问题标题】:Clang: Retrieving public methodsClang:检索公共方法
【发布时间】:2014-07-03 23:02:19
【问题描述】:

我想定义一个函数,该函数将使用 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() 指向方法定义的末尾,所以它没有我希望的那么有用。

【问题讨论】:

  • 这个级别的编译时反射(目前)不被支持。运行时类型信息更少。

标签: c++ clang llvm-clang


【解决方案1】:

这可能是您可以尝试的。我有一个变量,它将存储您明确定义的最后一个公共方法的名称,这意味着它不是由编译器自动添加的。每个匹配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/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;
}

希望对你有帮助。

【讨论】:

  • 我猜虽然这“技术上”可行。我的匹配器中有其他功能,我不能等到整个匹配过程完成后才能获得指向最后一个公共方法的指针。谢谢你的例子,我想我最终可能会为公共方法体编写一个匹配器。我希望有人可以帮助我使用 method_iterator,我是 C++ 新手。
猜你喜欢
  • 2011-11-19
  • 2011-04-19
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
相关资源
最近更新 更多