Remove uses of StringLiteral in format strings. (#4416)

Building on #4411, avoid using StringLiteral in format strings. This
includes a diagnostic check to prevent regressions (which is also how I
gathered issues).

Note, I haven't looked at `std::string` uses yet, but we might need
things like that to be able to pass strings in code back to the user.
StringLiteral though means that it's literally written down in the
toolchain, at which point it should probably be written in the format
string instead of separately.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Jon Ross-Perkins
2024-10-21 19:39:53 +00:00
committed by GitHub
co-authored by Geoff Romer
parent 62c36eceda
commit 302aa1bb30
11 changed files with 87 additions and 66 deletions
+10 -10
View File
@@ -23,6 +23,7 @@
#include "toolchain/check/sem_ir_diagnostic_converter.h"
#include "toolchain/diagnostics/diagnostic.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/diagnostics/format_providers.h"
#include "toolchain/lex/token_kind.h"
#include "toolchain/parse/node_ids.h"
#include "toolchain/parse/tree.h"
@@ -1189,19 +1190,18 @@ static auto BuildApiMapAndDiagnosePackaging(
bool is_api_with_impl_ext = !is_impl && filename.ends_with(ImplExt);
auto want_ext = is_impl ? ImplExt : ApiExt;
if (is_api_with_impl_ext || !filename.ends_with(want_ext)) {
CARBON_DIAGNOSTIC(IncorrectExtension, Error,
"file extension of `{0}` required for `{1}`",
llvm::StringLiteral, Lex::TokenKind);
CARBON_DIAGNOSTIC(
IncorrectExtension, Error,
"file extension of `{0:.impl|}.carbon` required for {0:`impl`|api}",
BoolAsSelect);
auto diag = unit_info.emitter.Build(
packaging ? packaging->names.node_id : Parse::NodeId::Invalid,
IncorrectExtension, want_ext,
is_impl ? Lex::TokenKind::Impl : Lex::TokenKind::Api);
IncorrectExtension, is_impl);
if (is_api_with_impl_ext) {
CARBON_DIAGNOSTIC(IncorrectExtensionImplNote, Note,
"file extension of `{0}` only allowed for `{1}`",
llvm::StringLiteral, Lex::TokenKind);
diag.Note(Parse::NodeId::Invalid, IncorrectExtensionImplNote, ImplExt,
Lex::TokenKind::Impl);
CARBON_DIAGNOSTIC(
IncorrectExtensionImplNote, Note,
"file extension of `.impl.carbon` only allowed for `impl`");
diag.Note(Parse::NodeId::Invalid, IncorrectExtensionImplNote);
}
diag.Emit();
}