This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add Printf FormatSection Matcher
ClosedPublic

Authored by michaelrj on Apr 20 2022, 2:41 PM.

Details

Summary

This patch changes the printf parser tests to use a more robust matcher.
This allows for better debugging of parsing issues. This does not affect
the actual printf code at all, only the tests.

Diff Detail

Event Timeline

michaelrj created this revision.Apr 20 2022, 2:41 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 20 2022, 2:41 PM
michaelrj requested review of this revision.Apr 20 2022, 2:41 PM
lntue accepted this revision.Apr 20 2022, 3:21 PM
lntue added inline comments.
libc/utils/UnitTest/PrintfMatcher.cpp
72

You might want to wrap the template int_to_hex and display function inside an anonymous namespace.

libc/utils/UnitTest/PrintfMatcher.h
39

Since *form* is also an actual word, it might be better to change these macros to EXPECT/ASSERT_FORMAT_EQ

This revision is now accepted and ready to land.Apr 20 2022, 3:21 PM
michaelrj updated this revision to Diff 424052.Apr 20 2022, 3:52 PM
michaelrj marked 2 inline comments as done.

address comments

sivachandra added inline comments.Apr 20 2022, 10:36 PM
libc/utils/UnitTest/PrintfMatcher.cpp
18

Instead of all the comparisons in this function, can you define operator== on FormatSection?

25

We can use cpp::StringView to perform this comparison.

53

We have this: https://github.com/llvm/llvm-project/blob/main/libc/utils/UnitTest/FPMatcher.cpp#L23

You can move it to a common place may be? You can use std::string in this file.

67

Wrap this in a do-while?

118

"Expected format section: "

121

"Actual format section: "

michaelrj updated this revision to Diff 424305.Apr 21 2022, 3:10 PM
michaelrj marked 6 inline comments as done.

add a util file for int_to_hex and address other comments

libc/utils/UnitTest/PrintfMatcher.cpp
18

I think it's better for final code size if FormatSection doesn't have the comparison operator since it's never used in actual printf code.

25

we could create StringViews for these, but I don't see a reason to, since this is closer to the way these objects are actually used.

53

I've done that, does the bazel also need an update?

sivachandra added inline comments.Apr 21 2022, 3:53 PM
libc/utils/UnitTest/PrintfMatcher.cpp
18

Unused inline functions/methods will not contribute to code size. Also, applications which care about code size will/should use LTO which will kick out all unused functions. You can add a comment along side operator== to make it clear that it is used only for testing.

25

It could be used differently elsewhere but you are comparing two string views here. So, the following is much better and avoids duplication of code:

if (cpp::StringView(expected.raw_string, expected.raw_len) != cpp::StringView(expected.raw_string, expected.raw_len))
  return false;

It also makes the conditional on line 23 simpler:

if (expected.has_conv != actual.has_conv)
  return false;
michaelrj updated this revision to Diff 424332.Apr 21 2022, 5:06 PM
michaelrj marked 2 inline comments as done.

address comments

libc/utils/UnitTest/PrintfMatcher.cpp
18

alright, in that case I'll just do it.

25

Ah, I thought StringView was more complicated than that. I've done that.

lntue added inline comments.Apr 21 2022, 8:58 PM
libc/src/stdio/printf_core/core_structs.h
55

FormatSection is quite big, you should use const-ref:

bool operator==(const FormatSection &other) const { ... }
sivachandra accepted this revision.Apr 21 2022, 9:49 PM

OK after addressing @lntue's comment.

sivachandra added inline comments.Apr 21 2022, 9:55 PM
libc/utils/UnitTest/CMakeLists.txt
20

Ah, if you add this with add_header_library, you will not require the empty cpp file.

michaelrj updated this revision to Diff 424598.Apr 22 2022, 1:58 PM
michaelrj marked 2 inline comments as done.

address comments

michaelrj updated this revision to Diff 424603.Apr 22 2022, 2:13 PM

final clean up before land

This revision was landed with ongoing or failed builds.Apr 22 2022, 2:21 PM
This revision was automatically updated to reflect the committed changes.