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

2022-08-11 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 added a comment.

Right, I think we can fix this in the same way. I'll look into it soon.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-08-11 Thread Ben Smith via Phabricator via cfe-commits
binji added a comment.

This change seems to have also regressed code like this:

  // before
  bool b = 3 == int{3} && true;
  // after
  bool b = 3 == int{3}&& true;

I suppose a similar fix to the one done for multiply should be done for `&&` 
too?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-07-14 Thread Krasimir Georgiev via Phabricator via cfe-commits
krasimir added a comment.

Proposed https://reviews.llvm.org/D129771.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-07-14 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 added a comment.

Good point!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-07-14 Thread Krasimir Georgiev via Phabricator via cfe-commits
krasimir added a comment.

In D127873#3651364 , @jackhong12 
wrote:

> I think we cannot identify `struct`, `union`, `class` or `enum` by the right 
> bracket. Clang-format will split the input into multiple lines. For instance 
> `struct Tmp {} *tmp;` will be separated as `struct Tmp {` and `} *tmp;`.  In 
> annotating, we only handle the relation of tokens in a single line, so we 
> cannot know whether `}` belongs to `struct` or not.

Interesting. Actually, it seems the splitting into multiple lines happens if 
we're in the `struct`, etc. case, but not if this was likely a braced list:

  % cat test.cc
  int i = int{16} * 1024;
  % ./clang-format -style=google -debug test.cc
  Args: ./clang-format -style=google -debug test.cc 
  File encoding: UTF8
  Language: C++
  
  Next [0] Token: int / int, Macro: 0
  Next [1] Token: identifier / i, Macro: 0
  Next [2] Token: equal / =, Macro: 0
  Next [3] Token: int / int, Macro: 0
  Next [4] Token: l_brace / {, Macro: 0
  Getting Position: 4
  Next [5] Token: numeric_constant / 16, Macro: 0
  Next [6] Token: r_brace / }, Macro: 0
  Next [7] Token: star / *, Macro: 0
  Setting Position: 4
  Next [5] Token: numeric_constant / 16, Macro: 0
  Next [6] Token: r_brace / }, Macro: 0
  Next [7] Token: star / *, Macro: 0
  Next [8] Token: numeric_constant / 1024, Macro: 0
  Next [9] Token: semi / ;, Macro: 0
  Next [10] Token: eof / , Macro: 0
  Line(0, FSC=0): int[T=108, OC=0] identifier[T=108, OC=4] equal[T=108, OC=6] 
int[T=108, OC=8] l_brace[T=108, OC=11] numeric_constant[T=108, OC=12] 
r_brace[T=108, OC=14] star[T=108, OC=16] numeric_constant[T=108, OC=18] 
semi[T=108, OC=22] 
  Line(0, FSC=0): eof[T=108, OC=0] 
  Run 0...
  Replacements for run 0:
  File encoding: UTF8
  Language: C++
  
  Next [0] Token: int / int, Macro: 0
  Next [1] Token: identifier / i, Macro: 0
  Next [2] Token: equal / =, Macro: 0
  Next [3] Token: int / int, Macro: 0
  Next [4] Token: l_brace / {, Macro: 0
  Getting Position: 4
  Next [5] Token: numeric_constant / 16, Macro: 0
  Next [6] Token: r_brace / }, Macro: 0
  Next [7] Token: star / *, Macro: 0
  Setting Position: 4
  Next [5] Token: numeric_constant / 16, Macro: 0
  Next [6] Token: r_brace / }, Macro: 0
  Next [7] Token: star / *, Macro: 0
  Next [8] Token: numeric_constant / 1024, Macro: 0
  Next [9] Token: semi / ;, Macro: 0
  Next [10] Token: eof / , Macro: 0
  Line(0, FSC=0): int[T=108, OC=0] identifier[T=108, OC=4] equal[T=108, OC=6] 
