This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add initial assert definition
ClosedPublic

Authored by abrachet on Feb 29 2020, 11:12 PM.

Details

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).

Diff Detail

Event Timeline

abrachet created this revision.Feb 29 2020, 11:12 PM
abrachet marked an inline comment as done.Feb 29 2020, 11:24 PM
abrachet added inline comments.
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?

sivachandra marked an inline comment as done.Mar 1 2020, 10:51 PM
sivachandra added inline comments.
libc/src/assert/__assert_fail.cpp
2

Ah, scrub this. I missed the message in the summary.

sivachandra added inline comments.Mar 4 2020, 11:34 AM
libc/config/linux/api.td
62

I think extern C for c++ is missing here.

69

Why not the assert macro?

72

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.

abrachet updated this revision to Diff 249035.EditedMar 9 2020, 12:05 AM
  • Prefer unsigned over size_t
  • Remove __assert_fail from stdc.td
  • Add comment to __assert_fail.cpp
abrachet marked 9 inline comments as done.Mar 9 2020, 12:08 AM
abrachet added inline comments.
libc/config/linux/api.td
62

I'll add just commit this outside of this patch to not mess with the diff.

69

assert shouldn't be inside the include guards of assert.h so I think it needs to be handwritten in assert.h.def

72

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
sivachandra added inline comments.Mar 9 2020, 11:05 AM
libc/include/assert.h.def
15

What do you think about moving %%public_api outside of the header guards?
Leave the __assert_fail definition within the header guards but define static_assert with an undef like:

#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?

abrachet updated this revision to Diff 249278.Mar 9 2020, 10:42 PM
abrachet marked 3 inline comments as done.

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.

sivachandra accepted this revision.Mar 11 2020, 7:17 AM
This revision is now accepted and ready to land.Mar 11 2020, 7:17 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2020, 9:07 PM