Skip to content

[clang-format] Fix a bug in DerivePointerAlignment: true #150387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jul 24, 2025

Fixes #150327

@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #150327


Full diff: https://github.com/llvm/llvm-project/pull/150387.diff

2 Files Affected:

  • (modified) clang/lib/Format/Format.cpp (+3-2)
  • (modified) clang/unittests/Format/FormatTest.cpp (+2)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 62feb3db0ed5e..3eab44f0d1642 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2643,13 +2643,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 dbf6950446ef0..e21d7a88e81bd 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12103,6 +12103,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) {

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Jul 24, 2025
@tru tru moved this from Needs Merge to Needs Backport PR in LLVM Release Status Jul 24, 2025
@owenca owenca merged commit a4d4859 into llvm:main Jul 24, 2025
11 checks passed
@owenca owenca deleted the 150327 branch July 24, 2025 16:41
@github-project-automation github-project-automation bot moved this from Needs Backport PR to Done in LLVM Release Status Jul 24, 2025
@owenca
Copy link
Contributor Author

owenca commented Jul 24, 2025

/cherry-pick a4d4859

@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2025

/pull-request #150458

mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

clang-format adds extra spaces between type and pointer
3 participants