Skip to content

Commit b429f79

Browse files
authored
Merge pull request #198 from ahoppen/foreach-not-escaping
Mark the closures to the `forEach` functions as non-escaping
2 parents a3dee5f + 4bde31e commit b429f79

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ public final class IndexStoreDB {
159159
/// Stop iteration if `body` returns `false`.
160160
/// - Returns: `false` if iteration was terminated by `body` returning `true` or `true` if iteration finished.
161161
@discardableResult
162-
public func forEachSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
163-
return indexstoredb_index_symbol_occurrences_by_usr(impl, usr, roles.rawValue) { occur in
164-
return body(SymbolOccurrence(occur))
162+
public func forEachSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: (SymbolOccurrence) -> Bool) -> Bool {
163+
return withoutActuallyEscaping(body) { body in
164+
return indexstoredb_index_symbol_occurrences_by_usr(impl, usr, roles.rawValue) { occur in
165+
return body(SymbolOccurrence(occur))
166+
}
165167
}
166168
}
167169

@@ -176,9 +178,12 @@ public final class IndexStoreDB {
176178
}
177179

178180
@discardableResult
179-
public func forEachRelatedSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
180-
return indexstoredb_index_related_symbol_occurrences_by_usr(impl, usr, roles.rawValue) { occur in
181-
return body(SymbolOccurrence(occur))
181+
public func forEachRelatedSymbolOccurrence(byUSR usr: String, roles: SymbolRole, _ body: (SymbolOccurrence) -> Bool) -> Bool {
182+
return withoutActuallyEscaping(body) { body in
183+
return indexstoredb_index_related_symbol_occurrences_by_usr(impl, usr, roles.rawValue) {
184+
occur in
185+
return body(SymbolOccurrence(occur))
186+
}
182187
}
183188
}
184189

@@ -191,9 +196,11 @@ public final class IndexStoreDB {
191196
return result
192197
}
193198

194-
@discardableResult public func forEachCanonicalSymbolOccurrence(byName: String, body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
195-
return indexstoredb_index_canonical_symbol_occurences_by_name(impl, byName) { occur in
196-
return body(SymbolOccurrence(occur))
199+
@discardableResult public func forEachCanonicalSymbolOccurrence(byName: String, body: (SymbolOccurrence) -> Bool) -> Bool {
200+
return withoutActuallyEscaping(body) { body in
201+
return indexstoredb_index_canonical_symbol_occurences_by_name(impl, byName) { occur in
202+
return body(SymbolOccurrence(occur))
203+
}
197204
}
198205
}
199206

@@ -212,17 +219,19 @@ public final class IndexStoreDB {
212219
anchorEnd: Bool,
213220
subsequence: Bool,
214221
ignoreCase: Bool,
215-
body: @escaping (SymbolOccurrence) -> Bool
222+
body: (SymbolOccurrence) -> Bool
216223
) -> Bool {
217-
return indexstoredb_index_canonical_symbol_occurences_containing_pattern(
218-
impl,
219-
pattern,
220-
anchorStart,
221-
anchorEnd,
222-
subsequence,
223-
ignoreCase
224-
) { occur in
225-
body(SymbolOccurrence(occur))
224+
return withoutActuallyEscaping(body) { body in
225+
return indexstoredb_index_canonical_symbol_occurences_containing_pattern(
226+
impl,
227+
pattern,
228+
anchorStart,
229+
anchorEnd,
230+
subsequence,
231+
ignoreCase
232+
) { occur in
233+
body(SymbolOccurrence(occur))
234+
}
226235
}
227236
}
228237

@@ -248,15 +257,19 @@ public final class IndexStoreDB {
248257
}
249258

250259
@discardableResult
251-
public func forEachMainFileContainingFile(path: String, crossLanguage: Bool, body: @escaping (String) -> Bool) -> Bool {
260+
public func forEachMainFileContainingFile(
261+
path: String, crossLanguage: Bool, body: (String) -> Bool
262+
) -> Bool {
252263
let fromSwift = path.hasSuffix(".swift")
253-
return indexstoredb_index_units_containing_file(impl, path) { unit in
254-
let mainFileStr = String(cString: indexstoredb_unit_info_main_file_path(unit))
255-
let toSwift = mainFileStr.hasSuffix(".swift")
256-
if !crossLanguage && fromSwift != toSwift {
257-
return true // continue
264+
return withoutActuallyEscaping(body) { body in
265+
return indexstoredb_index_units_containing_file(impl, path) { unit in
266+
let mainFileStr = String(cString: indexstoredb_unit_info_main_file_path(unit))
267+
let toSwift = mainFileStr.hasSuffix(".swift")
268+
if !crossLanguage && fromSwift != toSwift {
269+
return true // continue
270+
}
271+
return body(mainFileStr)
258272
}
259-
return body(mainFileStr)
260273
}
261274
}
262275

@@ -270,10 +283,12 @@ public final class IndexStoreDB {
270283
}
271284

272285
@discardableResult
273-
public func forEachUnitNameContainingFile(path: String, body: @escaping (String) -> Bool) -> Bool {
274-
return indexstoredb_index_units_containing_file(impl, path) { unit in
275-
let unitName = String(cString: indexstoredb_unit_info_unit_name(unit))
276-
return body(unitName)
286+
public func forEachUnitNameContainingFile(path: String, body: (String) -> Bool) -> Bool {
287+
return withoutActuallyEscaping(body) { body in
288+
return indexstoredb_index_units_containing_file(impl, path) { unit in
289+
let unitName = String(cString: indexstoredb_unit_info_unit_name(unit))
290+
return body(unitName)
291+
}
277292
}
278293
}
279294

@@ -322,10 +337,12 @@ public final class IndexStoreDB {
322337
}
323338

324339
@discardableResult
325-
public func foreachFileIncludedByFile(path: String, body: @escaping (String) -> Bool) -> Bool {
326-
return indexstoredb_index_files_included_by_file(impl, path) { targetPath, line in
340+
public func foreachFileIncludedByFile(path: String, body: (String) -> Bool) -> Bool {
341+
return withoutActuallyEscaping(body) { body in
342+
return indexstoredb_index_files_included_by_file(impl, path) { targetPath, line in
327343
let targetPathStr = String(cString: targetPath)
328344
return body(targetPathStr)
345+
}
329346
}
330347
}
331348

@@ -339,10 +356,12 @@ public final class IndexStoreDB {
339356
}
340357

341358
@discardableResult
342-
public func foreachFileIncludingFile(path: String, body: @escaping (String) -> Bool) -> Bool {
343-
return indexstoredb_index_files_including_file(impl, path) { sourcePath, line in
359+
public func foreachFileIncludingFile(path: String, body: (String) -> Bool) -> Bool {
360+
return withoutActuallyEscaping(body) { body in
361+
return indexstoredb_index_files_including_file(impl, path) { sourcePath, line in
344362
let sourcePathStr = String(cString: sourcePath)
345363
return body(sourcePathStr)
364+
}
346365
}
347366
}
348367

@@ -373,11 +392,14 @@ public final class IndexStoreDB {
373392

374393
/// Iterates over recorded `#include`s of a unit.
375394
@discardableResult
376-
public func forEachIncludeOfUnit(unitName: String, body: @escaping (UnitIncludeEntry) -> Bool) -> Bool {
377-
return indexstoredb_index_includes_of_unit(impl, unitName) { sourcePath, targetPath, line in
378-
let sourcePathStr = String(cString: sourcePath)
379-
let targetPathStr = String(cString: targetPath)
380-
return body(UnitIncludeEntry(sourcePath: sourcePathStr, targetPath: targetPathStr, line: line))
395+
public func forEachIncludeOfUnit(unitName: String, body: (UnitIncludeEntry) -> Bool) -> Bool {
396+
return withoutActuallyEscaping(body) { body in
397+
return indexstoredb_index_includes_of_unit(impl, unitName) { sourcePath, targetPath, line in
398+
let sourcePathStr = String(cString: sourcePath)
399+
let targetPathStr = String(cString: targetPath)
400+
return body(
401+
UnitIncludeEntry(sourcePath: sourcePathStr, targetPath: targetPathStr, line: line))
402+
}
381403
}
382404
}
383405

@@ -396,9 +418,11 @@ public final class IndexStoreDB {
396418
/// - Parameter body: A closure to be called for each symbol. The closure should return true to
397419
/// continue iterating.
398420
@discardableResult
399-
public func forEachSymbolName(body: @escaping (String) -> Bool) -> Bool {
400-
return indexstoredb_index_symbol_names(impl) { name in
401-
body(String(cString: name))
421+
public func forEachSymbolName(body: (String) -> Bool) -> Bool {
422+
return withoutActuallyEscaping(body) { body in
423+
return indexstoredb_index_symbol_names(impl) { name in
424+
body(String(cString: name))
425+
}
402426
}
403427
}
404428

@@ -422,9 +446,11 @@ public final class IndexStoreDB {
422446
}
423447

424448
@discardableResult
425-
func forEachSymbol(inFilePath filePath: String, body: @escaping (Symbol) -> Bool) -> Bool {
426-
return indexstoredb_index_symbols_contained_in_file_path(impl, filePath) { symbol in
427-
return body(Symbol(symbol))
449+
func forEachSymbol(inFilePath filePath: String, body: (Symbol) -> Bool) -> Bool {
450+
return withoutActuallyEscaping(body) { body in
451+
return indexstoredb_index_symbols_contained_in_file_path(impl, filePath) { symbol in
452+
return body(Symbol(symbol))
453+
}
428454
}
429455
}
430456

0 commit comments

Comments
 (0)