[clang] 6e608dc - [clang-format] Correctly annotate return type of function pointer (#66893)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 26 14:17:53 PDT 2023
Author: Owen Pan
Date: 2023-09-26T14:17:49-07:00
New Revision: 6e608dc44b5f8998813a3051da0a5f80c2db7d2b
URL: https://github.com/llvm/llvm-project/commit/6e608dc44b5f8998813a3051da0a5f80c2db7d2b
DIFF: https://github.com/llvm/llvm-project/commit/6e608dc44b5f8998813a3051da0a5f80c2db7d2b.diff
LOG: [clang-format] Correctly annotate return type of function pointer (#66893)
Fixes #66857.
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 5becb86c0f37081..8162d6ddb8c16e4 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) {
More information about the cfe-commits
mailing list