Revert "Merge type of diagnostic and its substitutions. (#351)"

This reverts commit 33fdabf462.
This commit is contained in:
Dave Abrahams
2021-03-07 18:47:38 -08:00
parent 33fdabf462
commit f61b1b18ca
2 changed files with 38 additions and 29 deletions
+15 -11
View File
@@ -37,28 +37,30 @@ class DiagnosticEmitter {
: callback_(std::move(callback)) {}
~DiagnosticEmitter() = default;
// Emits an error unconditionally.
// Emits an error unconditionally after applying the provided `substitutions`.
template <typename DiagnosticT>
auto EmitError(DiagnosticT diag) -> void {
void EmitError(typename DiagnosticT::Substitutions substitutions) {
callback_({.short_name = DiagnosticT::ShortName,
.message = diag.Format()});
.message = DiagnosticT::Format(substitutions)});
}
// Emits a stateless error unconditionally.
// Emits an error unconditionally when there are no substitutions.
template <typename DiagnosticT>
auto EmitError() -> std::enable_if_t<std::is_empty_v<DiagnosticT>> {
std::enable_if_t<std::is_empty_v<typename DiagnosticT::Substitutions>>
EmitError() {
EmitError<DiagnosticT>({});
}
// Emits a warning if `F` returns true. `F` may or may not be called if the
// warning is disabled.
template <typename DiagnosticT>
auto EmitWarningIf(llvm::function_ref<bool(DiagnosticT&)> f) -> void {
void EmitWarningIf(
llvm::function_ref<bool(typename DiagnosticT::Substitutions&)> f) {
// TODO(kfm): check if this warning is enabled
DiagnosticT diag;
if (f(diag)) {
callback_(
{.short_name = DiagnosticT::ShortName, .message = diag.Format()});
typename DiagnosticT::Substitutions substitutions;
if (f(substitutions)) {
callback_({.short_name = DiagnosticT::ShortName,
.message = DiagnosticT::Format(substitutions)});
}
}
@@ -81,7 +83,9 @@ inline auto NullDiagnosticEmitter() -> DiagnosticEmitter& {
template <typename Derived>
struct SimpleDiagnostic {
struct Substitutions {};
static auto Format() -> std::string { return Derived::Message.str(); }
static auto Format(const Substitutions&) -> std::string {
return Derived::Message.str();
}
};
} // namespace Carbon
+23 -18
View File
@@ -21,14 +21,16 @@ struct EmptyDigitSequence : SimpleDiagnostic<EmptyDigitSequence> {
struct InvalidDigit {
static constexpr llvm::StringLiteral ShortName = "syntax-invalid-number";
char digit;
int radix;
auto Format() -> std::string {
return llvm::formatv("Invalid digit '{0}' in {1} numeric literal.", digit,
(radix == 2 ? "binary"
: radix == 16 ? "hexadecimal"
: "decimal"))
struct Substitutions {
char digit;
int radix;
};
static auto Format(const Substitutions& subst) -> std::string {
return llvm::formatv("Invalid digit '{0}' in {1} numeric literal.",
subst.digit,
(subst.radix == 2 ? "binary"
: subst.radix == 16 ? "hexadecimal"
: "decimal"))
.str();
}
};
@@ -43,15 +45,16 @@ struct IrregularDigitSeparators {
static constexpr llvm::StringLiteral ShortName =
"syntax-irregular-digit-separators";
int radix;
auto Format() -> std::string {
assert((radix == 10 || radix == 16) && "unexpected radix");
struct Substitutions {
int radix;
};
static auto Format(const Substitutions& subst) -> std::string {
assert((subst.radix == 10 || subst.radix == 16) && "unexpected radix");
return llvm::formatv(
"Digit separators in {0} number should appear every {1} "
"characters from the right.",
(radix == 10 ? "decimal" : "hexadecimal"),
(radix == 10 ? "3" : "4"))
(subst.radix == 10 ? "decimal" : "hexadecimal"),
(subst.radix == 10 ? "3" : "4"))
.str();
}
};
@@ -71,10 +74,12 @@ struct BinaryRealLiteral : SimpleDiagnostic<BinaryRealLiteral> {
struct WrongRealLiteralExponent {
static constexpr llvm::StringLiteral ShortName = "syntax-invalid-number";
char expected;
auto Format() -> std::string {
return llvm::formatv("Expected '{0}' to introduce exponent.", expected)
struct Substitutions {
char expected;
};
static auto Format(const Substitutions& subst) -> std::string {
return llvm::formatv("Expected '{0}' to introduce exponent.",
subst.expected)
.str();
}
};