[clang] 29f79ea - [clang-format] Handle token-pasted function decl names (#142251)

via cfe-commits cfe-commits at lists.llvm.org
Sat May 31 11:11:04 PDT 2025


Author: Owen Pan
Date: 2025-05-31T11:11:00-07:00
New Revision: 29f79ea3c59649f7686a09845665660c25ca3f9b

URL: https://github.com/llvm/llvm-project/commit/29f79ea3c59649f7686a09845665660c25ca3f9b
DIFF: https://github.com/llvm/llvm-project/commit/29f79ea3c59649f7686a09845665660c25ca3f9b.diff

LOG: [clang-format] Handle token-pasted function decl names (#142251)

Fix #142178

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/TokenAnnotatorTest.cpp

Removed: 
    


################################################################################
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