-
Notifications
You must be signed in to change notification settings - Fork 13
/
c-ast.js
220 lines (191 loc) · 8.15 KB
/
c-ast.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const libclang = require('libclang');
module.exports = function(filename) {
const idx = new libclang.Index();
const tu = libclang.TranslationUnit.fromSource(idx, filename, ['-Xclang', '-fsyntax-only', '-std=c++11']);
const cData = {
Functions: [],
Enums: [],
};
tu.cursor.visitChildren(function(parent) {
const cursor = this;
switch (cursor.kind) {
case libclang.Cursor.EnumDecl:
const enumName = cursor.spelling;
const constants = [];
cursor.visitChildren(function(parent) {
const cursor = this;
if (cursor.kind === libclang.Cursor.EnumConstantDecl) {
const constName = cursor.spelling;
let constValue = cursor.enumValue;
console.log(cursor.getComment());
constants.push({
name: constName,
value: constValue
});
}
return libclang.Cursor.Continue;
});
cData.Enums.push({
name: enumName,
values: constants
});
return libclang.Cursor.Continue;
case libclang.Cursor.FunctionDecl:
case libclang.Cursor.ObjCInstanceMethodDecl:
const returnType = cursor.type.spelling.replace(/ ?\([^\(]+$/, '');
const funcName = cursor.spelling;
const params = [];
const fnAnnotations = {};
cursor.visitChildren(function(parent) {
const cursor = this;
if (cursor.kind === libclang.Cursor.AnnotateAttr) {
const [type, value] = cursor.spelling.split(':', 2);
if (value) {
fnAnnotations[type] = value;
} else {
fnAnnotations[type] = true;
}
} else if (cursor.kind === libclang.Cursor.ParmDecl) {
const paramName = cursor.spelling;
const type = cursor.type;
let paramType = getKindName(cursor.type.kind);
if (type.kind === libclang.Type.Typedef || type.kind === libclang.Type.Pointer) {
paramType = cursor.type.spelling;
}
const annotations = {};
cursor.visitChildren(function(parent) {
const cursor = this;
if (cursor.kind === libclang.Cursor.AnnotateAttr) {
const [type, value] = cursor.spelling.split(':', 2);
if (value) {
annotations[type] = value;
} else {
annotations[type] = true;
}
}
return libclang.Cursor.Continue;
});
params.push({
name: paramName,
type: paramType,
annotations: Object.keys(annotations).length > 0 ? annotations : undefined
});
}
return libclang.Cursor.Continue;
});
cData.Functions.push({
Name: funcName,
Return: returnType,
Parameters: params,
Annotations: Object.keys(fnAnnotations).length > 0 ? fnAnnotations : undefined
});
return libclang.Cursor.Continue;
}
return libclang.Cursor.Recurse;
function getKindName(kind) {
switch (kind) {
case libclang.Type.Invalid:
return "Invalid";
case libclang.Type.Unexposed:
return "Unexposed";
case libclang.Type.Void:
return "void";
case libclang.Type.Bool:
return "bool";
case libclang.Type.Char_U:
return "char_t";
case libclang.Type.UChar:
return "uchar_t";
case libclang.Type.Char16:
return "uint16_t";
case libclang.Type.Char32:
return "uint32_t";
case libclang.Type.UInt:
return "uint";
case libclang.Type.ULong:
return "ulong";
case libclang.Type.ULongLong:
return "ulonglong";
case libclang.Type.UInt128:
return "uint128_t";
case libclang.Type.Char_S:
return "char";
case libclang.Type.SChar:
return "SChar";
case libclang.Type.WChar:
return "wchar_t";
case libclang.Type.Short:
return "short";
case libclang.Type.Int:
return "int";
case libclang.Type.Long:
return "long";
case libclang.Type.LongLong:
return "longlong";
case libclang.Type.Int128:
return "int128_t";
case libclang.Type.Float:
return "float";
case libclang.Type.Double:
return "double";
case libclang.Type.LongDouble:
return "longdouble";
case libclang.Type.NullPtr:
return "nullptr";
case libclang.Type.Overload:
return "Overload";
case libclang.Type.Dependent:
return "Dependent";
case libclang.Type.ObjCId:
return "ObjCId";
case libclang.Type.ObjCClass:
return "ObjCClass";
case libclang.Type.ObjCSel:
return "ObjCSel";
case libclang.Type.Float128:
return "float128";
case libclang.Type.Complex:
return "complex";
case libclang.Type.Pointer:
return "pointer";
case libclang.Type.BlockPointer:
return "block-pointer";
case libclang.Type.LValueReference:
return "lvalue-reference";
case libclang.Type.RValueReference:
return "rvalue-reference";
case libclang.Type.Record:
return "record";
case libclang.Type.Enum:
return "enum";
case libclang.Type.Typedef:
return "typedef";
case libclang.Type.ObjCInterface:
return "obj-c-interface";
case libclang.Type.ObjCObjectPointer:
return "obj-c-object-pointer";
case libclang.Type.FunctionNoProto:
return "function-no-proto";
case libclang.Type.FunctionProto:
return "function-proto";
case libclang.Type.ConstantArray:
return "const-array";
case libclang.Type.Vector:
return "vector";
case libclang.Type.IncompleteArray:
return "incomplete-array";
case libclang.Type.VariableArray:
return "variable-array";
case libclang.Type.DependentSizedArray:
return "dependent-sized-array";
case libclang.Type.MemberPointer:
return "member-pointer";
case libclang.Type.Auto:
return "auto";
case libclang.Type.Elaborated:
return "elaborated";
}
}
});
return cData;
};