Clang: Retrieving public methods

冷暖自知 提交于 2019-12-06 14:27:55

问题


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

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);

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.


回答1:


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.

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.

This is the code I parse:

/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;
};

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.



来源:https://stackoverflow.com/questions/24564494/clang-retrieving-public-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!