From aec2534e9d666cd1e4930eb83e488e9d541aaeee Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Tue, 31 Mar 2026 12:41:26 -0700 Subject: [PATCH] Fix formatting of compound-type variable declarations in macros (#6997) By default clang-format interprets function-like macro invocations as function calls. E.g. the argument of `CARBON_KIND(llvm::ListSeparator* sep)` is interpreted as an expression, meaning the `*` is an infix binary operator, so it inserts a space before the `*`. This change teaches clang-format that `CARBON_KIND(x)` and `CARBON_ASSIGN_OR_RETURN(x)` rewrite to `x`, which is close enough to the truth to enable it to format them correctly. See the [clang-format docs](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#macros) for details. --- .clang-format | 5 +++++ toolchain/lex/lex.cpp | 4 ++-- toolchain/sem_ir/stringify.cpp | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.clang-format b/.clang-format index 9225257c145c..37633fd7283e 100644 --- a/.clang-format +++ b/.clang-format @@ -27,3 +27,8 @@ StatementMacros: ['ABSTRACT'] QualifierAlignment: Custom QualifierOrder: [inline, static, friend, constexpr, const, volatile, restrict, type] +Macros: + # These macros can contain variable declarations, so clang-format needs to + # "see through" them in order to format them correctly. + - CARBON_ASSIGN_OR_RETURN(x)=x + - CARBON_KIND(x)=x diff --git a/toolchain/lex/lex.cpp b/toolchain/lex/lex.cpp index 4ca61c56b829..e636fe6a65cf 100644 --- a/toolchain/lex/lex.cpp +++ b/toolchain/lex/lex.cpp @@ -1111,14 +1111,14 @@ auto Lexer::LexNumericLiteral(llvm::StringRef source_text, ssize_t& position) position += token_size; CARBON_KIND_SWITCH(literal->ComputeValue(emitter_)) { - case CARBON_KIND(NumericLiteral::IntValue && value): { + case CARBON_KIND(NumericLiteral::IntValue&& value): { return LexTokenWithPayload(TokenKind::IntLiteral, buffer_.value_stores_->ints() .AddUnsigned(std::move(value.value)) .AsTokenPayload(), byte_offset); } - case CARBON_KIND(NumericLiteral::RealValue && value): { + case CARBON_KIND(NumericLiteral::RealValue&& value): { auto real_id = buffer_.value_stores_->reals().Add( Real{.mantissa = value.mantissa, .exponent = value.exponent, diff --git a/toolchain/sem_ir/stringify.cpp b/toolchain/sem_ir/stringify.cpp index 435b16d686e1..b6df128e7331 100644 --- a/toolchain/sem_ir/stringify.cpp +++ b/toolchain/sem_ir/stringify.cpp @@ -176,7 +176,7 @@ class StepStack { PushSpecificNamedConstraint(specific_named_constraint); break; } - case CARBON_KIND(llvm::ListSeparator * sep): { + case CARBON_KIND(llvm::ListSeparator* sep): { PushString(*sep); break; }