Skip to content

Commit cd658a3

Browse files
authored
Merge pull request #202 from tayloraswift/occurrences-in-file
Symbol occurrences in file
2 parents d319552 + da98045 commit cd658a3

File tree

11 files changed

+124
-13
lines changed

11 files changed

+124
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
.vscode
23
default.profraw
34
Package.resolved
45
/.build

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public final class IndexStoreDB {
321321
}
322322
}
323323
}
324-
324+
325325
public func filesIncludedByFile(path: String) -> [String] {
326326
var result: [String] = []
327327
foreachFileIncludedByFile(path: path) { targetPath in
@@ -330,7 +330,7 @@ public final class IndexStoreDB {
330330
}
331331
return result
332332
}
333-
333+
334334
@discardableResult
335335
public func foreachFileIncludingFile(path: String, body: (String) -> Bool) -> Bool {
336336
return withoutActuallyEscaping(body) { body in
@@ -340,7 +340,7 @@ public final class IndexStoreDB {
340340
}
341341
}
342342
}
343-
343+
344344
public func filesIncludingFile(path: String) -> [String] {
345345
var result: [String] = []
346346
foreachFileIncludingFile(path: path) { targetPath in
@@ -421,6 +421,15 @@ public final class IndexStoreDB {
421421
return result
422422
}
423423

424+
public func symbolOccurrences(inFilePath path: String) -> [SymbolOccurrence] {
425+
var result: [SymbolOccurrence] = []
426+
forEachSymbolOccurrence(inFilePath: path) { occur in
427+
result.append(occur)
428+
return true
429+
}
430+
return result
431+
}
432+
424433
@discardableResult
425434
func forEachSymbol(inFilePath filePath: String, body: (Symbol) -> Bool) -> Bool {
426435
return withoutActuallyEscaping(body) { body in
@@ -430,6 +439,15 @@ public final class IndexStoreDB {
430439
}
431440
}
432441

442+
@discardableResult
443+
func forEachSymbolOccurrence(inFilePath filePath: String, body: (SymbolOccurrence) -> Bool) -> Bool {
444+
return withoutActuallyEscaping(body) { body in
445+
return indexstoredb_index_symbol_occurrences_in_file_path(impl, filePath) { occur in
446+
return body(SymbolOccurrence(occur))
447+
}
448+
}
449+
}
450+
433451
/// Returns all unit test symbol in unit files that reference one of the main files in `mainFilePaths`.
434452
public func unitTests(referencedByMainFiles mainFilePaths: [String]) -> [SymbolOccurrence] {
435453
var result: [SymbolOccurrence] = []

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ indexstoredb_index_symbols_contained_in_file_path(_Nonnull indexstoredb_index_t
328328
const char *_Nonnull path,
329329
_Nonnull indexstoredb_symbol_receiver_t);
330330

331+
/// Iterates over all the symbol occurrences contained in the source file at \p path
332+
///
333+
/// The occurrence passed to the receiver is only valid for the duration of the
334+
/// receiver call.
335+
INDEXSTOREDB_PUBLIC bool
336+
indexstoredb_index_symbol_occurrences_in_file_path(_Nonnull indexstoredb_index_t index,
337+
const char *_Nonnull path,
338+
_Nonnull indexstoredb_symbol_occurrence_receiver_t);
339+
331340
/// Returns the USR of the given symbol.
332341
///
333342
/// The string has the same lifetime as the \c indexstoredb_symbol_t.

include/IndexStoreDB/Index/IndexSystem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class INDEXSTOREDB_EXPORT IndexSystem {
104104
bool foreachSymbolInFilePath(StringRef FilePath,
105105
function_ref<bool(SymbolRef Symbol)> Receiver);
106106

107+
bool foreachSymbolOccurrenceInFilePath(StringRef FilePath,
108+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver);
109+
107110
bool foreachSymbolOccurrenceByUSR(StringRef USR, SymbolRoleSet RoleSet,
108111
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver);
109112

@@ -178,7 +181,7 @@ class INDEXSTOREDB_EXPORT IndexSystem {
178181
bool foreachUnitTestSymbol(function_ref<bool(SymbolOccurrenceRef Occur)> receiver);
179182

180183
/// Returns the latest modification date of a unit that contains the given source file.
181-
///
184+
///
182185
/// If no unit containing the given source file exists, returns `None`.
183186
llvm::Optional<llvm::sys::TimePoint<>> timestampOfLatestUnitForFile(StringRef filePath);
184187
private:

include/IndexStoreDB/Index/SymbolDataProvider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class SymbolDataProvider {
4848
SymbolRoleSet Roles,
4949
SymbolRoleSet RelatedRoles)> Receiver) = 0;
5050

51+
virtual bool foreachSymbolOccurrence(function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) = 0;
52+
5153
virtual bool foreachSymbolOccurrenceByUSR(ArrayRef<db::IDCode> USRs,
5254
SymbolRoleSet RoleSet,
5355
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) = 0;

include/IndexStoreDB/Index/SymbolIndex.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class SymbolIndex {
6868
bool foreachSymbolInFilePath(CanonicalFilePathRef filePath,
6969
function_ref<bool(SymbolRef Occur)> Receiver);
7070

71+
bool foreachSymbolOccurrenceInFilePath(CanonicalFilePathRef filePath,
72+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver);
73+
7174
bool foreachCanonicalSymbolOccurrenceContainingPattern(StringRef Pattern,
7275
bool AnchorStart,
7376
bool AnchorEnd,
@@ -104,7 +107,7 @@ class SymbolIndex {
104107
bool foreachUnitTestSymbol(function_ref<bool(SymbolOccurrenceRef Occur)> receiver);
105108

106109
/// Returns the latest modification date of a unit that contains the given source file.
107-
///
110+
///
108111
/// If no unit containing the given source file exists, returns `None`.
109112
llvm::Optional<llvm::sys::TimePoint<>> timestampOfLatestUnitForFile(CanonicalFilePathRef filePath);
110113
private:

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ indexstoredb_index_symbols_contained_in_file_path(_Nonnull indexstoredb_index_t
314314
});
315315
}
316316

317+
bool
318+
indexstoredb_index_symbol_occurrences_in_file_path(_Nonnull indexstoredb_index_t index,
319+
const char *_Nonnull path,
320+
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver) {
321+
auto obj = (Object<std::shared_ptr<IndexSystem>> *)index;
322+
return obj->value->foreachSymbolOccurrenceInFilePath(path, [&](SymbolOccurrenceRef Occur) -> bool {
323+
return receiver((indexstoredb_symbol_occurrence_t)Occur.get());
324+
});
325+
}
326+
317327
const char *
318328
indexstoredb_symbol_usr(indexstoredb_symbol_t symbol) {
319329
auto value = (Symbol *)symbol;

lib/Index/IndexSystem.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,14 @@ class IndexSystemImpl {
160160

161161
void addDelegate(std::shared_ptr<IndexSystemDelegate> Delegate);
162162

163-
bool foreachSymbolInFilePath(StringRef filePath,
164-
function_ref<bool(const SymbolRef &symbol)> receiver);
165-
166-
bool foreachSymbolOccurrenceByUSR(StringRef USR, SymbolRoleSet RoleSet,
167-
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver);
163+
bool foreachSymbolInFilePath(StringRef filePath,
164+
function_ref<bool(const SymbolRef &symbol)> receiver);
165+
166+
bool foreachSymbolOccurrenceInFilePath(StringRef filePath,
167+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver);
168+
169+
bool foreachSymbolOccurrenceByUSR(StringRef USR, SymbolRoleSet RoleSet,
170+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver);
168171

169172
bool foreachCanonicalSymbolOccurrenceContainingPattern(StringRef Pattern,
170173
bool AnchorStart,
@@ -236,7 +239,7 @@ class IndexSystemImpl {
236239
bool foreachUnitTestSymbol(function_ref<bool(SymbolOccurrenceRef Occur)> receiver);
237240

238241
/// Returns the latest modification date of a unit that contains the given source file.
239-
///
242+
///
240243
/// If no unit containing the given source file exists, returns `None`.
241244
llvm::Optional<llvm::sys::TimePoint<>> timestampOfLatestUnitForFile(StringRef filePath);
242245
};
@@ -579,6 +582,12 @@ bool IndexSystemImpl::foreachSymbolInFilePath(StringRef filePath,
579582
return SymIndex->foreachSymbolInFilePath(canonPath, std::move(receiver));
580583
}
581584

585+
bool IndexSystemImpl::foreachSymbolOccurrenceInFilePath(StringRef filePath,
586+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) {
587+
auto canonPath = PathIndex->getCanonicalPath(filePath);
588+
return SymIndex->foreachSymbolOccurrenceInFilePath(canonPath, std::move(Receiver));
589+
}
590+
582591
bool IndexSystemImpl::foreachFileOfUnit(StringRef unitName,
583592
bool followDependencies,
584593
function_ref<bool(CanonicalFilePathRef filePath)> receiver) {
@@ -778,6 +787,11 @@ bool IndexSystem::foreachSymbolInFilePath(StringRef FilePath,
778787
return IMPL->foreachSymbolInFilePath(FilePath, std::move(Receiver));
779788
}
780789

790+
bool IndexSystem::foreachSymbolOccurrenceInFilePath(StringRef FilePath,
791+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) {
792+
return IMPL->foreachSymbolOccurrenceInFilePath(FilePath, std::move(Receiver));
793+
}
794+
781795
bool IndexSystem::isKnownFile(StringRef filePath) {
782796
return IMPL->isKnownFile(filePath);
783797
}

lib/Index/StoreSymbolRecord.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ bool StoreSymbolRecord::foreachCoreSymbolData(function_ref<bool(StringRef USR,
289289
return !Err && Finished;
290290
}
291291

292+
bool StoreSymbolRecord::foreachSymbolOccurrence(function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) {
293+
bool Finished;
294+
bool Err = doForData([&](IndexRecordReader &Reader) {
295+
// Return all occurrences.
296+
auto Pred = [](IndexRecordOccurrence) -> bool { return true; };
297+
PredOccurrenceConverter Converter(*this, Pred, Receiver);
298+
Finished = Reader.foreachOccurrence(/*symbolsFilter=*/None,
299+
/*relatedSymbolsFilter=*/None,
300+
Converter);
301+
});
302+
303+
return !Err && Finished;
304+
}
305+
292306
static void searchDeclsByUSR(IndexRecordReader &Reader,
293307
ArrayRef<db::IDCode> USRs,
294308
SmallVectorImpl<IndexRecordSymbol> &FoundDecls) {

lib/Index/StoreSymbolRecord.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class StoreSymbolRecord : public SymbolDataProvider {
7575
SymbolRoleSet Roles,
7676
SymbolRoleSet RelatedRoles)> Receiver) override;
7777

78+
virtual bool foreachSymbolOccurrence(function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) override;
79+
7880
virtual bool foreachSymbolOccurrenceByUSR(ArrayRef<db::IDCode> USRs,
7981
SymbolRoleSet RoleSet,
8082
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) override;

lib/Index/SymbolIndex.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class SymbolIndexImpl {
7676
bool foreachSymbolInFilePath(CanonicalFilePathRef filePath,
7777
function_ref<bool(SymbolRef Symbol)> Receiver);
7878

79+
bool foreachSymbolOccurrenceInFilePath(CanonicalFilePathRef filePath,
80+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver);
81+
7982
bool foreachSymbolName(function_ref<bool(StringRef name)> receiver);
8083

8184
bool foreachCanonicalSymbolOccurrenceByUSR(StringRef USR,
@@ -100,7 +103,7 @@ class SymbolIndexImpl {
100103
bool foreachUnitTestSymbol(function_ref<bool(SymbolOccurrenceRef Occur)> receiver);
101104

102105
/// Returns the latest modification date of a unit that contains the given source file.
103-
///
106+
///
104107
/// If no unit containing the given source file exists, returns `None`.
105108
llvm::Optional<sys::TimePoint<>> timestampOfLatestUnitForFile(CanonicalFilePathRef filePath);
106109

@@ -481,7 +484,7 @@ bool SymbolIndexImpl::foreachSymbolInFilePath(CanonicalFilePathRef filePath,
481484
SymbolInfo info,
482485
SymbolRoleSet roles,
483486
SymbolRoleSet relatedRoles) -> bool {
484-
487+
485488
if (roles.containsAny(SymbolRoleSet(SymbolRole::Definition) | SymbolRole::Declaration)) {
486489
return Receiver(std::make_shared<Symbol>(info, name, usr));
487490
} else {
@@ -500,6 +503,33 @@ bool SymbolIndexImpl::foreachSymbolInFilePath(CanonicalFilePathRef filePath,
500503
return didFinish;
501504
}
502505

506+
bool SymbolIndexImpl::foreachSymbolOccurrenceInFilePath(CanonicalFilePathRef filePath,
507+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) {
508+
bool didFinish = true;
509+
ReadTransaction reader(DBase);
510+
511+
IDCode filePathCode = reader.getFilePathCode(filePath);
512+
reader.foreachUnitContainingFile(filePathCode, [&](ArrayRef<IDCode> idCodes) -> bool {
513+
for (IDCode idCode : idCodes) {
514+
UnitInfo unitInfo = reader.getUnitInfo(idCode);
515+
516+
for (UnitInfo::Provider provider : unitInfo.ProviderDepends) {
517+
IDCode providerCode = provider.ProviderCode;
518+
if (provider.FileCode == filePathCode) {
519+
auto record = createVisibleProviderForCode(providerCode, reader);
520+
didFinish = record->foreachSymbolOccurrence(Receiver);
521+
522+
return false;
523+
}
524+
}
525+
}
526+
527+
return true;
528+
});
529+
530+
return didFinish;
531+
}
532+
503533
bool SymbolIndexImpl::foreachCanonicalSymbolOccurrenceByKind(SymbolKind symKind, bool workspaceOnly,
504534
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) {
505535
return foreachCanonicalSymbolOccurrenceImpl(workspaceOnly,
@@ -706,6 +736,11 @@ bool SymbolIndex::foreachSymbolInFilePath(CanonicalFilePathRef filePath,
706736
return IMPL->foreachSymbolInFilePath(filePath, std::move(Receiver));
707737
}
708738

739+
bool SymbolIndex::foreachSymbolOccurrenceInFilePath(CanonicalFilePathRef filePath,
740+
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) {
741+
return IMPL->foreachSymbolOccurrenceInFilePath(filePath, std::move(Receiver));
742+
}
743+
709744
bool SymbolIndex::foreachUnitTestSymbolReferencedByOutputPaths(ArrayRef<CanonicalFilePathRef> FilePaths,
710745
function_ref<bool(SymbolOccurrenceRef Occur)> Receiver) {
711746
return IMPL->foreachUnitTestSymbolReferencedByOutputPaths(FilePaths, std::move(Receiver));

0 commit comments

Comments
 (0)