[PATCH] D127873: [clang-format] Fix misplacemnt of `*` in declartion of pointer to struct

Jack Huang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 15 09:28:20 PDT 2022


jackhong12 created this revision.
jackhong12 added a reviewer: clang-format.
Herald added a project: All.
jackhong12 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixes #55810

Star tokens should be categorized as TT_PointerOrReference instead of TT_BinaryOperator when behind the right bracket.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127873

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/TokenAnnotatorTest.cpp


Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -85,6 +85,12 @@
   Tokens = annotate("case &x:");
   EXPECT_EQ(Tokens.size(), 5u) << Tokens;
   EXPECT_TOKEN(Tokens[1], tok::amp, TT_UnaryOperator);
+
+  Tokens = annotate("struct {\n"
+                    "  int foo;\n"
+                    "} *ptr;\n");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::star, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2306,10 +2306,13 @@
 
     if (PrevToken->Tok.isLiteral() ||
         PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
-                           tok::kw_false, tok::r_brace)) {
+                           tok::kw_false)) {
       return TT_BinaryOperator;
     }
 
+    if (PrevToken->is(tok::r_brace) && Tok.isOneOf(tok::amp, tok::ampamp))
+      return TT_BinaryOperator;
+
     const FormatToken *NextNonParen = NextToken;
     while (NextNonParen && NextNonParen->is(tok::l_paren))
       NextNonParen = NextNonParen->getNextNonComment();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127873.437214.patch
Type: text/x-patch
Size: 1405 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220615/ba6ddfc7/attachment.bin>


More information about the cfe-commits mailing list