[clang] 6ae14c0 - [clang-format] Annotate the l_paren of function pointer types (#109229)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 2 18:01:29 PDT 2024
Author: Owen Pan
Date: 2024-10-02T18:01:25-07:00
New Revision: 6ae14c0505e92cceefc1757467e4a2eb797429e6
URL: https://github.com/llvm/llvm-project/commit/6ae14c0505e92cceefc1757467e4a2eb797429e6
DIFF: https://github.com/llvm/llvm-project/commit/6ae14c0505e92cceefc1757467e4a2eb797429e6.diff
LOG: [clang-format] Annotate the l_paren of function pointer types (#109229)
Fixes #109146.
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 f665ce2ad81eb0..33f26d3c87b292 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -468,16 +468,18 @@ class AnnotatingParser {
OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
FormatToken *PossibleObjCForInToken = nullptr;
while (CurrentToken) {
- if (CurrentToken->Previous->is(TT_PointerOrReference) &&
- CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
- tok::coloncolon)) {
+ const auto &Prev = *CurrentToken->Previous;
+ if (Prev.is(TT_PointerOrReference) &&
+ Prev.Previous->isOneOf(tok::l_paren, tok::coloncolon)) {
ProbablyFunctionType = true;
}
if (CurrentToken->is(tok::comma))
MightBeFunctionType = false;
- if (CurrentToken->Previous->is(TT_BinaryOperator))
+ if (Prev.is(TT_BinaryOperator))
Contexts.back().IsExpression = true;
if (CurrentToken->is(tok::r_paren)) {
+ if (Prev.is(TT_PointerOrReference) && Prev.Previous == &OpeningParen)
+ MightBeFunctionType = true;
if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
ProbablyFunctionType && CurrentToken->Next &&
(CurrentToken->Next->is(tok::l_paren) ||
@@ -550,8 +552,8 @@ class AnnotatingParser {
bool ProbablyFunctionTypeLParen =
(CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
- if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
- CurrentToken->Previous->isTypeName(LangOpts)) &&
+ if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
+ Prev.isTypeName(LangOpts)) &&
!(CurrentToken->is(tok::l_brace) ||
(CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
Contexts.back().IsExpression = false;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1884d41a5f23f5..205e4712d6c8cd 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -809,6 +809,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen);
+ Tokens = annotate("return (Foo (*)(void *, Bar, ...))&foo;");
+ ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionTypeLParen);
+ EXPECT_TOKEN(Tokens[14], tok::r_paren, TT_CastRParen);
+ EXPECT_TOKEN(Tokens[15], tok::amp, TT_UnaryOperator);
+
auto Style = getLLVMStyle();
Style.TypeNames.push_back("Foo");
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);
More information about the cfe-commits
mailing list