【发布时间】:2019-12-06 02:41:10
【问题描述】:
我尝试使用 libclang 解析 c++ 标头,但解析器仅解析类名 - 并将其类型显示为 VarDec1。 当文件扩展名从 .h 更改为 .cpp 时,它就可以正常工作了。 经过几天的搜索,我找不到答案,有人可以帮我解决这个问题吗?
下面是parser.cpp:
#include <iostream>
#include <clang-c/Index.h> // This is libclang.
using namespace std;
ostream& operator<<(ostream& stream, const CXString& str)
{
stream << clang_getCString(str);
clang_disposeString(str);
return stream;
}
int main()
{
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(
index,
"tt.h", nullptr, 0,
nullptr, 0,
CXTranslationUnit_None);
if (unit == nullptr)
{
cerr << "Unable to parse translation unit. Quitting." << endl;
exit(-1);
}
CXCursor cursor = clang_getTranslationUnitCursor(unit);
clang_visitChildren(
cursor,
[](CXCursor c, CXCursor parent, CXClientData client_data)
{
cout << "Cursor '" << (clang_getCursorSpelling(c)) << "' of kind '"
<<(clang_getCursorKindSpelling(clang_getCursorKind(c))) << "'\n";
return CXChildVisit_Recurse;
},
nullptr);
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
fgetc(stdin);
}
以下是tt.h:
class MyClass
{
public:
int field;
virtual void method() const = 0;
static const int static_field;
static int static_method(int a1);
};
class MyClass2
{
public:
int field;
virtual void method() const = 0;
static const string static_field;
static int static_method(int a1, string a2);
};
我使用以下编译命令:
clang++ main.cpp -lclang
当文件扩展名为 .h 时: parse header
当文件扩展名为 .cpp 时: enter image description here
【问题讨论】:
标签: c++ c clang clang++ libclang