int[T=108, OC=8] l_brace[T=108, OC=11] numeric_constant[T=108, OC=12] 
r_brace[T=108, OC=14] star[T=108, OC=16] numeric_constant[T=108, OC=18] 
semi[T=108, OC=22] 
  Line(0, FSC=0): eof[T=108, OC=0] 
  Run 0...
  Replacements for run 0:
  File encoding: UTF8
  Language: C++
  
  Next [0] Token: int / int, Macro: 0
  Next [1] Token: identifier / i, Macro: 0
  Next [2] Token: equal / =, Macro: 0
  Next [3] Token: int / int, Macro: 0
  Next [4] Token: l_brace / {, Macro: 0
  Getting Position: 4
  Next [5] Token: numeric_constant / 16, Macro: 0
  Next [6] Token: r_brace / }, Macro: 0
  Next [7] Token: star / *, Macro: 0
  Setting Position: 4
  Next [5] Token: numeric_constant / 16, Macro: 0
  Next [6] Token: r_brace / }, Macro: 0
  Next [7] Token: star / *, Macro: 0
  Next [8] Token: numeric_constant / 1024, Macro: 0
  Next [9] Token: semi / ;, Macro: 0
  Next [10] Token: eof / , Macro: 0
  Line(0, FSC=0): int[T=108, OC=0] identifier[T=108, OC=4] equal[T=108, OC=6] 
int[T=108, OC=8] l_brace[T=108, OC=11] numeric_constant[T=108, OC=12] 
r_brace[T=108, OC=14] star[T=108, OC=16] numeric_constant[T=108, OC=18] 
semi[T=108, OC=22] 
  Line(0, FSC=0): eof[T=108, OC=0] 
  Run 0...
  AnnotatedTokens(L=0):
   M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=0 Name=int L=3 PPK=2 FakeLParens=2/ 
FakeRParens=0 II=0x1a4ea20 Text='int'
   M=0 C=1 T=StartOfName S=1 F=0 B=0 BK=0 P=220 Name=identifier L=5 PPK=2 
FakeLParens= FakeRParens=0 II=0x1a8d578 Text='i'
   M=0 C=0 T=BinaryOperator S=1 F=0 B=0 BK=0 P=22 Name=equal L=7 PPK=2 
FakeLParens= FakeRParens=0 II=0x0 Text='='
   M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=22 Name=int L=11 PPK=2 FakeLParens= 
FakeRParens=0 II=0x1a4ea20 Text='int'
   M=0 C=0 T=Unknown S=0 F=0 B=0 BK=2 P=23 Name=l_brace L=12 PPK=2 FakeLParens= 
FakeRParens=0 II=0x0 Text='{'
   M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=59 Name=numeric_constant L=14 PPK=2 
FakeLParens= FakeRParens=0 II=0x0 Text='16'
   M=0 C=0 T=Unknown S=0 F=0 B=0 BK=2 P=41 Name=r_brace L=15 PPK=2 FakeLParens= 
FakeRParens=0 II=0x0 Text='}'
   M=0 C=0 T=PointerOrReference S=0 F=0 B=0 BK=0 P=210 Name=star L=16 PPK=2 
FakeLParens= FakeRParens=0 II=0x0 Text='*'
   M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=34 Name=numeric_constant L=21 PPK=2 
FakeLParens= FakeRParens=1 II=0x0 Text='1024'
   M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=23 Name=semi L=22 PPK=2 FakeLParens= 
FakeRParens=0 II=0x0 Text=';'
  
  AnnotatedTokens(L=0):
   M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=0 Name=eof 

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

2022-07-14 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 added a comment.

I think we cannot identify `struct`, `union`, `class` or `enum` by the right 
bracket. Clang-format will split the input into multiple lines. For instance 
`struct Tmp {} *tmp;` will be separated as `struct Tmp {` and `} *tmp;`.  In 
annotating, we only handle the relation of tokens in a single line, so we 
cannot know whether `}` belongs to `struct` or not.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-07-14 Thread Krasimir Georgiev via Phabricator via cfe-commits
krasimir added a comment.

Appears that this regressed examples like this:

  // before
  int i = int{16} * 1024;
  // after
  int i = int{16}* 1024;

