This is an archive of the discontinued LLVM Phabricator instance.

[libc][NFC] Simplify assert message generation
ClosedPublic

Authored by michaelrj on Jul 24 2023, 2:11 PM.

Details

Summary

Previously displaying a failed assert would involve a runtime integer to
string conversion. This patch changes that to be a compile time string
conversion.

This was inspired by a comment by JonChesterfield on https://reviews.llvm.org/D155899

Diff Detail

Event Timeline

michaelrj created this revision.Jul 24 2023, 2:11 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 24 2023, 2:11 PM
michaelrj requested review of this revision.Jul 24 2023, 2:11 PM
JonChesterfield accepted this revision.Jul 24 2023, 2:44 PM
JonChesterfield added a reviewer: jhuber6.

LGTM. I'm slightly surprised __PRETTY_FUNCTION__ can't be concatenated with string literals at preprocessor time but I'm getting errors when I try it.

This might leak into user code. Is there an existing __LIBC_ prefix or similar?

Thanks for dropping the runtime int -> string conversion, always pleased to have fewer things that can go wrong at runtime.

This revision is now accepted and ready to land.Jul 24 2023, 2:44 PM

I'm also kinda confused why __PRETTY_FUNCTION__ isn't available to the preprocessor, maybe it's to do with C++ mangling semantics?

I'll look into better names for the macros, I think something like __LIBC would probably work.

Yeah, my first try involved constexpr strings that were concatenated at compile time, but then I realized that the macros are just strings themselves (or close enough with line).

lntue accepted this revision.Jul 24 2023, 5:48 PM
sivachandra accepted this revision.Jul 24 2023, 6:44 PM
sivachandra added inline comments.
libc/src/__support/libc_assert.h
27–28

With this change I think it is better to inline these calls also in the macro below. The format relation ship between error_str and funcname is decided in that macro so keep it there.

add a prefix to the macros and inline the write_to_stderr calls.

This revision was landed with ongoing or failed builds.Jul 25 2023, 11:45 AM
This revision was automatically updated to reflect the committed changes.
mcgrathr added inline comments.
libc/src/__support/libc_assert.h
57

even if it's not a pastable string literal, it should be possible to do constexpr stuff to turn this into a single string constant for a single write call