Skip to content

Commit b13a7f5

Browse files
author
Simon Barinka
committed
[SourceKit] Add option to index local symbols to IndexSource request
Adds optional `key.should_index_locals` flag to the SourceKit `source.request.indexsource` request. If true, the response includes symbol info for both locals and globals.
1 parent 43b6117 commit b13a7f5

File tree

7 files changed

+44
-14
lines changed

7 files changed

+44
-14
lines changed

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ struct RenameLocations {
878878
std::vector<RenameLocation> LineColumnLocs;
879879
};
880880

881+
struct IndexSourceOptions {
882+
bool IndexLocals = false;
883+
};
884+
881885
struct IndexStoreOptions {
882886
std::string IndexStorePath;
883887
std::string IndexUnitOutputPath;
@@ -998,7 +1002,8 @@ class LangSupport {
9981002

9991003
virtual void indexSource(StringRef Filename,
10001004
IndexingConsumer &Consumer,
1001-
ArrayRef<const char *> Args) = 0;
1005+
ArrayRef<const char *> Args,
1006+
IndexSourceOptions Opts) = 0;
10021007

10031008
virtual void indexToStore(StringRef InputFile,
10041009
ArrayRef<const char *> Args,

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static UIdent getUIDForDependencyKind(bool isClangModule) {
3838

3939
class SKIndexDataConsumer : public IndexDataConsumer {
4040
public:
41-
SKIndexDataConsumer(IndexingConsumer &C) : impl(C) {}
41+
SKIndexDataConsumer(IndexingConsumer &C, IndexSourceOptions Opts) : impl(C), Opts(Opts) {}
4242

4343
private:
4444
void failed(StringRef error) override { impl.failed(error); }
@@ -51,6 +51,8 @@ class SKIndexDataConsumer : public IndexDataConsumer {
5151
return Logger::isLoggingEnabledForLevel(Logger::Level::Warning);
5252
}
5353

54+
virtual bool indexLocals() override { return Opts.IndexLocals; }
55+
5456
bool startDependency(StringRef name, StringRef path, bool isClangModule, bool isSystem) override {
5557
auto kindUID = getUIDForDependencyKind(isClangModule);
5658
return impl.startDependency(kindUID, name, path, isSystem);
@@ -61,7 +63,7 @@ class SKIndexDataConsumer : public IndexDataConsumer {
6163
}
6264

6365
Action startSourceEntity(const IndexSymbol &symbol) override {
64-
if (symbol.symInfo.Kind == SymbolKind::Parameter)
66+
if (!indexLocals() && symbol.symInfo.Kind == SymbolKind::Parameter)
6567
return Skip;
6668

6769
// report any parent relations to this reference
@@ -204,13 +206,15 @@ class SKIndexDataConsumer : public IndexDataConsumer {
204206

205207
private:
206208
IndexingConsumer &impl;
209+
IndexSourceOptions Opts;
207210
};
208211

209212
static void indexModule(llvm::MemoryBuffer *Input,
210213
StringRef ModuleName,
211214
IndexingConsumer &IdxConsumer,
212215
CompilerInstance &CI,
213-
ArrayRef<const char *> Args) {
216+
ArrayRef<const char *> Args,
217+
IndexSourceOptions Opts) {
214218
ASTContext &Ctx = CI.getASTContext();
215219
std::unique_ptr<ImplicitSerializedModuleLoader> Loader;
216220
ModuleDecl *Mod = nullptr;
@@ -245,7 +249,7 @@ static void indexModule(llvm::MemoryBuffer *Input,
245249
Mod->setHasResolvedImports();
246250
}
247251

248-
SKIndexDataConsumer IdxDataConsumer(IdxConsumer);
252+
SKIndexDataConsumer IdxDataConsumer(IdxConsumer, Opts);
249253
index::indexModule(Mod, IdxDataConsumer);
250254
}
251255

@@ -277,7 +281,8 @@ void trace::initTraceInfo(trace::SwiftInvocation &SwiftArgs,
277281

278282
void SwiftLangSupport::indexSource(StringRef InputFile,
279283
IndexingConsumer &IdxConsumer,
280-
ArrayRef<const char *> OrigArgs) {
284+
ArrayRef<const char *> OrigArgs,
285+
IndexSourceOptions Opts) {
281286
std::string Error;
282287
auto InputBuf =
283288
ASTMgr->getMemoryBuffer(InputFile, llvm::vfs::getRealFileSystem(), Error);
@@ -333,7 +338,7 @@ void SwiftLangSupport::indexSource(StringRef InputFile,
333338
}
334339

335340
indexModule(InputBuf.get(), llvm::sys::path::stem(Filename),
336-
IdxConsumer, CI, Args);
341+
IdxConsumer, CI, Args, Opts);
337342
return;
338343
}
339344

@@ -365,7 +370,7 @@ void SwiftLangSupport::indexSource(StringRef InputFile,
365370
return;
366371
}
367372

368-
SKIndexDataConsumer IdxDataConsumer(IdxConsumer);
373+
SKIndexDataConsumer IdxDataConsumer(IdxConsumer, Opts);
369374
index::indexSourceFile(CI.getPrimarySourceFile(), IdxDataConsumer);
370375
}
371376

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "clang/Lex/HeaderSearch.h"
3535
#include "clang/Lex/Preprocessor.h"
36+
#include "clang/Index/IndexSymbol.h"
3637
#include "llvm/ADT/APInt.h"
3738
#include "llvm/ADT/Hashing.h"
3839
#include "llvm/Support/ConvertUTF.h"
@@ -58,6 +59,7 @@ using swift::index::SymbolPropertySet;
5859
using swift::index::SymbolInfo;
5960
using swift::index::SymbolRole;
6061
using swift::index::SymbolRoleSet;
62+
using swift::index::operator&;
6163

6264
#define KIND(NAME, CONTENT) static UIdent Kind##NAME(CONTENT);
6365
#include "SourceKit/Core/ProtocolUIDs.def"
@@ -711,8 +713,19 @@ UIdent SwiftLangSupport::getUIDForSymbol(SymbolInfo sym, bool isRef) {
711713
} else {
712714
return UID_FOR(FunctionFree);
713715
}
714-
case SymbolKind::Variable:
716+
case SymbolKind::Parameter:
717+
if (isRef) {
718+
return KindRefVarLocal;
719+
} else {
720+
return KindDeclVarParam;
721+
}
722+
case SymbolKind::Variable: {
723+
bool IsLocalSymbol = sym.Properties & SymbolProperty::Local;
724+
if (IsLocalSymbol) {
725+
return UID_FOR(VarLocal);
726+
}
715727
return UID_FOR(VarGlobal);
728+
}
716729
case SymbolKind::InstanceMethod:
717730
return UID_FOR(MethodInstance);
718731
case SymbolKind::ClassMethod:

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class SwiftLangSupport : public LangSupport {
561561
Receiver) override;
562562

563563
void indexSource(StringRef Filename, IndexingConsumer &Consumer,
564-
ArrayRef<const char *> Args) override;
564+
ArrayRef<const char *> Args, IndexSourceOptions Opts) override;
565565

566566
void indexToStore(StringRef InputFile,
567567
ArrayRef<const char *> Args,

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
725725

726726
case SourceKitRequest::Index:
727727
sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestIndex);
728+
addRequestOptionsDirect(Req, Opts);
728729
break;
729730

730731
case SourceKitRequest::CodeComplete:

tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static SourceKit::Context &getGlobalContext() {
183183
}
184184
185185
static sourcekitd_response_t indexSource(StringRef Filename,
186-
ArrayRef<const char *> Args);
186+
ArrayRef<const char *> Args, IndexSourceOptions Opts);
187187
188188
static sourcekitd_response_t reportDocInfo(llvm::MemoryBuffer *InputBuf,
189189
StringRef ModuleName,
@@ -1417,7 +1417,12 @@ static void handleRequestIndex(const RequestDict &Req,
14171417
SmallVector<const char *, 8> Args;
14181418
if (getCompilerArgumentsForRequestOrEmitError(Req, Args, Rec))
14191419
return;
1420-
return Rec(indexSource(*PrimaryFilePath, Args));
1420+
1421+
int64_t ShouldIndexLocals = false;
1422+
Req.getInt64(KeyShouldIndexLocals, ShouldIndexLocals, /*isOptional*/true);
1423+
IndexSourceOptions Opts{.IndexLocals = ShouldIndexLocals > 0};
1424+
1425+
return Rec(indexSource(*PrimaryFilePath, Args, Opts));
14211426
});
14221427
}
14231428
@@ -2174,11 +2179,11 @@ class SKIndexingConsumer : public IndexingConsumer {
21742179
} // end anonymous namespace
21752180

21762181
static sourcekitd_response_t indexSource(StringRef Filename,
2177-
ArrayRef<const char *> Args) {
2182+
ArrayRef<const char *> Args, IndexSourceOptions Opts) {
21782183
ResponseBuilder RespBuilder;
21792184
SKIndexingConsumer IdxConsumer(RespBuilder);
21802185
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
2181-
Lang.indexSource(Filename, IdxConsumer, Args);
2186+
Lang.indexSource(Filename, IdxConsumer, Args, Opts);
21822187

21832188
if (!IdxConsumer.ErrorDescription.empty())
21842189
return createErrorRequestFailed(IdxConsumer.ErrorDescription.c_str());

utils/gyb_sourcekit_support/UIDs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def __init__(self, internal_name, external_name):
214214
KEY('ExpandedMacroReplacements', 'key.expanded_macro_replacements'),
215215
KEY('IndexStorePath', 'key.index_store_path'),
216216
KEY('IndexUnitOutputPath', 'key.index_unit_output_path'),
217+
KEY('ShouldIndexLocals', 'key.should_index_locals'),
217218
]
218219

219220

0 commit comments

Comments
 (0)