This is an archive of the discontinued LLVM Phabricator instance.

[Support] Proposed alternative to llvm::format()
ClosedPublic

Authored by kledzik on Sep 17 2014, 7:34 PM.

Details

Summary

In using llvm::format() to dump columnar tables of data in my recent enhancements to llvm-objdump, I ran into some issues uncovered by the built bots that show how unsafe llvm::format() is. The issues I hit where that the compiler did not check that %x match the integer size and that %s can match a StringRef but produce the wrong output. To use a StringRef with format() is also ugly because you have to convert to a std::string then call c_str().

All I really needed was a way to generate fixed with hexadecimal (e.g. 0x00001234) and fixed with strings (e.g. right or left justified strings in a column). I whipped up an alternative that is easy to use with the stream operator and generates fixed with output.

Diff Detail

Event Timeline

kledzik updated this revision to Diff 13817.Sep 17 2014, 7:34 PM
kledzik retitled this revision from to [PATCH] Proposed alternative to llvm::format().
kledzik updated this object.
kledzik edited the test plan for this revision. (Show Details)
kledzik added reviewers: rafael, silvas.
kledzik set the repository for this revision to rL LLVM.
kledzik added a subscriber: Unknown Object (MLST).
silvas edited edge metadata.Sep 17 2014, 8:14 PM

This is the approach I've always wanted ever since I experienced YAMLIO's
Hex* classes. I can never remember the right printf format specifiers for
width and stuff (not to mention the iostream manipulators), and the PRI*
macros are ugly and error-prone.

Also you're not the only one running into these issues: llvm::format, such
as r185766 and PR20715. I also seem to remember one more, but I can't find
it.

The argument could be made that we should improve clang's printf checking
to handle things like the snprintf in the function template like what
llvm::format uses, but I think that's orthogonal.

  • Sean Silva
rafael edited edge metadata.Sep 18 2014, 12:48 PM

This looks really nice!

kledzik updated this revision to Diff 13886.Sep 19 2014, 1:35 PM
kledzik retitled this revision from [PATCH] Proposed alternative to llvm::format() to [Support] Proposed alternative to llvm::format().
kledzik updated this object.
kledzik edited edge metadata.

Add doxygen comments and unit tests.

rnk accepted this revision.Sep 25 2014, 12:43 PM
rnk added a reviewer: rnk.
rnk added a subscriber: rnk.

I also like this, and I think that's enough +1 power to go ahead and land it.

This revision is now accepted and ready to land.Sep 25 2014, 12:43 PM
kledzik closed this revision.Sep 25 2014, 1:41 PM

committed in r218463