-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
88 lines (79 loc) · 3.14 KB
/
parser.cpp
File metadata and controls
88 lines (79 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <clang-c/Index.h>
using namespace std;
ostream& operator<<(ostream& stream, const CXString& str)
{
stream << clang_getCString(str);
clang_disposeString(str);
return stream;
}
void printClassAndMethods(CXCursor cursor)
{
if (clang_getCursorKind(cursor) == CXCursor_CXXMethod || clang_getCursorKind(cursor) == CXCursor_FunctionDecl)
{
CXType methodType = clang_getCursorType(cursor);
CXString methodTypeString = clang_getTypeSpelling(methodType);
cout << "Method: " << clang_getCursorSpelling(cursor) << " (" << methodTypeString << ")" << endl;
// 获取方法调用的其他方法
clang_visitChildren(
cursor,
[](CXCursor c, CXCursor parent, CXClientData client_data)
{
if (clang_getCursorKind(c) == CXCursor_CallExpr)
{
CXCursor referencedCursor = clang_getCursorReferenced(c);
CXCursor methodCursor = clang_getCursorDefinition(referencedCursor);
CXCursor classCursor = clang_getCursorSemanticParent(methodCursor);
if (clang_getCursorKind(classCursor) == CXCursor_ClassDecl) {
CXType referencedMethodType = clang_getCursorType(methodCursor);
CXString referencedMethodTypeString = clang_getTypeSpelling(referencedMethodType);
CXString className = clang_getCursorSpelling(classCursor);
if (clang_getCString(className) != nullptr && clang_getCString(className)[0] != '\0') {
cout << " Calls: " << clang_getCursorSpelling(referencedCursor) << " (" << referencedMethodTypeString << ") (Class: " << className << ")" << endl;
} else {
cout << " Calls: " << clang_getCursorSpelling(referencedCursor) << " (" << referencedMethodTypeString << ")" << endl;
}
} else {
CXType referencedMethodType = clang_getCursorType(methodCursor);
CXString referencedMethodTypeString = clang_getTypeSpelling(referencedMethodType);
CXString fileName = clang_getCursorSpelling(classCursor);
if (clang_getCString(fileName) != nullptr && clang_getCString(fileName)[0] != '\0') {
cout << " Calls: " << clang_getCursorSpelling(referencedCursor) <<" (File:" << fileName << ")" << " (" << referencedMethodTypeString << ")"<< endl;
}
}
}
return CXChildVisit_Recurse;
},
nullptr);
}
else if (clang_getCursorKind(cursor) == CXCursor_ClassDecl)
{
cout << "Class: " << clang_getCursorSpelling(cursor) << endl;
}
clang_visitChildren(
cursor,
[](CXCursor c, CXCursor parent, CXClientData client_data)
{
printClassAndMethods(c);
return CXChildVisit_Continue;
},
nullptr);
}
int main()
{
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(
index,
"test.cpp", nullptr, 0,
nullptr, 0,
CXTranslationUnit_None);
if (unit == nullptr)
{
cerr << "Unable to parse translation unit. Quitting." << endl;
exit(-1);
}
CXCursor cursor = clang_getTranslationUnitCursor(unit);
printClassAndMethods(cursor);
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
}