Просмотр исходного кода

Change DiagnosticKind to use EnumBase (#2532)

Although the implementation is similar in size, I think the consistency benefits are helpful. registry.def -> kind.def is also consistent with other implementations.

Also switching int32_t -> uint16_t because I think we it might be enough space (?). We use uint8_t elsewhere, but that's certainly going to be too small. Sticking with int32_t felt inconsistent.
Jon Ross-Perkins 3 лет назад
Родитель
Сommit
3d90a85f24

+ 2 - 15
toolchain/diagnostics/BUILD

@@ -32,23 +32,10 @@ cc_library(
     srcs = ["diagnostic_kind.cpp"],
     hdrs = ["diagnostic_kind.h"],
     textual_hdrs = [
-        "diagnostic_registry.def",
+        "diagnostic_kind.def",
     ],
     deps = [
-        "//common:ostream",
-        "@llvm-project//llvm:Support",
-    ],
-)
-
-cc_test(
-    name = "diagnostic_kind_test",
-    size = "small",
-    srcs = ["diagnostic_kind_test.cpp"],
-    deps = [
-        ":diagnostic_kind",
-        "//common:gtest_main",
-        "@com_google_googletest//:gtest",
-        "@llvm-project//llvm:Support",
+        "//toolchain/common:enum_base",
     ],
 )
 

+ 1 - 1
toolchain/diagnostics/check_diagnostics.py

@@ -41,7 +41,7 @@ def load_diagnostic_kind() -> Set[str]:
 
     This isn't validated for uniqueness because the compiler does that.
     """
-    path = Path("toolchain/diagnostics/diagnostic_registry.def")
+    path = Path("toolchain/diagnostics/diagnostic_kind.def")
     content = path.read_text()
     decls = set(re.findall(r"CARBON_DIAGNOSTIC_KIND\((.+)\)", content))
     return decls.difference(IGNORED)

+ 4 - 9
toolchain/diagnostics/diagnostic_kind.cpp

@@ -6,14 +6,9 @@
 
 namespace Carbon {
 
-auto operator<<(llvm::raw_ostream& out, DiagnosticKind kind)
-    -> llvm::raw_ostream& {
-  static constexpr llvm::StringLiteral Names[] = {
-#define CARBON_DIAGNOSTIC_KIND(DiagnosticName) #DiagnosticName,
-#include "toolchain/diagnostics/diagnostic_registry.def"
-  };
-  out << Names[static_cast<int>(kind)];
-  return out;
-}
+CARBON_DEFINE_ENUM_CLASS_NAMES(DiagnosticKind) = {
+#define CARBON_DIAGNOSTIC_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
+#include "toolchain/diagnostics/diagnostic_kind.def"
+};
 
 }  // namespace Carbon

+ 5 - 1
toolchain/diagnostics/diagnostic_registry.def → toolchain/diagnostics/diagnostic_kind.def

@@ -10,7 +10,11 @@
 //
 // The viable X-macros to define prior to including the header are:
 //
-// - `DIAGNOSTIC_KIND`
+// - `CARBON_DIAGNOSTIC_KIND`
+
+#ifndef CARBON_DIAGNOSTIC_KIND
+#error "Must define the x-macro to use this file."
+#endif
 
 // ============================================================================
 // Lexer diagnostics

+ 17 - 12
toolchain/diagnostics/diagnostic_kind.h

@@ -5,11 +5,17 @@
 #ifndef CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_KIND_H_
 #define CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_KIND_H_
 
-#include "common/ostream.h"
-#include "llvm/ADT/StringRef.h"
+#include <cstdint>
+
+#include "toolchain/common/enum_base.h"
 
 namespace Carbon {
 
+CARBON_DEFINE_RAW_ENUM_CLASS(DiagnosticKind, uint16_t) {
+#define CARBON_DIAGNOSTIC_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
+#include "toolchain/diagnostics/diagnostic_kind.def"
+};
+
 // An enumeration of all diagnostics provided by the toolchain. Diagnostics must
 // be added to diagnostic_registry.def, and defined locally to where they're
 // used using the `DIAGNOSTIC` macro in diagnostic_emitter.h.
@@ -18,19 +24,18 @@ namespace Carbon {
 // definitions centrally is expected to create a compilation bottleneck
 // long-term, and we also see value to keeping diagnostic format strings close
 // to the consuming code.
-enum class DiagnosticKind : int32_t {
-#define CARBON_DIAGNOSTIC_KIND(DiagnosticName) DiagnosticName,
-#include "toolchain/diagnostics/diagnostic_registry.def"
+class DiagnosticKind : public CARBON_ENUM_BASE(DiagnosticKind) {
+ public:
+#define CARBON_DIAGNOSTIC_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name)
+#include "toolchain/diagnostics/diagnostic_kind.def"
 };
 
-auto operator<<(llvm::raw_ostream& out, DiagnosticKind kind)
-    -> llvm::raw_ostream&;
+#define CARBON_DIAGNOSTIC_KIND(Name) \
+  CARBON_ENUM_CONSTANT_DEFINITION(DiagnosticKind, Name)
+#include "toolchain/diagnostics/diagnostic_kind.def"
 
-inline auto operator<<(std::ostream& out, DiagnosticKind kind)
-    -> std::ostream& {
-  llvm::raw_os_ostream(out) << kind;
-  return out;
-}
+// We expect DiagnosticKind to fit into 2 bits.
+static_assert(sizeof(DiagnosticKind) == 2, "DiagnosticKind includes padding!");
 
 }  // namespace Carbon
 

+ 0 - 22
toolchain/diagnostics/diagnostic_kind_test.cpp

@@ -1,22 +0,0 @@
-// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
-// Exceptions. See /LICENSE for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-#include "toolchain/diagnostics/diagnostic_kind.h"
-
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-
-#include "llvm/Support/raw_ostream.h"
-
-namespace Carbon::Testing {
-namespace {
-
-TEST(DiagnosticKindTest, Name) {
-  std::string buffer;
-  llvm::raw_string_ostream(buffer) << DiagnosticKind::TestDiagnostic;
-  EXPECT_EQ(buffer, "TestDiagnostic");
-}
-
-}  // namespace
-}  // namespace Carbon::Testing