Skip to content

[clang-format] Correctly annotate return type of function pointer #66893

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
Sep 26, 2023

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Sep 20, 2023

Fixes #66857.

@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2023

@llvm/pr-subscribers-clang-format

Changes

Fixes #66857.


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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+6-2)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+7)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 138f7e8562dcc39..3c490ab7fa00d60 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3144,8 +3144,12 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line) {
     }
 
     // Make sure the name is followed by a pair of parentheses.
-    if (Name)
-      return Tok->is(tok::l_paren) && Tok->MatchingParen ? Name : nullptr;
+    if (Name) {
+      return Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
+                     Tok->MatchingParen
+                 ? Name
+                 : nullptr;
+    }
 
     // Skip keywords that may precede the constructor/destructor name.
     if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 22698f6faf3cb1e..c5abdbad21351ef 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1655,6 +1655,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
                     "FOO Foo();");
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
   EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
+
+  Tokens = annotate("struct Foo {\n"
+                    "  Bar (*func)();\n"
+                    "};");
+  ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
+  EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsC11GenericSelection) {

@prj-
Copy link

prj- commented Sep 26, 2023

Pinging @owenca.

@owenca
Copy link
Contributor Author

owenca commented Sep 26, 2023

Pinging @owenca.

Waiting for one of @mydeveloperday @HazardyKnusperkeks @rymiel

@owenca owenca merged commit 6e608dc into llvm:main Sep 26, 2023
@owenca owenca deleted the 66857 branch September 26, 2023 21:17
legrosbuffle pushed a commit to legrosbuffle/llvm-project that referenced this pull request Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression in clang-format-17 or clang-format-18?
4 participants