This is an archive of the discontinued LLVM Phabricator instance.

Allow VersionPrinter to print to arbitrary raw_ostreams
ClosedPublic

Authored by dim on Jun 5 2017, 9:48 AM.

Details

Summary

I would like to add printing of registered targets to clang's version
information. For this to work correctly, the VersionPrinter logic in
CommandLine.cpp should support printing to arbitrary raw_ostreams,
instead of always defaulting to outs().

Add a raw_ostream& parameter to the function pointer type used for
VersionPrinter, and while doing so, introduce a typedef for convenience.

Note that VersionPrinter::print() will still default to using outs(),
the clang part will necessarily go into a separate review.

Event Timeline

dim created this revision.Jun 5 2017, 9:48 AM
mehdi_amini added inline comments.Jun 5 2017, 11:38 AM
include/llvm/Support/CommandLine.h
69

Using a std::function (or maybe llvm::function_ref) would be allowing for more rich object to be passed in (i.e. a client that may want state to be passed around).

dim added inline comments.Jun 6 2017, 12:21 PM
include/llvm/Support/CommandLine.h
69

Maybe, but how would VersionPrinter::print or VersionPrinter::operator= know how to call that function object, and which parameters to pass?

In any case it looks like llvm::function_ref also doesn't support variable function types, you'd still need to settle on function_ref<void(raw_ostream &)> as 'the' type for version printing functions. With std::function it seems to be the same.

The only advantage would maybe be the ownership of the function object, but I'm not sure if that is a big deal breaker in this case. It's just for printing a version field. :)

dim added a comment.Jun 6 2017, 12:42 PM

Alternatively, as a lower impact change, I could just add the code from printRegisteredTargetsForVersion to tools/clang/lib/Driver/Driver.cpp, but it's a little ugly...

mehdi_amini added inline comments.Jun 6 2017, 12:52 PM
include/llvm/Support/CommandLine.h
69

It is not about the function signature, it is about being able to have a callback with *state*.

i.e. what people do in C is adding a void * parameter to the callback signature and have the caller passing it in alongside the callback, C++ has std::function for this.

dim added inline comments.Jun 6 2017, 1:25 PM
include/llvm/Support/CommandLine.h
69

Sure, but in this case the callback signature has a raw_ostream &. Did you mean you wanted the following:

typedef void (*VersionPrintTy)(std::function<whatever> &);

? I'm not sure how that would be handy, though.

mehdi_amini added inline comments.Jun 6 2017, 1:29 PM
include/llvm/Support/CommandLine.h
69

No I meant something like:

typedef std::function<void(raw_ostream &>)> VersionPrintTy;
dim updated this revision to Diff 101622.Jun 6 2017, 2:14 PM

Use a std::function object instead of a plain function pointer.

mehdi_amini accepted this revision.Jun 6 2017, 2:22 PM

LGTM, thanks.

This revision is now accepted and ready to land.Jun 6 2017, 2:22 PM
dim closed this revision.Jun 6 2017, 2:54 PM