]> git.saurik.com Git - cycript.git/blob - Analyze.cpp
922dfb1cd3d56d88c56ff74a6ccc23dbfb81eacd
[cycript.git] / Analyze.cpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <cmath>
23 #include <cstring>
24 #include <iostream>
25 #include <map>
26 #include <sstream>
27 #include <string>
28
29 #include <clang-c/Index.h>
30
31 #include "Functor.hpp"
32 #include "Replace.hpp"
33 #include "Syntax.hpp"
34
35 static CXChildVisitResult CYVisit(CXCursor cursor, CXCursor parent, CXClientData arg) {
36 (*reinterpret_cast<const Functor<void (CXCursor)> *>(arg))(cursor);
37 return CXChildVisit_Continue;
38 }
39
40 static unsigned CYForChild(CXCursor cursor, const Functor<void (CXCursor)> &visitor) {
41 return clang_visitChildren(cursor, &CYVisit, const_cast<void *>(static_cast<const void *>(&visitor)));
42 }
43
44 static bool CYOneChild(CXCursor cursor, const Functor<void (CXCursor)> &visitor) {
45 bool visited(false);
46 CYForChild(cursor, fun([&](CXCursor child) {
47 _assert(!visited);
48 visited = true;
49 visitor(child);
50 }));
51 return visited;
52 }
53
54 struct CYCXString {
55 CXString value_;
56
57 CYCXString(CXString value) :
58 value_(value)
59 {
60 }
61
62 CYCXString(CXCursor cursor) :
63 value_(clang_getCursorSpelling(cursor))
64 {
65 }
66
67 CYCXString(CXCursorKind kind) :
68 value_(clang_getCursorKindSpelling(kind))
69 {
70 }
71
72 CYCXString(CXFile file) :
73 value_(clang_getFileName(file))
74 {
75 }
76
77 CYCXString(CXTranslationUnit unit, CXToken token) :
78 value_(clang_getTokenSpelling(unit, token))
79 {
80 }
81
82 ~CYCXString() {
83 clang_disposeString(value_);
84 }
85
86 operator const char *() const {
87 return clang_getCString(value_);
88 }
89
90 const char *Pool(CYPool &pool) const {
91 return pool.strdup(*this);
92 }
93
94 bool operator ==(const char *rhs) const {
95 const char *lhs(*this);
96 return lhs == rhs || strcmp(lhs, rhs) == 0;
97 }
98 };
99
100 template <void (&clang_get_Location)(CXSourceLocation, CXFile *, unsigned *, unsigned *, unsigned *) = clang_getSpellingLocation>
101 struct CYCXPosition {
102 CXFile file_;
103 unsigned line_;
104 unsigned column_;
105 unsigned offset_;
106
107 CYCXPosition(CXSourceLocation location) {
108 clang_get_Location(location, &file_, &line_, &column_, &offset_);
109 }
110
111 CYCXPosition(CXTranslationUnit unit, CXToken token) :
112 CYCXPosition(clang_getTokenLocation(unit, token))
113 {
114 }
115
116 CXSourceLocation Get(CXTranslationUnit unit) const {
117 return clang_getLocation(unit, file_, line_, column_);
118 }
119 };
120
121 template <void (&clang_get_Location)(CXSourceLocation, CXFile *, unsigned *, unsigned *, unsigned *)>
122 std::ostream &operator <<(std::ostream &out, const CYCXPosition<clang_get_Location> &position) {
123 if (position.file_ != NULL)
124 out << "[" << CYCXString(position.file_) << "]:";
125 out << position.line_ << ":" << position.column_ << "@" << position.offset_;
126 return out;
127 }
128
129 struct CYKey {
130 unsigned priority_ = 0;
131
132 std::string code_;
133 unsigned flags_;
134 };
135
136 typedef std::map<std::string, CYKey> CYKeyMap;
137
138 struct CYChildBaton {
139 CXTranslationUnit unit;
140 CYKeyMap &keys;
141
142 CYChildBaton(CXTranslationUnit unit, CYKeyMap &keys) :
143 unit(unit),
144 keys(keys)
145 {
146 }
147 };
148
149 struct CYTokens {
150 private:
151 CXTranslationUnit unit_;
152 CXToken *tokens_;
153 unsigned count_;
154 unsigned valid_;
155
156 public:
157 CYTokens(CXTranslationUnit unit, CXSourceRange range) :
158 unit_(unit)
159 {
160 clang_tokenize(unit_, range, &tokens_, &count_);
161
162
163 // libclang's tokenizer is horribly broken and returns "extra" tokens.
164 // this code goes back through the tokens and filters for good ones :/
165
166 CYCXPosition<> end(clang_getRangeEnd(range));
167 CYCXString file(end.file_);
168
169 for (valid_ = 0; valid_ != count_; ++valid_) {
170 CYCXPosition<> position(unit, tokens_[valid_]);
171 _assert(CYCXString(position.file_) == file);
172 if (position.offset_ >= end.offset_)
173 break;
174 }
175 }
176
177 CYTokens(CXTranslationUnit unit, CXCursor cursor) :
178 CYTokens(unit, clang_getCursorExtent(cursor))
179 {
180 }
181
182 ~CYTokens() {
183 clang_disposeTokens(unit_, tokens_, count_);
184 }
185
186 operator CXToken *() const {
187 return tokens_;
188 }
189
190 size_t size() const {
191 return valid_;
192 }
193 };
194
195 static CYUTF8String CYCXPoolUTF8Range(CYPool &pool, CXSourceRange range) {
196 CYCXPosition<> start(clang_getRangeStart(range));
197 CYCXPosition<> end(clang_getRangeEnd(range));
198 CYCXString file(start.file_);
199 _assert(file == CYCXString(end.file_));
200
201 CYPool temp;
202 size_t size;
203 char *data(static_cast<char *>(CYPoolFile(temp, file, &size)));
204 _assert(start.offset_ <= size && end.offset_ <= size && start.offset_ <= end.offset_);
205
206 CYUTF8String code;
207 code.size = end.offset_ - start.offset_;
208 code.data = pool.strndup(data + start.offset_, code.size);
209 return code;
210 }
211
212 static CYExpression *CYTranslateExpression(CXTranslationUnit unit, CXCursor cursor) {
213 switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
214 case CXCursor_CallExpr: {
215 CYExpression *function(NULL);
216 CYList<CYArgument> arguments;
217 CYForChild(cursor, fun([&](CXCursor child) {
218 CYExpression *expression(CYTranslateExpression(unit, child));
219 if (function == NULL)
220 function = expression;
221 else
222 arguments->*$C_(expression);
223 }));
224 return $C(function, arguments);
225 } break;
226
227 case CXCursor_DeclRefExpr: {
228 return $V(CYCXString(cursor).Pool($pool));
229 } break;
230
231 case CXCursor_IntegerLiteral: {
232 // libclang doesn't provide any reasonable way to do this
233 // note: clang_tokenize doesn't work if this is a macro
234 // the token range starts inside the macro but ends after it
235 // the tokenizer freaks out and either fails with 0 tokens
236 // or returns some massive number of tokens ending here :/
237
238 CYUTF8String token(CYCXPoolUTF8Range($pool, clang_getCursorExtent(cursor)));
239 double value(CYCastDouble(token));
240 if (std::isnan(value))
241 return $V(token.data);
242 return $ CYNumber(value);
243 } break;
244
245 case CXCursor_CStyleCastExpr:
246 // XXX: most of the time, this is a "NoOp" integer cast; but we should check it
247
248 case CXCursor_UnexposedExpr:
249 // there is a very high probability that this is actually an "ImplicitCastExpr"
250 // "Douglas Gregor" <dgregor@apple.com> err'd on the incorrect side of this one
251 // http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20110926/046998.html
252
253 case CXCursor_ParenExpr: {
254 CYExpression *pass(NULL);
255 CYOneChild(cursor, fun([&](CXCursor child) {
256 pass = CYTranslateExpression(unit, child);
257 }));
258 return pass;
259 } break;
260
261 default:
262 //std::cerr << "E:" << CYCXString(kind) << std::endl;
263 _assert(false);
264 }
265 }
266
267 static CYStatement *CYTranslateStatement(CXTranslationUnit unit, CXCursor cursor) {
268 switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
269 case CXCursor_ReturnStmt: {
270 CYExpression *value(NULL);
271 CYOneChild(cursor, fun([&](CXCursor child) {
272 value = CYTranslateExpression(unit, child);
273 }));
274 return $ CYReturn(value);
275 } break;
276
277 default:
278 //std::cerr << "S:" << CYCXString(kind) << std::endl;
279 _assert(false);
280 }
281 }
282
283 static CYStatement *CYTranslateBlock(CXTranslationUnit unit, CXCursor cursor) {
284 CYList<CYStatement> statements;
285 CYForChild(cursor, fun([&](CXCursor child) {
286 statements->*CYTranslateStatement(unit, child);
287 }));
288 return $ CYBlock(statements);
289 }
290
291 static CYTypedIdentifier *CYDecodeType(CXType type);
292 static void CYParseType(CXType type, CYTypedIdentifier *typed);
293
294 static CYTypedIdentifier *CYDecodeType(CXType type, const CYCXString &identifier) {
295 CYTypedIdentifier *typed(CYDecodeType(type));
296 typed->identifier_ = $ CYIdentifier(identifier.Pool($pool));
297 return typed;
298 }
299
300 static void CYParseCursor(CXType type, CXCursor cursor, CYTypedIdentifier *typed) {
301 CYCXString spelling(cursor);
302
303 switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
304 case CXCursor_EnumDecl:
305 if (spelling[0] != '\0')
306 // XXX: should we have a special enum keyword?
307 typed->specifier_ = $ CYTypeVariable($I(spelling.Pool($pool)));
308 else
309 // XXX: maybe replace with "enum : int" instead of "int"
310 CYParseType(clang_getEnumDeclIntegerType(cursor), typed);
311 break;
312
313 case CXCursor_StructDecl: {
314 if (spelling[0] != '\0')
315 typed->specifier_ = $ CYTypeReference($I(spelling.Pool($pool)));
316 else {
317 CYList<CYTypeStructField> fields;
318 CYForChild(cursor, fun([&](CXCursor child) {
319 if (clang_getCursorKind(child) == CXCursor_FieldDecl) {
320 CYTypedIdentifier *field(CYDecodeType(clang_getCursorType(child), child));
321 fields->*$ CYTypeStructField(field);
322 }
323 }));
324
325 typed->specifier_ = $ CYTypeStruct(NULL, $ CYStructTail(fields));
326 }
327 } break;
328
329 case CXCursor_UnionDecl: {
330 _assert(false);
331 } break;
332
333 default:
334 std::cerr << "C:" << CYCXString(kind) << std::endl;
335 _assert(false);
336 break;
337 }
338 }
339
340 static CYTypedParameter *CYParseSignature(CXType type, CYTypedIdentifier *typed) {
341 CYParseType(clang_getResultType(type), typed);
342 CYList<CYTypedParameter> parameters;
343 for (int i(0), e(clang_getNumArgTypes(type)); i != e; ++i)
344 parameters->*$ CYTypedParameter(CYDecodeType(clang_getArgType(type, i)));
345 return parameters;
346 }
347
348 static void CYParseFunction(CXType type, CYTypedIdentifier *typed) {
349 typed = typed->Modify($ CYTypeFunctionWith(clang_isFunctionTypeVariadic(type), CYParseSignature(type, typed)));
350 }
351
352 static void CYParseType(CXType type, CYTypedIdentifier *typed) {
353 switch (CXTypeKind kind = type.kind) {
354 case CXType_Unexposed: {
355 CXType result(clang_getResultType(type));
356 if (result.kind == CXType_Invalid)
357 CYParseCursor(type, clang_getTypeDeclaration(type), typed);
358 else
359 // clang marks function pointers as Unexposed but still supports them
360 CYParseFunction(type, typed);
361 } break;
362
363 case CXType_Bool: typed->specifier_ = $ CYTypeVariable("bool"); break;
364 case CXType_Float: typed->specifier_ = $ CYTypeVariable("float"); break;
365 case CXType_Double: typed->specifier_ = $ CYTypeVariable("double"); break;
366
367 case CXType_Char_U: typed->specifier_ = $ CYTypeCharacter(CYTypeNeutral); break;
368 case CXType_Char_S: typed->specifier_ = $ CYTypeCharacter(CYTypeNeutral); break;
369 case CXType_SChar: typed->specifier_ = $ CYTypeCharacter(CYTypeSigned); break;
370 case CXType_UChar: typed->specifier_ = $ CYTypeCharacter(CYTypeUnsigned); break;
371
372 case CXType_Short: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 0); break;
373 case CXType_UShort: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 0); break;
374
375 case CXType_Int: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 1); break;
376 case CXType_UInt: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 1); break;
377
378 case CXType_Long: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 2); break;
379 case CXType_ULong: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 2); break;
380
381 case CXType_LongLong: typed->specifier_ = $ CYTypeIntegral(CYTypeSigned, 3); break;
382 case CXType_ULongLong: typed->specifier_ = $ CYTypeIntegral(CYTypeUnsigned, 3); break;
383
384 case CXType_BlockPointer: {
385 CXType pointee(clang_getPointeeType(type));
386 _assert(!clang_isFunctionTypeVariadic(pointee));
387 typed = typed->Modify($ CYTypeBlockWith(CYParseSignature(pointee, typed)));
388 } break;
389
390 case CXType_ConstantArray:
391 CYParseType(clang_getArrayElementType(type), typed);
392 typed = typed->Modify($ CYTypeArrayOf($D(clang_getArraySize(type))));
393 break;
394
395 case CXType_Enum:
396 typed->specifier_ = $ CYTypeVariable($pool.strdup(CYCXString(clang_getTypeSpelling(type))));
397 break;
398
399 case CXType_FunctionProto:
400 CYParseFunction(type, typed);
401 break;
402
403 case CXType_IncompleteArray:
404 // XXX: I should support these :/
405 _assert(false);
406 break;
407
408 case CXType_ObjCId:
409 typed->specifier_ = $ CYTypeVariable("id");
410 break;
411
412 case CXType_ObjCInterface:
413 typed->specifier_ = $ CYTypeVariable($pool.strdup(CYCXString(clang_getTypeSpelling(type))));
414 break;
415
416 case CXType_ObjCObjectPointer: {
417 CXType pointee(clang_getPointeeType(type));
418 if (pointee.kind != CXType_Unexposed) {
419 CYParseType(pointee, typed);
420 typed = typed->Modify($ CYTypePointerTo());
421 } else
422 // Clang seems to have internal typedefs for id and Class that are awkward
423 _assert(false);
424 } break;
425
426 case CXType_ObjCSel:
427 typed->specifier_ = $ CYTypeVariable("SEL");
428 break;
429
430 case CXType_Pointer:
431 CYParseType(clang_getPointeeType(type), typed);
432 typed = typed->Modify($ CYTypePointerTo());
433 break;
434
435 case CXType_Record:
436 typed->specifier_ = $ CYTypeReference($I($pool.strdup(CYCXString(clang_getTypeSpelling(type)))));
437 break;
438
439 case CXType_Typedef:
440 // use the declaration in order to isolate the name of the typedef itself
441 typed->specifier_ = $ CYTypeVariable($pool.strdup(CYCXString(clang_getTypeDeclaration(type))));
442 break;
443
444 case CXType_Vector:
445 _assert(false);
446 break;
447
448 case CXType_Void:
449 typed->specifier_ = $ CYTypeVoid();
450 break;
451
452 default:
453 std::cerr << "T:" << CYCXString(clang_getTypeKindSpelling(kind)) << std::endl;
454 std::cerr << "_: " << CYCXString(clang_getTypeSpelling(type)) << std::endl;
455 _assert(false);
456 }
457
458 if (clang_isConstQualifiedType(type))
459 typed = typed->Modify($ CYTypeConstant());
460 }
461
462 static CYTypedIdentifier *CYDecodeType(CXType type) {
463 CYTypedIdentifier *typed($ CYTypedIdentifier(NULL));
464 CYParseType(type, typed);
465 return typed;
466 }
467
468 static CXChildVisitResult CYChildVisit(CXCursor cursor, CXCursor parent, CXClientData arg) {
469 CYChildBaton &baton(*static_cast<CYChildBaton *>(arg));
470 CXTranslationUnit &unit(baton.unit);
471
472 CYCXString spelling(cursor);
473 std::string name(spelling);
474 std::ostringstream value;
475 unsigned priority(2);
476 unsigned flags(0);
477
478 /*CXSourceLocation location(clang_getCursorLocation(cursor));
479 CYCXPosition<> position(location);
480 std::cerr << spelling << " " << position << std::endl;*/
481
482 switch (CXCursorKind kind = clang_getCursorKind(cursor)) {
483 case CXCursor_EnumConstantDecl: {
484 value << clang_getEnumConstantDeclValue(cursor);
485 } break;
486
487 case CXCursor_MacroDefinition: try {
488 CXSourceRange range(clang_getCursorExtent(cursor));
489 CYTokens tokens(unit, range);
490 _assert(tokens.size() != 0);
491
492 CXCursor cursors[tokens.size()];
493 clang_annotateTokens(unit, tokens, tokens.size(), cursors);
494
495 CYLocalPool local;
496 CYList<CYFunctionParameter> parameters;
497 unsigned offset(1);
498
499 if (tokens.size() != 1) {
500 CYCXPosition<> start(clang_getRangeStart(range));
501 CYCXString first(unit, tokens[offset]);
502 if (first == "(") {
503 CYCXPosition<> paren(unit, tokens[offset]);
504 if (start.offset_ + strlen(spelling) == paren.offset_) {
505 for (;;) {
506 _assert(++offset != tokens.size());
507 CYCXString token(unit, tokens[offset]);
508 parameters->*$P($B($I(token.Pool($pool))));
509 _assert(++offset != tokens.size());
510 CYCXString comma(unit, tokens[offset]);
511 if (comma == ")")
512 break;
513 _assert(comma == ",");
514 }
515 ++offset;
516 }
517 }
518 }
519
520 std::ostringstream body;
521 for (unsigned i(offset); i != tokens.size(); ++i) {
522 CYCXString token(unit, tokens[i]);
523 if (i != offset)
524 body << " ";
525 body << token;
526 }
527
528 if (!parameters)
529 value << body.str();
530 else {
531 CYOptions options;
532 CYOutput out(*value.rdbuf(), options);
533 out << '(' << "function" << '(';
534 out << parameters;
535 out << ')' << '{';
536 out << "return" << ' ';
537 value << body.str();
538 out << ';' << '}' << ')';
539 }
540 } catch (const CYException &error) {
541 CYPool pool;
542 //std::cerr << error.PoolCString(pool) << std::endl;
543 goto skip;
544 } break;
545
546 case CXCursor_StructDecl: {
547 if (spelling[0] == '\0')
548 goto skip;
549 if (!clang_isCursorDefinition(cursor))
550 priority = 1;
551
552 std::ostringstream types;
553 std::ostringstream names;
554
555 CYForChild(cursor, fun([&](CXCursor child) {
556 if (clang_getCursorKind(child) == CXCursor_FieldDecl) {
557 CXType type(clang_getCursorType(child));
558 types << "(typedef " << CYCXString(clang_getTypeSpelling(type)) << "),";
559 names << "'" << CYCXString(child) << "',";
560 }
561 }));
562
563 value << "new Type([" << types.str() << "],[" << names.str() << "]).withName(\"" << name << "\")";
564 name += "$cy";
565 } break;
566
567 case CXCursor_TypedefDecl: try {
568 CYLocalPool local;
569
570 CYTypedIdentifier *typed(CYDecodeType(clang_getTypedefDeclUnderlyingType(cursor)));
571 if (typed->specifier_ == NULL)
572 value << "(typedef " << CYCXString(clang_getTypeSpelling(clang_getTypedefDeclUnderlyingType(cursor))) << ")";
573 else {
574 CYOptions options;
575 CYOutput out(*value.rdbuf(), options);
576 CYTypeExpression(typed).Output(out, CYNoBFC);
577 }
578 } catch (const CYException &error) {
579 CYPool pool;
580 //std::cerr << error.PoolCString(pool) << std::endl;
581 goto skip;
582 } break;
583
584 case CXCursor_FunctionDecl:
585 case CXCursor_VarDecl: try {
586 std::string label;
587
588 CYList<CYFunctionParameter> parameters;
589 CYStatement *code(NULL);
590
591 CYLocalPool local;
592
593 CYForChild(cursor, fun([&](CXCursor child) {
594 switch (CXCursorKind kind = clang_getCursorKind(child)) {
595 case CXCursor_AsmLabelAttr:
596 label = CYCXString(child);
597 break;
598
599 case CXCursor_CompoundStmt:
600 code = CYTranslateBlock(unit, child);
601 break;
602
603 case CXCursor_ParmDecl:
604 parameters->*$P($B($I(CYCXString(child).Pool($pool))));
605 break;
606
607 case CXCursor_IntegerLiteral:
608 case CXCursor_ObjCClassRef:
609 case CXCursor_TypeRef:
610 case CXCursor_UnexposedAttr:
611 break;
612
613 default:
614 //std::cerr << "A:" << CYCXString(child) << std::endl;
615 break;
616 }
617 }));
618
619 if (label.empty()) {
620 label = spelling;
621 label = '_' + label;
622 } else if (label[0] != '_')
623 goto skip;
624
625 if (code == NULL) {
626 CXType type(clang_getCursorType(cursor));
627 value << "*(typedef " << CYCXString(clang_getTypeSpelling(type)) << ").pointerTo()(dlsym(RTLD_DEFAULT,'" << label.substr(1) << "'))";
628 } else {
629 CYOptions options;
630 CYOutput out(*value.rdbuf(), options);
631 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters, code));
632 function->Output(out, CYNoBFC);
633 //std::cerr << value.str() << std::endl;
634 }
635 } catch (const CYException &error) {
636 CYPool pool;
637 //std::cerr << error.PoolCString(pool) << std::endl;
638 goto skip;
639 } break;
640
641 default: {
642 return CXChildVisit_Recurse;
643 } break;
644 }
645
646 {
647 CYKey &key(baton.keys[name]);
648 if (key.priority_ < priority) {
649 key.priority_ = priority;
650 key.code_ = value.str();
651 key.flags_ = flags;
652 }
653 }
654
655 skip:
656 return CXChildVisit_Continue;
657 }
658
659 int main(int argc, const char *argv[]) {
660 CXIndex index(clang_createIndex(0, 0));
661
662 const char *file(argv[1]);
663
664 unsigned offset(3);
665 #if CY_OBJECTIVEC
666 argv[--offset] = "-ObjC++";
667 #endif
668
669 CXTranslationUnit unit(clang_parseTranslationUnit(index, file, argv + offset, argc - offset, NULL, 0, CXTranslationUnit_DetailedPreprocessingRecord));
670
671 for (unsigned i(0), e(clang_getNumDiagnostics(unit)); i != e; ++i) {
672 CXDiagnostic diagnostic(clang_getDiagnostic(unit, i));
673 CYCXString spelling(clang_getDiagnosticSpelling(diagnostic));
674 std::cerr << spelling << std::endl;
675 }
676
677 CYKeyMap keys;
678 CYChildBaton baton(unit, keys);
679 clang_visitChildren(clang_getTranslationUnitCursor(unit), &CYChildVisit, &baton);
680
681 for (CYKeyMap::const_iterator key(keys.begin()); key != keys.end(); ++key) {
682 std::string code(key->second.code_);
683 for (size_t i(0), e(code.size()); i != e; ++i)
684 if (code[i] <= 0 || code[i] >= 0x7f || code[i] == '\n')
685 goto skip;
686 std::cout << key->first << "|" << key->second.flags_ << "\"" << code << "\"" << std::endl;
687 skip:; }
688
689 clang_disposeTranslationUnit(unit);
690 clang_disposeIndex(index);
691
692 return 0;
693 }