-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.cpp
76 lines (57 loc) · 1.91 KB
/
ast.cpp
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
#include <cstring>
#include "ast.hpp"
std::vector<std::string> split(std::string &data) {
std::istringstream iss(data);
std::string tmp;
std::vector<std::string> res;
while (iss >> tmp)
res.push_back(tmp);
return res;
}
ASTData::ASTData(std::string &header_path, std::string &comp_args) {
auto splited = split(comp_args);
std::vector<const char *> args;
for (auto &s : splited)
args.push_back(s.c_str());
index = clang_createIndex(0, 0);
translation_unit = clang_parseTranslationUnit(
index,
header_path.c_str(),
args.data(),
args.size(),
0,
0,
CXTranslationUnit_None
);
if (!translation_unit)
throw std::runtime_error("Cannot create translation unit");
root_cursor = clang_getTranslationUnitCursor(translation_unit);
}
class ClassSearchInfo {
public:
const char *name;
std::optional<CXCursor> cursor;
ClassSearchInfo(const char *class_name) : name(class_name), cursor({}) {}
};
CXChildVisitResult class_search_visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) {
if (clang_getCursorKind(cursor) == CXCursor_ClassDecl) {
CXString current_class = clang_getCursorSpelling(cursor);
ClassSearchInfo *i = (ClassSearchInfo * )client_data;
if (strcmp(clang_getCString(current_class), i->name) == 0) {
i->cursor.emplace(cursor);
clang_disposeString(current_class);
return CXChildVisit_Break;
}
clang_disposeString(current_class);
}
return CXChildVisit_Recurse;
}
std::optional<CXCursor> ASTData::find_class(const char *class_name) const {
ClassSearchInfo i(class_name);
clang_visitChildren(root_cursor, class_search_visitor, (CXClientData)&i);
return i.cursor;
}
ASTData::~ASTData() {
clang_disposeTranslationUnit(translation_unit);
clang_disposeIndex(index);
}