Skip to content

Commit d2a54af

Browse files
authored
Merge pull request #60000 from allevato/index-locals-flag
Add `-index-include-locals` flag.
2 parents 20b99f3 + 4d16c43 commit d2a54af

File tree

8 files changed

+78
-22
lines changed

8 files changed

+78
-22
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ class FrontendOptions {
105105
/// If indexing system modules, don't index the stdlib.
106106
bool IndexIgnoreStdlib = false;
107107

108+
/// Include local definitions/references in the index data.
109+
bool IndexIncludeLocals = false;
110+
108111
/// The module for which we should verify all of the generic signatures.
109112
std::string VerifyGenericSignaturesInModule;
110113

include/swift/Index/IndexRecord.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ namespace index {
4343
/// \param skipStdlib If indexing system modules, don't index the standard
4444
/// library.
4545
///
46+
/// \param includeLocals If true, emit index data for local definitions and
47+
/// references.
48+
///
4649
/// \param isDebugCompilation true for non-optimized compiler invocation.
4750
///
4851
/// \param targetTriple The target for this compilation.
@@ -53,7 +56,8 @@ namespace index {
5356
bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
5457
StringRef indexStorePath, bool indexClangModules,
5558
bool indexSystemModules, bool skipStdlib,
56-
bool isDebugCompilation, StringRef targetTriple,
59+
bool includeLocals, bool isDebugCompilation,
60+
StringRef targetTriple,
5761
const DependencyTracker &dependencyTracker,
5862
const PathRemapper &pathRemapper);
5963

@@ -81,6 +85,9 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
8185
/// \param skipStdlib If indexing system modules, don't index the standard
8286
/// library.
8387
///
88+
/// \param includeLocals If true, emit index data for local definitions and
89+
/// references.
90+
///
8491
/// \param isDebugCompilation true for non-optimized compiler invocation.
8592
///
8693
/// \param targetTriple The target for this compilation.
@@ -91,8 +98,8 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
9198
bool indexAndRecord(ModuleDecl *module, ArrayRef<std::string> indexUnitTokens,
9299
StringRef moduleUnitToken, StringRef indexStorePath,
93100
bool indexClangModules, bool indexSystemModules,
94-
bool skipStdlib, bool isDebugCompilation,
95-
StringRef targetTriple,
101+
bool skipStdlib, bool includeLocals,
102+
bool isDebugCompilation, StringRef targetTriple,
96103
const DependencyTracker &dependencyTracker,
97104
const PathRemapper &pathRemapper);
98105
// FIXME: indexUnitTokens could be StringRef, but that creates an impedance

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,10 @@ def index_ignore_clang_modules : Flag<["-"], "index-ignore-clang-modules">,
12621262
Flags<[FrontendOption]>,
12631263
HelpText<"Avoid indexing clang modules (pcms)">;
12641264

1265+
def index_include_locals : Flag<["-"], "index-include-locals">,
1266+
Flags<[FrontendOption]>,
1267+
HelpText<"Include local definitions/references in the produced index data.">;
1268+
12651269
def index_ignore_system_modules : Flag<["-"], "index-ignore-system-modules">,
12661270
Flags<[NoInteractiveOption]>,
12671271
HelpText<"Avoid indexing system modules">;

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ ToolChain::constructInvocation(const CompileJobAction &job,
575575
if (!context.Args.hasArg(options::OPT_index_ignore_system_modules))
576576
Arguments.push_back("-index-system-modules");
577577
context.Args.AddLastArg(Arguments, options::OPT_index_ignore_clang_modules);
578+
context.Args.AddLastArg(Arguments, options::OPT_index_include_locals);
578579
}
579580

580581
if (context.Args.hasArg(options::OPT_debug_info_store_invocation) ||

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bool ArgsToFrontendOptionsConverter::convert(
7272
Opts.IndexIgnoreClangModules |= Args.hasArg(OPT_index_ignore_clang_modules);
7373
Opts.IndexSystemModules |= Args.hasArg(OPT_index_system_modules);
7474
Opts.IndexIgnoreStdlib |= Args.hasArg(OPT_index_ignore_stdlib);
75+
Opts.IndexIncludeLocals |= Args.hasArg(OPT_index_include_locals);
7576

7677
Opts.EmitVerboseSIL |= Args.hasArg(OPT_emit_verbose_sil);
7778
Opts.EmitSortedSIL |= Args.hasArg(OPT_emit_sorted_sil);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,9 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17751775
opts.IndexStorePath,
17761776
!opts.IndexIgnoreClangModules,
17771777
opts.IndexSystemModules,
1778-
opts.IndexIgnoreStdlib, isDebugCompilation,
1778+
opts.IndexIgnoreStdlib,
1779+
opts.IndexIncludeLocals,
1780+
isDebugCompilation,
17791781
Invocation.getTargetTriple(),
17801782
*Instance.getDependencyTracker(),
17811783
Invocation.getIRGenOptions().FilePrefixMap);
@@ -1792,6 +1794,7 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17921794
!opts.IndexIgnoreClangModules,
17931795
opts.IndexSystemModules,
17941796
opts.IndexIgnoreStdlib,
1797+
opts.IndexIncludeLocals,
17951798
isDebugCompilation,
17961799
Invocation.getTargetTriple(),
17971800
*Instance.getDependencyTracker(),

lib/Index/IndexRecord.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,14 @@ class IndexRecordingConsumer : public IndexDataConsumer {
156156
// we actually need it (once per Decl instead of once per occurrence).
157157
std::vector<IndexSymbol> symbolStack;
158158

159+
bool includeLocals;
160+
159161
std::function<void(SymbolTracker &)> onFinish;
160162

161163
public:
162-
IndexRecordingConsumer(std::function<void(SymbolTracker &)> onFinish)
163-
: onFinish(std::move(onFinish)) {}
164+
IndexRecordingConsumer(bool includeLocals,
165+
std::function<void(SymbolTracker &)> onFinish)
166+
: includeLocals(includeLocals), onFinish(std::move(onFinish)) {}
164167

165168
void failed(StringRef error) override {
166169
// FIXME: expose errors?
@@ -183,6 +186,8 @@ class IndexRecordingConsumer : public IndexDataConsumer {
183186
}
184187

185188
void finish() override { onFinish(record); }
189+
190+
bool indexLocals() override { return includeLocals; }
186191
};
187192

188193
class StdlibGroupsIndexRecordingConsumer : public IndexDataConsumer {
@@ -323,24 +328,25 @@ static bool writeRecord(SymbolTracker &record, std::string Filename,
323328

324329
static std::unique_ptr<IndexRecordingConsumer>
325330
makeRecordingConsumer(std::string Filename, std::string indexStorePath,
326-
DiagnosticEngine *diags,
331+
bool includeLocals, DiagnosticEngine *diags,
327332
std::string *outRecordFile,
328333
bool *outFailed) {
329-
return std::make_unique<IndexRecordingConsumer>([=](SymbolTracker &record) {
334+
return std::make_unique<IndexRecordingConsumer>(includeLocals,
335+
[=](SymbolTracker &record) {
330336
*outFailed = writeRecord(record, Filename, indexStorePath, diags,
331337
*outRecordFile);
332338
});
333339
}
334340

335341
static bool
336342
recordSourceFile(SourceFile *SF, StringRef indexStorePath,
337-
DiagnosticEngine &diags,
343+
bool includeLocals, DiagnosticEngine &diags,
338344
llvm::function_ref<void(StringRef, StringRef)> callback) {
339345
std::string recordFile;
340346
bool failed = false;
341347
auto consumer =
342348
makeRecordingConsumer(SF->getFilename().str(), indexStorePath.str(),
343-
&diags, &recordFile, &failed);
349+
includeLocals, &diags, &recordFile, &failed);
344350
indexSourceFile(SF, *consumer);
345351

346352
if (!failed && !recordFile.empty())
@@ -379,6 +385,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
379385
bool indexClangModules,
380386
bool indexSystemModules,
381387
bool skipStdlib,
388+
bool includeLocals,
382389
StringRef targetTriple,
383390
const clang::CompilerInstance &clangCI,
384391
DiagnosticEngine &diags,
@@ -391,6 +398,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
391398
bool indexClangModules,
392399
bool indexSystemModules,
393400
bool skipStdlib,
401+
bool includeLocals,
394402
StringRef targetTriple,
395403
const clang::CompilerInstance &clangCI,
396404
DiagnosticEngine &diags,
@@ -448,7 +456,8 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
448456
emitDataForSwiftSerializedModule(mod, indexStorePath,
449457
indexClangModules,
450458
indexSystemModules, skipStdlib,
451-
targetTriple, clangCI, diags,
459+
includeLocals, targetTriple,
460+
clangCI, diags,
452461
unitWriter,
453462
pathRemapper,
454463
initialFile);
@@ -479,6 +488,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
479488
bool indexClangModules,
480489
bool indexSystemModules,
481490
bool skipStdlib,
491+
bool includeLocals,
482492
StringRef targetTriple,
483493
const clang::CompilerInstance &clangCI,
484494
DiagnosticEngine &diags,
@@ -514,7 +524,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
514524
std::string recordFile;
515525
bool failed = false;
516526
auto consumer = makeRecordingConsumer(filename.str(), indexStorePath.str(),
517-
&diags, &recordFile, &failed);
527+
includeLocals, &diags, &recordFile, &failed);
518528
indexModule(module, *consumer);
519529

520530
if (failed)
@@ -602,9 +612,9 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
602612
ModuleDecl::ImportFilterKind::Default});
603613
StringScratchSpace moduleNameScratch;
604614
addModuleDependencies(imports, indexStorePath, indexClangModules,
605-
indexSystemModules, skipStdlib, targetTriple, clangCI,
606-
diags, unitWriter, moduleNameScratch, pathRemapper,
607-
initialFile);
615+
indexSystemModules, skipStdlib, includeLocals,
616+
targetTriple, clangCI, diags, unitWriter,
617+
moduleNameScratch, pathRemapper, initialFile);
608618

609619
if (unitWriter.write(error)) {
610620
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
@@ -618,7 +628,8 @@ static bool
618628
recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
619629
StringRef indexStorePath, bool indexClangModules,
620630
bool indexSystemModules, bool skipStdlib,
621-
bool isDebugCompilation, StringRef targetTriple,
631+
bool includeLocals, bool isDebugCompilation,
632+
StringRef targetTriple,
622633
ArrayRef<const clang::FileEntry *> fileDependencies,
623634
const clang::CompilerInstance &clangCI,
624635
const PathRemapper &pathRemapper,
@@ -645,15 +656,15 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
645656
ModuleDecl::ImportFilterKind::ImplementationOnly});
646657
StringScratchSpace moduleNameScratch;
647658
addModuleDependencies(imports, indexStorePath, indexClangModules,
648-
indexSystemModules, skipStdlib, targetTriple, clangCI,
649-
diags, unitWriter, moduleNameScratch, pathRemapper,
650-
primarySourceFile);
659+
indexSystemModules, skipStdlib, includeLocals,
660+
targetTriple, clangCI, diags, unitWriter,
661+
moduleNameScratch, pathRemapper, primarySourceFile);
651662

652663
// File dependencies.
653664
for (auto *F : fileDependencies)
654665
unitWriter.addFileDependency(F, /*FIXME:isSystem=*/false, /*Module=*/nullptr);
655666

656-
recordSourceFile(primarySourceFile, indexStorePath, diags,
667+
recordSourceFile(primarySourceFile, indexStorePath, includeLocals, diags,
657668
[&](StringRef recordFile, StringRef filename) {
658669
auto file = fileMgr.getFile(filename);
659670
unitWriter.addRecordFile(
@@ -700,6 +711,7 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
700711
bool indexClangModules,
701712
bool indexSystemModules,
702713
bool skipStdlib,
714+
bool includeLocals,
703715
bool isDebugCompilation,
704716
StringRef targetTriple,
705717
const DependencyTracker &dependencyTracker,
@@ -729,7 +741,7 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
729741

730742
return recordSourceFileUnit(primarySourceFile, indexUnitToken,
731743
indexStorePath, indexClangModules,
732-
indexSystemModules, skipStdlib,
744+
indexSystemModules, skipStdlib, includeLocals,
733745
isDebugCompilation, targetTriple,
734746
fileDependencies.getArrayRef(),
735747
clangCI, pathRemapper, diags);
@@ -742,6 +754,7 @@ bool index::indexAndRecord(ModuleDecl *module,
742754
bool indexClangModules,
743755
bool indexSystemModules,
744756
bool skipStdlib,
757+
bool includeLocals,
745758
bool isDebugCompilation,
746759
StringRef targetTriple,
747760
const DependencyTracker &dependencyTracker,
@@ -779,7 +792,7 @@ bool index::indexAndRecord(ModuleDecl *module,
779792
}
780793
if (recordSourceFileUnit(SF, indexUnitTokens[unitIndex],
781794
indexStorePath, indexClangModules,
782-
indexSystemModules, skipStdlib,
795+
indexSystemModules, skipStdlib, includeLocals,
783796
isDebugCompilation, targetTriple,
784797
fileDependencies.getArrayRef(),
785798
clangCI, pathRemapper, diags))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This test just verifies that the flag is passed from the driver to the
2+
// frontend. More thorough testing of the frontend behavior is in
3+
// test/Index/local.swift.
4+
//
5+
// Verify the driver sets the flag for the frontend jobs:
6+
//
7+
// RUN: %target-swiftc_driver -driver-print-jobs -emit-object -index-ignore-system-modules -index-store-path %t/index_store -o %t/file.o %s | %FileCheck -check-prefix=JOBS %s
8+
// RUN: %target-swiftc_driver -driver-print-jobs -emit-object -index-ignore-system-modules -index-include-locals -index-store-path %t/index_store -o %t/file.o %s | %FileCheck -check-prefix=LOCAL-JOBS %s
9+
// JOBS-NOT: {{.*}}swift{{(-frontend)?(c\.exe")?}} -frontend {{.*}}-index-store-path {{.*}}/index_store{{"?}} -index-include-locals
10+
// LOCAL-JOBS: {{.*}}swift{{(-frontend)?(c\.exe")?}} -frontend {{.*}}-index-store-path {{.*}}/index_store{{"?}} -index-include-locals
11+
//
12+
// Verify the frontend actually generates local index data:
13+
//
14+
// RUN: rm -rf %t
15+
// RUN: %target-swiftc_driver -emit-object -index-ignore-system-modules -index-store-path %t/idx-no-locals -o %t/file.o %s
16+
// RUN: %target-swiftc_driver -emit-object -index-ignore-system-modules -index-include-locals -index-store-path %t/idx-include-locals -o %t/file.o %s
17+
// RUN: c-index-test core -print-record %t/idx-no-locals | %FileCheck -check-prefix=CHECK %s
18+
// RUN: c-index-test core -print-record %t/idx-include-locals | %FileCheck -check-prefix=LOCAL %s
19+
20+
func foo(a: Int, b: Int) {
21+
let x = a + b
22+
// LOCAL: [[@LINE-1]]:9 | variable(local)/Swift | [[x_USR:.*]] | Def,RelChild | rel: 1
23+
// CHECK-NOT: [[@LINE-2]]:9 | variable(local)/Swift | {{.*}} | Def,RelChild | rel: 1
24+
}

0 commit comments

Comments
 (0)