-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinding.cpp
422 lines (362 loc) · 10.3 KB
/
binding.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <napi.h>
using namespace Napi;
#include "clang-c/Index.h"
#define UNUSED(x) (x);
template<typename T, size_t S>
size_t ArrayLength(const T(&arr)[S]) { UNUSED(arr); return S; }
std::string
clang_getCStr(CXString cxString)
{
std::string string = clang_getCString(cxString);
clang_disposeString(cxString);
return string;
}
unsigned
clang_Cursor_isFunction(CXCursor cursor)
{
CXCursorKind kind = clang_getCursorKind(cursor);
switch (kind)
{
case CXCursor_FunctionDecl:
case CXCursor_CXXMethod:
case CXCursor_Constructor:
case CXCursor_Destructor:
case CXCursor_ConversionFunction:
case CXCursor_FunctionTemplate:
case CXCursor_LambdaExpr:
return 1;
}
return 0;
}
unsigned
clang_Cursor_isKeywordWithCompound(CXCursor cursor)
{
CXCursorKind kind = clang_getCursorKind(cursor);
switch (kind)
{
case CXCursor_CaseStmt:
case CXCursor_DefaultStmt:
case CXCursor_IfStmt:
case CXCursor_SwitchStmt:
case CXCursor_WhileStmt:
case CXCursor_DoStmt:
case CXCursor_ForStmt:
case CXCursor_CXXCatchStmt:
case CXCursor_CXXTryStmt:
case CXCursor_CXXForRangeStmt:
case CXCursor_SEHTryStmt:
case CXCursor_SEHExceptStmt:
case CXCursor_SEHFinallyStmt:
return 1;
}
return 0;
}
void
AppendFoldingRange(Env env, Array ranges, unsigned lineStart, unsigned lineEnd)
{
if (lineEnd > lineStart)
{
Object range = Object::New(env);
range.Set(String::New(env, "start"), lineStart - 1);
range.Set(String::New(env, "end"), lineEnd - 1);
ranges[ranges.Length()] = range;
}
}
void
AppendFoldingRange(Env env, Array ranges, CXSourceLocation srcStart, CXSourceLocation srcEnd)
{
unsigned lineStart, lineEnd;
clang_getFileLocation(srcStart, nullptr, &lineStart, nullptr, nullptr);
clang_getFileLocation(srcEnd, nullptr, &lineEnd, nullptr, nullptr);
AppendFoldingRange(env, ranges, lineStart, lineEnd);
}
void
AppendFoldingRange(Env env, Array ranges, CXSourceRange srcRange)
{
CXSourceLocation srcStart = clang_getRangeStart(srcRange);
CXSourceLocation srcEnd = clang_getRangeEnd (srcRange);
AppendFoldingRange(env, ranges, srcStart, srcEnd);
}
void
AppendFoldingRange(Env env, Array ranges, CXTranslationUnit unit, CXToken tokenStart, CXToken tokenEnd)
{
CXSourceRange tokenRangeStart = clang_getTokenExtent(unit, tokenStart);
CXSourceRange tokenRangeEnd = clang_getTokenExtent(unit, tokenEnd);
CXSourceLocation srcStart = clang_getRangeStart(tokenRangeStart);
CXSourceLocation srcEnd = clang_getRangeEnd (tokenRangeEnd);
AppendFoldingRange(env, ranges, srcStart, srcEnd);
}
struct Log
{
bool enable;
Env& env;
Array& errors;
template <class... T>
void operator()(T&&... args)
{
if (!enable) return;
std::ostringstream s;
(s << ... << args);
errors[errors.Length()] = String::New(env, s.str().c_str());
}
};
Value
ParseFile(const CallbackInfo& info)
{
Env env = info.Env();
Array ranges = Array::New(env);
Array errors = Array::New(env);
Object result = Object::New(env);
result.Set("ranges", ranges);
result.Set("errors", errors);
bool enable = false;
Log log = { enable, env, errors };
// HACK: Dear god...
if (info.Length() != 2)
{
log("Wrong number of arguments");
TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
return env.Null();
}
// HACK: Dear god...
if (!info[0].IsString() || !info[1].IsString())
{
log("Wrong type of arguments");
TypeError::New(env, "Wrong type of arguments").ThrowAsJavaScriptException();
return env.Null();
}
std::string fileName = info[0].As<String>().Utf8Value();
std::string fileContent = info[1].As<String>().Utf8Value();
log("Filename: ", fileName);
const char* clangArgs[] = {
"-fno-delayed-template-parsing",
"-std=c++17",
"-x", "c++",
};
CXUnsavedFile file = {};
file.Filename = fileName.c_str();
file.Contents = fileContent.c_str();
file.Length = fileContent.size();
// TODO: Print clang errors (usage errors, not compile errors)
CXIndex index = clang_createIndex(0, 1);
CXTranslationUnit unit = clang_createTranslationUnitFromSourceFile(
index,
file.Filename,
ArrayLength(clangArgs), clangArgs,
1, &file
);
CXCursor unitCursor = clang_getTranslationUnitCursor(unit);
CXSourceRange unitRange = clang_getCursorExtent(unitCursor);
struct
{
std::stack<CXToken*> tokenStack;
CXToken* prevToken;
void
HandleToken(Log log, Env env, Array ranges, CXTranslationUnit unit, CXToken& token, CXTokenKind tokenKind)
{
log("token: ", clang_getCStr(clang_getTokenSpelling(unit, token)));
switch (tokenKind)
{
default:
prevToken = &token;
break;
case CXToken_Comment:
prevToken = nullptr;
break;
case CXToken_Punctuation:
{
std::string tokenStr = clang_getCStr(clang_getTokenSpelling(unit, token));
switch (tokenStr[0])
{
default:
prevToken = &token;
break;
case '{':
{
tokenStack.push(prevToken ? prevToken : &token);
prevToken = nullptr;
// DEBUG
std::string pushTokenStr = clang_getCStr(clang_getTokenSpelling(unit, *tokenStack.top()));
log("push: ", pushTokenStr);
break;
}
case ';':
prevToken = nullptr;
break;
case '}':
{
if (tokenStack.empty()) break;
CXToken* tokenStart = tokenStack.top(); tokenStack.pop();
CXToken* tokenEnd = &token;
if (!tokenStart) break;
CXSourceRange tokenRangeStart = clang_getTokenExtent(unit, *tokenStart);
CXSourceRange tokenRangeEnd = clang_getTokenExtent(unit, *tokenEnd);
CXSourceLocation srcStart = clang_getRangeStart(tokenRangeStart);
CXSourceLocation srcEnd = clang_getRangeEnd (tokenRangeEnd);
unsigned lineStart, lineEnd;
clang_getFileLocation(srcStart, nullptr, &lineStart, nullptr, nullptr);
clang_getFileLocation(srcEnd, nullptr, &lineEnd, nullptr, nullptr);
AppendFoldingRange(env, ranges, lineStart, lineEnd - 1);
prevToken = nullptr;
// DEBUG
std::string tokenStartStr = clang_getCStr(clang_getTokenSpelling(unit, *tokenStart));
std::string tokenEndStr = clang_getCStr(clang_getTokenSpelling(unit, *tokenEnd));
log("pop & range: ", tokenStartStr, " - ", tokenEndStr);
break;
}
}
break;
}
}
}
} ScopeBlock = {};
struct
{
bool inComment;
unsigned commentStart;
unsigned commentEnd;
void
HandleToken(Log log, Env env, Array ranges, CXTranslationUnit unit, CXToken token, CXTokenKind tokenKind)
{
if (tokenKind == CXToken_Comment)
{
std::string tokenStr = clang_getCStr(clang_getTokenSpelling(unit, token));
CXSourceRange srcRange = clang_getTokenExtent(unit, token);
bool isBlockComment = strncmp("/*", tokenStr.c_str(), 2) == 0;
if (isBlockComment)
{
End(env, ranges);
AppendFoldingRange(env, ranges, srcRange);
}
else
{
Begin(env, ranges, srcRange);
}
}
else
{
End(env, ranges);
}
}
void
Begin(Env env, Array ranges, CXSourceRange srcRange)
{
CXSourceLocation srcStart = clang_getRangeStart(srcRange);
CXSourceLocation srcEnd = clang_getRangeEnd (srcRange);
unsigned lineStart, lineEnd;
clang_getFileLocation(srcStart, nullptr, &lineStart, nullptr, nullptr);
clang_getFileLocation(srcEnd, nullptr, &lineEnd, nullptr, nullptr);
Begin(env, ranges, lineStart, lineEnd);
}
void
Begin(Env env, Array ranges, unsigned lineStart, unsigned lineEnd)
{
if (!inComment)
{
inComment = true;
commentStart = lineStart;
commentEnd = lineEnd;
}
else
{
if (lineStart > commentEnd + 1)
{
End(env, ranges);
Begin(env, ranges, lineStart, lineEnd);
}
else
{
commentEnd = lineEnd;
}
}
};
void
End(Env env, Array ranges)
{
if (inComment)
{
inComment = false;
if (commentEnd - commentStart + 1 < 3) return;
AppendFoldingRange(env, ranges, commentStart, commentEnd);
}
};
} CommentSequence = {};
struct
{
std::stack<CXToken> tokenStack;
bool prevTokenIsHash;
CXToken prevNonPPToken;
void
HandleToken(Log log, Env env, Array ranges, CXTranslationUnit unit, CXToken token, CXTokenKind tokenKind)
{
std::string tokenStr = clang_getCStr(clang_getTokenSpelling(unit, token));
bool tokenIsHash = (strcmp("#", tokenStr.c_str()) == 0);
if (tokenIsHash)
{
prevTokenIsHash = true;
return;
}
if (prevTokenIsHash)
{
bool isBeginIf = false;
isBeginIf = isBeginIf || (strcmp("if", tokenStr.c_str()) == 0);
isBeginIf = isBeginIf || (strcmp("ifdef", tokenStr.c_str()) == 0);
isBeginIf = isBeginIf || (strcmp("ifndef", tokenStr.c_str()) == 0);
bool isElse = false;
isElse = isElse || (strcmp("else", tokenStr.c_str()) == 0);
isElse = isElse || (strcmp("elif", tokenStr.c_str()) == 0);
bool isEndIf = strcmp("endif", tokenStr.c_str()) == 0;
if (isBeginIf)
{
tokenStack.push(token);
}
else if (isElse)
{
if (tokenStack.empty()) return;
CXToken ppTokenStart = tokenStack.top(); tokenStack.pop();
CXToken ppTokenEnd = prevNonPPToken;
AppendFoldingRange(env, ranges, unit, ppTokenStart, ppTokenEnd);
tokenStack.push(token);
}
else if (isEndIf)
{
if (tokenStack.empty()) return;
CXToken ppTokenStart = tokenStack.top(); tokenStack.pop();
CXToken ppTokenEnd = prevNonPPToken;
AppendFoldingRange(env, ranges, unit, ppTokenStart, ppTokenEnd);
}
}
if (!tokenIsHash)
{
prevNonPPToken = token;
}
}
} PreprocessorBlock = {};
CXToken* tokens;
unsigned tokenCount;
clang_tokenize(unit, unitRange, &tokens, &tokenCount);
for (unsigned i = 0; i < tokenCount; i++)
{
CXToken& token = tokens[i];
CXTokenKind tokenKind = clang_getTokenKind(token);
ScopeBlock .HandleToken(log, env, ranges, unit, token, tokenKind);
CommentSequence .HandleToken(log, env, ranges, unit, token, tokenKind);
PreprocessorBlock.HandleToken(log, env, ranges, unit, token, tokenKind);
}
CommentSequence.End(env, ranges);
clang_disposeTokens(unit, tokens, tokenCount);
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
return result;
}
Object
Init(Env env, Object exports)
{
exports.Set(String::New(env, "ParseFile"), Function::New(env, ParseFile));
return exports;
}
NODE_API_MODULE(binding, Init)