Switch formatv adapters to format_provider (#3434)

The main difference I'm aiming for is that clangd doesn't complain about
the struct being unused, but it does miss the function's use. But really
these are specific to formatv for diagnostics, so this is more clearly
marking such, and probably makes for a better pattern for the future.
This commit is contained in:
Jon Ross-Perkins
2023-11-30 18:33:03 +00:00
committed by GitHub
parent 0db63ff17a
commit 05723095bc
2 changed files with 50 additions and 34 deletions
+25 -15
View File
@@ -23,23 +23,33 @@ enum class RelativeLocation : int8_t {
Before,
};
} // namespace Carbon::Parse
namespace llvm {
// Adapts RelativeLocation for use with formatv.
// TODO: Investigate for approaches that clangd is happier with.
static auto operator<<(llvm::raw_ostream& out, RelativeLocation loc)
-> llvm::raw_ostream& {
switch (loc) {
case RelativeLocation::Around:
out << "around";
break;
case RelativeLocation::After:
out << "after";
break;
case RelativeLocation::Before:
out << "before";
break;
template <>
struct format_provider<Carbon::Parse::RelativeLocation> {
using RelativeLocation = Carbon::Parse::RelativeLocation;
static void format(const RelativeLocation& loc, raw_ostream& out,
StringRef /*style*/) {
switch (loc) {
case RelativeLocation::Around:
out << "around";
break;
case RelativeLocation::After:
out << "after";
break;
case RelativeLocation::Before:
out << "before";
break;
}
}
return out;
}
};
} // namespace llvm
namespace Carbon::Parse {
Context::Context(Tree& tree, Lex::TokenizedBuffer& tokens,
Lex::TokenDiagnosticEmitter& emitter,