Skip to content

Mark the closures to the forEach functions as non-escaping #198

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

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 71 additions & 45 deletions Sources/IndexStoreDB/IndexStoreDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ public final class IndexStoreDB {
/// Stop iteration if `body` returns `false`.
/// - Returns: `false` if iteration was terminated by `body` returning `true` or `true` if iteration finished.
@discardableResult
public func forEachSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
return indexstoredb_index_symbol_occurrences_by_usr(impl, usr, roles.rawValue) { occur in
return body(SymbolOccurrence(occur))
public func forEachSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: (SymbolOccurrence) -> Bool) -> Bool {
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_symbol_occurrences_by_usr(impl, usr, roles.rawValue) { occur in
return body(SymbolOccurrence(occur))
}
}
}

Expand All @@ -176,9 +178,12 @@ public final class IndexStoreDB {
}

@discardableResult
public func forEachRelatedSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
return indexstoredb_index_related_symbol_occurrences_by_usr(impl, usr, roles.rawValue) { occur in
return body(SymbolOccurrence(occur))
public func forEachRelatedSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: (SymbolOccurrence) -> Bool) -> Bool {
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_related_symbol_occurrences_by_usr(impl, usr, roles.rawValue) {
occur in
return body(SymbolOccurrence(occur))
}
}
}

Expand All @@ -191,9 +196,11 @@ public final class IndexStoreDB {
return result
}

@discardableResult public func forEachCanonicalSymbolOccurrence(byName: String, body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
return indexstoredb_index_canonical_symbol_occurences_by_name(impl, byName) { occur in
return body(SymbolOccurrence(occur))
@discardableResult public func forEachCanonicalSymbolOccurrence(byName: String, body: (SymbolOccurrence) -> Bool) -> Bool {
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_canonical_symbol_occurences_by_name(impl, byName) { occur in
return body(SymbolOccurrence(occur))
}
}
}

Expand All @@ -212,17 +219,19 @@ public final class IndexStoreDB {
anchorEnd: Bool,
subsequence: Bool,
ignoreCase: Bool,
body: @escaping (SymbolOccurrence) -> Bool
body: (SymbolOccurrence) -> Bool
) -> Bool {
return indexstoredb_index_canonical_symbol_occurences_containing_pattern(
impl,
pattern,
anchorStart,
anchorEnd,
subsequence,
ignoreCase
) { occur in
body(SymbolOccurrence(occur))
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_canonical_symbol_occurences_containing_pattern(
impl,
pattern,
anchorStart,
anchorEnd,
subsequence,
ignoreCase
) { occur in
body(SymbolOccurrence(occur))
}
}
}

Expand All @@ -248,15 +257,19 @@ public final class IndexStoreDB {
}

@discardableResult
public func forEachMainFileContainingFile(path: String, crossLanguage: Bool, body: @escaping (String) -> Bool) -> Bool {
public func forEachMainFileContainingFile(
path: String, crossLanguage: Bool, body: (String) -> Bool
) -> Bool {
let fromSwift = path.hasSuffix(".swift")
return indexstoredb_index_units_containing_file(impl, path) { unit in
let mainFileStr = String(cString: indexstoredb_unit_info_main_file_path(unit))
let toSwift = mainFileStr.hasSuffix(".swift")
if !crossLanguage && fromSwift != toSwift {
return true // continue
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_units_containing_file(impl, path) { unit in
let mainFileStr = String(cString: indexstoredb_unit_info_main_file_path(unit))
let toSwift = mainFileStr.hasSuffix(".swift")
if !crossLanguage && fromSwift != toSwift {
return true // continue
}
return body(mainFileStr)
}
return body(mainFileStr)
}
}

Expand All @@ -270,10 +283,12 @@ public final class IndexStoreDB {
}

@discardableResult
public func forEachUnitNameContainingFile(path: String, body: @escaping (String) -> Bool) -> Bool {
return indexstoredb_index_units_containing_file(impl, path) { unit in
let unitName = String(cString: indexstoredb_unit_info_unit_name(unit))
return body(unitName)
public func forEachUnitNameContainingFile(path: String, body: (String) -> Bool) -> Bool {
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_units_containing_file(impl, path) { unit in
let unitName = String(cString: indexstoredb_unit_info_unit_name(unit))
return body(unitName)
}
}
}

Expand Down Expand Up @@ -322,10 +337,12 @@ public final class IndexStoreDB {
}

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

Expand All @@ -339,10 +356,12 @@ public final class IndexStoreDB {
}

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

Expand Down Expand Up @@ -373,11 +392,14 @@ public final class IndexStoreDB {

/// Iterates over recorded `#include`s of a unit.
@discardableResult
public func forEachIncludeOfUnit(unitName: String, body: @escaping (UnitIncludeEntry) -> Bool) -> Bool {
return indexstoredb_index_includes_of_unit(impl, unitName) { sourcePath, targetPath, line in
let sourcePathStr = String(cString: sourcePath)
let targetPathStr = String(cString: targetPath)
return body(UnitIncludeEntry(sourcePath: sourcePathStr, targetPath: targetPathStr, line: line))
public func forEachIncludeOfUnit(unitName: String, body: (UnitIncludeEntry) -> Bool) -> Bool {
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_includes_of_unit(impl, unitName) { sourcePath, targetPath, line in
let sourcePathStr = String(cString: sourcePath)
let targetPathStr = String(cString: targetPath)
return body(
UnitIncludeEntry(sourcePath: sourcePathStr, targetPath: targetPathStr, line: line))
}
}
}

Expand All @@ -396,9 +418,11 @@ public final class IndexStoreDB {
/// - Parameter body: A closure to be called for each symbol. The closure should return true to
/// continue iterating.
@discardableResult
public func forEachSymbolName(body: @escaping (String) -> Bool) -> Bool {
return indexstoredb_index_symbol_names(impl) { name in
body(String(cString: name))
public func forEachSymbolName(body: (String) -> Bool) -> Bool {
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_symbol_names(impl) { name in
body(String(cString: name))
}
}
}

Expand All @@ -422,9 +446,11 @@ public final class IndexStoreDB {
}

@discardableResult
func forEachSymbol(inFilePath filePath: String, body: @escaping (Symbol) -> Bool) -> Bool {
return indexstoredb_index_symbols_contained_in_file_path(impl, filePath) { symbol in
return body(Symbol(symbol))
func forEachSymbol(inFilePath filePath: String, body: (Symbol) -> Bool) -> Bool {
return withoutActuallyEscaping(body) { body in
return indexstoredb_index_symbols_contained_in_file_path(impl, filePath) { symbol in
return body(Symbol(symbol))
}
}
}

Expand Down