Skip to content

Commit acf296d

Browse files
authored
Provide an environment flag to disable error backtrace capturing. (#812)
In performance-sensitive environments that throw many errors, the cost of capturing a backtrace for each one may be prohibitive. This PR allows disabling backtrace capture in environments where this is a problem. Resolves #791. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 699ec22 commit acf296d

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Sources/Testing/SourceAttribution/Backtrace.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,25 @@ extension Backtrace {
353353
/// This value is named oddly so that it shows up clearly in symbolicated
354354
/// backtraces.
355355
private static let __SWIFT_TESTING_IS_CAPTURING_A_BACKTRACE_FOR_A_THROWN_ERROR__: Void = {
356-
_ = isFoundationCaptureEnabled
357-
358-
_oldWillThrowHandler.withLock { oldWillThrowHandler in
359-
oldWillThrowHandler = swt_setWillThrowHandler { errorAddress in
360-
let backtrace = Backtrace.current()
361-
_willThrow(errorAddress, from: backtrace)
362-
}
356+
#if SWT_TARGET_OS_APPLE && !SWT_NO_DYNAMIC_LINKING
357+
if Environment.flag(named: "SWT_FOUNDATION_ERROR_BACKTRACING_ENABLED") != false {
358+
_ = isFoundationCaptureEnabled
363359
}
364-
if #available(_typedThrowsAPI, *) {
365-
_oldWillThrowTypedHandler.withLock { oldWillThrowTypedHandler in
366-
oldWillThrowTypedHandler = swt_setWillThrowTypedHandler { errorAddress, errorType, errorConformance in
360+
#endif
361+
362+
if Environment.flag(named: "SWT_SWIFT_ERROR_BACKTRACING_ENABLED") != false {
363+
_oldWillThrowHandler.withLock { oldWillThrowHandler in
364+
oldWillThrowHandler = swt_setWillThrowHandler { errorAddress in
367365
let backtrace = Backtrace.current()
368-
_willThrowTyped(errorAddress, errorType, errorConformance, from: backtrace)
366+
_willThrow(errorAddress, from: backtrace)
367+
}
368+
}
369+
if #available(_typedThrowsAPI, *) {
370+
_oldWillThrowTypedHandler.withLock { oldWillThrowTypedHandler in
371+
oldWillThrowTypedHandler = swt_setWillThrowTypedHandler { errorAddress, errorType, errorConformance in
372+
let backtrace = Backtrace.current()
373+
_willThrowTyped(errorAddress, errorType, errorConformance, from: backtrace)
374+
}
369375
}
370376
}
371377
}

0 commit comments

Comments
 (0)