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).
Details
Diff Detail
Event Timeline
libc/test/src/assert/assert_test.cpp | ||
---|---|---|
17 | Should we close standard streams in death tests so it doesn't print to the terminal? |
Did not do a proper review. Left couple of very high level comments.
libc/src/assert/__assert_fail.cpp | ||
---|---|---|
2 | Looks like this is a linux only solution so should sit in a linux specific area. Or, is it because it is a temporary solution, and you envision that the final implementation would use stderr and make it platform independent? | |
20 | Can you add to the comment how the final solution is going to look like? |
libc/src/assert/__assert_fail.cpp | ||
---|---|---|
2 | Ah, scrub this. I missed the message in the summary. |
libc/config/linux/api.td | ||
---|---|---|
38 | I think extern C for c++ is missing here. | |
45 | Why not the assert macro? | |
48 | Instead, we should define the assert macro as: #ifdef __cplusplus extern C #endif void __assert_fail(...); // Don't use size_t for arg types #define assert ... #endif | |
libc/spec/stdc.td | ||
29 | This is implementation detail so should not be listed here. |
- Prefer unsigned over size_t
- Remove __assert_fail from stdc.td
- Add comment to __assert_fail.cpp
libc/config/linux/api.td | ||
---|---|---|
38 | I'll add just commit this outside of this patch to not mess with the diff. | |
45 | assert shouldn't be inside the include guards of assert.h so I think it needs to be handwritten in assert.h.def | |
48 | Same as above. For reference the generated header looks like //===---------------- C standard library header assert.h ------------------===// // // 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 // //===----------------------------------------------------------------------===// #ifndef LLVM_LIBC_ASSERT_H #define LLVM_LIBC_ASSERT_H #include <__llvm-libc-common.h> #ifndef __cplusplus #define static_assert _Static_assert #endif __BEGIN_C_DECLS __END_C_DECLS #ifdef __cplusplus extern "C" #endif _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *); #endif // LLVM_LIBC_ASSERT_H // This file may be usefully included multiple times to change assert()'s // definition based on NDEBUG. #undef assert #ifdef NDEBUG #define assert(e) (void)0 #else #define assert(e) \ ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__)) #endif |
libc/include/assert.h.def | ||
---|---|---|
14 | What do you think about moving %%public_api outside of the header guards? #ifndef __cplusplus #undef static_assert #define static_assert _Static_assert #endif Then, we don't have to put the assert macro definition in the .h.def file? |
Put %%public_api() outside of include guards and put assert's definition in config/linux.api.td.
Close standard error in assert death test so it doesn't print error message.
I think extern C for c++ is missing here.