Change TokenKind's Print overload to a format_provider. (#2534)

Fundamentally this `.Print()` is wrong for debug output at present because `.fixed_spelling()` can be empty. It's also inconsistent with other enums to use it. We frequently print tokens for debugging, and it's easy to forget to specify `.name()` there.

Diagnostics use formatv, so we can provide a format_provider and address it in one spot that way. It also makes it harder to just forget to do the right thing.
This commit is contained in:
Jon Ross-Perkins
2023-01-18 12:20:56 -08:00
committed by GitHub
parent b936bf9e04
commit 94872ef6da
9 changed files with 36 additions and 37 deletions
+8 -14
View File
@@ -652,30 +652,26 @@ auto TokenizedBuffer::GetTokenText(Token token) const -> llvm::StringRef {
return llvm::StringRef();
}
CARBON_CHECK(token_info.kind == TokenKind::Identifier)
<< token_info.kind.name();
CARBON_CHECK(token_info.kind == TokenKind::Identifier) << token_info.kind;
return GetIdentifierText(token_info.id);
}
auto TokenizedBuffer::GetIdentifier(Token token) const -> Identifier {
const auto& token_info = GetTokenInfo(token);
CARBON_CHECK(token_info.kind == TokenKind::Identifier)
<< token_info.kind.name();
CARBON_CHECK(token_info.kind == TokenKind::Identifier) << token_info.kind;
return token_info.id;
}
auto TokenizedBuffer::GetIntegerLiteral(Token token) const
-> const llvm::APInt& {
const auto& token_info = GetTokenInfo(token);
CARBON_CHECK(token_info.kind == TokenKind::IntegerLiteral)
<< token_info.kind.name();
CARBON_CHECK(token_info.kind == TokenKind::IntegerLiteral) << token_info.kind;
return literal_int_storage_[token_info.literal_index];
}
auto TokenizedBuffer::GetRealLiteral(Token token) const -> RealLiteralValue {
const auto& token_info = GetTokenInfo(token);
CARBON_CHECK(token_info.kind == TokenKind::RealLiteral)
<< token_info.kind.name();
CARBON_CHECK(token_info.kind == TokenKind::RealLiteral) << token_info.kind;
// Note that every real literal is at least three characters long, so we can
// safely look at the second character to determine whether we have a
@@ -690,16 +686,14 @@ auto TokenizedBuffer::GetRealLiteral(Token token) const -> RealLiteralValue {
auto TokenizedBuffer::GetStringLiteral(Token token) const -> llvm::StringRef {
const auto& token_info = GetTokenInfo(token);
CARBON_CHECK(token_info.kind == TokenKind::StringLiteral)
<< token_info.kind.name();
CARBON_CHECK(token_info.kind == TokenKind::StringLiteral) << token_info.kind;
return literal_string_storage_[token_info.literal_index];
}
auto TokenizedBuffer::GetTypeLiteralSize(Token token) const
-> const llvm::APInt& {
const auto& token_info = GetTokenInfo(token);
CARBON_CHECK(token_info.kind.is_sized_type_literal())
<< token_info.kind.name();
CARBON_CHECK(token_info.kind.is_sized_type_literal()) << token_info.kind;
return literal_int_storage_[token_info.literal_index];
}
@@ -707,7 +701,7 @@ auto TokenizedBuffer::GetMatchedClosingToken(Token opening_token) const
-> Token {
const auto& opening_token_info = GetTokenInfo(opening_token);
CARBON_CHECK(opening_token_info.kind.is_opening_symbol())
<< opening_token_info.kind.name();
<< opening_token_info.kind;
return opening_token_info.closing_token;
}
@@ -715,7 +709,7 @@ auto TokenizedBuffer::GetMatchedOpeningToken(Token closing_token) const
-> Token {
const auto& closing_token_info = GetTokenInfo(closing_token);
CARBON_CHECK(closing_token_info.kind.is_closing_symbol())
<< closing_token_info.kind.name();
<< closing_token_info.kind;
return closing_token_info.opening_token;
}