SwiftCompilerSources: forward assertion and precondition failures to the assertion-handling in the C++ code base.

This commit is contained in:
Erik Eckstein
2024-06-25 14:14:43 +02:00
parent 718ea4b018
commit 81005c18a2
3 changed files with 31 additions and 6 deletions

View File

@@ -21,20 +21,36 @@
/// case for `precondition`.
@_transparent
public func assert(_ condition: Bool, _ message: @autoclosure () -> String,
file: StaticString = #fileID, line: UInt = #line) {
if !condition {
fatalError(message(), file: file, line: line)
}
file: StaticString = #fileID, line: UInt = #line, function: StaticString = #function) {
precondition(condition, message(), file: file, line: line, function: function)
}
/// The assert function (without a message) to be used in the compiler.
///
/// Unforuntately it's not possible to just add a default argument to `message` in the
/// other `assert` function. We need to defined this overload.
/// TODO: For some reason the compiler is not happy when adding a `function` argument.
@_transparent
public func assert(_ condition: Bool, file: StaticString = #fileID, line: UInt = #line) {
if !condition {
fatalError("", file: file, line: line)
precondition(condition, "", file: file, line: line, function: "")
}
/// The assert function to be used in the compiler.
///
/// This overrides the standard Swift precondition and forwards an assertion failure
/// to the assertion-handling in the C++ code base.
@_transparent
public func precondition(_ condition: Bool, _ message: @autoclosure () -> String,
file: StaticString = #fileID, line: UInt = #line, function: StaticString = #function) {
if !_fastPath(condition) {
let msg = message()
msg.withCString { msgStr in
file.withUTF8Buffer { fileBytes in
function.withUTF8Buffer { functionBytes in
assertFail(msgStr, fileBytes.baseAddress!, line, functionBytes.baseAddress!)
}
}
}
}
}