18
18
#include " swift/Basic/Debug.h"
19
19
#include " swift/Basic/LLVM.h"
20
20
#include " swift/Basic/OptionSet.h"
21
+ #include " swift/Basic/StringExtras.h"
21
22
#include " swift/Frontend/Frontend.h"
22
23
#include " llvm/ADT/ArrayRef.h"
23
24
#include " llvm/ADT/StringMap.h"
@@ -64,12 +65,6 @@ std::string removeCodeCompletionTokens(StringRef Input,
64
65
StringRef TokenName,
65
66
unsigned *CompletionOffset);
66
67
67
- StringRef copyString (llvm::BumpPtrAllocator &Allocator,
68
- StringRef Str);
69
-
70
- const char *copyCString (llvm::BumpPtrAllocator &Allocator,
71
- StringRef Str);
72
-
73
68
template <typename T>
74
69
ArrayRef<T> copyArray (llvm::BumpPtrAllocator &Allocator,
75
70
ArrayRef<T> Arr) {
@@ -723,9 +718,9 @@ class ContextFreeCodeCompletionResult {
723
718
724
719
bool IsSystem : 1 ;
725
720
CodeCompletionString *CompletionString;
726
- StringRef ModuleName;
727
- StringRef BriefDocComment;
728
- ArrayRef<StringRef > AssociatedUSRs;
721
+ NullTerminatedStringRef ModuleName;
722
+ NullTerminatedStringRef BriefDocComment;
723
+ ArrayRef<NullTerminatedStringRef > AssociatedUSRs;
729
724
CodeCompletionResultType ResultType;
730
725
731
726
ContextFreeNotRecommendedReason NotRecommended : 3 ;
@@ -734,7 +729,8 @@ class ContextFreeCodeCompletionResult {
734
729
CodeCompletionDiagnosticSeverity DiagnosticSeverity : 3 ;
735
730
static_assert (int (CodeCompletionDiagnosticSeverity::MAX_VALUE) < 1 << 3 , " " );
736
731
737
- StringRef DiagnosticMessage;
732
+ NullTerminatedStringRef DiagnosticMessage;
733
+ NullTerminatedStringRef FilterName;
738
734
739
735
public:
740
736
// / Memberwise initializer. \p AssociatedKInd is opaque and will be
@@ -748,18 +744,21 @@ class ContextFreeCodeCompletionResult {
748
744
ContextFreeCodeCompletionResult (
749
745
CodeCompletionResultKind Kind, uint8_t AssociatedKind,
750
746
CodeCompletionOperatorKind KnownOperatorKind, bool IsSystem,
751
- CodeCompletionString *CompletionString, StringRef ModuleName,
752
- StringRef BriefDocComment, ArrayRef<StringRef> AssociatedUSRs,
747
+ CodeCompletionString *CompletionString,
748
+ NullTerminatedStringRef ModuleName,
749
+ NullTerminatedStringRef BriefDocComment,
750
+ ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
753
751
CodeCompletionResultType ResultType,
754
752
ContextFreeNotRecommendedReason NotRecommended,
755
753
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
756
- StringRef DiagnosticMessage)
754
+ NullTerminatedStringRef DiagnosticMessage,
755
+ NullTerminatedStringRef FilterName)
757
756
: Kind(Kind), KnownOperatorKind(KnownOperatorKind), IsSystem(IsSystem),
758
757
CompletionString (CompletionString), ModuleName(ModuleName),
759
758
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
760
759
ResultType(ResultType), NotRecommended(NotRecommended),
761
760
DiagnosticSeverity(DiagnosticSeverity),
762
- DiagnosticMessage(DiagnosticMessage) {
761
+ DiagnosticMessage(DiagnosticMessage), FilterName(FilterName) {
763
762
this ->AssociatedKind .Opaque = AssociatedKind;
764
763
assert ((NotRecommended == ContextFreeNotRecommendedReason::None) ==
765
764
(DiagnosticSeverity == CodeCompletionDiagnosticSeverity::None) &&
@@ -777,78 +776,58 @@ class ContextFreeCodeCompletionResult {
777
776
" isOperator implies operator kind != None" );
778
777
}
779
778
780
- // / Constructs a \c Pattern, \c Keyword or \c BuiltinOperator result.
779
+ // / Constructs a \c Pattern or \c BuiltinOperator result.
781
780
// /
782
781
// / \note The caller must ensure that the \p CompletionString and \c
783
782
// / StringRefs outlive this result, typically by storing them in the same
784
783
// / \c CodeCompletionResultSink as the result itself.
785
- ContextFreeCodeCompletionResult (
786
- CodeCompletionResultKind Kind, CodeCompletionString *CompletionString,
787
- CodeCompletionOperatorKind KnownOperatorKind, StringRef BriefDocComment,
784
+ static ContextFreeCodeCompletionResult *createPatternOrBuiltInOperatorResult (
785
+ llvm::BumpPtrAllocator &Allocator, CodeCompletionResultKind Kind,
786
+ CodeCompletionString *CompletionString,
787
+ CodeCompletionOperatorKind KnownOperatorKind,
788
+ NullTerminatedStringRef BriefDocComment,
788
789
CodeCompletionResultType ResultType,
789
790
ContextFreeNotRecommendedReason NotRecommended,
790
791
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
791
- StringRef DiagnosticMessage)
792
- : ContextFreeCodeCompletionResult(
793
- Kind, /* AssociatedKind=*/ 0 , KnownOperatorKind,
794
- /* IsSystem=*/ false , CompletionString, /* ModuleName=*/ " " ,
795
- BriefDocComment, /* AssociatedUSRs=*/ {}, ResultType, NotRecommended,
796
- DiagnosticSeverity, DiagnosticMessage) {}
792
+ NullTerminatedStringRef DiagnosticMessage);
797
793
798
794
// / Constructs a \c Keyword result.
799
795
// /
800
796
// / \note The caller must ensure that the \p CompletionString and
801
- // / \p BriefDocComment outlive this result, typically by storing them in the
802
- // / same \c CodeCompletionResultSink as the result itself.
803
- ContextFreeCodeCompletionResult (CodeCompletionKeywordKind Kind,
804
- CodeCompletionString *CompletionString,
805
- StringRef BriefDocComment,
806
- CodeCompletionResultType ResultType)
807
- : ContextFreeCodeCompletionResult(
808
- CodeCompletionResultKind::Keyword, static_cast <uint8_t >(Kind),
809
- CodeCompletionOperatorKind::None, /* IsSystem=*/ false,
810
- CompletionString, /* ModuleName=*/ "", BriefDocComment,
811
- /* AssociatedUSRs=*/ {}, ResultType,
812
- ContextFreeNotRecommendedReason::None,
813
- CodeCompletionDiagnosticSeverity::None, /* DiagnosticMessage=*/ " " ) {}
797
+ // / \p BriefDocComment outlive this result, typically by storing them in
798
+ // / the same \c CodeCompletionResultSink as the result itself.
799
+ static ContextFreeCodeCompletionResult *
800
+ createKeywordResult (llvm::BumpPtrAllocator &Allocator,
801
+ CodeCompletionKeywordKind Kind,
802
+ CodeCompletionString *CompletionString,
803
+ NullTerminatedStringRef BriefDocComment,
804
+ CodeCompletionResultType ResultType);
814
805
815
806
// / Constructs a \c Literal result.
816
807
// /
817
808
// / \note The caller must ensure that the \p CompletionString outlives this
818
809
// / result, typically by storing them in the same \c CodeCompletionResultSink
819
810
// / as the result itself.
820
- ContextFreeCodeCompletionResult (CodeCompletionLiteralKind LiteralKind,
821
- CodeCompletionString *CompletionString,
822
- CodeCompletionResultType ResultType)
823
- : ContextFreeCodeCompletionResult(
824
- CodeCompletionResultKind::Literal,
825
- static_cast <uint8_t >(LiteralKind), CodeCompletionOperatorKind::None,
826
- /* IsSystem=*/ false , CompletionString, /* ModuleName=*/ " " ,
827
- /* BriefDocComment=*/ " " ,
828
- /* AssociatedUSRs=*/ {}, ResultType,
829
- ContextFreeNotRecommendedReason::None,
830
- CodeCompletionDiagnosticSeverity::None, /* DiagnosticMessage=*/ " " ) {}
811
+ static ContextFreeCodeCompletionResult *
812
+ createLiteralResult (llvm::BumpPtrAllocator &Allocator,
813
+ CodeCompletionLiteralKind LiteralKind,
814
+ CodeCompletionString *CompletionString,
815
+ CodeCompletionResultType ResultType);
831
816
832
817
// / Constructs a \c Declaration result.
833
818
// /
834
819
// / \note The caller must ensure that the \p CompletionString and all
835
820
// / \c StringRefs outlive this result, typically by storing them in the same
836
821
// / \c CodeCompletionResultSink as the result itself.
837
- ContextFreeCodeCompletionResult (
838
- CodeCompletionString *CompletionString, const Decl *AssociatedDecl,
839
- StringRef ModuleName, StringRef BriefDocComment,
840
- ArrayRef<StringRef> AssociatedUSRs, CodeCompletionResultType ResultType,
822
+ static ContextFreeCodeCompletionResult *createDeclResult (
823
+ llvm::BumpPtrAllocator &Allocator, CodeCompletionString *CompletionString,
824
+ const Decl *AssociatedDecl, NullTerminatedStringRef ModuleName,
825
+ NullTerminatedStringRef BriefDocComment,
826
+ ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
827
+ CodeCompletionResultType ResultType,
841
828
ContextFreeNotRecommendedReason NotRecommended,
842
829
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
843
- StringRef DiagnosticMessage)
844
- : ContextFreeCodeCompletionResult(
845
- CodeCompletionResultKind::Declaration,
846
- static_cast <uint8_t >(getCodeCompletionDeclKind(AssociatedDecl)),
847
- CodeCompletionOperatorKind::None, getDeclIsSystem(AssociatedDecl),
848
- CompletionString, ModuleName, BriefDocComment, AssociatedUSRs,
849
- ResultType, NotRecommended, DiagnosticSeverity, DiagnosticMessage) {
850
- assert (AssociatedDecl && " should have a decl" );
851
- }
830
+ NullTerminatedStringRef DiagnosticMessage);
852
831
853
832
CodeCompletionResultKind getKind () const { return Kind; }
854
833
@@ -878,11 +857,13 @@ class ContextFreeCodeCompletionResult {
878
857
879
858
CodeCompletionString *getCompletionString () const { return CompletionString; }
880
859
881
- StringRef getModuleName () const { return ModuleName; }
860
+ NullTerminatedStringRef getModuleName () const { return ModuleName; }
882
861
883
- StringRef getBriefDocComment () const { return BriefDocComment; }
862
+ NullTerminatedStringRef getBriefDocComment () const { return BriefDocComment; }
884
863
885
- ArrayRef<StringRef> getAssociatedUSRs () const { return AssociatedUSRs; }
864
+ ArrayRef<NullTerminatedStringRef> getAssociatedUSRs () const {
865
+ return AssociatedUSRs;
866
+ }
886
867
887
868
const CodeCompletionResultType &getResultType () const { return ResultType; }
888
869
@@ -893,7 +874,11 @@ class ContextFreeCodeCompletionResult {
893
874
CodeCompletionDiagnosticSeverity getDiagnosticSeverity () const {
894
875
return DiagnosticSeverity;
895
876
}
896
- StringRef getDiagnosticMessage () const { return DiagnosticMessage; };
877
+ NullTerminatedStringRef getDiagnosticMessage () const {
878
+ return DiagnosticMessage;
879
+ }
880
+
881
+ NullTerminatedStringRef getFilterName () const { return FilterName; }
897
882
898
883
bool isOperator () const {
899
884
if (getKind () == CodeCompletionResultKind::Declaration) {
@@ -935,7 +920,7 @@ class CodeCompletionResult {
935
920
CodeCompletionDiagnosticSeverity DiagnosticSeverity : 3 ;
936
921
static_assert (int (CodeCompletionDiagnosticSeverity::MAX_VALUE) < 1 << 3 , " " );
937
922
938
- StringRef DiagnosticMessage;
923
+ NullTerminatedStringRef DiagnosticMessage;
939
924
940
925
// / The number of bytes to the left of the code completion point that
941
926
// / should be erased first if this completion string is inserted in the
@@ -959,7 +944,7 @@ class CodeCompletionResult {
959
944
CodeCompletionResultTypeRelation TypeDistance,
960
945
ContextualNotRecommendedReason NotRecommended,
961
946
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
962
- StringRef DiagnosticMessage)
947
+ NullTerminatedStringRef DiagnosticMessage)
963
948
: ContextFree(ContextFree), SemanticContext(SemanticContext),
964
949
Flair (Flair.toRaw()), NotRecommended(NotRecommended),
965
950
DiagnosticSeverity(DiagnosticSeverity),
@@ -981,7 +966,7 @@ class CodeCompletionResult {
981
966
const DeclContext *DC,
982
967
ContextualNotRecommendedReason NotRecommended,
983
968
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
984
- StringRef DiagnosticMessage);
969
+ NullTerminatedStringRef DiagnosticMessage);
985
970
986
971
const ContextFreeCodeCompletionResult &getContextFreeResult () const {
987
972
return ContextFree;
@@ -1090,15 +1075,15 @@ class CodeCompletionResult {
1090
1075
return getContextFreeResult ().getCompletionString ();
1091
1076
}
1092
1077
1093
- StringRef getModuleName () const {
1078
+ NullTerminatedStringRef getModuleName () const {
1094
1079
return getContextFreeResult ().getModuleName ();
1095
1080
}
1096
1081
1097
- StringRef getBriefDocComment () const {
1082
+ NullTerminatedStringRef getBriefDocComment () const {
1098
1083
return getContextFreeResult ().getBriefDocComment ();
1099
1084
}
1100
1085
1101
- ArrayRef<StringRef > getAssociatedUSRs () const {
1086
+ ArrayRef<NullTerminatedStringRef > getAssociatedUSRs () const {
1102
1087
return getContextFreeResult ().getAssociatedUSRs ();
1103
1088
}
1104
1089
@@ -1110,7 +1095,9 @@ class CodeCompletionResult {
1110
1095
1111
1096
// / Get the contextual diagnostic message. This disregards context-free
1112
1097
// / diagnostics.
1113
- StringRef getContextualDiagnosticMessage () const { return DiagnosticMessage; }
1098
+ NullTerminatedStringRef getContextualDiagnosticMessage () const {
1099
+ return DiagnosticMessage;
1100
+ }
1114
1101
1115
1102
// / Return the contextual diagnostic severity if there was a contextual
1116
1103
// / diagnostic. If there is no contextual diagnostic, return the context-free
@@ -1126,14 +1113,18 @@ class CodeCompletionResult {
1126
1113
// / Return the contextual diagnostic message if there was a contextual
1127
1114
// / diagnostic. If there is no contextual diagnostic, return the context-free
1128
1115
// / diagnostic message.
1129
- StringRef getDiagnosticMessage () const {
1116
+ NullTerminatedStringRef getDiagnosticMessage () const {
1130
1117
if (NotRecommended != ContextualNotRecommendedReason::None) {
1131
1118
return DiagnosticMessage;
1132
1119
} else {
1133
1120
return getContextFreeResult ().getDiagnosticMessage ();
1134
1121
}
1135
1122
}
1136
1123
1124
+ NullTerminatedStringRef getFilterName () const {
1125
+ return getContextFreeResult ().getFilterName ();
1126
+ }
1127
+
1137
1128
// / Print a debug representation of the code completion result to \p OS.
1138
1129
void printPrefix (raw_ostream &OS) const ;
1139
1130
SWIFT_DEBUG_DUMP;
@@ -1169,7 +1160,7 @@ struct CodeCompletionResultSink {
1169
1160
1170
1161
// / A single-element cache for module names stored in Allocator, keyed by a
1171
1162
// / clang::Module * or swift::ModuleDecl *.
1172
- std::pair<void *, StringRef > LastModule;
1163
+ std::pair<void *, NullTerminatedStringRef > LastModule;
1173
1164
1174
1165
CodeCompletionResultSink ()
1175
1166
: Allocator(std::make_shared<llvm::BumpPtrAllocator>()) {}
0 commit comments