Skip to content

Commit 9e1db56

Browse files
committed
Allow retrieval of the symbol provider kind for a source file
1 parent da098e0 commit 9e1db56

File tree

6 files changed

+97
-5
lines changed

6 files changed

+97
-5
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public struct PathMapping {
3535
}
3636
}
3737

38+
public enum SymbolProviderKind {
39+
case clang
40+
case swift
41+
}
42+
3843
/// IndexStoreDB index.
3944
public final class IndexStoreDB {
4045

@@ -275,6 +280,41 @@ public final class IndexStoreDB {
275280
return result
276281
}
277282

283+
public func symbolProvider(for sourceFilePath: String) -> SymbolProviderKind? {
284+
var result: SymbolProviderKind? = nil
285+
indexstoredb_index_units_containing_file(impl, sourceFilePath) { unit in
286+
let providerKind: SymbolProviderKind? = switch indexstoredb_unit_info_symbol_provider_kind(unit) {
287+
case INDEXSTOREDB_SYMBOL_PROVIDER_KIND_SWIFT:
288+
.swift
289+
case INDEXSTOREDB_SYMBOL_PROVIDER_KIND_CLANG:
290+
.clang
291+
case INDEXSTOREDB_SYMBOL_PROVIDER_KIND_UNKNOWN:
292+
nil
293+
default:
294+
preconditionFailure("Unknown enum case in indexstoredb_symbol_provider_kind_t")
295+
}
296+
297+
let mainFilePath = String(cString: indexstoredb_unit_info_main_file_path(unit))
298+
if providerKind == .swift && mainFilePath != sourceFilePath {
299+
// We have a unit that is "included" from Swift. This happens for header
300+
// files that Swift files depend on. But Swift doesn't have includes and
301+
// we shouldn't infer the header file's language to Swift based on this unit.
302+
// Ignore it.
303+
return true
304+
}
305+
306+
if result == nil {
307+
result = providerKind
308+
} else if result != providerKind {
309+
// Found two conflicting provider kinds. Return nil as we don't know the provider in this case.
310+
result = nil
311+
return false
312+
}
313+
return true
314+
}
315+
return result
316+
}
317+
278318
@discardableResult
279319
public func foreachFileIncludedByFile(path: String, body: @escaping (String) -> Bool) -> Bool {
280320
return indexstoredb_index_files_included_by_file(impl, path) { targetPath, line in

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ typedef enum {
144144
INDEXSTOREDB_LANGUAGE_SWIFT
145145
} indexstoredb_language_t;
146146

147+
typedef enum {
148+
INDEXSTOREDB_SYMBOL_PROVIDER_KIND_CLANG,
149+
INDEXSTOREDB_SYMBOL_PROVIDER_KIND_SWIFT,
150+
INDEXSTOREDB_SYMBOL_PROVIDER_KIND_UNKNOWN,
151+
} indexstoredb_symbol_provider_kind_t;
152+
147153
typedef void *indexstoredb_delegate_event_t;
148154

149155
/// Returns true on success.
@@ -483,6 +489,9 @@ indexstoredb_unit_info_main_file_path(_Nonnull indexstoredb_unit_info_t);
483489
INDEXSTOREDB_PUBLIC const char *_Nonnull
484490
indexstoredb_unit_info_unit_name(_Nonnull indexstoredb_unit_info_t);
485491

492+
INDEXSTOREDB_PUBLIC indexstoredb_symbol_provider_kind_t
493+
indexstoredb_unit_info_symbol_provider_kind(_Nonnull indexstoredb_unit_info_t info);
494+
486495
/// Iterates over the compilation units that contain \p path and return their units.
487496
///
488497
/// This can be used to find information for units that include a given header.

include/IndexStoreDB/Index/StoreUnitInfo.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef INDEXSTOREDB_INDEX_STOREUNITINFO_H
1414
#define INDEXSTOREDB_INDEX_STOREUNITINFO_H
1515

16+
#include "IndexStoreDB/Core/Symbol.h"
1617
#include "IndexStoreDB/Support/Path.h"
1718
#include "llvm/Support/Chrono.h"
1819
#include <string>
@@ -26,16 +27,18 @@ struct StoreUnitInfo {
2627
std::string OutFileIdentifier;
2728
bool HasTestSymbols = false;
2829
llvm::sys::TimePoint<> ModTime;
30+
Optional<SymbolProviderKind> SymProviderKind;
2931

3032
StoreUnitInfo() = default;
3133
StoreUnitInfo(std::string unitName, CanonicalFilePath mainFilePath,
3234
StringRef outFileIdentifier, bool hasTestSymbols,
33-
llvm::sys::TimePoint<> modTime)
35+
llvm::sys::TimePoint<> modTime, Optional
36+
<SymbolProviderKind> SymProviderKind)
3437
: UnitName(unitName),
3538
MainFilePath(mainFilePath),
3639
OutFileIdentifier(outFileIdentifier),
3740
HasTestSymbols(hasTestSymbols),
38-
ModTime(modTime) {}
41+
ModTime(modTime), SymProviderKind(SymProviderKind) {}
3942
};
4043

4144
} // namespace index

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,20 @@ indexstoredb_unit_info_unit_name(indexstoredb_unit_info_t info) {
566566
return obj->UnitName.c_str();
567567
}
568568

569+
indexstoredb_symbol_provider_kind_t
570+
indexstoredb_unit_info_symbol_provider_kind(_Nonnull indexstoredb_unit_info_t info) {
571+
auto obj = (const StoreUnitInfo *)info;
572+
if (!obj->SymProviderKind) {
573+
return INDEXSTOREDB_SYMBOL_PROVIDER_KIND_UNKNOWN;
574+
}
575+
switch (*obj->SymProviderKind) {
576+
case IndexStoreDB::SymbolProviderKind::Clang:
577+
return INDEXSTOREDB_SYMBOL_PROVIDER_KIND_CLANG;
578+
case IndexStoreDB::SymbolProviderKind::Swift:
579+
return INDEXSTOREDB_SYMBOL_PROVIDER_KIND_SWIFT;
580+
}
581+
}
582+
569583
bool
570584
indexstoredb_index_units_containing_file(
571585
indexstoredb_index_t index,

lib/Index/FilePathIndex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ bool FileIndexImpl::foreachMainUnitContainingFile(CanonicalFilePathRef filePath,
166166
currUnit.ModTime = unitInfo.ModTime;
167167
currUnit.MainFilePath = reader.getFullFilePathFromCode(unitInfo.MainFileCode);
168168
currUnit.OutFileIdentifier = reader.getUnitFileIdentifierFromCode(unitInfo.OutFileCode);
169+
currUnit.SymProviderKind = unitInfo.SymProviderKind;
169170
return true;
170171
});
171172
}