Naively it looks like in all added examples, the token preceding the matching 
`{` of the `}` is one of `struct`, `class`, `union`, `enum`, or a `>`. Could we 
adapt this heuristic to only apply `TT_PointerOrReference` in cases like this?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-29 Thread Jack Huang via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb646f0955574: [clang-format] Fix misplacement of `*` in 
declaration of pointer to struct (authored by Huang Zhen-Hong 
jackhunag1...@gmail.com, committed by jackhong12).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.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,32 @@
   Tokens = annotate("case :");
   EXPECT_EQ(Tokens.size(), 5u) << Tokens;
   EXPECT_TOKEN(Tokens[1], tok::amp, TT_UnaryOperator);
+
+  Tokens = annotate("struct {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  Tokens = annotate("union {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  Tokens = annotate("class {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+
+  Tokens = annotate("struct {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
+  Tokens = annotate("union {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
+  Tokens = annotate("class {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -10431,6 +10431,67 @@
   "void F();",
   getGoogleStyleWithColumns(68));
 
+  FormatStyle Style = getLLVMStyle();
+  Style.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("struct {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "}&& ptr = {};",
+   Style);
+  verifyFormat("union {\n"
+   "}&& ptr = {};",
+   Style);
+  verifyFormat("class {\n"
+   "}&& ptr = {};",
+   Style);
+
+  Style.PointerAlignment = FormatStyle::PAS_Middle;
+  verifyFormat("struct {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "} && ptr = {};",
+   Style);
+  verifyFormat("union {\n"
+   "} && ptr = {};",
+   Style);
+  verifyFormat("class {\n"
+   "} && ptr = {};",
+   Style);
+
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  verifyFormat("struct {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "} & = {};",
+   Style);
+  verifyFormat("union {\n"
+   "} & = {};",
+   Style);
+  verifyFormat("class {\n"
+   "} & = {};",
+   Style);
+
   verifyIndependentOfContext("MACRO(int *i);");
   verifyIndependentOfContext("MACRO(auto *a);");
   verifyIndependentOfContext("MACRO(const A *a);");
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2314,6 +2314,31 @@
 if (NextToken->isOneOf(tok::comma, tok::semi))
   return TT_PointerOrReference;
 
+// After right braces, star tokens are likely to be pointers to struct,
+// union, or class.
+//   struct {} *ptr;
+if (PrevToken->is(tok::r_brace) && Tok.is(tok::star))
+  return TT_PointerOrReference;
+
+// For "} &&"
+if (PrevToken->is(tok::r_brace) && Tok.is(tok::ampamp)) {
+  const FormatToken *MatchingLBrace = PrevToken->MatchingParen;
+
+  // We check whether there is a TemplateCloser(">") to indicate it's a
+  // template or not. If it's not a 

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

2022-06-28 Thread Björn Schäpers via Phabricator via cfe-commits
HazardyKnusperkeks added a comment.

In D127873#3614559 , @jackhong12 
wrote:

> Sorry, I don't have commit access. @HazardyKnusperkeks, could you help me 
> commit it?
>
> If I want to contribute to LLVM in the future, how do I get the commit 
> permission? Does it depend on the number of patches I submit?

https://llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access

I can push it for you, but if you want to get the commit access, you can wait 
and do it yourself. Just ask for it, I didn't have to wait long.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-28 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 added a comment.

Sorry, I don't have commit access. @HazardyKnusperkeks, could you help me 
commit it?

If I want to contribute to LLVM in the future, how do I get the commit 
permission? Does it depend on the number of patches I submit?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-26 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay accepted this revision.
MyDeveloperDay added a comment.
This revision is now accepted and ready to land.

LGTM


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-26 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 added a comment.

Got it. Thanks for your reply!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-25 Thread Björn Schäpers via Phabricator via cfe-commits
HazardyKnusperkeks added a comment.

But for now you should wait for @MyDeveloperDay that he at least removes the 
changes needed, or state what you have to change, or even better accept it.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-25 Thread Björn Schäpers via Phabricator via cfe-commits
HazardyKnusperkeks added a comment.

In D127873#3610106 , @jackhong12 
wrote:

> Hi. I passed the unit tests on my computer. But the build status here is 
> failed. The build log only shows the issue is related to `git reset --hard`. 
> So, I don't know where the bug is. Could you give me some hints to fix this 
> issue? Thanks!

If everything is fine on your side, and you can't see a problem related to you 
on the build bot, just push it. If it really breaks something you will notice. 
:)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-25 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 marked 2 inline comments as done.
jackhong12 added a comment.

Hi. I passed the unit tests on my computer. But the build status here is 
failed. The build log only shows the issue is related to `git reset --hard`. 
So, I don't know where the bug is. Could you give me some hints to fix this 
issue? Thanks!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-22 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 updated this revision to Diff 439091.
jackhong12 added a comment.

Right. It looks better.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.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,32 @@
   Tokens = annotate("case :");
   EXPECT_EQ(Tokens.size(), 5u) << Tokens;
   EXPECT_TOKEN(Tokens[1], tok::amp, TT_UnaryOperator);
+
+  Tokens = annotate("struct {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  Tokens = annotate("union {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  Tokens = annotate("class {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+
+  Tokens = annotate("struct {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
+  Tokens = annotate("union {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
+  Tokens = annotate("class {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -10431,6 +10431,67 @@
   "void F();",
   getGoogleStyleWithColumns(68));
 
+  FormatStyle Style = getLLVMStyle();
+  Style.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("struct {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "}&& ptr = {};",
+   Style);
+  verifyFormat("union {\n"
+   "}&& ptr = {};",
+   Style);
+  verifyFormat("class {\n"
+   "}&& ptr = {};",
+   Style);
+
+  Style.PointerAlignment = FormatStyle::PAS_Middle;
+  verifyFormat("struct {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "} && ptr = {};",
+   Style);
+  verifyFormat("union {\n"
+   "} && ptr = {};",
+   Style);
+  verifyFormat("class {\n"
+   "} && ptr = {};",
+   Style);
+
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  verifyFormat("struct {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "} & = {};",
+   Style);
+  verifyFormat("union {\n"
+   "} & = {};",
+   Style);
+  verifyFormat("class {\n"
+   "} & = {};",
+   Style);
+
   verifyIndependentOfContext("MACRO(int *i);");
   verifyIndependentOfContext("MACRO(auto *a);");
   verifyIndependentOfContext("MACRO(const A *a);");
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2304,6 +2304,31 @@
 if (NextToken->isOneOf(tok::comma, tok::semi))
   return TT_PointerOrReference;
 
+// After right braces, star tokens are likely to be pointers to struct,
+// union, or class.
+//   struct {} *ptr;
+if (PrevToken->is(tok::r_brace) && Tok.is(tok::star))
+  return TT_PointerOrReference;
+
+// For "} &&"
+if (PrevToken->is(tok::r_brace) && Tok.is(tok::ampamp)) {
+  const FormatToken *MatchingLBrace = PrevToken->MatchingParen;
+
+  // We check whether there is a TemplateCloser(">") to indicate it's a
+  // template or not. If it's not a template, "&&" is likely a reference
+  // operator.
+  //   struct {} & = {};
+  if (!MatchingLBrace)
+return TT_PointerOrReference;
+  FormatToken *BeforeLBrace = MatchingLBrace->getPreviousNonComment();
+  if (!BeforeLBrace || 

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

2022-06-22 Thread Owen Pan via Phabricator via cfe-commits
owenpan added a comment.

In D127873#3600978 , @jackhong12 
wrote:

> Thanks. I am not sure which modification will be better, the patch I 
> submitted or the following code.
>
>   FormatToken *BeforeLBraceToken = nullptr;
>   if (MatchingLBrace)
> BeforeLBraceToken = MatchingLBrace->getPreviousNonComment();
>   
>   if (BeforeLBraceToken && BeforeLBraceToken->is(TT_TemplateCloser))
> return TT_BinaryOperator;

How about the following?

  if (!MatchingLBrace)
return TT_PointerOrReference;
  const FormatToken *BeforeLBrace = MatchingLBrace->getPreviousNonComment();
  if (!BeforeLBrace || BeforeLBrace->isNot(TT_TemplateCloser))
return TT_PointerOrReference;
  return TT_BinaryOperator;


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2022-06-22 Thread Jack Huang via Phabricator via cfe-commits
jackhong12 updated this revision to Diff 438921.
jackhong12 marked an inline comment as done.
jackhong12 added a comment.

I have added a variable for `MatchingLBrace->getPreviousNonComment()`.

Thanks. I am not sure which modification will be better, the patch I submitted 
or the following code.

  FormatToken *BeforeLBraceToken = nullptr;
  if (MatchingLBrace)
BeforeLBraceToken = MatchingLBrace->getPreviousNonComment();
  
  if (BeforeLBraceToken && BeforeLBraceToken->is(TT_TemplateCloser))
return TT_BinaryOperator;


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.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,32 @@
   Tokens = annotate("case :");
   EXPECT_EQ(Tokens.size(), 5u) << Tokens;
   EXPECT_TOKEN(Tokens[1], tok::amp, TT_UnaryOperator);
+
+  Tokens = annotate("struct {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  Tokens = annotate("union {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  Tokens = annotate("class {\n"
+"} *ptr;");
+  EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+
+  Tokens = annotate("struct {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
+  Tokens = annotate("union {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
+  Tokens = annotate("class {\n"
+"} & = {};");
+  EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -10431,6 +10431,67 @@
   "void F();",
   getGoogleStyleWithColumns(68));
 
+  FormatStyle Style = getLLVMStyle();
+  Style.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("struct {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "}* ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "}&& ptr = {};",
+   Style);
+  verifyFormat("union {\n"
+   "}&& ptr = {};",
+   Style);
+  verifyFormat("class {\n"
+   "}&& ptr = {};",
+   Style);
+
+  Style.PointerAlignment = FormatStyle::PAS_Middle;
+  verifyFormat("struct {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "} * ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "} && ptr = {};",
+   Style);
+  verifyFormat("union {\n"
+   "} && ptr = {};",
+   Style);
+  verifyFormat("class {\n"
+   "} && ptr = {};",
+   Style);
+
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  verifyFormat("struct {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("union {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("class {\n"
+   "} *ptr;",
+   Style);
+  verifyFormat("struct {\n"
+   "} & = {};",
+   Style);
+  verifyFormat("union {\n"
+   "} & = {};",
+   Style);
+  verifyFormat("class {\n"
+   "} & = {};",
+   Style);
+
   verifyIndependentOfContext("MACRO(int *i);");
   verifyIndependentOfContext("MACRO(auto *a);");
   verifyIndependentOfContext("MACRO(const A *a);");
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2304,6 +2304,28 @@
 if (NextToken->isOneOf(tok::comma, tok::semi))
   return TT_PointerOrReference;
 
+// After right braces, star tokens are likely to be pointers to struct,
+// union, or class.
+//   struct {} *ptr;
+if (PrevToken->is(tok::r_brace) && Tok.is(tok::star))
+  return TT_PointerOrReference;
+
+// For "} &&"
+if (PrevToken->is(tok::r_brace) && Tok.is(tok::ampamp)) {
+  const 

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

2022-06-21 Thread Owen Pan via Phabricator via cfe-commits
owenpan accepted this revision.
owenpan added inline comments.



Comment at: clang/lib/Format/TokenAnnotator.cpp:2319-2320
+  //   enable_if<>{} && ...
+  if (MatchingLBrace && MatchingLBrace->getPreviousNonComment() &&
+  MatchingLBrace->getPreviousNonComment()->is(TT_TemplateCloser))
+return TT_BinaryOperator;

Can we have a variable for `MatchingLBrace->getPreviousNonComment()` so that 
the function doesn't get called twice?



Comment at: clang/lib/Format/TokenAnnotator.cpp:2324
+  //   struct {} & = {};
+  else
+return TT_PointerOrReference;

Nit: Don’t use `else` after a `return`.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127873/new/

https://reviews.llvm.org/D127873

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits