Skip to content

Commit 5a4e3ef

Browse files
committed
add file including api
1 parent 7223f30 commit 5a4e3ef

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ public final class IndexStoreDB {
239239
return result
240240
}
241241

242-
@discardableResult
242+
@discardableResult
243243
public func foreachFileIncludedByFile(path: String, body: @escaping (String) -> Bool) -> Bool {
244-
return indexstoredb_index_files_included_by_file(impl, path) { targetPath, line in
244+
return indexstoredb_index_files_included_by_file(impl, path) { targetPath, line in
245245
let targetPathStr = String(cString: targetPath)
246246
return body(targetPathStr)
247-
}
248-
}
247+
}
248+
}
249249

250250
public func filesIncludedByFile(path: String) -> [String] {
251251
var result: [String] = []
@@ -255,6 +255,23 @@ public final class IndexStoreDB {
255255
}
256256
return result
257257
}
258+
259+
@discardableResult
260+
public func foreachFileIncludingFile(path: String, body: @escaping (String) -> Bool) -> Bool {
261+
return indexstoredb_index_files_including_file(impl, path) { sourcePath, line in
262+
let sourcePathStr = String(cString: sourcePath)
263+
return body(sourcePathStr)
264+
}
265+
}
266+
267+
public func filesIncludingFile(path: String) -> [String] {
268+
var result: [String] = []
269+
foreachFileIncludingFile(path: path) { targetPath in
270+
result.append(targetPath)
271+
return true
272+
}
273+
return result
274+
}
258275

259276
/// A recorded header `#include` from a unit file.
260277
public struct UnitIncludeEntry: Equatable {

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ typedef bool(^indexstoredb_unit_info_receiver)(_Nonnull indexstoredb_unit_info_t
141141
/// Returns true to continue.
142142
typedef bool(^indexstoredb_files_included_receiver)(const char *_Nonnull targetPath, size_t line);
143143

144+
/// Returns true to continue.
145+
typedef bool(^indexstoredb_files_including_receiver)(const char *_Nonnull sourcePath, size_t line);
146+
144147
/// Returns true to continue.
145148
typedef bool(^indexstoredb_unit_includes_receiver)(const char *_Nonnull sourcePath, const char *_Nonnull targetPath, size_t line);
146149

@@ -388,7 +391,7 @@ indexstoredb_index_units_containing_file(
388391
const char *_Nonnull path,
389392
_Nonnull indexstoredb_unit_info_receiver receiver);
390393

391-
/// Return the file path which included by a given file path..
394+
/// Return the file path which included by a given file path.
392395
///
393396
/// \param index An IndexStoreDB object which contains the symbols.
394397
/// \param path The source file to search for.
@@ -400,6 +403,18 @@ indexstoredb_index_files_included_by_file(
400403
const char *_Nonnull path,
401404
_Nonnull indexstoredb_files_included_receiver receiver);
402405

406+
/// Return the file path which including a given header.
407+
///
408+
/// \param index An IndexStoreDB object which contains the symbols.
409+
/// \param path The source file to search for.
410+
/// \param receiver A function to be called for each include file path. The pointers are only valid for
411+
/// the duration of the call. The function should return a true to continue iterating.
412+
INDEXSTOREDB_PUBLIC bool
413+
indexstoredb_index_files_including_file(
414+
_Nonnull indexstoredb_index_t index,
415+
const char *_Nonnull path,
416+
_Nonnull indexstoredb_files_including_receiver receiver);
417+
403418
/// Iterates over recorded `#include`s of a unit.
404419
///
405420
/// \param index An IndexStoreDB object which contains the symbols.

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,18 @@ indexstoredb_index_files_included_by_file(
443443
});
444444
}
445445

446+
bool
447+
indexstoredb_index_files_including_file(
448+
indexstoredb_index_t index,
449+
const char *path,
450+
indexstoredb_files_including_receiver receiver)
451+
{
452+
auto obj = (Object<std::shared_ptr<IndexSystem>> *)index;
453+
return obj->value->foreachFileIncludingFile(path, [&](const CanonicalFilePathRef &sourcePath, unsigned line) -> bool {
454+
return receiver(sourcePath.getPath().str().c_str(), line);
455+
});
456+
}
457+
446458
bool
447459
indexstoredb_index_includes_of_unit(
448460
indexstoredb_index_t index,

0 commit comments

Comments
 (0)