[llvm-branch-commits] [clang] release/20.x: [clang-format] Correctly annotate token-pasted function decl names (#142337) (PR #142482)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jun 3 15:38:53 PDT 2025
https://github.com/llvmbot updated https://github.com/llvm/llvm-project/pull/142482
>From f6532710ace867e5660ea9e47ab77f2b8349896f Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 2 Jun 2025 13:35:27 -0700
Subject: [PATCH] [clang-format] Correctly annotate token-pasted function decl
names (#142337)
Fix #142178
(cherry picked from commit 7bf5862dbfda590282f50b14e6d7d5f990bf1900)
---
clang/lib/Format/TokenAnnotator.cpp | 2 ++
clang/unittests/Format/TokenAnnotatorTest.cpp | 7 +++++++
2 files changed, 9 insertions(+)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 11b941c5a0411..0c13356ca96de 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3839,6 +3839,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 757db66c3e298..602c2d5eba29a 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, baz) \\\n"
+ " auto foo##bar##baz() -> Type {}");
+ ASSERT_EQ(Tokens.size(), 23u) << Tokens;
+ EXPECT_TOKEN(Tokens[11], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[16], tok::l_paren, TT_FunctionDeclarationLParen);
+ EXPECT_TOKEN(Tokens[18], 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);
More information about the llvm-branch-commits
mailing list