Skip to content

[clang-format] Handle token-pasted function decl names #142251

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 1 commit into from
May 31, 2025
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented May 31, 2025

Fix #142178

@llvmbot
Copy link
Member

llvmbot commented May 31, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fix #142178


Full diff: https://github.com/llvm/llvm-project/pull/142251.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 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);

@owenca owenca merged commit 29f79ea into llvm:main May 31, 2025
13 checks passed
@owenca owenca deleted the 142178 branch May 31, 2025 18:11
@owenca owenca added this to the LLVM 20.X Release milestone May 31, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status May 31, 2025
@owenca
Copy link
Contributor Author

owenca commented May 31, 2025

/cherry-pick 29f79ea

@llvmbot
Copy link
Member

llvmbot commented May 31, 2025

/pull-request #142282

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status May 31, 2025
@llvm llvm deleted a comment from llvm-ci Jun 1, 2025
@prj-
Copy link

prj- commented Jun 2, 2025

@owenca, this introduced multiple regressions (or maybe they are all the same), I do believe.

diff --git a/include/petsc/private/hashmap.h b/include/petsc/private/hashmap.h
index c48d69d73ce..265a968a95c 100644
--- a/include/petsc/private/hashmap.h
+++ b/include/petsc/private/hashmap.h
@@ -37,3 +37,3 @@ M*/
 #define PETSC_HASH_MAP_DECL(HashT, KeyType, ValType) \
-  typedef kh_##HashT##_t                   *Petsc##HashT; \
+  typedef kh_##HashT##_t *Petsc##           HashT; \
   static inline PETSC_UNUSED PetscErrorCode Petsc##HashT##Create(Petsc##HashT *); \
diff --git a/include/petsc/private/logimpl.h b/include/petsc/private/logimpl.h
index 6645e51af15..59333efff6a 100644
--- a/include/petsc/private/logimpl.h
+++ b/include/petsc/private/logimpl.h
@@ -13,3 +13,3 @@
 #define PETSC_LOG_RESIZABLE_ARRAY(Container, Entry, Key, Constructor, Destructor, Equal) \
-  typedef struct _n_PetscLog##Container    *PetscLog##Container; \
+  typedef struct _n_PetscLog##Container *PetscLog##Container; \
   static inline PETSC_UNUSED PetscErrorCode PetscLog##Container##Create(int, PetscLog##Container *); \
diff --git a/src/dm/impls/plex/plexgmsh.c b/src/dm/impls/plex/plexgmsh.c
index faa5033598d..662c1981f80 100644
--- a/src/dm/impls/plex/plexgmsh.c
+++ b/src/dm/impls/plex/plexgmsh.c
@@ -10,3 +10,3 @@
     static int Gmsh_LexOrder_##T##_##p[GmshNumNodes_##T(p)] = {-1}; \
-    int       *lex                                          = Gmsh_LexOrder_##T##_##p; \
+    int                             *lex                    = Gmsh_LexOrder_##T##_##p; \
     if (lex[0] == -1) (void)GmshLexOrder_##T(p, lex, 0); \

A reproducer is attached (clang-format-21 --style=file:clang-format.txt foo.txt), let me know if you want me to open an issue. clang-format.txt foo.txt

owenca added a commit that referenced this pull request Jun 2, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 2, 2025
@owenca
Copy link
Contributor Author

owenca commented Jun 2, 2025

I've reverted this patch and sent in another one #142337.

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