Skip to content

release/20.x: [clang-format] Handle token-pasted function decl names (#142251) #142282

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

Closed
wants to merge 1 commit into from

Conversation

llvmbot
Copy link
Member

@llvmbot llvmbot commented May 31, 2025

Backport 29f79ea

Requested by: @owenca

@llvmbot
Copy link
Member Author

llvmbot commented May 31, 2025

@HazardyKnusperkeks What do you think about merging this PR to the release branch?

@llvmbot
Copy link
Member Author

llvmbot commented May 31, 2025

@llvm/pr-subscribers-clang-format

Author: None (llvmbot)

Changes

Backport 29f79ea

Requested by: @owenca


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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+6-1)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+7)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 11b941c5a0411..6ef2d665e1bc4 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3967,8 +3967,13 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
        Tok; Tok = Tok->Next) {
     if (Tok->is(TT_StartOfName))
       SeenName = true;
-    if (Tok->Previous->EndsCppAttributeGroup)
+    const auto *Previous = Tok->Previous;
+    if (Previous->EndsCppAttributeGroup) {
       AfterLastAttribute = Tok;
+    } else if (Line.InMacroBody &&
+               Previous->endsSequence(tok::hashhash, TT_StartOfName)) {
+      Tok->setType(TT_StartOfName);
+    }
     if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
         IsCtorOrDtor ||
         isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 757db66c3e298..2328dce8df8dc 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2187,6 +2187,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
   EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
   EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
 
+  Tokens = annotate("#define FUNC(foo, bar) \\\n"
+                    "  auto foo##bar() -> Type {}");
+  ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+  EXPECT_TOKEN(Tokens[11], tok::identifier, TT_FunctionDeclarationName);
+  EXPECT_TOKEN(Tokens[12], tok::l_paren, TT_FunctionDeclarationLParen);
+  EXPECT_TOKEN(Tokens[14], tok::arrow, TT_TrailingReturnArrow);
+
   Tokens = annotate("int iso_time(time_t);");
   ASSERT_EQ(Tokens.size(), 7u) << Tokens;
   EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status May 31, 2025
@owenca
Copy link
Contributor

owenca commented Jun 2, 2025

Backport 29f79ea

Reverted by c5cce48

@owenca owenca closed this Jun 2, 2025
@tstellar tstellar moved this from Needs Merge to Won't Merge in LLVM Release Status Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Won't Merge
Development

Successfully merging this pull request may close these issues.

3 participants