Skip to content

Commit e093926

Browse files
authored
Merge pull request #109 from woshiccm/file-include-api
add file include api
2 parents b68eaae + cbd377a commit e093926

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,40 @@ public final class IndexStoreDB {
238238
}
239239
return result
240240
}
241+
242+
@discardableResult
243+
public func foreachFileIncludedByFile(path: String, body: @escaping (String) -> Bool) -> Bool {
244+
return indexstoredb_index_files_included_by_file(impl, path) { targetPath, line in
245+
let targetPathStr = String(cString: targetPath)
246+
return body(targetPathStr)
247+
}
248+
}
249+
250+
public func filesIncludedByFile(path: String) -> [String] {
251+
var result: [String] = []
252+
foreachFileIncludedByFile(path: path) { targetPath in
253+
result.append(targetPath)
254+
return true
255+
}
256+
return result
257+
}
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+
}
241275

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

Tests/IndexStoreDBTests/IndexTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ final class IndexTests: XCTestCase {
299299
IndexStoreDB.UnitIncludeEntry(sourcePath: main1, targetPath: uniq1, line: ws.testLoc("include_main1_uniq1").line),
300300
])
301301
}
302+
303+
func testFilesIncludes() throws {
304+
guard let ws = try staticTibsTestWorkspace(name: "MainFiles") else { return }
305+
try ws.buildAndIndex()
306+
let index = ws.index
307+
308+
let main1 = ws.testLoc("main1").url.path
309+
let main2 = ws.testLoc("main2").url.path
310+
let uniq1 = ws.testLoc("uniq1").url.path
311+
let shared = ws.testLoc("shared").url.path
312+
313+
let includedFiles = index.filesIncludedByFile(path: main1).sorted()
314+
XCTAssertEqual(includedFiles, [shared, uniq1])
315+
316+
let includingFiles = index.filesIncludingFile(path: shared).sorted()
317+
XCTAssertEqual(includingFiles, [main1, main2])
318+
}
302319

303320
func testAllSymbolNames() throws {
304321
guard let ws = try staticTibsTestWorkspace(name: "proj1") else { return }

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ typedef void(^indexstoredb_delegate_event_receiver_t)(_Nonnull indexstoredb_dele
138138
/// Returns true to continue.
139139
typedef bool(^indexstoredb_unit_info_receiver)(_Nonnull indexstoredb_unit_info_t);
140140

141+
/// Returns true to continue.
142+
typedef bool(^indexstoredb_file_includes_receiver)(const char *_Nonnull sourcePath, size_t line);
143+
141144
/// Returns true to continue.
142145
typedef bool(^indexstoredb_unit_includes_receiver)(const char *_Nonnull sourcePath, const char *_Nonnull targetPath, size_t line);
143146

@@ -385,6 +388,30 @@ indexstoredb_index_units_containing_file(
385388
const char *_Nonnull path,
386389
_Nonnull indexstoredb_unit_info_receiver receiver);
387390

391+
/// Return the file path which included by a given file path.
392+
///
393+
/// \param index An IndexStoreDB object which contains the symbols.
394+
/// \param path The source file to search for.
395+
/// \param receiver A function to be called for each include file path. The pointers are only valid for
396+
/// the duration of the call. The function should return a true to continue iterating.
397+
INDEXSTOREDB_PUBLIC bool
398+
indexstoredb_index_files_included_by_file(
399+
_Nonnull indexstoredb_index_t index,
400+
const char *_Nonnull path,
401+
_Nonnull indexstoredb_file_includes_receiver receiver);
402+
403+
/// Return the file path which including a given header.
404+
///
405+
/// \param index An IndexStoreDB object which contains the symbols.
406+
/// \param path The source file to search for.
407+
/// \param receiver A function to be called for each include file path. The pointers are only valid for
408+
/// the duration of the call. The function should return a true to continue iterating.
409+
INDEXSTOREDB_PUBLIC bool
410+
indexstoredb_index_files_including_file(
411+
_Nonnull indexstoredb_index_t index,
412+
const char *_Nonnull path,
413+
_Nonnull indexstoredb_file_includes_receiver receiver);
414+
388415
/// Iterates over recorded `#include`s of a unit.
389416
///
390417
/// \param index An IndexStoreDB object which contains the symbols.

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,30 @@ indexstoredb_index_units_containing_file(
431431
});
432432
}
433433

434+
bool
435+
indexstoredb_index_files_included_by_file(
436+
indexstoredb_index_t index,
437+
const char *path,
438+
indexstoredb_file_includes_receiver receiver)
439+
{
440+
auto obj = (Object<std::shared_ptr<IndexSystem>> *)index;
441+
return obj->value->foreachFileIncludedByFile(path, [&](const CanonicalFilePathRef &sourcePath, unsigned line) -> bool {
442+
return receiver(sourcePath.getPath().str().c_str(), line);
443+
});
444+
}
445+
446+
bool
447+
indexstoredb_index_files_including_file(
448+
indexstoredb_index_t index,
449+
const char *path,
450+
indexstoredb_file_includes_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+
434458
bool
435459
indexstoredb_index_includes_of_unit(
436460
indexstoredb_index_t index,

0 commit comments

Comments
 (0)