16
16
#include " swift/AST/SourceEntityWalker.h"
17
17
#include " swift/AST/USRGeneration.h"
18
18
#include " swift/Basic/SourceManager.h"
19
+ #include " swift/Basic/StringExtras.h"
19
20
#include " llvm/ADT/APInt.h"
20
21
#include " llvm/Support/FileSystem.h"
21
22
22
23
using namespace swift ;
23
24
using namespace swift ::index;
24
25
26
+ static bool printDisplayName (const swift::ValueDecl *D, llvm::raw_ostream &OS) {
27
+ if (!D->hasName ())
28
+ return true ;
29
+
30
+ OS << D->getFullName ();
31
+ return false ;
32
+ }
33
+
25
34
namespace {
26
35
// Adapter providing a common interface for a SourceFile/Module.
27
36
class SourceFileOrModule {
@@ -83,6 +92,53 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
83
92
SmallVector<Expr *, 8 > ExprStack;
84
93
bool Cancelled = false ;
85
94
95
+ struct NameAndUSR {
96
+ StringRef USR;
97
+ StringRef name;
98
+ };
99
+ typedef llvm::PointerIntPair<Decl *, 3 > DeclAccessorPair;
100
+ llvm::DenseMap<Decl *, NameAndUSR> nameAndUSRCache;
101
+ llvm::DenseMap<DeclAccessorPair, StringRef> accessorUSRCache;
102
+ StringScratchSpace stringStorage;
103
+
104
+ bool getNameAndUSR (ValueDecl *D, StringRef &name, StringRef &USR) {
105
+ auto &result = nameAndUSRCache[D];
106
+ if (result.USR .empty ()) {
107
+ SmallString<128 > storage;
108
+ {
109
+ llvm::raw_svector_ostream OS (storage);
110
+ if (ide::printDeclUSR (D, OS))
111
+ return true ;
112
+ result.USR = stringStorage.copyString (OS.str ());
113
+ }
114
+
115
+ storage.clear ();
116
+ {
117
+ llvm::raw_svector_ostream OS (storage);
118
+ printDisplayName (D, OS);
119
+ result.name = stringStorage.copyString (OS.str ());
120
+ }
121
+ }
122
+
123
+ name = result.name ;
124
+ USR = result.USR ;
125
+ return false ;
126
+ }
127
+
128
+ StringRef getAccessorUSR (AbstractStorageDecl *D, AccessorKind AK) {
129
+ assert (AK != AccessorKind::NotAccessor);
130
+ assert (static_cast <int >(AK) < 0x111 && " AccessorKind too big for pair" );
131
+ DeclAccessorPair key (D, static_cast <int >(AK));
132
+ auto &result = accessorUSRCache[key];
133
+ if (result.empty ()) {
134
+ SmallString<128 > storage;
135
+ llvm::raw_svector_ostream OS (storage);
136
+ ide::printAccessorUSR (D, AK, OS);
137
+ result = stringStorage.copyString (OS.str ());
138
+ }
139
+ return result;
140
+ }
141
+
86
142
public:
87
143
IndexSwiftASTWalker (IndexDataConsumer &IdxConsumer, ASTContext &Ctx,
88
144
unsigned BufferID)
@@ -522,11 +578,9 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
522
578
auto handleInfo = [this , D, AccKind](IndexSymbol &Info) {
523
579
Info.kind = SymbolKind::Accessor;
524
580
Info.subKind = getSubKindForAccessor (AccKind);
525
- Info.name .clear ();
526
- Info.USR .clear ();
527
- Info.group .clear ();
528
- llvm::raw_svector_ostream OS (Info.USR );
529
- ide::printAccessorUSR (D, AccKind, OS);
581
+ Info.name = " " ;
582
+ Info.USR = getAccessorUSR (D, AccKind);
583
+ Info.group = " " ;
530
584
531
585
if (!IdxConsumer.startSourceEntity (Info)) {
532
586
Cancelled = true ;
@@ -708,14 +762,6 @@ bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc) {
708
762
return !Cancelled;
709
763
}
710
764
711
- static bool printDisplayName (const swift::ValueDecl *D, llvm::raw_ostream &OS) {
712
- if (!D->hasName ())
713
- return true ;
714
-
715
- OS << D->getFullName ();
716
- return false ;
717
- }
718
-
719
765
bool IndexSwiftASTWalker::initIndexSymbol (ValueDecl *D, SourceLoc Loc,
720
766
bool IsRef, IndexSymbol &Info) {
721
767
assert (D);
@@ -729,18 +775,12 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
729
775
730
776
Info.isRef = IsRef;
731
777
732
- Info.name .clear ();
733
- llvm::raw_svector_ostream NameOS (Info.name );
734
- printDisplayName (D, NameOS);
735
-
736
- llvm::raw_svector_ostream OS (Info.USR );
737
- if (ide::printDeclUSR (D, OS))
778
+ if (getNameAndUSR (D, Info.name , Info.USR ))
738
779
return true ;
780
+
739
781
std::tie (Info.line , Info.column ) = getLineCol (Loc);
740
- if (auto Group = D->getGroupName ()) {
741
- Info.group .clear ();
742
- Info.group .append (Group.getValue ());
743
- }
782
+ if (auto Group = D->getGroupName ())
783
+ Info.group = Group.getValue ();
744
784
return false ;
745
785
}
746
786
@@ -784,10 +824,8 @@ bool IndexSwiftASTWalker::initFuncDeclIndexSymbol(ValueDecl *D,
784
824
return true ;
785
825
786
826
Info.IsTestCandidate = isTestCandidate (D);
787
- if (auto Group = D->getGroupName ()) {
788
- Info.group .clear ();
789
- Info.group .append (Group.getValue ());
790
- }
827
+ if (auto Group = D->getGroupName ())
828
+ Info.group = Group.getValue ();
791
829
return false ;
792
830
}
793
831
@@ -848,9 +886,9 @@ bool IndexSwiftASTWalker::initCallRefIndexSymbol(Expr *CurrentE, Expr *ParentE,
848
886
ReceiverTy = MetaT->getInstanceType ();
849
887
850
888
if (auto TyD = ReceiverTy->getAnyNominal ()) {
851
- llvm::raw_svector_ostream OS (Info. ReceiverUSR ) ;
852
- ide::printDeclUSR ( TyD, OS);
853
-
889
+ StringRef unused ;
890
+ if ( getNameAndUSR ( TyD, unused, Info. ReceiverUSR ))
891
+ return true ;
854
892
Info.IsDynamic = isDynamicCall (BaseE, D);
855
893
}
856
894
}
0 commit comments