使用clang获取类中的方法列表 [英] Get list of methods in class using clang
问题描述
在常见的IDE(选择一个)中,你经常有一个大纲视图,显示一个特定类的方法列表。
In common IDEs (pick one) you often have an outline view showing you the list of methods for a specific class.
假设我有一个C ++接口类 IFoo.h
如下所示:
Suppose I have a C++ interface class in IFoo.h
that looks like this:
#ifndef IFOO_H_
#define IFOO_H_
class IFoo {
public:
virtual ~IFoo() {}
virtual void bar() = 0;
};
#endif
如何(以编程方式)我可以获得这样的IDE大纲列表, code> IFoo.h 文件上面使用可能的铛库?对于第一个开始,它将有助于如果我可以得到一个函数名称的列表。
How (programmatically) can I get such an IDE outliner list for my IFoo.h
file above using maybe the clang libraries? For a first start, it would help if I can get a list of names of functions.
我特意打算使用clang,所以任何帮助如何分析我的
I specifically intend to use clang, so any help on how to analyze the my header file with clang would be really appreciated.
同时我将看一下clang教程: https://github.com/loarabia/Clang-tutorial
Meanwhile I will have a look at the clang tutorial here: https://github.com/loarabia/Clang-tutorial
感谢您的帮助。
推荐答案
我完成了本教程 http://clang.llvm.org/docs/LibASTMatchersTutorial.html ,发现了一些非常有用的东西,这是我想出来的:
I went through this tutorial http://clang.llvm.org/docs/LibASTMatchersTutorial.html and found some pretty helpful stuff there, this is what I came up with:
我不得不重命名我的文件从 IFoo.h
到 IFoo.hpp
被检测为Cxx而不是C代码。
I had to rename my file from IFoo.h
to IFoo.hpp
to be detected as Cxx and not C code.
我不得不使用 -x c ++
IFoo.h
文件被识别为C ++代码而不是C代码(clang将 * .h
:
I had to call my program with -x c++
to have my IFoo.h
file being recognized as C++ code rather than C code (clang interprets *.h
files as C by default:
~/Development/llvm-build/bin/mytool ~/IFoo.h -- -x c++
这是我从所提供的类中转储所有虚拟函数的代码:
This is my code to dump all virtual functions from the provided class:
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <cstdio>
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
using namespace llvm;
DeclarationMatcher methodMatcher = methodDecl(isVirtual()).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")) {
md->dump();
}
}
};
// 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...");
int main(int argc, const char **argv) {
cl::OptionCategory cat("myname", "mydescription");
CommonOptionsParser optionsParser(argc, argv, cat, 0);
ClangTool tool(optionsParser.getCompilations(), optionsParser.getSourcePathList());
MethodPrinter printer;
MatchFinder finder;
finder.addMatcher(methodMatcher, &printer);
return tool.run(newFrontendActionFactory(&finder));
}
输出如下,当传递 IFoo .h
文件:
The output looks like this, when passed the IFoo.h
file:
CXXDestructorDecl 0x1709c30 <~/IFoo.h:5:3, col:20> ~IFoo 'void (void)' virtual
`-CompoundStmt 0x1758128 <col:19, col:20>
CXXMethodDecl 0x1757e60 <~/IFoo.h:6:3, col:24> bar 'void (void)' virtual pure
这篇关于使用clang获取类中的方法列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!