This is an archive of the discontinued LLVM Phabricator instance.

[libc] Adds string and TestLogger classes, use them in LibcTest
ClosedPublic

Authored by gchatelet on Mar 30 2023, 6:15 AM.

Diff Detail

Event Timeline

gchatelet created this revision.Mar 30 2023, 6:15 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMar 30 2023, 6:15 AM
gchatelet requested review of this revision.Mar 30 2023, 6:15 AM
gchatelet updated this revision to Diff 509654.Mar 30 2023, 6:36 AM
  • Also add bazel configurations
gchatelet updated this revision to Diff 509658.Mar 30 2023, 6:58 AM
  • Simplifies TestLogger and LibCTest
gchatelet updated this revision to Diff 509684.Mar 30 2023, 8:02 AM
  • Fix clang-format
sivachandra added inline comments.Mar 30 2023, 11:15 AM
libc/src/__support/CPP/string.h
94

C++11 and on wards, it is expected that c_str and data are equivalent. Making this class exhibit older behavior can be confusing. Any reason why you have chosen to implement the old behavior?

108

Is this a good candidate for a realloc? If not, then we should follow this pattern for allocations in general in this file: https://libc.llvm.org/dev/code_style.html#allocations-in-the-libc-runtime-code.

172

May be put this in a namespace named internal?

libc/test/UnitTest/TestLogger.cpp
6

We want this to work on really minimal systems on which fwrite, fputs stdout etc are not [yet] available. So, we should use a simple function like https://github.com/llvm/llvm-project/blob/main/libc/src/__support/OSUtil/linux/io.h which does a raw write to stdout.

libc/test/UnitTest/TestLogger.h
20

If you can make this constexpr, something like this:

constexpr TestLogger() = default;

then, we don't need the function log() below. We can simply do:

extern TestLogger tlog; // Bikeshed the name here but I find the name "log" to be very generic.

Irrespective of whether we want to use a function returning a reference to a file static object or an extern non-static-global, the constructor should be made constexpr. The reason is that, global objects with a non-constexpr constructor will require runtime initialization. So, we will not be able to run tests on platforms which do not [yet] support global object initialization.

sivachandra added inline comments.Mar 30 2023, 12:32 PM
libc/src/__support/CPP/string.h
30

Forgot to mention: the code style should be snake_case for var names and function names including class/struct members.

gchatelet updated this revision to Diff 509948.Mar 31 2023, 2:37 AM
gchatelet marked 4 inline comments as done.
  • Address comments
gchatelet marked an inline comment as done.Mar 31 2023, 2:42 AM
gchatelet added inline comments.
libc/src/__support/CPP/string.h
94

I missed this requirement, thx for catching it.
I've made sure that data() and c_str() always point to the same pointer and that the string is always null terminated.

108

It is indeed a good use of realloc. FWIW I initially tried to use the suggested pattern but had a bunch of linker errors complaining about missing __llvm_libc_delete.
As a matter of fact I couldn't find the symbol in the codebase.

libc/test/UnitTest/TestLogger.cpp
6

It would be helpful to provide a version of __llvm_libc::write_to_stderr that takes the length as an argument. Would that be OK?
As is, the call is suboptimal for single char and string_view.

libc/test/UnitTest/TestLogger.h
20

thx for the great suggestion!

gchatelet updated this revision to Diff 509952.Mar 31 2023, 2:51 AM
gchatelet marked an inline comment as done.
  • Use write_to_stderr instead of fwrite, fputs, stdout
sivachandra accepted this revision.Mar 31 2023, 8:27 AM

Couple of nits but this is looking great. Thanks for working on this.

libc/src/__support/CPP/string.h
30

Nit: NULL_CHARACTER

31

Nit: get_empty_string()

108

The standard does not allow us to implement delete functions inline. So, they are implemented in new.cpp. Given the linker error, I suspect you missed adding a dep on lib.src.__support.CPP.new. I guess its not required now anyway.

libc/test/UnitTest/TestLogger.cpp
6

A lot of those utils are improved as required. So, if you think this patch will benefit, either add more args or add overloads. Totally fine.

This revision is now accepted and ready to land.Mar 31 2023, 8:27 AM
gchatelet marked 5 inline comments as done.Apr 1 2023, 2:57 AM
gchatelet added inline comments.
libc/src/__support/CPP/string.h
30

Thx, I keep on forgetting the style guide.
We should try to enforce it via linter.

libc/test/UnitTest/TestLogger.cpp
6

Will do in a separate patch.

gchatelet updated this revision to Diff 510200.Apr 1 2023, 2:58 AM
gchatelet marked 2 inline comments as done.
  • Address comments
gchatelet updated this revision to Diff 510201.Apr 1 2023, 3:01 AM
  • rename a few variables
gchatelet updated this revision to Diff 510214.Apr 1 2023, 5:10 AM
  • simplify code a bit and use more consistent naming
sivachandra added inline comments.Apr 1 2023, 8:54 AM
libc/test/UnitTest/TestLogger.cpp
12

Nit: s/String/str and s/CString/cstr here and below.

31

ch

33

Nit: *this << cpp::string_view(&ch, 1);

sivachandra accepted this revision.Apr 1 2023, 8:54 AM
gchatelet updated this revision to Diff 510340.Apr 2 2023, 5:40 AM
gchatelet marked 3 inline comments as done.
  • Address comments