-
Notifications
You must be signed in to change notification settings - Fork 8
/
clangproxy.cpp
1160 lines (1073 loc) · 44.3 KB
/
clangproxy.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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Communication proxy to libclang-c
*/
#include <sdk.h>
#include "clangproxy.h"
#include <wx/tokenzr.h>
#ifndef CB_PRECOMP
#include <algorithm>
#endif // CB_PRECOMP
#include "tokendatabase.h"
#include "translationunit.h"
namespace ProxyHelper
{
static TokenCategory GetTokenCategory(CXCursorKind kind, CX_CXXAccessSpecifier access = CX_CXXInvalidAccessSpecifier)
{
switch (kind)
{
case CXCursor_StructDecl:
case CXCursor_UnionDecl:
case CXCursor_ClassDecl:
case CXCursor_ClassTemplate:
switch (access)
{
case CX_CXXPublic:
return tcClassPublic;
case CX_CXXProtected:
return tcClassProtected;
case CX_CXXPrivate:
return tcClassPrivate;
default:
case CX_CXXInvalidAccessSpecifier:
return tcClass;
}
case CXCursor_Constructor:
switch (access)
{
default:
case CX_CXXInvalidAccessSpecifier:
case CX_CXXPublic:
return tcCtorPublic;
case CX_CXXProtected:
return tcCtorProtected;
case CX_CXXPrivate:
return tcCtorPrivate;
}
case CXCursor_Destructor:
switch (access)
{
default:
case CX_CXXInvalidAccessSpecifier:
case CX_CXXPublic:
return tcDtorPublic;
case CX_CXXProtected:
return tcDtorProtected;
case CX_CXXPrivate:
return tcDtorPrivate;
}
case CXCursor_FunctionDecl:
case CXCursor_CXXMethod:
case CXCursor_FunctionTemplate:
switch (access)
{
default:
case CX_CXXInvalidAccessSpecifier:
case CX_CXXPublic:
return tcFuncPublic;
case CX_CXXProtected:
return tcFuncProtected;
case CX_CXXPrivate:
return tcFuncPrivate;
}
case CXCursor_FieldDecl:
case CXCursor_VarDecl:
case CXCursor_ParmDecl:
switch (access)
{
default:
case CX_CXXInvalidAccessSpecifier:
case CX_CXXPublic:
return tcVarPublic;
case CX_CXXProtected:
return tcVarProtected;
case CX_CXXPrivate:
return tcVarPrivate;
}
case CXCursor_MacroDefinition:
return tcMacroDef;
case CXCursor_EnumDecl:
switch (access)
{
case CX_CXXPublic:
return tcEnumPublic;
case CX_CXXProtected:
return tcEnumProtected;
case CX_CXXPrivate:
return tcEnumPrivate;
default:
case CX_CXXInvalidAccessSpecifier:
return tcEnum;
}
case CXCursor_EnumConstantDecl:
return tcEnumerator;
case CXCursor_Namespace:
return tcNamespace;
case CXCursor_TypedefDecl:
switch (access)
{
case CX_CXXPublic:
return tcTypedefPublic;
case CX_CXXProtected:
return tcTypedefProtected;
case CX_CXXPrivate:
return tcTypedefPrivate;
default:
case CX_CXXInvalidAccessSpecifier:
return tcTypedef;
}
default:
return tcNone;
}
}
static CXChildVisitResult ClCallTipCtorAST_Visitor(CXCursor cursor,
CXCursor WXUNUSED(parent),
CXClientData client_data)
{
switch (cursor.kind)
{
case CXCursor_Constructor:
{
std::vector<CXCursor>* tokenSet
= static_cast<std::vector<CXCursor>*>(client_data);
tokenSet->push_back(cursor);
break;
}
case CXCursor_FunctionDecl:
case CXCursor_CXXMethod:
case CXCursor_FunctionTemplate:
{
CXString str = clang_getCursorSpelling(cursor);
if (strcmp(clang_getCString(str), "operator()") == 0)
{
std::vector<CXCursor>* tokenSet
= static_cast<std::vector<CXCursor>*>(client_data);
tokenSet->push_back(cursor);
}
clang_disposeString(str);
break;
}
default:
break;
}
return CXChildVisit_Continue;
}
static CXChildVisitResult ClInheritance_Visitor(CXCursor cursor,
CXCursor WXUNUSED(parent),
CXClientData client_data)
{
if (cursor.kind != CXCursor_CXXBaseSpecifier)
return CXChildVisit_Break;
CXString str = clang_getTypeSpelling(clang_getCursorType(cursor));
static_cast<wxStringVec*>(client_data)->push_back(wxString::FromUTF8(clang_getCString(str)));
clang_disposeString(str);
return CXChildVisit_Continue;
}
static CXChildVisitResult ClEnum_Visitor(CXCursor cursor,
CXCursor WXUNUSED(parent),
CXClientData client_data)
{
if (cursor.kind != CXCursor_EnumConstantDecl)
return CXChildVisit_Break;
int* counts = static_cast<int*>(client_data);
long long val = clang_getEnumConstantDeclValue(cursor);
if (val > 0 && !((val - 1) & val)) // is power of 2
++counts[0];
++counts[1];
counts[2] = std::max(counts[2], static_cast<int>(val));
return CXChildVisit_Continue;
}
static void ResolveCursorDecl(CXCursor& token)
{
CXCursor resolve = clang_getCursorDefinition(token);
if (clang_Cursor_isNull(resolve) || clang_isInvalid(token.kind))
{
resolve = clang_getCursorReferenced(token);
if (!clang_Cursor_isNull(resolve) && !clang_isInvalid(token.kind))
token = resolve;
}
else
token = resolve;
}
static CXVisitorResult ReferencesVisitor(CXClientData context,
CXCursor WXUNUSED(cursor),
CXSourceRange range)
{
unsigned rgStart, rgEnd;
CXSourceLocation rgLoc = clang_getRangeStart(range);
clang_getSpellingLocation(rgLoc, nullptr, nullptr, nullptr, &rgStart);
rgLoc = clang_getRangeEnd(range);
clang_getSpellingLocation(rgLoc, nullptr, nullptr, nullptr, &rgEnd);
if (rgStart != rgEnd)
{
static_cast<std::vector< std::pair<int, int> >*>(context)
->push_back(std::make_pair<int, int>(rgStart, rgEnd - rgStart));
}
return CXVisit_Continue;
}
static wxString GetEnumValStr(CXCursor token)
{
int counts[] = {0, 0, 0}; // (numPowerOf2, numTotal, maxVal)
clang_visitChildren(clang_getCursorSemanticParent(token), &ProxyHelper::ClEnum_Visitor, counts);
wxLongLong val(clang_getEnumConstantDeclValue(token));
if (( (counts[0] == counts[1])
|| (counts[1] > 5 && counts[0] * 2 >= counts[1]) ) && val >= 0)
{
// lots of 2^n enum constants, probably bitmask -> display in hexadecimal
wxString formatStr
= wxString::Format(wxT("0x%%0%ulX"),
wxString::Format(wxT("%X"), // count max width for 0-padding
static_cast<unsigned>(counts[2])).Length());
return wxString::Format(formatStr, static_cast<unsigned long>(val.GetValue()));
}
else
return val.ToString();
}
}
namespace HTML_Writer
{
static wxString Escape(const wxString& text)
{
wxString html;
html.reserve(text.size());
for (wxString::const_iterator itr = text.begin();
itr != text.end(); ++itr)
{
switch (*itr)
{
case wxT('&'): html += wxT("&"); break;
case wxT('\"'): html += wxT("""); break;
case wxT('\''): html += wxT("'"); break;
case wxT('<'): html += wxT("<"); break;
case wxT('>'): html += wxT(">"); break;
case wxT('\n'): html += wxT("<br>"); break;
default: html += *itr; break;
}
}
return html;
}
static wxString Colourise(const wxString& text, const wxString& colour)
{
return wxT("<font color=\"") + colour + wxT("\">") + text + wxT("</font>");
}
static wxString SyntaxHl(const wxString& code, const std::vector<wxString>& cppKeywords) // C++ style (ish)
{
wxString html;
html.reserve(code.size());
int stRg = 0;
int style = wxSCI_C_DEFAULT;
const int codeLen = code.Length();
for (int enRg = 0; enRg <= codeLen; ++enRg)
{
wxChar ch = (enRg < codeLen ? code[enRg] : wxT('\0'));
wxChar nextCh = (enRg < codeLen - 1 ? code[enRg + 1] : wxT('\0'));
switch (style)
{
default:
case wxSCI_C_DEFAULT:
{
if (wxIsalpha(ch) || ch == wxT('_'))
style = wxSCI_C_IDENTIFIER;
else if (wxIsdigit(ch))
style = wxSCI_C_NUMBER;
else if (ch == wxT('"'))
style = wxSCI_C_STRING;
else if (ch == wxT('\''))
style = wxSCI_C_CHARACTER;
else if (ch == wxT('/') && nextCh == wxT('/'))
style = wxSCI_C_COMMENTLINE;
else if (wxIspunct(ch))
style = wxSCI_C_OPERATOR;
else
break;
if (stRg != enRg)
{
html += Escape(code.Mid(stRg, enRg - stRg));
stRg = enRg;
}
break;
}
case wxSCI_C_IDENTIFIER:
{
if (wxIsalnum(ch) || ch == wxT('_'))
break;
if (stRg != enRg)
{
const wxString& tkn = code.Mid(stRg, enRg - stRg);
if (std::binary_search(cppKeywords.begin(), cppKeywords.end(), tkn))
html += wxT("<b>") + Colourise(Escape(tkn), wxT("#00008b")) + wxT("</b>"); // DarkBlue
else
html += Escape(tkn);
stRg = enRg;
--enRg;
}
style = wxSCI_C_DEFAULT;
break;
}
case wxSCI_C_NUMBER:
{
if (wxIsalnum(ch))
break;
if (stRg != enRg)
{
html += Colourise(Escape(code.Mid(stRg, enRg - stRg)), wxT("Magenta"));
stRg = enRg;
--enRg;
}
style = wxSCI_C_DEFAULT;
break;
}
case wxSCI_C_STRING:
{
if (ch == wxT('\\'))
{
if (nextCh != wxT('\n'))
++enRg;
break;
}
else if (ch && ch != wxT('"') && ch != wxT('\n'))
break;
if (stRg != enRg)
{
if (ch == wxT('"'))
++enRg;
html += Colourise(Escape(code.Mid(stRg, enRg - stRg)), wxT("#0000cd")); // MediumBlue
stRg = enRg;
--enRg;
}
style = wxSCI_C_DEFAULT;
break;
}
case wxSCI_C_CHARACTER:
{
if (ch == wxT('\\'))
{
if (nextCh != wxT('\n'))
++enRg;
break;
}
else if (ch && ch != wxT('\'') && ch != wxT('\n'))
break;
if (stRg != enRg)
{
if (ch == wxT('\''))
++enRg;
html += Colourise(Escape(code.Mid(stRg, enRg - stRg)), wxT("GoldenRod"));
stRg = enRg;
--enRg;
}
style = wxSCI_C_DEFAULT;
break;
}
case wxSCI_C_COMMENTLINE:
{
if (ch && ch != wxT('\n'))
break;
if (stRg != enRg)
{
html += Colourise(Escape(code.Mid(stRg, enRg - stRg)), wxT("#778899")); // LightSlateGray
stRg = enRg;
}
style = wxSCI_C_DEFAULT;
break;
}
case wxSCI_C_OPERATOR:
{
if (wxIspunct(ch) && ch != wxT('"') && ch != wxT('\'') && ch != wxT('_'))
break;
if (stRg != enRg)
{
html += Colourise(Escape(code.Mid(stRg, enRg - stRg)), wxT("Red"));
stRg = enRg;
--enRg;
}
style = wxSCI_C_DEFAULT;
break;
}
}
}
return html;
}
static void FormatDocumentation(CXComment comment, wxString& doc, const std::vector<wxString>& cppKeywords)
{
size_t numChildren = clang_Comment_getNumChildren(comment);
for (size_t childIdx = 0; childIdx < numChildren; ++childIdx)
{
CXComment cmt = clang_Comment_getChild(comment, childIdx);
switch (clang_Comment_getKind(cmt))
{
case CXComment_Null:
break;
case CXComment_Text:
{
CXString str = clang_TextComment_getText(cmt);
doc += Escape(wxString::FromUTF8(clang_getCString(str)));
clang_disposeString(str);
break;
}
case CXComment_InlineCommand:
{
size_t numArgs = clang_InlineCommandComment_getNumArgs(cmt);
wxString argText;
for (size_t argIdx = 0; argIdx < numArgs; ++argIdx)
{
CXString str = clang_InlineCommandComment_getArgText(cmt, argIdx);
argText += Escape(wxString::FromUTF8(clang_getCString(str)));
clang_disposeString(str);
}
switch (clang_InlineCommandComment_getRenderKind(cmt))
{
default:
case CXCommentInlineCommandRenderKind_Normal:
doc += argText;
break;
case CXCommentInlineCommandRenderKind_Bold:
doc += wxT("<b>") + argText + wxT("</b>");
break;
case CXCommentInlineCommandRenderKind_Monospaced:
doc += wxT("<tt>") + argText + wxT("</tt>");
break;
case CXCommentInlineCommandRenderKind_Emphasized:
doc += wxT("<em>") + argText + wxT("</em>");
break;
}
break;
}
case CXComment_HTMLStartTag:
case CXComment_HTMLEndTag:
{
CXString str = clang_HTMLTagComment_getAsString(cmt);
doc += wxString::FromUTF8(clang_getCString(str));
clang_disposeString(str);
break;
}
case CXComment_Paragraph:
if (!clang_Comment_isWhitespace(cmt))
{
doc += wxT("<p>");
FormatDocumentation(cmt, doc, cppKeywords);
doc += wxT("</p>");
}
break;
case CXComment_BlockCommand: // TODO: follow the command's instructions
FormatDocumentation(cmt, doc, cppKeywords);
break;
case CXComment_ParamCommand: // TODO
case CXComment_TParamCommand: // TODO
break;
case CXComment_VerbatimBlockCommand:
doc += wxT("<table cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"black\" width=\"100%\"><tr><td>"
"<table bgcolor=\"white\" width=\"100%\"><tr><td><pre>");
FormatDocumentation(cmt, doc, cppKeywords);
doc += wxT("</pre></td></tr></table></td></tr></table>");
break;
case CXComment_VerbatimBlockLine:
{
CXString str = clang_VerbatimBlockLineComment_getText(cmt);
wxString codeLine = wxString::FromUTF8(clang_getCString(str));
clang_disposeString(str);
int endIdx = codeLine.Find(wxT("*/")); // clang will throw in the rest of the file when this happens
if (endIdx != wxNOT_FOUND)
{
endIdx = codeLine.Truncate(endIdx).Find(wxT("\\endcode")); // try to save a bit of grace, and recover what we can
if (endIdx == wxNOT_FOUND)
{
endIdx = codeLine.Find(wxT("@endcode"));
if (endIdx != wxNOT_FOUND)
codeLine.Truncate(endIdx);
}
else
codeLine.Truncate(endIdx);
doc += SyntaxHl(codeLine, cppKeywords) + wxT("<br><font color=\"red\"><em>__clang_doxygen_parsing_error__</em></font><br>");
return; // abort
}
doc += SyntaxHl(codeLine, cppKeywords) + wxT("<br>");
break;
}
case CXComment_VerbatimLine:
{
CXString str = clang_VerbatimLineComment_getText(cmt);
doc += wxT("<pre>") + Escape(wxString::FromUTF8(clang_getCString(str))) + wxT("</pre>"); // TODO: syntax highlight
clang_disposeString(str);
break;
}
case CXComment_FullComment: // ignore?
default:
break;
}
}
}
}
ClangProxy::ClangProxy(TokenDatabase& database, const std::vector<wxString>& cppKeywords):
m_Database(database),
m_CppKeywords(cppKeywords)
{
m_ClIndex = clang_createIndex(0, 0);
}
ClangProxy::~ClangProxy()
{
m_TranslUnits.clear();
clang_disposeIndex(m_ClIndex);
}
void ClangProxy::CreateTranslationUnit(const wxString& filename, const wxString& commands)
{
wxStringTokenizer tokenizer(commands);
if (!filename.EndsWith(wxT(".c"))) // force language reduces chance of error on STL headers
tokenizer.SetString(commands + wxT(" -x c++"));
std::vector<wxString> unknownOptions;
unknownOptions.push_back(wxT("-Wno-unused-local-typedefs"));
unknownOptions.push_back(wxT("-Wzero-as-null-pointer-constant"));
std::sort(unknownOptions.begin(), unknownOptions.end());
std::vector<wxCharBuffer> argsBuffer;
std::vector<const char*> args;
while (tokenizer.HasMoreTokens())
{
const wxString& compilerSwitch = tokenizer.GetNextToken();
if (std::binary_search(unknownOptions.begin(), unknownOptions.end(), compilerSwitch))
continue;
argsBuffer.push_back(compilerSwitch.ToUTF8());
args.push_back(argsBuffer.back().data());
}
m_TranslUnits.push_back(TranslationUnit(filename, args, m_ClIndex, &m_Database));
}
int ClangProxy::GetTranslationUnitId(FileId fId)
{
for (size_t i = 0; i < m_TranslUnits.size(); ++i)
{
if (m_TranslUnits[i].Contains(fId))
return i;
}
return wxNOT_FOUND;
}
int ClangProxy::GetTranslationUnitId(const wxString& filename)
{
return GetTranslationUnitId(m_Database.GetFilenameId(filename));
}
void ClangProxy::CodeCompleteAt(bool isAuto, const wxString& filename,
int line, int column, int translId,
const std::map<wxString, wxString>& unsavedFiles,
std::vector<ClToken>& results)
{
wxCharBuffer chName = filename.ToUTF8();
std::vector<CXUnsavedFile> clUnsavedFiles;
std::vector<wxCharBuffer> clFileBuffer;
for (std::map<wxString, wxString>::const_iterator fileIt = unsavedFiles.begin();
fileIt != unsavedFiles.end(); ++fileIt)
{
CXUnsavedFile unit;
clFileBuffer.push_back(fileIt->first.ToUTF8());
unit.Filename = clFileBuffer.back().data();
clFileBuffer.push_back(fileIt->second.ToUTF8());
unit.Contents = clFileBuffer.back().data();
#if wxCHECK_VERSION(2, 9, 4)
unit.Length = clFileBuffer.back().length();
#else
unit.Length = strlen(unit.Contents); // extra work needed because wxString::Length() treats multibyte character length as '1'
#endif
clUnsavedFiles.push_back(unit);
}
CXCodeCompleteResults* clResults
= m_TranslUnits[translId].CodeCompleteAt(chName.data(), line, column,
clUnsavedFiles.empty() ? nullptr : &clUnsavedFiles[0],
clUnsavedFiles.size());
if (!clResults)
return;
if (isAuto && clang_codeCompleteGetContexts(clResults) == CXCompletionContext_Unknown)
return;
const int numResults = clResults->NumResults;
results.reserve(numResults);
for (int resIdx = 0; resIdx < numResults; ++resIdx)
{
const CXCompletionResult& token = clResults->Results[resIdx];
if (CXAvailability_Available != clang_getCompletionAvailability(token.CompletionString))
continue;
const int numChunks = clang_getNumCompletionChunks(token.CompletionString);
wxString type;
for (int chunkIdx = 0; chunkIdx < numChunks; ++chunkIdx)
{
CXCompletionChunkKind kind = clang_getCompletionChunkKind(token.CompletionString, chunkIdx);
if (kind == CXCompletionChunk_ResultType)
{
CXString str = clang_getCompletionChunkText(token.CompletionString, chunkIdx);
type = wxT(": ") + wxString::FromUTF8(clang_getCString(str));
wxString prefix;
if (type.EndsWith(wxT(" *"), &prefix) || type.EndsWith(wxT(" &"), &prefix))
type = prefix + type.Last();
clang_disposeString(str);
}
else if (kind == CXCompletionChunk_TypedText)
{
if (type.Length() > 40)
{
type.Truncate(35);
if (wxIsspace(type.Last()))
type.Trim();
else if (wxIspunct(type.Last()))
{
for (int i = type.Length() - 2; i > 10; --i)
{
if (!wxIspunct(type[i]))
{
type.Truncate(i + 1);
break;
}
}
}
else if (wxIsalnum(type.Last()) || type.Last() == wxT('_'))
{
for (int i = type.Length() - 2; i > 10; --i)
{
if (!( wxIsalnum(type[i]) || type[i] == wxT('_') ))
{
type.Truncate(i + 1);
break;
}
}
}
type += wxT("...");
}
CXString completeTxt = clang_getCompletionChunkText(token.CompletionString, chunkIdx);
results.push_back(ClToken(wxString::FromUTF8(clang_getCString(completeTxt)) + type,
resIdx, clang_getCompletionPriority(token.CompletionString),
ProxyHelper::GetTokenCategory(token.CursorKind)));
clang_disposeString(completeTxt);
type.Empty();
break;
}
}
}
}
wxString ClangProxy::DocumentCCToken(int translId, int tknId)
{
const CXCompletionResult* token = m_TranslUnits[translId].GetCCResult(tknId);
if (!token)
return wxEmptyString;
int upperBound = clang_getNumCompletionChunks(token->CompletionString);
wxString doc;
if (token->CursorKind == CXCursor_Namespace)
doc = wxT("namespace ");
for (int i = 0; i < upperBound; ++i)
{
CXCompletionChunkKind kind = clang_getCompletionChunkKind(token->CompletionString, i);
if (kind == CXCompletionChunk_TypedText)
{
CXString str = clang_getCompletionParent(token->CompletionString, nullptr);
wxString parent = wxString::FromUTF8(clang_getCString(str));
if (!parent.IsEmpty())
doc += parent + wxT("::");
clang_disposeString(str);
}
CXString str = clang_getCompletionChunkText(token->CompletionString, i);
doc += wxString::FromUTF8(clang_getCString(str));
if (kind == CXCompletionChunk_ResultType)
{
if (doc.Length() > 2 && doc[doc.Length() - 2] == wxT(' '))
doc.RemoveLast(2) += doc.Last();
doc += wxT(' ');
}
clang_disposeString(str);
}
wxString descriptor;
wxString identifier;
unsigned tokenHash = HashToken(token->CompletionString, identifier);
if (!identifier.IsEmpty())
{
TokenId tId = m_Database.GetTokenId(identifier, tokenHash);
if (tId != wxNOT_FOUND)
{
const AbstractToken& aTkn = m_Database.GetToken(tId);
CXCursor clTkn = m_TranslUnits[translId].GetTokensAt(m_Database.GetFilename(aTkn.fileId),
aTkn.line, aTkn.column);
if (!clang_Cursor_isNull(clTkn) && !clang_isInvalid(clTkn.kind))
{
CXComment docComment = clang_Cursor_getParsedComment(clTkn);
HTML_Writer::FormatDocumentation(docComment, descriptor, m_CppKeywords);
if (clTkn.kind == CXCursor_EnumConstantDecl)
doc += wxT("=") + ProxyHelper::GetEnumValStr(clTkn);
else if (clTkn.kind == CXCursor_TypedefDecl)
{
CXString str = clang_getTypeSpelling(clang_getTypedefDeclUnderlyingType(clTkn));
wxString type = wxString::FromUTF8(clang_getCString(str));
if (!type.IsEmpty())
doc.Prepend(wxT("typedef ") + type + wxT(" "));
clang_disposeString(str);
}
}
}
}
if (descriptor.IsEmpty())
{
CXString comment = clang_getCompletionBriefComment(token->CompletionString);
descriptor = HTML_Writer::Escape(wxT("\n") + wxString::FromUTF8(clang_getCString(comment)));
clang_disposeString(comment);
}
return wxT("<html><body><br><tt>") + HTML_Writer::SyntaxHl(doc, m_CppKeywords)
+ wxT("</tt>") + descriptor + wxT("</body></html>");
}
wxString ClangProxy::GetCCInsertSuffix(int translId, int tknId, const wxString& newLine, std::pair<int, int>& offsets)
{
const CXCompletionResult* token = m_TranslUnits[translId].GetCCResult(tknId);
if (!token)
return wxEmptyString;
const CXCompletionString& clCompStr = token->CompletionString;
int upperBound = clang_getNumCompletionChunks(clCompStr);
enum BuilderState { init, store, exit };
BuilderState state = init;
wxString suffix;
for (int i = 0; i < upperBound; ++i)
{
switch (clang_getCompletionChunkKind(clCompStr, i))
{
case CXCompletionChunk_TypedText:
if (state == init)
state = store;
break;
case CXCompletionChunk_Placeholder:
if (state == store)
{
CXString str = clang_getCompletionChunkText(clCompStr, i);
const wxString& param = wxT("/*! ") + wxString::FromUTF8(clang_getCString(str)) + wxT(" !*/");
offsets = std::make_pair(suffix.Length(), suffix.Length() + param.Length());
suffix += param;
clang_disposeString(str);
state = exit;
}
break;
case CXCompletionChunk_Informative:
break;
case CXCompletionChunk_VerticalSpace:
if (state != init)
suffix += newLine;
break;
default:
if (state != init)
{
CXString str = clang_getCompletionChunkText(clCompStr, i);
suffix += wxString::FromUTF8(clang_getCString(str));
clang_disposeString(str);
}
break;
}
}
if (state != exit)
offsets = std::make_pair(suffix.Length(), suffix.Length());
return suffix;
}
void ClangProxy::RefineTokenType(int translId, int tknId, int& tknType)
{
const CXCompletionResult* token = m_TranslUnits[translId].GetCCResult(tknId);
if (!token)
return;
wxString identifier;
unsigned tokenHash = HashToken(token->CompletionString, identifier);
if (!identifier.IsEmpty())
{
TokenId tId = m_Database.GetTokenId(identifier, tokenHash);
if (tId != wxNOT_FOUND)
{
const AbstractToken& aTkn = m_Database.GetToken(tId);
CXCursor clTkn = m_TranslUnits[translId].GetTokensAt(m_Database.GetFilename(aTkn.fileId),
aTkn.line, aTkn.column);
if (!clang_Cursor_isNull(clTkn) && !clang_isInvalid(clTkn.kind))
{
TokenCategory tkCat
= ProxyHelper::GetTokenCategory(token->CursorKind, clang_getCXXAccessSpecifier(clTkn));
if (tkCat != tcNone)
tknType = tkCat;
}
}
}
}
void ClangProxy::GetCallTipsAt(const wxString& filename, int line, int column,
int translId, const wxString& tokenStr,
std::vector<wxStringVec>& results)
{
std::vector<CXCursor> tokenSet;
if (column > static_cast<int>(tokenStr.Length()))
{
column -= tokenStr.Length() / 2;
CXCursor token = m_TranslUnits[translId].GetTokensAt(filename, line, column);
if (!clang_Cursor_isNull(token))
{
CXCursor resolve = clang_getCursorDefinition(token);
if (clang_Cursor_isNull(resolve) || clang_isInvalid(token.kind))
{
resolve = clang_getCursorReferenced(token);
if (!clang_Cursor_isNull(resolve) && !clang_isInvalid(token.kind))
token = resolve;
}
else
token = resolve;
tokenSet.push_back(token);
}
}
// TODO: searching the database is very inexact, but necessary, as clang
// does not resolve the token when the code is invalid (incomplete)
std::vector<TokenId> tknIds = m_Database.GetTokenMatches(tokenStr);
for (std::vector<TokenId>::const_iterator itr = tknIds.begin(); itr != tknIds.end(); ++itr)
{
const AbstractToken& aTkn = m_Database.GetToken(*itr);
CXCursor token = m_TranslUnits[translId].GetTokensAt(m_Database.GetFilename(aTkn.fileId),
aTkn.line, aTkn.column);
if (!clang_Cursor_isNull(token) && !clang_isInvalid(token.kind))
tokenSet.push_back(token);
}
std::set<wxString> uniqueTips;
for (size_t tknIdx = 0; tknIdx < tokenSet.size(); ++tknIdx)
{
CXCursor token = tokenSet[tknIdx];
switch (ProxyHelper::GetTokenCategory(token.kind, CX_CXXPublic))
{
case tcVarPublic:
{
token = clang_getTypeDeclaration(clang_getCursorResultType(token));
if (!clang_Cursor_isNull(token) && !clang_isInvalid(token.kind))
tokenSet.push_back(token);
break;
}
case tcTypedefPublic:
{
token = clang_getTypeDeclaration(clang_getTypedefDeclUnderlyingType(token));
if (!clang_Cursor_isNull(token) && !clang_isInvalid(token.kind))
tokenSet.push_back(token);
break;
}
case tcClassPublic:
{
// search for constructors and 'operator()'
clang_visitChildren(token, &ProxyHelper::ClCallTipCtorAST_Visitor, &tokenSet);
break;
}
case tcCtorPublic:
{
if (clang_getCXXAccessSpecifier(token) == CX_CXXPrivate)
break;
// fall through
}
case tcFuncPublic:
{
const CXCompletionString& clCompStr = clang_getCursorCompletionString(token);
wxStringVec entry;
int upperBound = clang_getNumCompletionChunks(clCompStr);
entry.push_back(wxEmptyString);
for (int chunkIdx = 0; chunkIdx < upperBound; ++chunkIdx)
{
CXCompletionChunkKind kind = clang_getCompletionChunkKind(clCompStr, chunkIdx);
if (kind == CXCompletionChunk_TypedText)
{
CXString str = clang_getCompletionParent(clCompStr, nullptr);
wxString parent = wxString::FromUTF8(clang_getCString(str));
if (!parent.IsEmpty())
entry[0] += parent + wxT("::");
clang_disposeString(str);
}
else if (kind == CXCompletionChunk_LeftParen)
{
if (entry[0].IsEmpty() || !entry[0].EndsWith(wxT("operator")))
break;
}
CXString str = clang_getCompletionChunkText(clCompStr, chunkIdx);
entry[0] += wxString::FromUTF8(clang_getCString(str));
if (kind == CXCompletionChunk_ResultType)
{
if (entry[0].Length() > 2 && entry[0][entry[0].Length() - 2] == wxT(' '))
entry[0].RemoveLast(2) += entry[0].Last();
entry[0] += wxT(' ');
}
clang_disposeString(str);
}
entry[0] += wxT('(');
int numArgs = clang_Cursor_getNumArguments(token);
for (int argIdx = 0; argIdx < numArgs; ++argIdx)
{
CXCursor arg = clang_Cursor_getArgument(token, argIdx);
wxString tknStr;
const CXCompletionString& argStr = clang_getCursorCompletionString(arg);
upperBound = clang_getNumCompletionChunks(argStr);
for (int chunkIdx = 0; chunkIdx < upperBound; ++chunkIdx)
{
CXCompletionChunkKind kind = clang_getCompletionChunkKind(argStr, chunkIdx);
if (kind == CXCompletionChunk_TypedText)
{
CXString str = clang_getCompletionParent(argStr, nullptr);
wxString parent = wxString::FromUTF8(clang_getCString(str));
if (!parent.IsEmpty())
tknStr += parent + wxT("::");
clang_disposeString(str);
}
CXString str = clang_getCompletionChunkText(argStr, chunkIdx);
tknStr += wxString::FromUTF8(clang_getCString(str));
if (kind == CXCompletionChunk_ResultType)
{
if (tknStr.Length() > 2 && tknStr[tknStr.Length() - 2] == wxT(' '))
tknStr.RemoveLast(2) += tknStr.Last();
tknStr += wxT(' ');
}
clang_disposeString(str);
}
entry.push_back(tknStr.Trim());
}
entry.push_back(wxT(')'));
wxString composit;
for (wxStringVec::const_iterator itr = entry.begin();
itr != entry.end(); ++itr)
{
composit += *itr;
}
if (uniqueTips.find(composit) != uniqueTips.end())
break;
uniqueTips.insert(composit);
results.push_back(entry);
break;
}
default:
break;
}
}
}