-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
437 lines (354 loc) · 11.4 KB
/
main.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* This file is part of clang-faces
*
* clang-faces is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clang-faces is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clang-faces. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* main.cpp
*
* Author: Brian Fransioli <assem@terranpro.org>
* Created: Tue Jul 18:55:32 KST 2013
* Last modified: Thu Aug 15 23:53:20 KST 2013
*/
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <memory>
#include "clang-c/Index.h"
const auto end_pattern = "!!!!$$$$!!!!";
std::string TokenKindSpelling( CXTokenKind kind )
{
switch( kind ) {
case CXToken_Punctuation: return "Punctuation";
case CXToken_Keyword: return "Keyword";
case CXToken_Identifier: return "Identifier";
case CXToken_Literal: return "Literal";
case CXToken_Comment: return "Comment";
default:
break;
}
return {};
}
// std::ostream& operator<<( std::ostream &os, CXCursor cursor )
// {
// auto spelling = clang_getCursorKindSpelling( cursor.kind );
// os << clang_getCString( spelling );
// clang_disposeString( spelling );
// return os;
// }
std::ostream& operator<<( std::ostream &os, CXCursor cursor )
{
auto spelling = clang_getCursorKindSpelling( cursor.kind );
auto morespell = clang_getCursorSpelling( cursor );
os << clang_getCString( spelling ) << ":"
<< clang_getCString( morespell ) << " ";
clang_disposeString( spelling );
clang_disposeString( morespell );
auto refcursor = clang_getCursorReferenced( cursor );
if ( clang_equalCursors( refcursor, clang_getNullCursor() )||
clang_equalCursors( refcursor, cursor ) )
return os;
return os << " [ " << refcursor << " ] ";
}
std::ostream& operator<<( std::ostream& os, CXSourceLocation loc )
{
CXFile cxfile;
unsigned line, col, off;
clang_getFileLocation( loc, &cxfile, &line, &col, &off );
auto filestr = clang_getFileName( cxfile );
os << clang_getCString( filestr ) << ":" << line << ":" << col;
clang_disposeString( filestr );
return os;
}
std::ostream& operator<<( std::ostream& os, CXSourceRange range )
{
auto beg = clang_getRangeStart( range );
auto end = clang_getRangeEnd( range );
return os << beg << " -> " << end << "\n";
}
std::ostream& operator<<( std::ostream& os, CXType type )
{
auto typestr = clang_getTypeSpelling( type );
os << "Type: " << clang_getCString( typestr ) << "\n";
clang_disposeString( typestr );
return os;
}
std::ostream& operator<<( std::ostream& os, CXString str )
{
os << clang_getCString( str );
return os;
}
CXChildVisitResult
CallExprVisitor( CXCursor cursor, CXCursor parent, CXClientData d )
{
std::string *result = reinterpret_cast<std::string *>( d );
std::cout << "Child of CallExpr = " << cursor << "\n";
if ( cursor.kind == CXCursor_TypeRef )
*result = "Identifier";
// *result = "Variable";
return CXChildVisit_Break;
}
std::string AdvancedCallExprHandler( CXCursor cursor )
{
auto newcursor = clang_getCursorReferenced( cursor );
std::string result = "Function";
if ( newcursor.kind == CXCursor_Constructor )
result = "Identifier";
else
clang_visitChildren( cursor, CallExprVisitor,
reinterpret_cast<std::string *>(&result) );
return result;
}
std::string CursorKindSpelling( CXCursor cursor )
{
auto kind = cursor.kind;
CXCursor newcursor;
switch( kind ) {
case CXCursor_DeclRefExpr:
case CXCursor_MemberRefExpr:
newcursor = clang_getCursorReferenced( cursor );
std::cout << "NEW CURSOR: " << newcursor << "\n";
return CursorKindSpelling( newcursor );
case CXCursor_CallExpr:
return AdvancedCallExprHandler( cursor );
case CXCursor_CXXMethod:
case CXCursor_FunctionDecl:
case CXCursor_Constructor:
case CXCursor_Destructor:
case CXCursor_OverloadedDeclRef:
return "Function";
case CXCursor_ParmDecl:
case CXCursor_VarDecl:
case CXCursor_FieldDecl:
case CXCursor_MemberRef:
case CXCursor_VariableRef:
case CXCursor_NonTypeTemplateParameter:
return "Variable";
case CXCursor_NamespaceRef:
return "Namespace";
default:
return "Identifier";
}
return {};
}
std::vector<CXCursor>
my_annotateTokens( CXTranslationUnit tu, CXToken *tokens,
unsigned token_count )
{
std::vector<CXCursor> cursors( token_count );
for ( auto n = 0u; n < token_count; ++n ) {
auto location = clang_getTokenLocation( tu, tokens[ n ] );
cursors[n] = ( clang_getCursor( tu, location ) );
}
return cursors;
}
void TokenizeSource(CXTranslationUnit tu)
{
CXSourceRange range =
clang_getCursorExtent( clang_getTranslationUnitCursor(tu) );
CXToken *tokens;
unsigned int token_count;
clang_tokenize( tu, range, &tokens, &token_count );
//CXCursor cursors[ token_count ];
//clang_annotateTokens( tu, tokens, token_count, cursors );
auto cursors = my_annotateTokens( tu, tokens, token_count );
for ( auto t = 0u; t < token_count; ++t ) {
auto tkind = clang_getTokenKind(tokens[t] );
auto tspelling = tkind == CXToken_Identifier ?
CursorKindSpelling( cursors[ t ] ) :
TokenKindSpelling( tkind );
auto textent = clang_getTokenExtent( tu, tokens[t] );
auto tstartloc = clang_getRangeStart( textent );
auto tendloc = clang_getRangeEnd( textent );
auto tokspell = clang_getTokenSpelling( tu, tokens[ t ] );
std::cout << "TokenSpelling: " << tokspell << "\n";
std::cout << "Cursor: " << cursors[ t ] << "\n";
// if ( !( cursors[t].kind >= CXCursor_FirstInvalid &&
// cursors[t].kind <= CXCursor_LastInvalid ) ) {
// auto rr = clang_getCursorExtent( cursors[ t ] );
// std::cout << "Range: " << rr << "\n";
// }
// std::cout << clang_getCursorDisplayName( cursors[ t ] ) << "\n";
// std::cout << "USR: " << clang_getCursorUSR( cursors[ t ] ) << "\n";
unsigned int startoffset, endoffset;
clang_getSpellingLocation( tstartloc, nullptr, nullptr, nullptr, &startoffset );
clang_getSpellingLocation( tendloc, nullptr, nullptr, nullptr, &endoffset );
// TODO: testing this hack for int -> identifier instead of keyword
// but this loses const to an identifier also! fvck!
if ( tspelling == "Keyword" ) {
auto type = clang_getCursorType( cursors[ t ] );
auto typekind = type.kind;
CXString typespelling;
if ( cursors[t].kind == CXCursor_FunctionDecl ||
cursors[t].kind == CXCursor_CXXMethod ) {
type = clang_getResultType( type );
typekind = type.kind;
typespelling = clang_getTypeSpelling( type );
}
else
typespelling = clang_getTypeSpelling( type );
// std::cout << "Type = " << type << " kind: " << typekind << "\n";
// std::cout << clang_getCString(typespelling) << " <-> " << clang_getCString(tokspell) << "\n";
// std::cout << " Const? " << clang_isConstQualifiedType( type ) << "\n";
if ( (( typekind >= CXType_FirstBuiltin && typekind <= CXType_LastBuiltin ) &&
( std::string(clang_getCString(typespelling)) ==
std::string(clang_getCString(tokspell) ) )) ||
// ( cursors[t].kind == CXCursor_VarDecl ) ||
( cursors[t].kind == CXCursor_ParmDecl ) )
tspelling = "Identifier";
}
//if ( tspelling != "Punctuation" )
std::cout
<< startoffset << ":" << endoffset << " @ "
<< tspelling << "\n";
clang_disposeString( tokspell );
}
std::cout << "\n" << end_pattern << "\n";
clang_disposeTokens( tu, tokens, token_count );
}
std::vector<char> ReparseSource()
{
//std::cout << "Entered ReparseSource!\n";
std::vector<char> buffer;
std::string input;
auto buffer_ins = std::back_inserter( buffer );
while( 1 ) {
if ( !std::getline( std::cin, input ) )
return buffer;
if ( input == end_pattern )
break;
std::copy( input.begin(), input.end(),
buffer_ins );
*buffer_ins++ = '\n';
}
//*buffer_ins++ = '\0';
std::cout << "Leaving Reparse Source w/buffer size: "
<< buffer.size();
return buffer;
}
struct ArgList
{
std::vector<std::string> args;
std::unique_ptr<const char *> c_args;
ArgList(int argc, char **argv)
: args( argv, argv+argc ), c_args( new const char *[argc] )
{ build_c_args(); }
ArgList( std::vector<std::string> vargs )
: args( vargs ), c_args( new const char *[ args.size() ] )
{ build_c_args(); }
void build_c_args()
{
auto i = 0u;
for ( auto s : args )
std::cout << s << " ";
std::cout << std::endl;
for ( auto &s : args )
(c_args.get())[ i++ ] = s.c_str();
}
operator const char **()
{
return c_args.get();
}
std::size_t count() const
{ return args.size(); }
};
struct TUnit
{
CXIndex index;
CXTranslationUnit unit;
std::string filename;
bool valid;
unsigned const options = clang_defaultEditingTranslationUnitOptions();
int orig_argc;
const char **orig_argv;
TUnit( CXIndex idx, std::string file )
: index( idx ), filename( file )
{ std::cout << "File: " << filename << "\n"; }
~TUnit()
{ clang_disposeTranslationUnit( unit ); }
bool parse( int argc, const char **argv,
CXUnsavedFile *unsaved_files = nullptr,
unsigned int unsaved_file_count = 0 )
{
orig_argc = argc;
orig_argv = argv;
for ( auto i = 0; i < argc; ++i )
std::cout << argv[i] << " ";
std::cout << "\n";
unit = clang_parseTranslationUnit( index,
filename.c_str(),
argv,
argc,
unsaved_files,
unsaved_file_count,
options );
return valid = (unit != NULL);
}
CXTranslationUnit handle()
{ return unit; }
bool parse( std::vector<CXUnsavedFile> unsavedfiles )
{
if ( !valid ) {
std::cout << "Warning, not valid!\n";
if ( !parse( orig_argc, orig_argv,
unsavedfiles.data(),
unsavedfiles.size() ) )
return false;
}
valid = !clang_reparseTranslationUnit( unit,
unsavedfiles.size(),
unsavedfiles.data(),
options );
return valid;
}
};
int main(int argc, char *argv[])
{
auto index = clang_createIndex(0, 0);
std::vector<std::string> default_args = { {"-x"}, {"c++"}, {"-std=c++11"} };
std::string filename;
ArgList arglist( default_args );
if ( argc > 1 ) {
arglist = ArgList( argc - 2, argv + 1 );
filename = argv[argc - 1];
} else {
filename = argv[1];
}
TUnit tu( index, filename );
if ( !tu.parse( arglist.count(), arglist ) ) {
std::cout << "Translation Unit Initial Parse Failed!\n";
}
std::string input;
std::vector<char> filebuffer;
while( std::getline( std::cin, input ) ) {
if ( input == "REPARSE" ) {
filebuffer = ReparseSource();
CXUnsavedFile unsaved_file = { filename.c_str(),
filebuffer.data(),
filebuffer.size() };
// std::cout << "Size = " << filebuffer.size()
// << "Contents:\n" << filebuffer.data()
// << "\n";
if ( tu.parse( std::vector<CXUnsavedFile>( 1, unsaved_file ) ) ) {
TokenizeSource( tu.handle() );
} else {
std::cout << "Reparse FAILED!\n" << end_pattern << "\n";
}
}
}
clang_disposeIndex( index );
return 0;
}