https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/99433
>From ebf25d890ff47be48bd153942d943906943a247b Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Wed, 17 Jul 2024 22:30:21 -0700 Subject: [PATCH 1/3] [clang-format] Fix a bug in annotating `*` in `#define`s Fixes #99271. --- clang/lib/Format/TokenAnnotator.cpp | 3 ++- clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index b6d6e52ccb8f8..f1125fd35b427 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -374,7 +374,8 @@ class AnnotatingParser { Contexts.back().IsExpression = true; } else if (Line.InPPDirective && (!OpeningParen.Previous || - OpeningParen.Previous->isNot(tok::identifier))) { + OpeningParen.Previous->isNot(tok::identifier) || + !OpeningParen.Previous->Previous)) { Contexts.back().IsExpression = true; } else if (Contexts[Contexts.size() - 2].CaretFound) { // This is the parameter list of an ObjC block. diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index c5e8aa72cd2cb..82c9be0f4df71 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -75,6 +75,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_TypeDeclarationParen); EXPECT_TOKEN(Tokens[11], tok::star, TT_PointerOrReference); + Tokens = annotate("#define FOO bar(a * b)"); + ASSERT_EQ(Tokens.size(), 10u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::star, TT_BinaryOperator); + Tokens = annotate("void f() {\n" " while (p < a && *p == 'a')\n" " p++;\n" >From 70af6643045a6ded03fe1b3bbb21c55a2c5971b1 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 19 Jul 2024 08:59:14 -0700 Subject: [PATCH 2/3] Handles tokens before the function name. --- clang/lib/Format/TokenAnnotator.cpp | 20 +++++++++++++------ clang/unittests/Format/TokenAnnotatorTest.cpp | 16 +++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index f1125fd35b427..50201c970ab60 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -372,11 +372,6 @@ class AnnotatingParser { OpeningParen.Previous->is(tok::kw__Generic)) { Contexts.back().ContextType = Context::C11GenericSelection; Contexts.back().IsExpression = true; - } else if (Line.InPPDirective && - (!OpeningParen.Previous || - OpeningParen.Previous->isNot(tok::identifier) || - !OpeningParen.Previous->Previous)) { - Contexts.back().IsExpression = true; } else if (Contexts[Contexts.size() - 2].CaretFound) { // This is the parameter list of an ObjC block. Contexts.back().IsExpression = false; @@ -389,7 +384,20 @@ class AnnotatingParser { OpeningParen.Previous->MatchingParen->isOneOf( TT_ObjCBlockLParen, TT_FunctionTypeLParen)) { Contexts.back().IsExpression = false; - } else if (!Line.MustBeDeclaration && !Line.InPPDirective) { + } else if (Line.InPPDirective) { + auto IsExpr = [](const FormatToken &LParen) { + const auto *Tok = LParen.Previous; + if (!Tok || Tok->isNot(tok::identifier)) + return true; + Tok = Tok->Previous; + while (Tok && Tok->endsSequence(tok::coloncolon, tok::identifier)) { + assert(Tok->Previous); + Tok = Tok->Previous->Previous; + } + return !Tok || !Tok->Tok.getIdentifierInfo(); + }; + Contexts.back().IsExpression = IsExpr(OpeningParen); + } else if (!Line.MustBeDeclaration) { bool IsForOrCatch = OpeningParen.Previous && OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 82c9be0f4df71..f70424c3ee060 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -79,6 +79,22 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { ASSERT_EQ(Tokens.size(), 10u) << Tokens; EXPECT_TOKEN(Tokens[6], tok::star, TT_BinaryOperator); + Tokens = annotate("#define FOO foo.bar(a & b)"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[8], tok::amp, TT_BinaryOperator); + + Tokens = annotate("#define FOO foo::bar(a && b)"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[8], tok::ampamp, TT_BinaryOperator); + + Tokens = annotate("#define FOO foo bar(a *b)"); + ASSERT_EQ(Tokens.size(), 11u) << Tokens; + EXPECT_TOKEN(Tokens[7], tok::star, TT_PointerOrReference); + + Tokens = annotate("#define FOO void foo::bar(a &b)"); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; + EXPECT_TOKEN(Tokens[9], tok::amp, TT_PointerOrReference); + Tokens = annotate("void f() {\n" " while (p < a && *p == 'a')\n" " p++;\n" >From f484fe3db9fc2bd2069cf9156f0d5cf642b4378a Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 19 Jul 2024 23:06:54 -0700 Subject: [PATCH 3/3] Get rid of the parameter of the `IsExpr` lambda. --- clang/lib/Format/TokenAnnotator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 50201c970ab60..db66911f00f63 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -385,8 +385,8 @@ class AnnotatingParser { TT_ObjCBlockLParen, TT_FunctionTypeLParen)) { Contexts.back().IsExpression = false; } else if (Line.InPPDirective) { - auto IsExpr = [](const FormatToken &LParen) { - const auto *Tok = LParen.Previous; + auto IsExpr = [&OpeningParen] { + const auto *Tok = OpeningParen.Previous; if (!Tok || Tok->isNot(tok::identifier)) return true; Tok = Tok->Previous; @@ -396,7 +396,7 @@ class AnnotatingParser { } return !Tok || !Tok->Tok.getIdentifierInfo(); }; - Contexts.back().IsExpression = IsExpr(OpeningParen); + Contexts.back().IsExpression = IsExpr(); } else if (!Line.MustBeDeclaration) { bool IsForOrCatch = OpeningParen.Previous && _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits