Summary: This patch adds a temporary `__assert_fail` and `assert` definition to make it available to internal llvm libc code. `__assert_fail` writes to fd 2 directly instead of `stderr`, using SYS_write. I have not put it in its own linux directory because this is temporary and it should be using stdio's api in the future. It does not currently print out the line number (although we could do that by stringifying `__LINE__` if reviewers wish). Reviewers: sivachandra, gchatelet, PaulkaToast Reviewed By: sivachandra Subscribers: mgorny, MaskRay, tschuett, libc-commits Differential Revision: https://reviews.llvm.org/D75420
39 lines
1.3 KiB
C++
39 lines
1.3 KiB
C++
//===----------------- Implementation of __assert_fail --------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "src/assert/assert.h"
|
|
#include "src/stdlib/abort.h"
|
|
|
|
// These includes are temporary.
|
|
#include "config/linux/syscall.h" // For internal syscall function.
|
|
#include "include/sys/syscall.h" // For syscall numbers.
|
|
|
|
namespace __llvm_libc {
|
|
|
|
// This is just a temporary solution to make assert available to internal
|
|
// llvm libc code. In the future writeToStderr will not exist and __assert_fail
|
|
// will call fprintf(stderr, ...).
|
|
static void writeToStderr(const char *s) {
|
|
size_t length = 0;
|
|
for (const char *curr = s; *curr; ++curr, ++length);
|
|
__llvm_libc::syscall(SYS_write, 2, s, length);
|
|
}
|
|
|
|
void LLVM_LIBC_ENTRYPOINT(__assert_fail)(const char *assertion, const char *file,
|
|
unsigned line, const char *function) {
|
|
writeToStderr(file);
|
|
writeToStderr(": Assertion failed: '");
|
|
writeToStderr(assertion);
|
|
writeToStderr("' in function: '");
|
|
writeToStderr(function);
|
|
writeToStderr("'\n");
|
|
__llvm_libc::abort();
|
|
}
|
|
|
|
} // namespace __llvm_libc
|