Page MenuHomePhabricator

[libc] Add initial assert definition

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



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.

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.


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?


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.

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

sivachandra added inline comments.Mar 4 2020, 11:34 AM

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


Why not the assert macro?


Instead, we should define the assert macro as:

#ifdef __cplusplus
extern C
void __assert_fail(...); // Don't use size_t for arg types
#define assert ...

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
  • Add comment to __assert_fail.cpp
abrachet marked 9 inline comments as done.Mar 9 2020, 12:08 AM
abrachet added inline comments.

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


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


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 for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception


#include <__llvm-libc-common.h>

#ifndef __cplusplus
#define static_assert _Static_assert



#ifdef __cplusplus
extern "C"
_Noreturn void __assert_fail(const char *, const char *, 
                             unsigned, const char *);


// 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
#define assert(e) \
    ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
sivachandra added inline comments.Mar 9 2020, 11:05 AM

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

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/
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