- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSwiftEditor.cpp
2615 lines (2231 loc) · 91.6 KB
/
SwiftEditor.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
//===--- SwiftEditor.cpp --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include"SwiftASTManager.h"
#include"SwiftEditorDiagConsumer.h"
#include"SwiftLangSupport.h"
#include"SourceKit/Core/Context.h"
#include"SourceKit/Core/NotificationCenter.h"
#include"SourceKit/Support/FileSystemProvider.h"
#include"SourceKit/Support/ImmutableTextBuffer.h"
#include"SourceKit/Support/Logging.h"
#include"SourceKit/Support/Tracing.h"
#include"SourceKit/Support/UIdent.h"
#include"swift/AST/ASTPrinter.h"
#include"swift/AST/ASTVisitor.h"
#include"swift/AST/ASTWalker.h"
#include"swift/AST/DiagnosticsClangImporter.h"
#include"swift/AST/DiagnosticsParse.h"
#include"swift/AST/DiagnosticsFrontend.h"
#include"swift/AST/DiagnosticsSIL.h"
#include"swift/Basic/Compiler.h"
#include"swift/Basic/SourceManager.h"
#include"swift/Demangling/ManglingUtils.h"
#include"swift/Frontend/Frontend.h"
#include"swift/Frontend/PrintingDiagnosticConsumer.h"
#include"swift/IDE/CommentConversion.h"
#include"swift/IDE/Indenting.h"
#include"swift/IDE/SourceEntityWalker.h"
#include"swift/IDE/SyntaxModel.h"
#include"swift/Subsystems.h"
#include"llvm/Support/ErrorHandling.h"
#include"llvm/Support/FileSystem.h"
#include"llvm/Support/MemoryBuffer.h"
#include"llvm/Support/Mutex.h"
usingnamespaceSourceKit;
usingnamespaceswift;
usingnamespaceide;
static std::vector<unsigned> getSortedBufferIDs(
const llvm::DenseMap<unsigned, std::vector<DiagnosticEntryInfo>> &Map) {
std::vector<unsigned> bufferIDs;
bufferIDs.reserve(Map.size());
for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
bufferIDs.push_back(I->getFirst());
}
llvm::array_pod_sort(bufferIDs.begin(), bufferIDs.end());
return bufferIDs;
}
voidEditorDiagConsumer::getAllDiagnostics(
SmallVectorImpl<DiagnosticEntryInfo> &Result) {
Result.append(InvalidLocDiagnostics.begin(), InvalidLocDiagnostics.end());
// Note: We cannot use the input buffer IDs because there may be diagnostics
// outside the inputs. Instead, sort the extant buffers.
auto bufferIDs = getSortedBufferIDs(BufferDiagnostics);
for (unsigned bufferID : bufferIDs) {
constauto &diags = BufferDiagnostics[bufferID];
Result.append(diags.begin(), diags.end());
}
}
/// Retrieve the raw range from a range, if it exists in the provided buffer.
/// Otherwise returns \c None.
static std::optional<RawCharSourceRange>
getRawRangeInBuffer(CharSourceRange range, unsigned bufferID,
SourceManager &SM) {
if (!range.isValid() ||
SM.findBufferContainingLoc(range.getStart()) != bufferID) {
return std::nullopt;
}
unsigned offset = SM.getLocOffsetInBuffer(range.getStart(), bufferID);
unsigned length = range.getByteLength();
return {{offset, length}};
}
BufferInfoSharedPtr
EditorDiagConsumer::getBufferInfo(StringRef FileName,
std::optional<unsigned> BufferID,
swift::SourceManager &SM) {
auto Result = BufferInfos.find(FileName);
if (Result != BufferInfos.end())
return Result->second;
std::optional<std::string> GeneratedFileText;
std::optional<BufferInfo::OriginalLocation> OriginalLocInfo;
// If we have a generated buffer, we need to include the source text, as
// clients otherwise won't be able to access to it.
if (BufferID) {
if (auto Info = SM.getGeneratedSourceInfo(*BufferID)) {
// Don't include this information for replaced function body buffers, as
// they'll just be referencing the original file.
// FIXME: Does this mean that the location information we'll be giving
// will be wrong? Seems like we may need to adjust it to correspond to the
// original file.
if (Info->kind != GeneratedSourceInfo::ReplacedFunctionBody) {
GeneratedFileText.emplace(SM.getEntireTextForBuffer(*BufferID));
auto OrigRange = Info->originalSourceRange;
if (OrigRange.isValid()) {
auto OrigLoc = OrigRange.getStart();
auto OrigBuffer = SM.findBufferContainingLoc(OrigLoc);
auto OrigRawRange = getRawRangeInBuffer(OrigRange, OrigBuffer, SM);
assert(OrigRawRange);
auto OrigFileName = SM.getDisplayNameForLoc(OrigLoc);
auto OrigInfo = getBufferInfo(OrigFileName, OrigBuffer, SM);
OriginalLocInfo.emplace(OrigInfo, OrigBuffer, *OrigRawRange);
}
}
}
}
auto Info = std::make_shared<BufferInfo>(FileName.str(), GeneratedFileText,
OriginalLocInfo);
BufferInfos.insert({FileName, Info});
return Info;
}
voidEditorDiagConsumer::handleDiagnostic(SourceManager &SM,
const DiagnosticInfo &Info) {
if (Info.Kind == DiagnosticKind::Error) {
HadAnyError = true;
}
// Filter out benign diagnostics for editing.
// oslog_invalid_log_message is spuriously output for live issues as modules
// in the index build are built without function bodies (including inline
// functions). OSLogOptimization expects SIL for bodies and hence errors
// when there isn't any. Ignore in live issues for now and re-evaluate if
// this (not having SIL for inline functions) becomes a more widespread issue.
if (Info.ID == diag::lex_editor_placeholder.ID ||
Info.ID == diag::oslog_invalid_log_message.ID)
return;
bool IsNote = (Info.Kind == DiagnosticKind::Note);
if (IsNote && !haveLastDiag())
// Is this possible?
return;
if (Info.Kind == DiagnosticKind::Remark) {
// FIXME: we may want to handle optimization remarks in sourcekitd.
LOG_WARN_FUNC("unhandled optimization remark");
return;
}
DiagnosticEntryInfo SKInfo;
SKInfo.ID = DiagnosticEngine::diagnosticIDStringFor(Info.ID).str();
if (Info.Category == "deprecation" ||
Info.Category.starts_with("Deprecated")) {
SKInfo.Categories.push_back(DiagnosticCategory::Deprecation);
} elseif (Info.Category == "no-usage") {
SKInfo.Categories.push_back(DiagnosticCategory::NoUsage);
}
// Actually substitute the diagnostic arguments into the diagnostic text.
llvm::SmallString<256> Text;
{
llvm::raw_svector_ostream Out(Text);
DiagnosticEngine::formatDiagnosticText(Out, Info.FormatString,
Info.FormatArgs);
}
SKInfo.Description = Text.str();
if (!Info.CategoryDocumentationURL.empty())
SKInfo.EducationalNotePaths.push_back(Info.CategoryDocumentationURL);
std::optional<unsigned> BufferIDOpt;
if (Info.Loc.isValid()) {
BufferIDOpt = SM.findBufferContainingLoc(Info.Loc);
}
if (BufferIDOpt.has_value()) {
unsigned BufferID = *BufferIDOpt;
if (auto info = SM.getGeneratedSourceInfo(BufferID)) {
if (IsNote && info->kind == GeneratedSourceInfo::PrettyPrinted) {
// FIXME: This is a note pointing to a synthesized declaration buffer
// for a declaration coming from a module. We should be able to remove
// this check once clients have been updated to deal with the buffer contents
// that we'll include in the response once this check is removed.
//
// For now instead of ignoring it, pick up the declaration name from the
// buffer identifier and append it to the diagnostic message.
auto &LastDiag = getLastDiag();
SKInfo.Description += " (";
SKInfo.Description += SM.getIdentifierForBuffer(*BufferIDOpt);
SKInfo.Description += ")";
SKInfo.Offset = LastDiag.Offset;
SKInfo.Line = LastDiag.Line;
SKInfo.Column = LastDiag.Column;
SKInfo.FileInfo = LastDiag.FileInfo;
LastDiag.Notes.push_back(std::move(SKInfo));
return;
}
}
SKInfo.Offset = SM.getLocOffsetInBuffer(Info.Loc, BufferID);
std::tie(SKInfo.Line, SKInfo.Column) =
SM.getPresumedLineAndColumnForLoc(Info.Loc, BufferID);
auto Filename = SM.getDisplayNameForLoc(Info.Loc);
SKInfo.FileInfo = getBufferInfo(Filename, BufferID, SM);
for (auto R : Info.Ranges) {
if (auto Raw = getRawRangeInBuffer(R, BufferID, SM))
SKInfo.Ranges.push_back(*Raw);
}
for (auto F : Info.FixIts) {
if (auto Range = getRawRangeInBuffer(F.getRange(), BufferID, SM))
SKInfo.Fixits.emplace_back(*Range, F.getText().str());
}
} else {
SKInfo.FileInfo = getBufferInfo("<unknown>", /*BufferID*/ std::nullopt, SM);
}
if (IsNote) {
getLastDiag().Notes.push_back(std::move(SKInfo));
return;
}
switch (Info.Kind) {
case DiagnosticKind::Error:
SKInfo.Severity = DiagnosticSeverityKind::Error;
break;
case DiagnosticKind::Warning:
SKInfo.Severity = DiagnosticSeverityKind::Warning;
break;
case DiagnosticKind::Note:
case DiagnosticKind::Remark:
llvm_unreachable("already covered");
}
if (!BufferIDOpt) {
InvalidLocDiagnostics.push_back(std::move(SKInfo));
clearLastDiag();
return;
}
// Look through to the original buffer, so we produce diagnostics that are
// present in generated buffers that originated there.
unsigned OrigBufferID = *BufferIDOpt;
{
auto BufferInfo = SKInfo.FileInfo;
while (auto &OrigLocation = BufferInfo->OrigLocation) {
OrigBufferID = OrigLocation->OrigBufferID;
BufferInfo = OrigLocation->OrigBufferInfo;
}
}
DiagnosticsTy &Diagnostics = BufferDiagnostics[OrigBufferID];
if (Diagnostics.empty() || Diagnostics.back().Offset <= SKInfo.Offset) {
Diagnostics.push_back(std::move(SKInfo));
LastDiagBufferID = OrigBufferID;
LastDiagIndex = Diagnostics.size() - 1;
return;
}
// Keep the diagnostics array in source order.
auto Pos = std::lower_bound(Diagnostics.begin(), Diagnostics.end(), SKInfo.Offset,
[&](const DiagnosticEntryInfo &LHS, unsigned Offset) -> bool {
return LHS.Offset < Offset;
});
LastDiagBufferID = OrigBufferID;
LastDiagIndex = Pos - Diagnostics.begin();
Diagnostics.insert(Pos, std::move(SKInfo));
}
SwiftEditorDocumentRef
SwiftEditorDocumentFileMap::getByUnresolvedName(StringRef FilePath) {
SwiftEditorDocumentRef EditorDoc;
Queue.dispatchSync([&]{
auto It = Docs.find(FilePath);
if (It != Docs.end())
EditorDoc = It->second.DocRef;
});
return EditorDoc;
}
SwiftEditorDocumentRef
SwiftEditorDocumentFileMap::findByPath(StringRef FilePath, bool IsRealpath) {
SwiftEditorDocumentRef EditorDoc;
std::string Scratch;
if (!IsRealpath) {
Scratch = SwiftLangSupport::resolvePathSymlinks(FilePath);
FilePath = Scratch;
}
Queue.dispatchSync([&]{
for (auto &Entry : Docs) {
if (Entry.getKey() == FilePath ||
Entry.getValue().ResolvedPath == FilePath) {
EditorDoc = Entry.getValue().DocRef;
break;
}
}
});
return EditorDoc;
}
boolSwiftEditorDocumentFileMap::getOrUpdate(
StringRef FilePath, SwiftLangSupport &LangSupport,
SwiftEditorDocumentRef &EditorDoc) {
bool found = false;
std::string ResolvedPath = SwiftLangSupport::resolvePathSymlinks(FilePath);
Queue.dispatchBarrierSync([&]{
DocInfo &Doc = Docs[FilePath];
if (!Doc.DocRef) {
Doc.DocRef = EditorDoc;
Doc.ResolvedPath = ResolvedPath;
} else {
EditorDoc = Doc.DocRef;
found = true;
}
});
return found;
}
SwiftEditorDocumentRef SwiftEditorDocumentFileMap::remove(StringRef FilePath) {
SwiftEditorDocumentRef Removed;
Queue.dispatchBarrierSync([&]{
auto I = Docs.find(FilePath);
if (I != Docs.end()) {
Removed = I->second.DocRef;
Docs.erase(I);
}
});
return Removed;
}
namespace {
/// Merges two overlapping ranges and splits the first range into two
/// ranges before and after the overlapping range.
voidmergeSplitRanges(unsigned Off1, unsigned Len1, unsigned Off2, unsigned Len2,
std::function<void(unsigned BeforeOff, unsigned BeforeLen,
unsigned AfterOff,
unsigned AfterLen)> applier) {
unsigned End1 = Off1 + Len1;
unsigned End2 = Off2 + Len2;
if (End1 > Off2) {
// Overlapping. Split into before and after ranges.
unsigned BeforeOff = Off1;
unsigned BeforeLen = Off2 > Off1 ? Off2 - Off1 : 0;
unsigned AfterOff = End2;
unsigned AfterLen = End1 > End2 ? End1 - End2 : 0;
applier(BeforeOff, BeforeLen, AfterOff, AfterLen);
}
else {
// Not overlapping.
applier(Off1, Len1, 0, 0);
}
}
structSwiftSyntaxToken {
unsigned Offset;
unsigned Length:24;
SyntaxNodeKind Kind:8;
static SwiftSyntaxToken createInvalid() {
return {0, 0, SyntaxNodeKind::AttributeBuiltin};
}
SwiftSyntaxToken(unsigned Offset, unsigned Length, SyntaxNodeKind Kind)
: Offset(Offset), Length(Length), Kind(Kind) {}
unsignedendOffset() const { return Offset + Length; }
boolisInvalid() const { return Length == 0; }
booloperator==(const SwiftSyntaxToken &Other) const {
return Offset == Other.Offset && Length == Other.Length &&
Kind == Other.Kind;
}
booloperator!=(const SwiftSyntaxToken &Other) const {
return Offset != Other.Offset || Length != Other.Length ||
Kind != Other.Kind;
}
};
structSwiftEditorCharRange {
unsigned Offset;
unsigned EndOffset;
SwiftEditorCharRange(unsigned Offset, unsigned EndOffset) :
Offset(Offset), EndOffset(EndOffset) {}
SwiftEditorCharRange(SwiftSyntaxToken Token) :
Offset(Token.Offset), EndOffset(Token.endOffset()) {}
size_tlength() const { return EndOffset - Offset; }
boolisEmpty() const { return Offset == EndOffset; }
boolintersects(const SwiftSyntaxToken &Token) const {
returnthis->Offset < (Token.endOffset()) && this->EndOffset > Token.Offset;
}
voidextendToInclude(const SwiftEditorCharRange &Range) {
if (Range.Offset < Offset)
Offset = Range.Offset;
if (Range.EndOffset > EndOffset)
EndOffset = Range.EndOffset;
}
voidextendToInclude(unsigned OtherOffset) {
extendToInclude({OtherOffset, OtherOffset});
}
};
/// Finds and represents the first mismatching tokens in two syntax maps,
/// ignoring invalidated tokens.
template <classIter>
structTokenMismatch {
/// The begin and end iterators of the previous syntax map
Iter PrevTok, PrevEnd;
/// The begin and end iterators of the current syntax map
Iter CurrTok, CurrEnd;
TokenMismatch(Iter CurrTok, Iter CurrEnd, Iter PrevTok, Iter PrevEnd) :
PrevTok(PrevTok), PrevEnd(PrevEnd), CurrTok(CurrTok), CurrEnd(CurrEnd) {
skipInvalid();
while(advance());
}
/// Returns true if a mismatch was found
boolfoundMismatch() const {
return CurrTok != CurrEnd || PrevTok != PrevEnd;
}
/// Returns the smallest start offset of the mismatched token ranges
unsignedmismatchStart() const {
assert(foundMismatch());
if (CurrTok != CurrEnd) {
if (PrevTok != PrevEnd)
returnstd::min(CurrTok->Offset, PrevTok->Offset);
return CurrTok->Offset;
}
return PrevTok->Offset;
}
/// Returns the largest end offset of the mismatched token ranges
unsignedmismatchEnd() const {
assert(foundMismatch());
if (CurrTok != CurrEnd) {
if (PrevTok != PrevEnd)
returnstd::max(CurrTok->endOffset(), PrevTok->endOffset());
return CurrTok->endOffset();
}
return PrevTok->endOffset();
}
private:
voidskipInvalid() {
while (PrevTok != PrevEnd && PrevTok->isInvalid())
++PrevTok;
}
booladvance() {
if (CurrTok == CurrEnd || PrevTok == PrevEnd || *CurrTok != *PrevTok)
returnfalse;
++CurrTok;
++PrevTok;
skipInvalid();
returntrue;
}
};
/// Represents a the syntax highlighted token ranges in a source file
structSwiftSyntaxMap {
std::vector<SwiftSyntaxToken> Tokens;
explicitSwiftSyntaxMap(unsigned Capacity = 0) {
if (Capacity)
Tokens.reserve(Capacity);
}
voidaddToken(const SwiftSyntaxToken &Token) {
assert(Tokens.empty() || Token.Offset >= Tokens.back().Offset);
Tokens.push_back(Token);
}
/// Merge this nested token into the last token that was added
voidmergeToken(const SwiftSyntaxToken &Token) {
if (Tokens.empty()) {
Tokens.push_back(Token);
return;
}
auto &LastTok = Tokens.back();
assert(LastTok.Offset <= Token.Offset);
mergeSplitRanges(LastTok.Offset, LastTok.Length, Token.Offset, Token.Length,
[&](unsigned BeforeOff, unsigned BeforeLen,
unsigned AfterOff, unsigned AfterLen) {
auto LastKind = LastTok.Kind;
Tokens.pop_back();
if (BeforeLen)
Tokens.emplace_back(BeforeOff, BeforeLen, LastKind);
Tokens.push_back(Token);
if (AfterLen)
Tokens.emplace_back(AfterOff, AfterLen, LastKind);
});
}
/// Adjusts the token offsets and lengths in this syntax map to account for
/// replacing \p Len bytes at the given \p Offset with \p NewLen bytes. Tokens
/// before the replacement stay the same, tokens after it are shifted, and
/// tokens that intersect it are 'removed' (really just marked invalid).
/// Clients are expected to match this behavior.
///
/// Returns the union of the replaced range and the token ranges it
/// intersected, or nothing if no tokens were intersected.
std::optional<SwiftEditorCharRange>
adjustForReplacement(unsigned Offset, unsigned Len, unsigned NewLen) {
unsigned ReplacedStart = Offset;
unsigned ReplacedEnd = Offset + Len;
bool TokenIntersected = false;
SwiftEditorCharRange Affected = { /*Offset=*/ReplacedStart,
/*EndOffset=*/ReplacedEnd};
// Adjust the tokens
auto Token = Tokens.begin();
while (Token != Tokens.end() && Token->endOffset() <= ReplacedStart) {
// Completely before the replaced range – no change needed
++Token;
}
while (Token != Tokens.end() && Token->Offset < ReplacedEnd) {
// Intersecting the replaced range – extend Affected and invalidate
TokenIntersected = true;
Affected.extendToInclude(*Token);
*Token = SwiftSyntaxToken::createInvalid();
++Token;
}
while (Token != Tokens.end()) {
// Completely after the replaced range - shift to account for NewLen
if (NewLen >= Len)
Token->Offset += NewLen - Len;
else
Token->Offset -= Len - NewLen;
++Token;
}
// If the replaced range didn't intersect with any existing tokens, there's
// no need to report an affected range
if (!TokenIntersected)
return std::nullopt;
// Update the end of the affected range to account for NewLen
if (NewLen >= Len) {
Affected.EndOffset += NewLen - Len;
} else {
Affected.EndOffset -= Len - NewLen;
}
return Affected;
}
/// Passes each token in this SwiftSyntaxMap to the given \p Consumer
voidforEach(EditorConsumer &Consumer) {
for (auto &Token: Tokens) {
auto Kind = SwiftLangSupport::getUIDForSyntaxNodeKind(Token.Kind);
Consumer.handleSyntaxMap(Token.Offset, Token.Length, Kind);
}
}
/// Finds the delta between the given SwiftSyntaxMap, \p Prev, and this one.
/// It passes each token not in \p Prev to the given \p Consumer and, if
/// needed, also expands or sets the given \p Affected range to cover all
/// non-matching tokens in the two lists.
///
/// Returns true if this SwiftSyntaxMap is different to \p Prev.
boolforEachChanged(const SwiftSyntaxMap &Prev,
std::optional<SwiftEditorCharRange> &Affected,
EditorConsumer &Consumer) const {
typedef std::vector<SwiftSyntaxToken>::const_iterator ForwardIt;
typedef std::vector<SwiftSyntaxToken>::const_reverse_iterator ReverseIt;
// Find the first pair of tokens that don't match
TokenMismatch<ForwardIt>
Forward(Tokens.begin(), Tokens.end(), Prev.Tokens.begin(), Prev.Tokens.end());
// Exit early if there was no mismatch
if (!Forward.foundMismatch())
return Affected && !Affected->isEmpty();
// Find the last pair of tokens that don't match
TokenMismatch<ReverseIt>
Backward(Tokens.rbegin(), Tokens.rend(), Prev.Tokens.rbegin(), Prev.Tokens.rend());
assert(Backward.foundMismatch());
// Set or extend the affected range to include the mismatched range
SwiftEditorCharRange
MismatchRange = {Forward.mismatchStart(),Backward.mismatchEnd()};
if (!Affected) {
Affected = MismatchRange;
} else {
Affected->extendToInclude(MismatchRange);
}
// Report all tokens in the affected range to the EditorConsumer
auto From = Forward.CurrTok;
auto To = Backward.CurrTok;
while (From != Tokens.begin() && (From-1)->Offset >= Affected->Offset)
--From;
while (To != Tokens.rbegin() && (To-1)->endOffset() <= Affected->EndOffset)
--To;
for (; From < To.base(); ++From) {
auto Kind = SwiftLangSupport::getUIDForSyntaxNodeKind(From->Kind);
Consumer.handleSyntaxMap(From->Offset, From->Length, Kind);
}
returntrue;
}
};
structEditorConsumerSyntaxMapEntry {
unsigned Offset;
unsigned Length;
UIdent Kind;
EditorConsumerSyntaxMapEntry(unsigned Offset, unsigned Length, UIdent Kind)
:Offset(Offset), Length(Length), Kind(Kind) { }
};
classSwiftDocumentSemanticInfo :
public ThreadSafeRefCountedBase<SwiftDocumentSemanticInfo> {
const std::string Filename;
std::weak_ptr<SwiftASTManager> ASTMgr;
std::shared_ptr<NotificationCenter> NotificationCtr;
ThreadSafeRefCntPtr<SwiftInvocation> InvokRef;
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem;
std::string CompilerArgsError;
uint64_t ASTGeneration = 0;
ImmutableTextSnapshotRef TokSnapshot;
std::vector<SwiftSemanticToken> SemaToks;
ImmutableTextSnapshotRef DiagSnapshot;
std::vector<DiagnosticEntryInfo> SemaDiags;
mutable llvm::sys::Mutex Mtx;
public:
SwiftDocumentSemanticInfo(
StringRef Filename, std::weak_ptr<SwiftASTManager> ASTMgr,
std::shared_ptr<NotificationCenter> NotificationCtr,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem)
: Filename(Filename), ASTMgr(ASTMgr), NotificationCtr(NotificationCtr),
fileSystem(fileSystem) {}
SwiftInvocationRef getInvocation() const {
return InvokRef;
}
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> getFileSystem() const {
return fileSystem;
}
uint64_tgetASTGeneration() const;
voidsetCompilerArgs(ArrayRef<constchar *> Args) {
if (auto ASTMgr = this->ASTMgr.lock()) {
InvokRef =
ASTMgr->getTypecheckInvocation(Args, Filename, CompilerArgsError);
}
}
voidreadSemanticInfo(ImmutableTextSnapshotRef NewSnapshot,
std::vector<SwiftSemanticToken> &Tokens,
std::optional<std::vector<DiagnosticEntryInfo>> &Diags,
ArrayRef<DiagnosticEntryInfo> ParserDiags);
voidprocessLatestSnapshotAsync(EditableTextBufferRef EditableBuffer,
SourceKitCancellationToken CancellationToken);
voidupdateSemanticInfo(std::vector<SwiftSemanticToken> Toks,
std::vector<DiagnosticEntryInfo> Diags,
ImmutableTextSnapshotRef Snapshot,
uint64_t ASTGeneration);
voidremoveCachedAST() {
if (InvokRef) {
if (auto ASTMgr = this->ASTMgr.lock()) {
ASTMgr->removeCachedAST(InvokRef);
}
}
}
voidcancelBuildsForCachedAST() {
if (!InvokRef)
return;
if (auto ASTMgr = this->ASTMgr.lock())
ASTMgr->cancelBuildsForCachedAST(InvokRef);
}
private:
std::vector<SwiftSemanticToken> takeSemanticTokens(
ImmutableTextSnapshotRef NewSnapshot);
std::optional<std::vector<DiagnosticEntryInfo>>
getSemanticDiagnostics(ImmutableTextSnapshotRef NewSnapshot,
ArrayRef<DiagnosticEntryInfo> ParserDiags);
};
classSwiftDocumentSyntaxInfo {
SourceManager SM;
EditorDiagConsumer DiagConsumer;
std::unique_ptr<ParserUnit> Parser;
unsigned BufferID;
std::vector<std::string> Args;
std::string PrimaryFile;
bool IsParsed;
public:
SwiftDocumentSyntaxInfo(const CompilerInvocation &CompInv,
ImmutableTextSnapshotRef Snapshot,
std::vector<std::string> &Args,
StringRef FilePath)
: Args(Args), PrimaryFile(FilePath) {
std::unique_ptr<llvm::MemoryBuffer> BufCopy =
llvm::MemoryBuffer::getMemBufferCopy(
Snapshot->getBuffer()->getText(), FilePath);
BufferID = SM.addNewSourceBuffer(std::move(BufCopy));
Parser.reset(newParserUnit(SM, SourceFileKind::Main, BufferID,
CompInv.getLangOptions(),
CompInv.getModuleName()));
registerTypeCheckerRequestFunctions(
Parser->getParser().Context.evaluator);
registerClangImporterRequestFunctions(Parser->getParser().Context.evaluator);
Parser->getDiagnosticEngine().addConsumer(DiagConsumer);
IsParsed = false;
}
voidparseIfNeeded() {
if (!IsParsed) {
// Perform parsing on a deep stack that's the same size as the main thread
// during normal compilation to avoid stack overflows.
static WorkQueue BigStackQueue{
WorkQueue::Dequeuing::Concurrent,
"SwiftDocumentSyntaxInfo::parseIfNeeded.BigStackQueue"};
BigStackQueue.dispatchSync(
[this]() {
Parser->parse();
IsParsed = true;
},
/*isStackDeep=*/true);
}
}
SourceFile &getSourceFile() {
return Parser->getSourceFile();
}
unsignedgetBufferID() {
return BufferID;
}
const LangOptions &getLangOptions() {
return Parser->getLangOptions();
}
SourceManager &getSourceManager() {
return SM;
}
ArrayRef<DiagnosticEntryInfo> getDiagnostics() {
return DiagConsumer.getDiagnosticsForBuffer(BufferID);
}
};
} // anonymous namespace
uint64_tSwiftDocumentSemanticInfo::getASTGeneration() const {
llvm::sys::ScopedLock L(Mtx);
return ASTGeneration;
}
voidSwiftDocumentSemanticInfo::readSemanticInfo(
ImmutableTextSnapshotRef NewSnapshot,
std::vector<SwiftSemanticToken> &Tokens,
std::optional<std::vector<DiagnosticEntryInfo>> &Diags,
ArrayRef<DiagnosticEntryInfo> ParserDiags) {
llvm::sys::ScopedLock L(Mtx);
Tokens = takeSemanticTokens(NewSnapshot);
Diags = getSemanticDiagnostics(NewSnapshot, ParserDiags);
}
std::vector<SwiftSemanticToken>
SwiftDocumentSemanticInfo::takeSemanticTokens(
ImmutableTextSnapshotRef NewSnapshot) {
llvm::sys::ScopedLock L(Mtx);
if (SemaToks.empty())
return {};
// Adjust the position of the tokens.
TokSnapshot->foreachReplaceUntil(NewSnapshot,
[&](ReplaceImmutableTextUpdateRef Upd) -> bool {
if (SemaToks.empty())
returnfalse;
auto ReplaceBegin = std::lower_bound(SemaToks.begin(), SemaToks.end(),
Upd->getByteOffset(),
[&](const SwiftSemanticToken &Tok, unsigned StartOffset) -> bool {
return Tok.ByteOffset+Tok.Length < StartOffset;
});
std::vector<SwiftSemanticToken>::iterator ReplaceEnd;
if (ReplaceBegin == SemaToks.end()) {
ReplaceEnd = ReplaceBegin;
} elseif (Upd->getLength() == 0) {
ReplaceEnd = ReplaceBegin + 1;
} else {
ReplaceEnd = std::upper_bound(ReplaceBegin, SemaToks.end(),
Upd->getByteOffset() + Upd->getLength(),
[&](unsigned EndOffset, const SwiftSemanticToken &Tok) -> bool {
return EndOffset < Tok.ByteOffset;
});
}
unsigned InsertLen = Upd->getText().size();
int Delta = InsertLen - Upd->getLength();
if (Delta != 0) {
for (std::vector<SwiftSemanticToken>::iterator
I = ReplaceEnd, E = SemaToks.end(); I != E; ++I)
I->ByteOffset += Delta;
}
SemaToks.erase(ReplaceBegin, ReplaceEnd);
returntrue;
});
returnstd::move(SemaToks);
}
std::optional<std::vector<DiagnosticEntryInfo>>
SwiftDocumentSemanticInfo::getSemanticDiagnostics(
ImmutableTextSnapshotRef NewSnapshot,
ArrayRef<DiagnosticEntryInfo> ParserDiags) {
std::vector<DiagnosticEntryInfo> curSemaDiags;
{
llvm::sys::ScopedLock L(Mtx);
if (!DiagSnapshot || DiagSnapshot->getStamp() != NewSnapshot->getStamp()) {
// The semantic diagnostics are out-of-date, ignore them.
return std::nullopt;
}
curSemaDiags = SemaDiags;
}
// Diagnostics from the AST and diagnostics from the parser are based on the
// same source text snapshot. But diagnostics from the AST may have excluded
// the parser diagnostics due to a fatal error, e.g. if the source has a
// 'so such module' error, which will suppress other diagnostics.
// We don't want to turn off the suppression to avoid a flood of diagnostics
// when a module import fails, but we also don't want to lose the parser
// diagnostics in such a case, so merge the parser diagnostics with the sema
// ones.
auto orderDiagnosticEntryInfos = [](const DiagnosticEntryInfo &LHS,
const DiagnosticEntryInfo &RHS) -> bool {
if (LHS.FileInfo->BufferName != RHS.FileInfo->BufferName)
return LHS.FileInfo->BufferName < RHS.FileInfo->BufferName;
if (LHS.Offset != RHS.Offset)
return LHS.Offset < RHS.Offset;
return LHS.Description < RHS.Description;
};
std::vector<DiagnosticEntryInfo> sortedParserDiags;
sortedParserDiags.reserve(ParserDiags.size());
sortedParserDiags.insert(sortedParserDiags.end(), ParserDiags.begin(),
ParserDiags.end());
std::stable_sort(sortedParserDiags.begin(), sortedParserDiags.end(),
orderDiagnosticEntryInfos);
std::vector<DiagnosticEntryInfo> finalDiags;
finalDiags.reserve(sortedParserDiags.size()+curSemaDiags.size());
// Add sema diagnostics unless it is an existing parser diagnostic.
// Note that we want to merge and eliminate diagnostics from the 'sema' set
// that also show up in the 'parser' set, but we don't want to remove
// duplicate diagnostics from within the same set (e.g. duplicates existing in
// the 'sema' set). We want to report the diagnostics as the compiler reported
// them, even if there's some duplicate one. This is why we don't just do a
// simple append/sort/keep-uniques step.
for (constauto &curDE : curSemaDiags) {
bool existsAsParserDiag = std::binary_search(sortedParserDiags.begin(),
sortedParserDiags.end(),
curDE, orderDiagnosticEntryInfos);
if (!existsAsParserDiag) {
finalDiags.push_back(curDE);
}
}
finalDiags.insert(finalDiags.end(),
sortedParserDiags.begin(), sortedParserDiags.end());
std::stable_sort(finalDiags.begin(), finalDiags.end(),
orderDiagnosticEntryInfos);
return finalDiags;
}
voidSwiftDocumentSemanticInfo::updateSemanticInfo(
std::vector<SwiftSemanticToken> Toks,
std::vector<DiagnosticEntryInfo> Diags,
ImmutableTextSnapshotRef Snapshot,
uint64_t ASTGeneration) {
{
llvm::sys::ScopedLock L(Mtx);
if (ASTGeneration > this->ASTGeneration) {
SemaToks = std::move(Toks);
SemaDiags = std::move(Diags);
TokSnapshot = DiagSnapshot = std::move(Snapshot);
this->ASTGeneration = ASTGeneration;
}
}
LOG_INFO_FUNC(High, "posted document update notification for: " << Filename);
NotificationCtr->postDocumentUpdateNotification(Filename);
}
namespace {
classSemanticAnnotator : publicSourceEntityWalker {
SourceManager &SM;
unsigned BufferID;
public:
std::vector<SwiftSemanticToken> SemaToks;
bool IsWalkingMacroExpansionBuffer = false;
SemanticAnnotator(SourceManager &SM, unsigned BufferID)
: SM(SM), BufferID(BufferID) {
if (auto GeneratedSourceInfo = SM.getGeneratedSourceInfo(BufferID)) {
switch (GeneratedSourceInfo->kind) {
#defineMACRO_ROLE(Name, Description) \
case GeneratedSourceInfo::Name##MacroExpansion:
#include"swift/Basic/MacroRoles.def"
IsWalkingMacroExpansionBuffer = true;
break;
case GeneratedSourceInfo::DefaultArgument:
case GeneratedSourceInfo::ReplacedFunctionBody:
case GeneratedSourceInfo::PrettyPrinted:
case GeneratedSourceInfo::AttributeFromClang:
break;
}
}
}
MacroWalking getMacroWalkingBehavior() constoverride {
if (IsWalkingMacroExpansionBuffer) {
// When we are walking a macro expansion buffer, we need to set the macro
// walking behavior to walk the expansion, otherwise we skip over all the
// declarations in the buffer.
return MacroWalking::ArgumentsAndExpansion;
} else {
returnSourceEntityWalker::getMacroWalkingBehavior();
}
}
boolvisitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef, Type T,
ReferenceMetaData Data) override {
if (Data.isImplicit)
returntrue;
if (isa<VarDecl>(D) && D->hasName() &&
D->getName() == D->getASTContext().Id_self)