This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add holder class for va_lists
ClosedPublic

Authored by michaelrj on Apr 4 2022, 12:11 PM.

Details

Summary

This class is intended to be used in cases where a class is being used
on a va_list. It provides destruction and copy semantics with small
overhead. This is intended to be used in printf.

Diff Detail

Event Timeline

michaelrj created this revision.Apr 4 2022, 12:11 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 4 2022, 12:11 PM
michaelrj requested review of this revision.Apr 4 2022, 12:11 PM
lntue accepted this revision.Apr 5 2022, 10:00 AM
lntue added inline comments.
libc/src/__support/vlist_holder.h
17 ↗(On Diff #420268)

Maybe VaListHolder is a bit clearer?

libc/test/src/__support/vlist_holder_test.cpp
1 ↗(On Diff #420268)

Need to be updated.

This revision is now accepted and ready to land.Apr 5 2022, 10:00 AM
sivachandra added inline comments.Apr 5 2022, 10:18 AM
libc/src/__support/vlist_holder.h
17 ↗(On Diff #420268)

I vote for something even simpler: ArgList?

libc/test/src/__support/vlist_holder_test.cpp
15 ↗(On Diff #420268)

Should the va_start and va_end be managed by the new class?

20 ↗(On Diff #420268)

Implementing the c++ iterator protocol will help will make the for-loop iteration cleaner?

michaelrj updated this revision to Diff 420578.Apr 5 2022, 10:53 AM
michaelrj marked 3 inline comments as done.

rename to ArgList and fix formatting

libc/src/__support/vlist_holder.h
17 ↗(On Diff #420268)

I went with ArgList since it's shorter.

libc/test/src/__support/vlist_holder_test.cpp
15 ↗(On Diff #420268)

The class handles its own va_end, but I don't think it can handle the va_start since its constructor is a new function and you can only create a va_list for the scope of the current function. I could probably create a macro to handle this invisibly, but that's as close as we can get, I think.

20 ↗(On Diff #420268)

I don't think an iterator works here, since the iterator needs to have a type, and the values may be of various types. In addition, there's no end marker or length in a va_list, meaning that a for loop won't know when to end.

sivachandra accepted this revision.Apr 5 2022, 11:06 AM
sivachandra added inline comments.
libc/test/src/__support/vlist_holder_test.cpp
15 ↗(On Diff #420268)

OK. I am not very confident about how va_list is implemented. For example, calling va_end in a destructor is also not OK as it is a different stack frame. OK for now.

20 ↗(On Diff #420268)

Ah, yes! Sorry for the noise.

This revision was automatically updated to reflect the committed changes.