mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
SwiftCompilerSources: forward assertion and precondition failures to the assertion-handling in the C++ code base.
This commit is contained in:
@@ -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!)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user