From d61531c82c26bb6a0087310fabaac74d050e939e Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Thu, 5 Jan 2023 09:38:21 -0800 Subject: [PATCH] Hack `clang-format` config to format our macros better. (#2514) This somewhat abuses control-flow macro handling in `clang-format` to get the behavior we want. Fortunately, I don't think we're ever likely to want this for its intended purpose so it seems harmless to co-opt it like this. If we really wanted, we can narrow it to `if`-macros, but I picked the simpler option to start. FWIW, I tried all the other macro formatting special cases to see if one would work but it didn't. Might be worth filing a feature request to get a `TypeDefinitionMacros` setting to compliment `TypenameMacros` and format like this does, but it seems (very) low priority. --- .clang-format | 3 +++ toolchain/common/enum_base.h | 2 +- toolchain/common/enum_base_test.cpp | 2 +- toolchain/parser/parser_state.h | 2 +- toolchain/semantics/semantics_builtin_kind.h | 2 +- toolchain/semantics/semantics_node_kind.h | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.clang-format b/.clang-format index faa5636305a6..32deb344b877 100644 --- a/.clang-format +++ b/.clang-format @@ -11,3 +11,6 @@ DerivePointerAlignment: 'false' ExperimentalAutoDetectBinPacking: 'false' FixNamespaceComments: 'true' PointerAlignment: Left +# We abuse control macros for formatting other kinds of macros. +SpaceBeforeParens: ControlStatementsExceptControlMacros +IfMacros: ['CARBON_DEFINE_RAW_ENUM_CLASS'] diff --git a/toolchain/common/enum_base.h b/toolchain/common/enum_base.h index 4ffa8e531993..b2bec1ac034d 100644 --- a/toolchain/common/enum_base.h +++ b/toolchain/common/enum_base.h @@ -30,7 +30,7 @@ namespace Carbon::Internal { // // In `my_kind.h`: // ``` -// CARBON_DEFINE_RAW_ENUM_CLASS(MyKind, uint8_t){ +// CARBON_DEFINE_RAW_ENUM_CLASS(MyKind, uint8_t) { // #define CARBON_MY_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) // #include "toolchain/.../my_kind.def" // }; diff --git a/toolchain/common/enum_base_test.cpp b/toolchain/common/enum_base_test.cpp index 1b6f88818f4c..f6349e60c534 100644 --- a/toolchain/common/enum_base_test.cpp +++ b/toolchain/common/enum_base_test.cpp @@ -8,7 +8,7 @@ namespace Carbon { -CARBON_DEFINE_RAW_ENUM_CLASS(TestKind, uint8_t){ +CARBON_DEFINE_RAW_ENUM_CLASS(TestKind, uint8_t) { #define CARBON_ENUM_BASE_TEST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) #include "toolchain/common/enum_base_test.def" }; diff --git a/toolchain/parser/parser_state.h b/toolchain/parser/parser_state.h index a50569c444a4..252e8ef6cc7b 100644 --- a/toolchain/parser/parser_state.h +++ b/toolchain/parser/parser_state.h @@ -9,7 +9,7 @@ namespace Carbon { -CARBON_DEFINE_RAW_ENUM_CLASS(ParserState, uint8_t){ +CARBON_DEFINE_RAW_ENUM_CLASS(ParserState, uint8_t) { #define CARBON_PARSER_STATE(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) #include "toolchain/parser/parser_state.def" }; diff --git a/toolchain/semantics/semantics_builtin_kind.h b/toolchain/semantics/semantics_builtin_kind.h index d0fe37fa034b..868b044b34de 100644 --- a/toolchain/semantics/semantics_builtin_kind.h +++ b/toolchain/semantics/semantics_builtin_kind.h @@ -9,7 +9,7 @@ namespace Carbon { -CARBON_DEFINE_RAW_ENUM_CLASS(SemanticsBuiltinKind, uint8_t){ +CARBON_DEFINE_RAW_ENUM_CLASS(SemanticsBuiltinKind, uint8_t) { #define CARBON_SEMANTICS_BUILTIN_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) #include "toolchain/semantics/semantics_builtin_kind.def" }; diff --git a/toolchain/semantics/semantics_node_kind.h b/toolchain/semantics/semantics_node_kind.h index b97793988b02..465639b63bce 100644 --- a/toolchain/semantics/semantics_node_kind.h +++ b/toolchain/semantics/semantics_node_kind.h @@ -9,7 +9,7 @@ namespace Carbon { -CARBON_DEFINE_RAW_ENUM_CLASS(SemanticsNodeKind, uint8_t){ +CARBON_DEFINE_RAW_ENUM_CLASS(SemanticsNodeKind, uint8_t) { #define CARBON_SEMANTICS_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name) #include "toolchain/semantics/semantics_node_kind.def" };