Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file include api #109

Merged
merged 5 commits into from Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions Sources/IndexStoreDB/IndexStoreDB.swift
Expand Up @@ -238,6 +238,40 @@ public final class IndexStoreDB {
}
return result
}

@discardableResult
public func foreachFileIncludedByFile(path: String, body: @escaping (String) -> Bool) -> Bool {
return indexstoredb_index_files_included_by_file(impl, path) { targetPath, line in
let targetPathStr = String(cString: targetPath)
return body(targetPathStr)
}
}

public func filesIncludedByFile(path: String) -> [String] {
var result: [String] = []
foreachFileIncludedByFile(path: path) { targetPath in
result.append(targetPath)
return true
}
return result
}

@discardableResult
public func foreachFileIncludingFile(path: String, body: @escaping (String) -> Bool) -> Bool {
return indexstoredb_index_files_including_file(impl, path) { sourcePath, line in
let sourcePathStr = String(cString: sourcePath)
return body(sourcePathStr)
}
}

public func filesIncludingFile(path: String) -> [String] {
var result: [String] = []
foreachFileIncludingFile(path: path) { targetPath in
result.append(targetPath)
return true
}
return result
}

/// A recorded header `#include` from a unit file.
public struct UnitIncludeEntry: Equatable {
Expand Down
17 changes: 17 additions & 0 deletions Tests/IndexStoreDBTests/IndexTests.swift
Expand Up @@ -299,6 +299,23 @@ final class IndexTests: XCTestCase {
IndexStoreDB.UnitIncludeEntry(sourcePath: main1, targetPath: uniq1, line: ws.testLoc("include_main1_uniq1").line),
])
}

func testFilesIncludes() throws {
guard let ws = try staticTibsTestWorkspace(name: "MainFiles") else { return }
try ws.buildAndIndex()
let index = ws.index

let main1 = ws.testLoc("main1").url.path
let main2 = ws.testLoc("main2").url.path
let uniq1 = ws.testLoc("uniq1").url.path
let shared = ws.testLoc("shared").url.path

let includedFiles = index.filesIncludedByFile(path: main1)
XCTAssertEqual(includedFiles, [shared, uniq1])

let includingFiles = index.filesIncludingFile(path: shared)
XCTAssertEqual(includingFiles, [main1, main2])
}

func testAllSymbolNames() throws {
guard let ws = try staticTibsTestWorkspace(name: "proj1") else { return }
Expand Down
27 changes: 27 additions & 0 deletions include/CIndexStoreDB/CIndexStoreDB.h
Expand Up @@ -138,6 +138,9 @@ typedef void(^indexstoredb_delegate_event_receiver_t)(_Nonnull indexstoredb_dele
/// Returns true to continue.
typedef bool(^indexstoredb_unit_info_receiver)(_Nonnull indexstoredb_unit_info_t);

/// Returns true to continue.
typedef bool(^indexstoredb_file_includes_receiver)(const char *_Nonnull sourcePath, size_t line);

/// Returns true to continue.
typedef bool(^indexstoredb_unit_includes_receiver)(const char *_Nonnull sourcePath, const char *_Nonnull targetPath, size_t line);

Expand Down Expand Up @@ -385,6 +388,30 @@ indexstoredb_index_units_containing_file(
const char *_Nonnull path,
_Nonnull indexstoredb_unit_info_receiver receiver);

/// Return the file path which included by a given file path.
///
/// \param index An IndexStoreDB object which contains the symbols.
/// \param path The source file to search for.
/// \param receiver A function to be called for each include file path. The pointers are only valid for
/// the duration of the call. The function should return a true to continue iterating.
INDEXSTOREDB_PUBLIC bool
indexstoredb_index_files_included_by_file(
_Nonnull indexstoredb_index_t index,
const char *_Nonnull path,
_Nonnull indexstoredb_file_includes_receiver receiver);

/// Return the file path which including a given header.
///
/// \param index An IndexStoreDB object which contains the symbols.
/// \param path The source file to search for.
/// \param receiver A function to be called for each include file path. The pointers are only valid for
/// the duration of the call. The function should return a true to continue iterating.
INDEXSTOREDB_PUBLIC bool
indexstoredb_index_files_including_file(
_Nonnull indexstoredb_index_t index,
const char *_Nonnull path,
_Nonnull indexstoredb_file_includes_receiver receiver);

/// Iterates over recorded `#include`s of a unit.
///
/// \param index An IndexStoreDB object which contains the symbols.
Expand Down
24 changes: 24 additions & 0 deletions lib/CIndexStoreDB/CIndexStoreDB.cpp
Expand Up @@ -431,6 +431,30 @@ indexstoredb_index_units_containing_file(
});
}

bool
indexstoredb_index_files_included_by_file(
indexstoredb_index_t index,
const char *path,
indexstoredb_file_includes_receiver receiver)
{
auto obj = (Object<std::shared_ptr<IndexSystem>> *)index;
return obj->value->foreachFileIncludedByFile(path, [&](const CanonicalFilePathRef &sourcePath, unsigned line) -> bool {
return receiver(sourcePath.getPath().str().c_str(), line);
});
}

bool
indexstoredb_index_files_including_file(
indexstoredb_index_t index,
const char *path,
indexstoredb_file_includes_receiver receiver)
{
auto obj = (Object<std::shared_ptr<IndexSystem>> *)index;
return obj->value->foreachFileIncludingFile(path, [&](const CanonicalFilePathRef &sourcePath, unsigned line) -> bool {
return receiver(sourcePath.getPath().str().c_str(), line);
});
}

bool
indexstoredb_index_includes_of_unit(
indexstoredb_index_t index,
Expand Down