-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyze.cpp
729 lines (593 loc) · 24.2 KB
/
Analyze.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/* Cycript - The Truly Universal Scripting Language
* Copyright (C) 2009-2016 Jay Freeman (saurik)
*/
/* GNU Affero General Public License, Version 3 {{{ */
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program 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 Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <clang-c/Index.h>
#include "Bridge.hpp"
#include "Functor.hpp"
#include "Replace.hpp"
#include "Syntax.hpp"
static CXChildVisitResult CYVisit(CXCursor cursor, CXCursor parent, CXClientData arg) {
(*reinterpret_cast<const Functor<void (CXCursor)> *>(arg))(cursor);
return CXChildVisit_Continue;
}
static unsigned CYForChild(CXCursor cursor, const Functor<void (CXCursor)> &visitor) {
return clang_visitChildren(cursor, &CYVisit, const_cast<void *>(static_cast<const void *>(&visitor)));
}
static bool CYOneChild(CXCursor cursor, const Functor<void (CXCursor)> &visitor) {
bool visited(false);
CYForChild(cursor, fun([&](CXCursor child) {
_assert(!visited);
visited = true;
visitor(child);
}));
return visited;
}
struct CYCXString {
CXString value_;
CYCXString(CXString value) :
value_(value)
{
}
CYCXString(CXCursor cursor) :
value_(clang_getCursorSpelling(cursor))
{
}
CYCXString(CXCursorKind kind) :
value_(clang_getCursorKindSpelling(kind))
{
}
CYCXString(CXFile file) :
value_(clang_getFileName(file))
{
}
CYCXString(CXTranslationUnit unit, CXToken token) :
value_(clang_getTokenSpelling(unit, token))
{
}
~CYCXString() {
clang_disposeString(value_);
}
operator const char *() const {
return clang_getCString(value_);
}
const char *Pool(CYPool &pool) const {
return pool.strdup(*this);
}
bool operator ==(const char *rhs) const {
const char *lhs(*this);
return lhs == rhs || strcmp(lhs, rhs) == 0;
}
};
template <void (&clang_get_Location)(CXSourceLocation, CXFile *, unsigned *, unsigned *, unsigned *) = clang_getSpellingLocation>
struct CYCXPosition {
CXFile file_;
unsigned line_;
unsigned column_;
unsigned offset_;
CYCXPosition(CXSourceLocation location) {
clang_get_Location(location, &file_, &line_, &column_, &offset_);
}
CYCXPosition(CXTranslationUnit unit, CXToken token) :
CYCXPosition(clang_getTokenLocation(unit, token))
{
}
CXSourceLocation Get(CXTranslationUnit unit) const {
return clang_getLocation(unit, file_, line_, column_);
}
};
template <void (&clang_get_Location)(CXSourceLocation, CXFile *, unsigned *, unsigned *, unsigned *)>
std::ostream &operator <<(std::ostream &out, const CYCXPosition<clang_get_Location> &position) {
if (position.file_ != NULL)
out << "[" << CYCXString(position.file_) << "]:";
out << position.line_ << ":" << position.column_ << "@" << position.offset_;
return out;
}
struct CYKey {
unsigned priority_ = 0;
std::string code_;
unsigned flags_;
};
typedef std::map<std::string, CYKey> CYKeyMap;
struct CYChildBaton {
CXTranslationUnit unit;
CYKeyMap &keys;
CYChildBaton(CXTranslationUnit unit, CYKeyMap &keys) :
unit(unit),
keys(keys)
{
}
};
struct CYTokens {
private:
CXTranslationUnit unit_;
CXToken *tokens_;
unsigned count_;
unsigned valid_;
public:
CYTokens(CXTranslationUnit unit, CXSourceRange range) :
unit_(unit)
{
clang_tokenize(unit_, range, &tokens_, &count_);
// libclang's tokenizer is horribly broken and returns "extra" tokens.
// this code goes back through the tokens and filters for good ones :/
CYCXPosition<> end(clang_getRangeEnd(range));
CYCXString file(end.file_);
for (valid_ = 0; valid_ != count_; ++valid_) {
CYCXPosition<> position(unit, tokens_[valid_]);
_assert(CYCXString(position.file_) == file);
if (position.offset_ >= end.offset_)
break;
}
}
CYTokens(CXTranslationUnit unit, CXCursor cursor) :
CYTokens(unit, clang_getCursorExtent(cursor))
{
}
~CYTokens() {
clang_disposeTokens(unit_, tokens_, count_);
}
operator CXToken *() const {
return tokens_;
}
size_t size() const {
return valid_;
}
};
static CYUTF8String CYCXPoolUTF8Range(CYPool &pool, CXSourceRange range) {
CYCXPosition<> start(clang_getRangeStart(range));
CYCXPosition<> end(clang_getRangeEnd(range));
CYCXString file(start.file_);
_assert(file == CYCXString(end.file_));
CYPool temp;
size_t size;
char *data(static_cast<char *>(CYPoolFile(temp, file, &size)));
_assert(start.offset_ <= size && end.offset_ <= size && start.offset_ <= end.offset_);
CYUTF8String code;
code.size = end.offset_ - start.offset_;
code.data = pool.strndup(data + start.offset_, code.size);
return code;
}
static CYExpression *CYTranslateExpression(CXTranslationUnit unit, CXCursor cursor) {
switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
case CXCursor_CallExpr: {
CYExpression *function(NULL);
CYList<CYArgument> arguments;
CYForChild(cursor, fun([&](CXCursor child) {
CYExpression *expression(CYTranslateExpression(unit, child));
if (function == NULL)
function = expression;
else
arguments->*$C_(expression);
}));
return $C(function, arguments);
} break;
case CXCursor_DeclRefExpr: {
return $V(CYCXString(cursor).Pool($pool));
} break;
case CXCursor_IntegerLiteral: {
// libclang doesn't provide any reasonable way to do this
// note: clang_tokenize doesn't work if this is a macro
// the token range starts inside the macro but ends after it
// the tokenizer freaks out and either fails with 0 tokens
// or returns some massive number of tokens ending here :/
CYUTF8String token(CYCXPoolUTF8Range($pool, clang_getCursorExtent(cursor)));
double value(CYCastDouble(token));
if (std::isnan(value))
return $V(token.data);
return $ CYNumber(value);
} break;
case CXCursor_CStyleCastExpr:
// XXX: most of the time, this is a "NoOp" integer cast; but we should check it
case CXCursor_UnexposedExpr:
// there is a very high probability that this is actually an "ImplicitCastExpr"
// "Douglas Gregor" <[email protected]> err'd on the incorrect side of this one
// http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20110926/046998.html
case CXCursor_ParenExpr: {
CYExpression *pass(NULL);
CYOneChild(cursor, fun([&](CXCursor child) {
pass = CYTranslateExpression(unit, child);
}));
return pass;
} break;
default:
//std::cerr << "E:" << CYCXString(kind) << std::endl;
_assert(false);
}
}
static CYStatement *CYTranslateStatement(CXTranslationUnit unit, CXCursor cursor) {
switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
case CXCursor_ReturnStmt: {
CYExpression *value(NULL);
CYOneChild(cursor, fun([&](CXCursor child) {
value = CYTranslateExpression(unit, child);
}));
return $ CYReturn(value);
} break;
default:
//std::cerr << "S:" << CYCXString(kind) << std::endl;
_assert(false);
}
}
static CYStatement *CYTranslateBlock(CXTranslationUnit unit, CXCursor cursor) {
CYList<CYStatement> statements;
CYForChild(cursor, fun([&](CXCursor child) {
statements->*CYTranslateStatement(unit, child);
}));
return $ CYBlock(statements);
}
static CYType *CYDecodeType(CXType type);
static void CYParseType(CXType type, CYType *typed);
static void CYParseEnumeration(CXCursor cursor, CYType *typed) {
CYList<CYEnumConstant> constants;
CYForChild(cursor, fun([&](CXCursor child) {
if (clang_getCursorKind(child) == CXCursor_EnumConstantDecl)
constants->*$ CYEnumConstant($I($pool.strdup(CYCXString(child))), $D(clang_getEnumConstantDeclValue(child)));
}));
CYType *integer(CYDecodeType(clang_getEnumDeclIntegerType(cursor)));
typed->specifier_ = $ CYTypeEnum(NULL, integer->specifier_, constants);
}
static void CYParseStructure(CXCursor cursor, CYType *typed) {
CYList<CYTypeStructField> fields;
CYForChild(cursor, fun([&](CXCursor child) {
if (clang_getCursorKind(child) == CXCursor_FieldDecl)
fields->*$ CYTypeStructField(CYDecodeType(clang_getCursorType(child)), $I(CYCXString(child).Pool($pool)));
}));
typed->specifier_ = $ CYTypeStruct(NULL, $ CYStructTail(fields));
}
static void CYParseCursor(CXType type, CXCursor cursor, CYType *typed) {
CYCXString spelling(cursor);
switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
case CXCursor_EnumDecl:
if (spelling[0] != '\0')
typed->specifier_ = $ CYTypeReference(CYTypeReferenceEnum, $I(spelling.Pool($pool)));
else
CYParseEnumeration(cursor, typed);
break;
case CXCursor_StructDecl: {
if (spelling[0] != '\0')
typed->specifier_ = $ CYTypeReference(CYTypeReferenceStruct, $I(spelling.Pool($pool)));
else
CYParseStructure(cursor, typed);
} break;
case CXCursor_UnionDecl: {
_assert(false);
} break;
default:
std::cerr << "C:" << CYCXString(kind) << std::endl;
_assert(false);
break;
}
}
static CYTypedParameter *CYParseSignature(CXType type, CYType *typed) {
CYParseType(clang_getResultType(type), typed);
CYList<CYTypedParameter> parameters;
for (int i(0), e(clang_getNumArgTypes(type)); i != e; ++i)
parameters->*$ CYTypedParameter(CYDecodeType(clang_getArgType(type, i)), NULL);
return parameters;
}
static void CYParseFunction(CXType type, CYType *typed) {
typed = typed->Modify($ CYTypeFunctionWith(clang_isFunctionTypeVariadic(type), CYParseSignature(type, typed)));
}
static void CYParseType(CXType type, CYType *typed) {
switch (CXTypeKind kind = type.kind) {
case CXType_Unexposed: {
CXType result(clang_getResultType(type));
if (result.kind == CXType_Invalid)
CYParseCursor(type, clang_getTypeDeclaration(type), typed);
else
// clang marks function pointers as Unexposed but still supports them
CYParseFunction(type, typed);
} break;
case CXType_Bool: typed->specifier_ = $ CYTypeVariable("bool"); break;
case CXType_WChar: typed->specifier_ = $ CYTypeVariable("wchar_t"); break;
case CXType_Float: typed->specifier_ = $ CYTypeFloating(0); break;
case CXType_Double: typed->specifier_ = $ CYTypeFloating(1); break;
case CXType_LongDouble: typed->specifier_ = $ CYTypeFloating(2); break;
case CXType_Char_U: typed->specifier_ = $ CYTypeCharacter(CYTypeNeutral); break;
case CXType_Char_S: typed->specifier_ = $ CYTypeCharacter(CYTypeNeutral); break;
case CXType_SChar: typed->specifier_ = $ CYTypeCharacter(CYTypeSigned); break;
case CXType_UChar: typed->specifier_ = $ CYTypeCharacter(CYTypeUnsigned); break;
case CXType_Short: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 0); break;
case CXType_UShort: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 0); break;
case CXType_Int: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 1); break;
case CXType_UInt: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 1); break;
case CXType_Long: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 2); break;
case CXType_ULong: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 2); break;
case CXType_LongLong: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 3); break;
case CXType_ULongLong: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 3); break;
case CXType_Int128: typed->specifier_ = $ CYTypeInt128(CYTypeSigned); break;
case CXType_UInt128: typed->specifier_ = $ CYTypeInt128(CYTypeUnsigned); break;
case CXType_BlockPointer: {
CXType pointee(clang_getPointeeType(type));
_assert(!clang_isFunctionTypeVariadic(pointee));
typed = typed->Modify($ CYTypeBlockWith(CYParseSignature(pointee, typed)));
} break;
case CXType_ConstantArray:
CYParseType(clang_getArrayElementType(type), typed);
typed = typed->Modify($ CYTypeArrayOf($D(clang_getArraySize(type))));
break;
case CXType_Enum:
typed->specifier_ = $ CYTypeVariable($pool.strdup(CYCXString(clang_getTypeSpelling(type))));
break;
case CXType_FunctionProto:
CYParseFunction(type, typed);
break;
case CXType_IncompleteArray:
// XXX: I probably should not decay to Pointer
CYParseType(clang_getArrayElementType(type), typed);
typed = typed->Modify($ CYTypePointerTo());
break;
case CXType_ObjCClass:
typed->specifier_ = $ CYTypeVariable("Class");
break;
case CXType_ObjCId:
typed->specifier_ = $ CYTypeVariable("id");
break;
case CXType_ObjCInterface:
typed->specifier_ = $ CYTypeVariable($pool.strdup(CYCXString(clang_getTypeSpelling(type))));
break;
case CXType_ObjCObjectPointer: {
CXType pointee(clang_getPointeeType(type));
if (pointee.kind != CXType_Unexposed) {
CYParseType(pointee, typed);
typed = typed->Modify($ CYTypePointerTo());
} else
// Clang seems to have internal typedefs for id and Class that are awkward
_assert(false);
} break;
case CXType_ObjCSel:
typed->specifier_ = $ CYTypeVariable("SEL");
break;
case CXType_Pointer:
CYParseType(clang_getPointeeType(type), typed);
typed = typed->Modify($ CYTypePointerTo());
break;
case CXType_Record:
typed->specifier_ = $ CYTypeReference(CYTypeReferenceStruct, $I($pool.strdup(CYCXString(clang_getTypeSpelling(type)))));
break;
case CXType_Typedef:
// use the declaration in order to isolate the name of the typedef itself
typed->specifier_ = $ CYTypeVariable($pool.strdup(CYCXString(clang_getTypeDeclaration(type))));
break;
case CXType_Vector:
_assert(false);
break;
case CXType_Void:
typed->specifier_ = $ CYTypeVoid();
break;
default:
std::cerr << "T:" << CYCXString(clang_getTypeKindSpelling(kind)) << std::endl;
std::cerr << "_: " << CYCXString(clang_getTypeSpelling(type)) << std::endl;
_assert(false);
}
if (clang_isConstQualifiedType(type))
typed = typed->Modify($ CYTypeConstant());
}
static CYType *CYDecodeType(CXType type) {
CYType *typed($ CYType(NULL));
CYParseType(type, typed);
return typed;
}
static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClientData arg) {
CYChildBaton &baton(*static_cast<CYChildBaton *>(arg));
CXTranslationUnit &unit(baton.unit);
CXChildVisitResult result(CXChildVisit_Continue);
CYCXString spelling(cursor);
std::string name(spelling);
std::ostringstream value;
unsigned priority(2);
unsigned flags(CYBridgeHold);
/*CXSourceLocation location(clang_getCursorLocation(cursor));
CYCXPosition<> position(location);
std::cerr << spelling << " " << position << std::endl;*/
try { switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
case CXCursor_EnumConstantDecl: {
value << clang_getEnumConstantDeclValue(cursor);
} break;
case CXCursor_EnumDecl: {
// the enum constants are implemented separately *also*
// XXX: maybe move output logic to function we can call
result = CXChildVisit_Recurse;
if (spelling[0] == '\0')
goto skip;
// XXX: this was blindly copied from StructDecl
if (!clang_isCursorDefinition(cursor))
priority = 1;
CYLocalPool pool;
CYType typed;
CYParseEnumeration(cursor, &typed);
CYOptions options;
CYOutput out(*value.rdbuf(), options);
CYTypeExpression(&typed).Output(out, CYNoBFC);
value << ".withName(\"" << name << "\")";
name = "$cye" + name;
flags = CYBridgeType;
} break;
case CXCursor_MacroDefinition: {
CXSourceRange range(clang_getCursorExtent(cursor));
CYTokens tokens(unit, range);
_assert(tokens.size() != 0);
CXCursor cursors[tokens.size()];
clang_annotateTokens(unit, tokens, tokens.size(), cursors);
CYLocalPool local;
CYList<CYFunctionParameter> parameters;
unsigned offset(1);
if (tokens.size() != 1) {
CYCXPosition<> start(clang_getRangeStart(range));
CYCXString first(unit, tokens[offset]);
if (first == "(") {
CYCXPosition<> paren(unit, tokens[offset]);
if (start.offset_ + strlen(spelling) == paren.offset_) {
for (;;) {
_assert(++offset != tokens.size());
CYCXString token(unit, tokens[offset]);
parameters->*$P($B($I(token.Pool($pool))));
_assert(++offset != tokens.size());
CYCXString comma(unit, tokens[offset]);
if (comma == ")")
break;
_assert(comma == ",");
}
++offset;
}
}
}
std::ostringstream body;
for (unsigned i(offset); i != tokens.size(); ++i) {
CYCXString token(unit, tokens[i]);
if (i != offset)
body << " ";
body << token;
}
if (!parameters)
value << body.str();
else {
CYOptions options;
CYOutput out(*value.rdbuf(), options);
out << '(' << "function" << '(';
out << parameters;
out << ')' << '{';
out << "return" << ' ';
value << body.str();
out << ';' << '}' << ')';
}
} break;
case CXCursor_StructDecl: {
if (spelling[0] == '\0')
goto skip;
if (!clang_isCursorDefinition(cursor))
priority = 1;
CYLocalPool pool;
CYType typed;
CYParseStructure(cursor, &typed);
CYOptions options;
CYOutput out(*value.rdbuf(), options);
CYTypeExpression(&typed).Output(out, CYNoBFC);
value << ".withName(\"" << name << "\")";
name = "$cys" + name;
flags = CYBridgeType;
} break;
case CXCursor_TypedefDecl: {
CYLocalPool local;
CYType *typed(CYDecodeType(clang_getTypedefDeclUnderlyingType(cursor)));
if (typed->specifier_ == NULL)
value << "(typedef " << CYCXString(clang_getTypeSpelling(clang_getTypedefDeclUnderlyingType(cursor))) << ")";
else {
CYOptions options;
CYOutput out(*value.rdbuf(), options);
CYTypeExpression(typed).Output(out, CYNoBFC);
}
} break;
case CXCursor_FunctionDecl:
case CXCursor_VarDecl: {
std::string label;
CYList<CYFunctionParameter> parameters;
CYStatement *code(NULL);
CYLocalPool local;
CYForChild(cursor, fun([&](CXCursor child) {
switch (CXCursorKind kind = clang_getCursorKind(child)) {
case CXCursor_AsmLabelAttr:
label = CYCXString(child);
break;
case CXCursor_CompoundStmt:
code = CYTranslateBlock(unit, child);
break;
case CXCursor_ParmDecl:
parameters->*$P($B($I(CYCXString(child).Pool($pool))));
break;
case CXCursor_IntegerLiteral:
case CXCursor_ObjCClassRef:
case CXCursor_TypeRef:
case CXCursor_UnexposedAttr:
break;
default:
//std::cerr << "A:" << CYCXString(child) << std::endl;
break;
}
}));
if (label.empty()) {
label = spelling;
label = '_' + label;
} else if (label[0] != '_')
goto skip;
if (code == NULL) {
value << "*";
CXType type(clang_getCursorType(cursor));
CYType *typed(CYDecodeType(type));
CYOptions options;
CYOutput out(*value.rdbuf(), options);
CYTypeExpression(typed).Output(out, CYNoBFC);
value << ".pointerTo()(dlsym(RTLD_DEFAULT,'" << label.substr(1) << "'))";
} else {
CYOptions options;
CYOutput out(*value.rdbuf(), options);
CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters, code));
function->Output(out, CYNoBFC);
//std::cerr << value.str() << std::endl;
}
} break;
default:
result = CXChildVisit_Recurse;
goto skip;
break;
} {
CYKey &key(baton.keys[name]);
if (key.priority_ <= priority) {
key.priority_ = priority;
key.code_ = value.str();
key.flags_ = flags;
}
} } catch (const CYException &error) {
CYPool pool;
//std::cerr << error.PoolCString(pool) << std::endl;
}
skip:
return result;
}
int main(int argc, const char *argv[]) {
CXIndex index(clang_createIndex(0, 0));
const char *file(argv[1]);
unsigned offset(3);
#if CY_OBJECTIVEC
argv[--offset] = "-ObjC++";
#endif
CXTranslationUnit unit(clang_parseTranslationUnit(index, file, argv + offset, argc - offset, NULL, 0, CXTranslationUnit_DetailedPreprocessingRecord));
for (unsigned i(0), e(clang_getNumDiagnostics(unit)); i != e; ++i) {
CXDiagnostic diagnostic(clang_getDiagnostic(unit, i));
CYCXString spelling(clang_getDiagnosticSpelling(diagnostic));
std::cerr << spelling << std::endl;
}
CYKeyMap keys;
CYChildBaton baton(unit, keys);
clang_visitChildren(clang_getTranslationUnitCursor(unit), &CYChildVisit, &baton);
for (CYKeyMap::const_iterator key(keys.begin()); key != keys.end(); ++key) {
std::string code(key->second.code_);
for (size_t i(0), e(code.size()); i != e; ++i)
if (code[i] <= 0 || code[i] >= 0x7f || code[i] == '\n')
goto skip;
std::cout << key->first << "|" << key->second.flags_ << "\"" << code << "\"" << std::endl;
skip:; }
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
return 0;
}