Printing support enables the production of more useful error messages in unit testing e.g. when using matchers such as UnorderedElementsAre() to inspect the contents of a StringMap.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Check if << operator is overloaded for value type, return a default if << is unimplemented.
Please clarify in the CL description that the more useful error messages will appear in unit test failures, for example when UnorderedElementsAre() is used with StringMap.
llvm/include/llvm/ADT/StringMap.h | ||
---|---|---|
502 ↗ | (On Diff #455963) | |
llvm/include/llvm/ADT/StringMapEntry.h | ||
150 ↗ | (On Diff #455963) | WDYT about "CanOutputToOstream"? |
150 ↗ | (On Diff #455963) | Similar to hiding implementation details in DenseMap.h and in other headers, I'd suggest to put this type trait inside namespace detail {}. |
163 ↗ | (On Diff #455963) | |
166 ↗ | (On Diff #455963) | I wonder if we should put these operators into llvm/Testing/ADT, since these operators are only supposed to be used from tests. For production code, LLVM strongly prefers llvm::raw_ostream, a much more light-weight type than std::ostream. In fact, this is the first operator<<(std::ostream&, <anything>) overload in llvm/ADT. Printable types (like APInt) are printable into raw_ostream. Another option is to provide operator<<(llvm::raw_ostream&) in llvm/ADT, and wrap them in operator<<(std::ostream&) in llvm/Testing/ADT. The llvm/ADT version won't have the SFINAE magic, but the llvm/Testing/ADT one will. WDYT? |
llvm/include/llvm/ADT/StringMapEntry.h | ||
---|---|---|
166 ↗ | (On Diff #455963) | Moved the printing into Testing/ADT. I also attempted the second approach but it wasn't very successful. Using <<(llvm::raw_ostream) from <<(std::ostream) meant that <<(llvm::raw_ostream) needed to be implemented for any structure we wanted to print. However for use cases related to google test, it is more likely that users implement the <<(std::ostream) operator and not the <<(llvm::raw_ostream). |
llvm/include/llvm/ADT/StringMapEntry.h | ||
---|---|---|
166 ↗ | (On Diff #455963) |
What I meant is that when implementing <<(std::ostream) you can call <<(llvm::raw_ostream) printing into a string, and then print the string into the std::ostream. But that's not necessary, what you have now also works. |
llvm/include/llvm/Testing/ADT/StringMap.h | ||
---|---|---|
27 | Is there a standard representation for empty maps and, if so, is this it? |
llvm/include/llvm/Testing/ADT/StringMap.h | ||
---|---|---|
27 | I'm not aware of one, imo { } seemed pretty representative of maps. |
Minor fixes:
- Update sfinea to check on ostream& and use decltype to get type of the input to be streamed
- Fix build target dependency
llvm/include/llvm/Testing/ADT/StringMap.h | ||
---|---|---|
27 | do we need the special case for empty, or can it flow out of the general case naturally? |
llvm/include/llvm/Testing/ADT/StringMap.h | ||
---|---|---|
27 | The special case keeps the braces on the same line for readability, otherwise they would be on separate lines. |
Is there a standard representation for empty maps and, if so, is this it?