[llvm-branch-commits] [clang] release/21.x: [clang-format] Fix a bug in `DerivePointerAlignment: true` (#150387) (PR #150458)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Jul 24 09:52:27 PDT 2025
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/150458
Backport a4d4859dc70c046ad928805ddeaf8fa101793394
Requested by: @owenca
>From efc77630448f98b34f6026734a3a62b42ba33731 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 24 Jul 2025 09:41:22 -0700
Subject: [PATCH] [clang-format] Fix a bug in `DerivePointerAlignment: true`
(#150387)
Fixes #150327
(cherry picked from commit a4d4859dc70c046ad928805ddeaf8fa101793394)
---
clang/lib/Format/Format.cpp | 5 +++--
clang/unittests/Format/FormatTest.cpp | 2 ++
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 78c09be458f0a..c6802872700ab 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2638,13 +2638,14 @@ class Formatter : public TokenAnalyzer {
for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
if (Tok->isNot(TT_PointerOrReference))
continue;
- // Don't treat space in `void foo() &&` as evidence.
+ // Don't treat space in `void foo() &&` or `void() &&` as evidence.
if (const auto *Prev = Tok->getPreviousNonComment()) {
if (Prev->is(tok::r_paren) && Prev->MatchingParen) {
if (const auto *Func =
Prev->MatchingParen->getPreviousNonComment()) {
if (Func->isOneOf(TT_FunctionDeclarationName, TT_StartOfName,
- TT_OverloadedOperator)) {
+ TT_OverloadedOperator) ||
+ Func->isTypeName(LangOpts)) {
continue;
}
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 0bc1c6d45656e..4c0a7709d5a4d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12091,6 +12091,8 @@ TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
Prefix = "void a() const &;\n"
"void b() const &;\n";
verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
+
+ verifyGoogleFormat("MACRO(int*, std::function<void() &&>);");
}
TEST_F(FormatTest, PointerAlignmentFallback) {
More information about the llvm-branch-commits
mailing list