Some more edits to EnumBase and EnumMaskBase (#6054)

Adds a unit test, and some smaller edits:

- Remove the `=` when defining names, in order to change `}` placement
by clang-format on uses.
- context:
https://github.com/carbon-language/carbon-lang/pull/6053#discussion_r2343423178
- I believe with `EnumBase` that keeping the `=` had been a deliberate
choice, so this PR is intended to confirm that removing it is okay.
- Delete `EnumMaskBase::name`
- context:
https://github.com/carbon-language/carbon-lang/pull/6053#discussion_r2344233707
- We can't just do nothing because `EnumBase::name` uses indexing that's
incompatible with `EnumMaskBase`.
- Some small comment cleanups.
- Tests don't need to be in the `Carbon` namespace anymore, macros work
fine in other namespaces, but it's still the right namespace.
- Documentation on `EnumBase::name` seems to be referring to a prior
structure, wherein we had a macro defining the function instead of the
`Names` array.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Jon Ross-Perkins
2025-09-12 22:59:37 +00:00
committed by GitHub
co-authored by Richard Smith
parent 1d19fa3954
commit 973d721916
17 changed files with 202 additions and 52 deletions
+2 -10
View File
@@ -30,7 +30,7 @@ class TestKind : public CARBON_ENUM_BASE(TestKind) {
CARBON_ENUM_CONSTANT_DEFINITION(TestKind, Name)
#include "common/enum_base_test.def"
CARBON_DEFINE_ENUM_CLASS_NAMES(TestKind) = {
CARBON_DEFINE_ENUM_CLASS_NAMES(TestKind) {
#define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
#include "common/enum_base_test.def"
};
@@ -79,23 +79,15 @@ TEST(EnumBaseTest, Switch) {
TEST(EnumBaseTest, Comparison) {
TestKind kind = TestKind::Beep;
// Make sure all the different comparisons work, and also to work with
// Make sure all the different comparisons work, and also work with
// GoogleTest expectations.
EXPECT_EQ(TestKind::Beep, kind);
EXPECT_NE(TestKind::Boop, kind);
EXPECT_LT(kind, TestKind::Boop);
EXPECT_GT(TestKind::Burr, kind);
EXPECT_LE(kind, TestKind::Beep);
EXPECT_GE(TestKind::Beep, kind);
// These should also all be constexpr.
constexpr TestKind Kind2 = TestKind::Beep;
static_assert(Kind2 == TestKind::Beep);
static_assert(Kind2 != TestKind::Boop);
static_assert(Kind2 < TestKind::Boop);
static_assert(!(Kind2 > TestKind::Burr));
static_assert(Kind2 <= TestKind::Beep);
static_assert(!(Kind2 >= TestKind::Burr));
}
TEST(EnumBaseTest, IntConversion) {