Jelajahi Sumber

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

This reverts commit 33fdabf4628f3aea90b118b26cb67405c985b4f5.
Dave Abrahams 5 tahun lalu
induk
melakukan
f61b1b18ca
2 mengubah file dengan 38 tambahan dan 29 penghapusan
  1. 15 11
      diagnostics/diagnostic_emitter.h
  2. 23 18
      lexer/numeric_literal.cpp

+ 15 - 11
diagnostics/diagnostic_emitter.h

@@ -37,28 +37,30 @@ class DiagnosticEmitter {
       : callback_(std::move(callback)) {}
       : callback_(std::move(callback)) {}
   ~DiagnosticEmitter() = default;
   ~DiagnosticEmitter() = default;
 
 
-  // Emits an error unconditionally.
+  // Emits an error unconditionally after applying the provided `substitutions`.
   template <typename DiagnosticT>
   template <typename DiagnosticT>
-  auto EmitError(DiagnosticT diag) -> void {
+  void EmitError(typename DiagnosticT::Substitutions substitutions) {
     callback_({.short_name = DiagnosticT::ShortName,
     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>
   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>({});
     EmitError<DiagnosticT>({});
   }
   }
 
 
   // Emits a warning if `F` returns true.  `F` may or may not be called if the
   // Emits a warning if `F` returns true.  `F` may or may not be called if the
   // warning is disabled.
   // warning is disabled.
   template <typename DiagnosticT>
   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
     // 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>
 template <typename Derived>
 struct SimpleDiagnostic {
 struct SimpleDiagnostic {
   struct Substitutions {};
   struct Substitutions {};
-  static auto Format() -> std::string { return Derived::Message.str(); }
+  static auto Format(const Substitutions&) -> std::string {
+    return Derived::Message.str();
+  }
 };
 };
 
 
 }  // namespace Carbon
 }  // namespace Carbon

+ 23 - 18
lexer/numeric_literal.cpp

@@ -21,14 +21,16 @@ struct EmptyDigitSequence : SimpleDiagnostic<EmptyDigitSequence> {
 struct InvalidDigit {
 struct InvalidDigit {
   static constexpr llvm::StringLiteral ShortName = "syntax-invalid-number";
   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();
         .str();
   }
   }
 };
 };
@@ -43,15 +45,16 @@ struct IrregularDigitSeparators {
   static constexpr llvm::StringLiteral ShortName =
   static constexpr llvm::StringLiteral ShortName =
       "syntax-irregular-digit-separators";
       "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(
     return llvm::formatv(
                "Digit separators in {0} number should appear every {1} "
                "Digit separators in {0} number should appear every {1} "
                "characters from the right.",
                "characters from the right.",
-               (radix == 10 ? "decimal" : "hexadecimal"),
-               (radix == 10 ? "3" : "4"))
+               (subst.radix == 10 ? "decimal" : "hexadecimal"),
+               (subst.radix == 10 ? "3" : "4"))
         .str();
         .str();
   }
   }
 };
 };
@@ -71,10 +74,12 @@ struct BinaryRealLiteral : SimpleDiagnostic<BinaryRealLiteral> {
 struct WrongRealLiteralExponent {
 struct WrongRealLiteralExponent {
   static constexpr llvm::StringLiteral ShortName = "syntax-invalid-number";
   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();
         .str();
   }
   }
 };
 };