lib/Index/IndexDatastore.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ void StoreUnitRepo::registerUnit(StringRef unitName, bool isInitialScan, std::sh
479479
bool needDatabaseUpdate;
480480
Optional<bool> optIsSystem;
481481
Optional<bool> PrevHasTestSymbols;
482+
Optional<SymbolProviderKind> PrevSymProviderKind;
482483
IDCode PrevMainFileCode;
483484
IDCode PrevOutFileCode;
484485
Optional<StoreUnitInfo> StoreUnitInfoOpt;
@@ -498,6 +499,7 @@ void StoreUnitRepo::registerUnit(StringRef unitName, bool isInitialScan, std::sh
498499
PrevMainFileCode = unitImport.getPrevMainFileCode();
499500
PrevOutFileCode = unitImport.getPrevOutFileCode();
500501
PrevHasTestSymbols = unitImport.getHasTestSymbols();
502+
PrevSymProviderKind = unitImport.getSymbolProviderKind();
501503
return false;
502504
}
503505

@@ -595,7 +597,14 @@ void StoreUnitRepo::registerUnit(StringRef unitName, bool isInitialScan, std::sh
595597
}
596598

597599
unitImport.commit();
598-
StoreUnitInfoOpt = StoreUnitInfo{unitName, CanonMainFile, OutFileIdentifier, unitImport.getHasTestSymbols().getValue(), unitModTime};
600+
StoreUnitInfoOpt = StoreUnitInfo{
601+
unitName,
602+
CanonMainFile,
603+
OutFileIdentifier,
604+
unitImport.getHasTestSymbols().getValue(),
605+
unitModTime,
606+
unitImport.getSymbolProviderKind()
607+
};
599608
import.commit();
600609
return false;
601610
};
@@ -608,7 +617,14 @@ void StoreUnitRepo::registerUnit(StringRef unitName, bool isInitialScan, std::sh
608617
ReadTransaction reader(SymIndex->getDBase());
609618
CanonicalFilePath mainFile = reader.getFullFilePathFromCode(PrevMainFileCode);
610619
std::string outFileIdentifier = reader.getUnitFileIdentifierFromCode(PrevOutFileCode);
611-
StoreUnitInfoOpt = StoreUnitInfo{unitName, mainFile, outFileIdentifier, PrevHasTestSymbols.getValue(), unitModTime};
620+
StoreUnitInfoOpt = StoreUnitInfo{
621+
unitName,
622+
mainFile,
623+
outFileIdentifier,
624+
PrevHasTestSymbols.getValue(),
625+
unitModTime,
626+
PrevSymProviderKind.getValue()
627+
};
612628
}
613629
Delegate->processedStoreUnit(StoreUnitInfoOpt.getValue());
614630
}
@@ -816,6 +832,7 @@ void StoreUnitRepo::onUnitOutOfDate(IDCode unitCode, StringRef unitName,
816832
CanonicalFilePath MainFilePath;
817833
std::string OutFileIdentifier;
818834
bool hasTestSymbols = false;
835+
Optional<SymbolProviderKind> SymProviderKind;
819836
llvm::sys::TimePoint<> CurrModTime;
820837
SmallVector<IDCode, 8> dependentUnits;
821838
{
@@ -827,13 +844,21 @@ void StoreUnitRepo::onUnitOutOfDate(IDCode unitCode, StringRef unitName,
827844
}
828845
OutFileIdentifier = reader.getUnitFileIdentifierFromCode(unitInfo.OutFileCode);
829846
hasTestSymbols = unitInfo.HasTestSymbols;
847+
SymProviderKind = unitInfo.SymProviderKind;
830848
CurrModTime = unitInfo.ModTime;
831849
}
832850
reader.getDirectDependentUnits(unitCode, dependentUnits);
833851
}
834852

835853
if (!MainFilePath.empty() && Delegate) {
836-
StoreUnitInfo unitInfo{unitName, MainFilePath, OutFileIdentifier, hasTestSymbols, CurrModTime};
854+
StoreUnitInfo unitInfo{
855+
unitName,
856+
MainFilePath,
857+
OutFileIdentifier,
858+
hasTestSymbols,
859+
CurrModTime,
860+
SymProviderKind
861+
};
837862
Delegate->unitIsOutOfDate(unitInfo, outOfDateModTime, hint, synchronous);
838863
}
839864

0 commit comments

Comments
 (0)