Repository navigation
Expand file tree
/
Copy pathEnum.cpp
More file actions
151 lines (121 loc) · 3.46 KB
/
Copy pathEnum.cpp
File metadata and controls
151 lines (121 loc) · 3.46 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "IR.h"
namespace metagen {
// Transforms verbose enum member names into shorter ones, as we fit them inside
// a JS object instead of making top level object.
// so enum like: NSBackgroundActivityResult
// - NSBackgroundActivityResultFinished
// - NSBackgroundActivityResultDeferred
// becomes:
// NSBackgroundActivityResult
// - Finished
// - Deferred
// Basically, split the name into parts (assuming camel case), and then remove
// the repeating parts from the beginning and end of the name.
void transformEnumMemberNames(std::vector<EnumConstDecl> &members) {
std::vector<std::string> result;
std::vector<std::vector<std::string>> parts;
size_t skip_begin = 0, skip_end = 0;
std::vector<std::string> largestPart;
for (auto part : members) {
auto split = splitCamelCase(part.name);
parts.emplace_back(split);
if (split.size() > largestPart.size()) {
largestPart = split;
}
}
skip_begin = skip_end = largestPart.size();
for (size_t i = 0; i < parts.size(); i++) {
auto part = parts[i];
size_t skip = 0;
for (size_t j = 0; j < part.size(); j++) {
if (j >= largestPart.size()) {
break;
}
if (part[j] == largestPart[j]) {
skip++;
} else {
break;
}
}
if (skip < skip_begin) {
skip_begin = skip;
}
skip = 0;
for (size_t j = part.size() - 1; j >= 0; j--) {
if (j >= largestPart.size()) {
break;
}
if (part[j] == largestPart[largestPart.size() - 1 - skip]) {
skip++;
} else {
break;
}
}
if (skip < skip_end) {
skip_end = skip;
}
}
for (size_t i = 0; i < members.size(); i++) {
auto part = parts[i];
std::string name = "";
for (size_t j = skip_begin; j < part.size() - skip_end; j++) {
name += part[j];
}
if (name == "") {
name = members[i].name;
}
members[i].name = name;
}
}
EnumDecl::EnumDecl(CXCursor cursor) {
framework = getFrameworkName(cursor);
CXString cxname = clang_getCursorSpelling(cursor);
name = clang_getCString(cxname);
clang_disposeString(cxname);
if (name.find("(unnamed") != std::string::npos) {
name = "";
}
clang_visitChildren(
cursor,
[](CXCursor cursor, CXCursor, CXClientData clientData) {
if (!isAvailable(cursor)) {
return CXChildVisit_Continue;
}
auto state = (EnumDecl *)clientData;
CXCursorKind kind = clang_getCursorKind(cursor);
switch (kind) {
case CXCursor_EnumConstantDecl: {
CXString name = clang_getCursorSpelling(cursor);
std::string nameStr = clang_getCString(name);
clang_disposeString(name);
auto value = clang_getEnumConstantDeclValue(cursor);
EnumConstDecl decl;
decl.name = nameStr;
decl.value = value;
state->constants.emplace_back(decl);
break;
}
default:
break;
}
return CXChildVisit_Continue;
},
this);
if (!name.empty())
transformEnumMemberNames(constants);
}
void MetadataFactory::processEnumRefs() {
for (const std::string &ref : referencedEnums) {
if (enums.contains(ref)) {
continue;
}
if (skippedEnums.contains(ref)) {
EnumDecl decl = skippedEnums[ref];
enums[decl.name] = decl;
} else {
std::cerr << "processEnumRef: Enum " << ref << " not found" << std::endl;
}
}
referencedEnums.clear();
}
} // namespace metagen