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

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 20 04:24:49 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

<details>
<summary>Changes</summary>

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
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) {

``````````

</details>


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


More information about the cfe-commits mailing list