[clang] [clang-format] Handle token-pasted function decl names (PR #142251)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Fri May 30 21:50:21 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/142251
Fix #142178
>From 8c5af1f937d6bdbd92628c41a685ed420604ce0a Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 30 May 2025 21:48:54 -0700
Subject: [PATCH] [clang-format] Handle token-pasted function decl names
Fix #142178
---
clang/lib/Format/TokenAnnotator.cpp | 7 ++++++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 7 +++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index f8272811bbe6c..963b9d2cd7c40 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3977,8 +3977,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 1a5ed4b9040c2..ba6a9f813f052 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[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);
More information about the cfe-commits
mailing list