Skip to content

[clang-format] Correctly annotate token-pasted function decl names #142337

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 2 commits into from
Jun 2, 2025

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jun 2, 2025

Fix #142178

@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fix #142178


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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+2)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+7)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index da1b6dd5254b8..da279d07b5918 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3848,6 +3848,8 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
   } else {
     if (Current.isNot(TT_StartOfName) || Current.NestingLevel != 0)
       return false;
+    while (Next && Next->startsSequence(tok::hashhash, tok::identifier))
+      Next = Next->Next->Next;
     for (; Next; Next = Next->Next) {
       if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
         Next = Next->MatchingParen;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1a5ed4b9040c2..9637085c70502 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2257,6 +2257,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[9], 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);

@prj-
Copy link

prj- commented Jun 2, 2025

Looking forward to testing this out!

@owenca
Copy link
Contributor Author

owenca commented Jun 2, 2025

Looking forward to testing this out!

I already tested it with your reproducer in #142251 (comment).

@prj-
Copy link

prj- commented Jun 2, 2025

Unfortunately, we have many other use-cases for triggering regressions in our code (hence why we are having this discussion in the first place 😅), but hopefully all will be good afterwards.

@owenca
Copy link
Contributor Author

owenca commented Jun 2, 2025

@prj- I understand. If you use AlignFunctionDeclarations, you might see formatting changes as the first token in a token-pasted function decl name was not annotated correctly before this patch.

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Jun 2, 2025
@owenca owenca merged commit 7bf5862 into llvm:main Jun 2, 2025
11 checks passed
@owenca owenca deleted the func-decl-name branch June 2, 2025 20:35
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Jun 2, 2025
@owenca
Copy link
Contributor Author

owenca commented Jun 2, 2025

/cherry-pick 7bf5862

@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

/pull-request #142482

sallto pushed a commit to sallto/llvm-project that referenced this pull request Jun 3, 2025
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Jun 3, 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 regression with spaces around trailing-return-type in macro
4 participants