Skip to content

Commit 0755fd3

Browse files
authored
Merge pull request #69076 from barinsim/local-symbols
[SourceKit] Add option to index local symbols to IndexSource request
2 parents 11ef6e5 + b97c636 commit 0755fd3

File tree

6 files changed

+81
-23
lines changed

6 files changed

+81
-23
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %sourcekitd-test -req=index-to-store %s -index-store-path %t/idx -index-unit-output-path %t/index_locals.o -req-opts=include_locals=1 -- %s
3+
// RUN: c-index-test core -print-record %t/idx | %FileCheck %s
4+
5+
6+
func foo(a: Int, b: Double) {
7+
var locVar = 1
8+
}
9+
10+
11+
// CHECK: variable(local)/Swift | locVar | s:12index_locals3foo1a1bySi_SdtF6locVarL_Sivp | <no-cgname> | Def,RelChild - RelChild,RelAcc
12+
// CHECK-NEXT: function/acc-set(local)/Swift | setter:locVar | s:12index_locals3foo1a1bySi_SdtF6locVarL_Sivs | <no-cgname> | Def,Impl,RelChild,RelAcc -

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,11 @@ struct CategorizedRenameRanges {
912912
struct IndexStoreOptions {
913913
std::string IndexStorePath;
914914
std::string IndexUnitOutputPath;
915+
bool IgnoreClangModules = false;
916+
bool IncludeSystemModules = false;
917+
bool IgnoreStdlib = false;
918+
bool DisableImplicitModules = false;
919+
bool IncludeLocals = false;
915920
};
916921

917922
struct IndexStoreInfo{};

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,11 @@ void SwiftLangSupport::indexSource(StringRef InputFile,
370370
}
371371

372372
static void emitIndexDataForSourceFile(SourceFile &PrimarySourceFile,
373-
StringRef IndexStorePath,
374-
StringRef IndexUnitOutputPath,
373+
IndexStoreOptions IndexOpts,
375374
const CompilerInstance &Instance) {
376375
const auto &Invocation = Instance.getInvocation();
377-
const auto &Opts = Invocation.getFrontendOptions();
376+
// FIXME: Compiler arguments should be the default for setting options, but this is currently broken (see PR #69076)
377+
// const auto &Opts = Invocation.getFrontendOptions();
378378

379379
bool isDebugCompilation;
380380
switch (Invocation.getSILOptions().OptMode) {
@@ -388,14 +388,15 @@ static void emitIndexDataForSourceFile(SourceFile &PrimarySourceFile,
388388
break;
389389
}
390390

391-
(void) index::indexAndRecord(&PrimarySourceFile, IndexUnitOutputPath,
392-
IndexStorePath,
393-
!Opts.IndexIgnoreClangModules,
394-
Opts.IndexSystemModules,
395-
Opts.IndexIgnoreStdlib,
396-
Opts.IndexIncludeLocals,
391+
(void) index::indexAndRecord(&PrimarySourceFile,
392+
IndexOpts.IndexUnitOutputPath,
393+
IndexOpts.IndexStorePath,
394+
!IndexOpts.IgnoreClangModules,
395+
IndexOpts.IncludeSystemModules,
396+
IndexOpts.IgnoreStdlib,
397+
IndexOpts.IncludeLocals,
397398
isDebugCompilation,
398-
Opts.DisableImplicitModules,
399+
IndexOpts.DisableImplicitModules,
399400
Invocation.getTargetTriple(),
400401
*Instance.getDependencyTracker(),
401402
Invocation.getIRGenOptions().FilePrefixMap);
@@ -425,8 +426,7 @@ void SwiftLangSupport::indexToStore(
425426
void handlePrimaryAST(ASTUnitRef AstUnit) override {
426427
auto &SF = AstUnit->getPrimarySourceFile();
427428
auto &CI = AstUnit->getCompilerInstance();
428-
emitIndexDataForSourceFile(
429-
SF, Opts.IndexStorePath, Opts.IndexUnitOutputPath, CI);
429+
emitIndexDataForSourceFile(SF, Opts, CI);
430430
Receiver(RequestResult<IndexStoreInfo>::fromResult(IndexStoreInfo{}));
431431
}
432432

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
11591159
sourcekitd_request_dictionary_set_string(Req, KeyIndexStorePath, Opts.IndexStorePath.c_str());
11601160
sourcekitd_request_dictionary_set_string(Req, KeyIndexUnitOutputPath, Opts.IndexUnitOutputPath.c_str());
11611161
sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestIndexToStore);
1162+
addRequestOptionsDirect(Req, Opts);
11621163
break;
11631164
}
11641165

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,48 @@ static void handleRequestIndex(const RequestDict &Req,
13991399
});
14001400
}
14011401
1402+
static std::optional<IndexStoreOptions>
1403+
getIndexStoreOpts(const RequestDict &Req, ResponseReceiver Rec) {
1404+
IndexStoreOptions Opts;
1405+
if (auto IndexStorePath = Req.getString(KeyIndexStorePath))
1406+
Opts.IndexStorePath = IndexStorePath->str();
1407+
else {
1408+
Rec(createErrorRequestInvalid("'key.index_store_path' is required"));
1409+
return std::nullopt;
1410+
}
1411+
1412+
if (auto IndexUnitOutputPath = Req.getString(KeyIndexUnitOutputPath))
1413+
Opts.IndexUnitOutputPath = IndexUnitOutputPath->str();
1414+
else {
1415+
Rec(createErrorRequestInvalid("'key.index_unit_output_path' is required"));
1416+
return std::nullopt;
1417+
}
1418+
1419+
if (auto IncludeLocals = Req.getOptionalInt64(KeyIncludeLocals)) {
1420+
Opts.IncludeLocals = IncludeLocals.value() > 0;
1421+
}
1422+
1423+
if (auto IgnoreClangModules = Req.getOptionalInt64(KeyIgnoreClangModules)) {
1424+
Opts.IgnoreClangModules = IgnoreClangModules.value() > 0;
1425+
}
1426+
1427+
if (auto IncludeSystemModules =
1428+
Req.getOptionalInt64(KeyIncludeSystemModules)) {
1429+
Opts.IncludeSystemModules = IncludeSystemModules.value() > 0;
1430+
}
1431+
1432+
if (auto IgnoreStdlib = Req.getOptionalInt64(KeyIgnoreStdlib)) {
1433+
Opts.IgnoreStdlib = IgnoreStdlib.value() > 0;
1434+
}
1435+
1436+
if (auto DisableImplicitModules =
1437+
Req.getOptionalInt64(KeyDisableImplicitModules)) {
1438+
Opts.DisableImplicitModules = DisableImplicitModules.value() > 0;
1439+
}
1440+
1441+
return Opts;
1442+
}
1443+
14021444
static void handleRequestIndexToStore(
14031445
const RequestDict &Req, SourceKitCancellationToken CancellationToken,
14041446
ResponseReceiver Rec) {
@@ -1411,22 +1453,15 @@ static void handleRequestIndexToStore(
14111453
if (!PrimaryFilePath)
14121454
return;
14131455
1414-
IndexStoreOptions Opts;
1415-
if (auto IndexStorePath = Req.getString(KeyIndexStorePath))
1416-
Opts.IndexStorePath = IndexStorePath->str();
1417-
else
1418-
return Rec(createErrorRequestInvalid("'key.index_store_path' is required"));
1419-
1420-
if (auto IndexUnitOutputPath = Req.getString(KeyIndexUnitOutputPath))
1421-
Opts.IndexUnitOutputPath = IndexUnitOutputPath->str();
1422-
else
1423-
return Rec(createErrorRequestInvalid("'key.index_unit_output_path' is required"));
1456+
std::optional<IndexStoreOptions> Opts = getIndexStoreOpts(Req, Rec);
1457+
if (!Opts)
1458+
return;
14241459
14251460
SmallVector<const char *, 0> Args;
14261461
if (getCompilerArgumentsForRequestOrEmitError(Req, Args, Rec))
14271462
return;
14281463
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
1429-
Lang.indexToStore(*PrimaryFilePath, Args, std::move(Opts),
1464+
Lang.indexToStore(*PrimaryFilePath, Args, std::move(*Opts),
14301465
CancellationToken,
14311466
[Rec](const RequestResult<IndexStoreInfo> &Result) {
14321467
if (Result.isCancelled())

utils/gyb_sourcekit_support/UIDs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ def __init__(self, internal_name, external_name):
216216
KEY('ExpandedMacroReplacements', 'key.expanded_macro_replacements'),
217217
KEY('IndexStorePath', 'key.index_store_path'),
218218
KEY('IndexUnitOutputPath', 'key.index_unit_output_path'),
219+
KEY('IncludeLocals', 'key.include_locals'),
220+
KEY('IgnoreClangModules', 'key.ignore_clang_modules'),
221+
KEY('IncludeSystemModules', 'key.include_system_modules'),
222+
KEY('IgnoreStdlib', 'key.ignore_stdlib'),
223+
KEY('DisableImplicitModules', 'key.disable_implicit_modules'),
219224
]
220225

221226

0 commit comments

Comments
 (0)