[PATCH] D116188: [clang-format] Fix short enums getting wrapped even when denied

2021-12-22 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid created this revision.
yodaldevoid added reviewers: HazardyKnusperkeks, MyDeveloperDay, curdeius, 
owenpan.
yodaldevoid requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Single-variant enums were still getting placed on a single line even
when AllowShortEnumsOnASingleLine was false. This fixes that by checking
that setting when looking to merge lines.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116188

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2511,6 +2511,10 @@
"  C\n"
"} ShortEnum1, ShortEnum2;",
Style);
+  verifyFormat("enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,16 @@
 
 // Try to merge a block with left brace wrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
+  const FormatToken *Tok = TheLine->First;
   bool ShouldMerge = false;
-  if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
+  if (Tok && Tok->is(tok::kw_typedef))
+Tok = Tok->getNextNonComment();
+  if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
 ShouldMerge = !Style.BraceWrapping.AfterClass ||
   (I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
+  } else if (Tok && Tok->is(tok::kw_enum)) {
+ShouldMerge = Style.AllowShortEnumsOnASingleLine;
   } else {
 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
   (I[1]->First->is(tok::r_brace) &&


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2511,6 +2511,10 @@
"  C\n"
"} ShortEnum1, ShortEnum2;",
Style);
+  verifyFormat("enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,16 @@
 
 // Try to merge a block with left brace wrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
+  const FormatToken *Tok = TheLine->First;
   bool ShouldMerge = false;
-  if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
+  if (Tok && Tok->is(tok::kw_typedef))
+Tok = Tok->getNextNonComment();
+  if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
 ShouldMerge = !Style.BraceWrapping.AfterClass ||
   (I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
+  } else if (Tok && Tok->is(tok::kw_enum)) {
+ShouldMerge = Style.AllowShortEnumsOnASingleLine;
   } else {
 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
   (I[1]->First->is(tok::r_brace) &&
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116189: [clang-format][NFC] Correct comment about checking merging of blocks

2021-12-22 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid created this revision.
yodaldevoid added reviewers: HazardyKnusperkeks, MyDeveloperDay, curdeius, 
owenpan.
yodaldevoid requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116189

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp


Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -391,7 +391,7 @@
   }
 }
 
-// Try to merge a block with left brace wrapped that wasn't yet covered
+// Try to merge a block with left brace unwrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
   bool ShouldMerge = false;
   if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {


Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -391,7 +391,7 @@
   }
 }
 
-// Try to merge a block with left brace wrapped that wasn't yet covered
+// Try to merge a block with left brace unwrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
   bool ShouldMerge = false;
   if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116189: [clang-format][NFC] Correct comment about checking merging of blocks

2021-12-22 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid added a comment.

I do not have commit access to the main repo. If someone who does could push it 
up I would appreciate it.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116189

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


[PATCH] D116188: [clang-format] Fix short enums getting wrapped even when denied

2021-12-23 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid updated this revision to Diff 396041.
yodaldevoid added a comment.

Addressed reviewer comments

- Removed unnecessary null checks
- Added tests for typedefs preceding short enums


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116188

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2504,6 +2504,7 @@
   FormatStyle Style = getLLVMStyle();
   Style.AllowShortEnumsOnASingleLine = true;
   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
+  verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
   Style.AllowShortEnumsOnASingleLine = false;
   verifyFormat("enum {\n"
"  A,\n"
@@ -2511,6 +2512,20 @@
"  C\n"
"} ShortEnum1, ShortEnum2;",
Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "  B,\n"
+   "  C\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,18 @@
 
 // Try to merge a block with left brace wrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
+  const FormatToken *Tok = TheLine->First;
   bool ShouldMerge = false;
-  if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
+  if (Tok->is(tok::kw_typedef)) {
+Tok = Tok->getNextNonComment();
+assert(Tok);
+  }
+  if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
 ShouldMerge = !Style.BraceWrapping.AfterClass ||
   (I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
+  } else if (Tok->is(tok::kw_enum)) {
+ShouldMerge = Style.AllowShortEnumsOnASingleLine;
   } else {
 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
   (I[1]->First->is(tok::r_brace) &&


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2504,6 +2504,7 @@
   FormatStyle Style = getLLVMStyle();
   Style.AllowShortEnumsOnASingleLine = true;
   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
+  verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
   Style.AllowShortEnumsOnASingleLine = false;
   verifyFormat("enum {\n"
"  A,\n"
@@ -2511,6 +2512,20 @@
"  C\n"
"} ShortEnum1, ShortEnum2;",
Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "  B,\n"
+   "  C\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,18 @@
 
 // Try to merge a block with left brace wrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
+  const FormatToken *Tok = TheLine->First;
   bool ShouldMerge = false;
-  if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
+  if (Tok->is(tok::kw_typedef)) {
+Tok = Tok->getNextNonComment();
+assert(Tok);
+  }
+  if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
 ShouldMerge = !Style.BraceWrapping.AfterClass ||
   (I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
+  } else if (Tok->is(tok::kw_enum)) {
+ShouldMerge = Style.AllowShortEnumsOnASingleLine;
   } else {
 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
 

[PATCH] D116229: [clang-format] Add option to align pointers in C style casts differently

2021-12-23 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid created this revision.
yodaldevoid added reviewers: HazardyKnusperkeks, MyDeveloperDay, curdeius, 
owenpan.
yodaldevoid requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

I personally prefer that pointers are aligned to the right in most cases to 
avoid problems when multiple variables are declared at once. That said, I 
prefer left alignment in C style casts to avoid the extra space and because 
there is no chance of the sorts of problems you see in declarations. This 
setting supports people with preferences like mine.

Currently this affects the alignment of both pointers and references in C style 
casts. If someone wanted to affect references separately then a separate 
setting for that, similar to ReferenceAlignmet, could be added.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116229

Files:
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/TokenAnnotator.h
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -15343,6 +15343,30 @@
Style));
 }
 
+TEST_F(FormatTest, AlignPointersInCasts) {
+  FormatStyle Style = getLLVMStyle();
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  verifyFormat("(const int (*(*foo)(const void *))[3]) a", Style);
+  Style.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("(const int (*(*foo)(const void*))[3]) a", Style);
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Right;
+  verifyFormat("(const int (*(*foo)(const void *))[3]) a", Style);
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Left;
+  verifyFormat("(const int (*(*foo)(const void*))[3]) a", Style);
+
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Pointer;
+  Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  verifyFormat("(const int (*(*foo)(const void &))[3]) a", Style);
+  Style.ReferenceAlignment = FormatStyle::RAS_Left;
+  verifyFormat("(const int (*(*foo)(const void&))[3]) a", Style);
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Right;
+  verifyFormat("(const int (*(*foo)(const void &))[3]) a", Style);
+  Style.ReferenceAlignment = FormatStyle::RAS_Right;
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Left;
+  verifyFormat("(const int (*(*foo)(const void&))[3]) a", Style);
+}
+
 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
   FormatStyle Alignment = getLLVMStyle();
   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
@@ -18817,6 +18841,13 @@
   FormatStyle::PAS_Right);
   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
   FormatStyle::PAS_Middle);
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Left;
+  CHECK_PARSE("PointerAlignmentInCast: Pointer", PointerAlignmentInCast,
+  FormatStyle::PACS_Pointer);
+  CHECK_PARSE("PointerAlignmentInCast: Left", PointerAlignmentInCast,
+  FormatStyle::PACS_Left);
+  CHECK_PARSE("PointerAlignmentInCast: Right", PointerAlignmentInCast,
+  FormatStyle::PACS_Right);
   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
   FormatStyle::RAS_Pointer);
Index: clang/lib/Format/TokenAnnotator.h
===
--- clang/lib/Format/TokenAnnotator.h
+++ clang/lib/Format/TokenAnnotator.h
@@ -199,6 +199,9 @@
   FormatStyle::PointerAlignmentStyle
   getTokenPointerOrReferenceAlignment(const FormatToken &PointerOrReference);
 
+  FormatStyle::PointerAlignmentStyle getTokenPointerOrReferenceAlignmentInCase(
+  const FormatToken &PointerOrReference);
+
   const FormatStyle &Style;
 
   const AdditionalKeywords &Keywords;
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3037,6 +3037,19 @@
 Right.Next->Next->is(TT_RangeBasedForLoopColon))
   return getTokenPointerOrReferenceAlignment(Right) !=
  FormatStyle::PAS_Left;
+// To find if we are currently in a C style cast we just have to follow the
+// linked list until one of three conditions. If we have either reached the
+// end of the list or find the opening parenthesis of a cast we can assume
+// we are not currently in a cast. If we instead find the right-most
+// parenthesis of a cast we know we are in a cast.
+if (!Left.isOneOf(TT_PointerOrReference, tok::l_paren))
+  for (const FormatToken *Tok = &Right;
+   Tok &&
+   (!Tok->MatchingParen || !Tok->MatchingParen->is(TT_CastRParen));
+   Tok = Tok->Next)
+if (Tok-

[PATCH] D116189: [clang-format][NFC] Correct comment about checking merging of blocks

2021-12-23 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid added a comment.

In D116189#3207717 , @MyDeveloperDay 
wrote:

> We need your name and email address to be able to commit as you

Ah, right. `arcanist` should keep that information, but I wish Phabricator 
would give a way to provide it as well.

At any rate, please use Gabriel Smith 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116189

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


[PATCH] D116188: [clang-format] Fix short enums getting wrapped even when denied

2021-12-23 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid updated this revision to Diff 396060.
yodaldevoid added a comment.

Correct commit email


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116188

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2504,6 +2504,7 @@
   FormatStyle Style = getLLVMStyle();
   Style.AllowShortEnumsOnASingleLine = true;
   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
+  verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
   Style.AllowShortEnumsOnASingleLine = false;
   verifyFormat("enum {\n"
"  A,\n"
@@ -2511,6 +2512,20 @@
"  C\n"
"} ShortEnum1, ShortEnum2;",
Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "  B,\n"
+   "  C\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,18 @@
 
 // Try to merge a block with left brace wrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
+  const FormatToken *Tok = TheLine->First;
   bool ShouldMerge = false;
-  if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
+  if (Tok->is(tok::kw_typedef)) {
+Tok = Tok->getNextNonComment();
+assert(Tok);
+  }
+  if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
 ShouldMerge = !Style.BraceWrapping.AfterClass ||
   (I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
+  } else if (Tok->is(tok::kw_enum)) {
+ShouldMerge = Style.AllowShortEnumsOnASingleLine;
   } else {
 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
   (I[1]->First->is(tok::r_brace) &&


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2504,6 +2504,7 @@
   FormatStyle Style = getLLVMStyle();
   Style.AllowShortEnumsOnASingleLine = true;
   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
+  verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
   Style.AllowShortEnumsOnASingleLine = false;
   verifyFormat("enum {\n"
"  A,\n"
@@ -2511,6 +2512,20 @@
"  C\n"
"} ShortEnum1, ShortEnum2;",
Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "  B,\n"
+   "  C\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
+  verifyFormat("typedef enum {\n"
+   "  A,\n"
+   "} ShortEnum1, ShortEnum2;",
+   Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,18 @@
 
 // Try to merge a block with left brace wrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
+  const FormatToken *Tok = TheLine->First;
   bool ShouldMerge = false;
-  if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
+  if (Tok->is(tok::kw_typedef)) {
+Tok = Tok->getNextNonComment();
+assert(Tok);
+  }
+  if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
 ShouldMerge = !Style.BraceWrapping.AfterClass ||
   (I[1]->First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
+  } else if (Tok->is(tok::kw_enum)) {
+ShouldMerge = Style.AllowShortEnumsOnASingleLine;
   } else {
 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
   (I[1]->First->is(tok::r_brace) &&
___
cfe

[PATCH] D116229: [clang-format] Add option to align pointers in C style casts differently

2021-12-23 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid updated this revision to Diff 396062.
yodaldevoid added a comment.

Correct commit email


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116229

Files:
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/TokenAnnotator.h
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -15343,6 +15343,30 @@
Style));
 }
 
+TEST_F(FormatTest, AlignPointersInCasts) {
+  FormatStyle Style = getLLVMStyle();
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  verifyFormat("(const int (*(*foo)(const void *))[3]) a", Style);
+  Style.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("(const int (*(*foo)(const void*))[3]) a", Style);
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Right;
+  verifyFormat("(const int (*(*foo)(const void *))[3]) a", Style);
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Left;
+  verifyFormat("(const int (*(*foo)(const void*))[3]) a", Style);
+
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Pointer;
+  Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
+  verifyFormat("(const int (*(*foo)(const void &))[3]) a", Style);
+  Style.ReferenceAlignment = FormatStyle::RAS_Left;
+  verifyFormat("(const int (*(*foo)(const void&))[3]) a", Style);
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Right;
+  verifyFormat("(const int (*(*foo)(const void &))[3]) a", Style);
+  Style.ReferenceAlignment = FormatStyle::RAS_Right;
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Left;
+  verifyFormat("(const int (*(*foo)(const void&))[3]) a", Style);
+}
+
 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
   FormatStyle Alignment = getLLVMStyle();
   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
@@ -18817,6 +18841,13 @@
   FormatStyle::PAS_Right);
   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
   FormatStyle::PAS_Middle);
+  Style.PointerAlignmentInCast = FormatStyle::PACS_Left;
+  CHECK_PARSE("PointerAlignmentInCast: Pointer", PointerAlignmentInCast,
+  FormatStyle::PACS_Pointer);
+  CHECK_PARSE("PointerAlignmentInCast: Left", PointerAlignmentInCast,
+  FormatStyle::PACS_Left);
+  CHECK_PARSE("PointerAlignmentInCast: Right", PointerAlignmentInCast,
+  FormatStyle::PACS_Right);
   Style.ReferenceAlignment = FormatStyle::RAS_Middle;
   CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment,
   FormatStyle::RAS_Pointer);
Index: clang/lib/Format/TokenAnnotator.h
===
--- clang/lib/Format/TokenAnnotator.h
+++ clang/lib/Format/TokenAnnotator.h
@@ -199,6 +199,9 @@
   FormatStyle::PointerAlignmentStyle
   getTokenPointerOrReferenceAlignment(const FormatToken &PointerOrReference);
 
+  FormatStyle::PointerAlignmentStyle getTokenPointerOrReferenceAlignmentInCase(
+  const FormatToken &PointerOrReference);
+
   const FormatStyle &Style;
 
   const AdditionalKeywords &Keywords;
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3037,6 +3037,19 @@
 Right.Next->Next->is(TT_RangeBasedForLoopColon))
   return getTokenPointerOrReferenceAlignment(Right) !=
  FormatStyle::PAS_Left;
+// To find if we are currently in a C style cast we just have to follow the
+// linked list until one of three conditions. If we have either reached the
+// end of the list or find the opening parenthesis of a cast we can assume
+// we are not currently in a cast. If we instead find the right-most
+// parenthesis of a cast we know we are in a cast.
+if (!Left.isOneOf(TT_PointerOrReference, tok::l_paren))
+  for (const FormatToken *Tok = &Right;
+   Tok &&
+   (!Tok->MatchingParen || !Tok->MatchingParen->is(TT_CastRParen));
+   Tok = Tok->Next)
+if (Tok->Next && Tok->Next->is(TT_CastRParen))
+  return getTokenPointerOrReferenceAlignmentInCase(Right) !=
+ FormatStyle::PAS_Left;
 return (
 (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
  (getTokenPointerOrReferenceAlignment(Right) != FormatStyle::PAS_Left ||
@@ -,5 +4457,20 @@
   return Style.PointerAlignment;
 }
 
+FormatStyle::PointerAlignmentStyle
+TokenAnnotator::getTokenPointerOrReferenceAlignmentInCase(
+const FormatToken &PointerOrReference) {
+  switch (Style.PointerAlignmentInCast) {
+  case FormatStyle::PACS_Pointer:
+return getTokenPointerOrReferenceAlignment(Pointer

[PATCH] D116189: [clang-format][NFC] Correct comment about checking merging of blocks

2021-12-23 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid updated this revision to Diff 396064.
yodaldevoid added a comment.

Correct commit email


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116189

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp


Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -391,7 +391,7 @@
   }
 }
 
-// Try to merge a block with left brace wrapped that wasn't yet covered
+// Try to merge a block with left brace unwrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
   bool ShouldMerge = false;
   if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {


Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -391,7 +391,7 @@
   }
 }
 
-// Try to merge a block with left brace wrapped that wasn't yet covered
+// Try to merge a block with left brace unwrapped that wasn't yet covered
 if (TheLine->Last->is(tok::l_brace)) {
   bool ShouldMerge = false;
   if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116188: [clang-format] Fix short enums getting wrapped even when denied

2021-12-24 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid added a comment.

In D116188#3208456 , 
@HazardyKnusperkeks wrote:

> Since you only upload a diff, there is no name or email. ;)

It had looked like arcanist was retaining this information, but I now realize 
what it was doing.

At any rate, I would appreciate it if someone could merge this for me as I 
don't have commit access.

Gabriel Smith 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116188

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


[PATCH] D116229: [clang-format] Add option to align pointers in C style casts differently

2021-12-24 Thread Gabriel Smith via Phabricator via cfe-commits
yodaldevoid abandoned this revision.
yodaldevoid added a comment.

In D116229#3209414 , @owenpan wrote:

> In D116229#3208507 , @curdeius 
> wrote:
>
>> Why this option is useful? Why would someone want to have a different 
>> alignment in casts than in other places?
>> As such I'm opposed to introducing one more option. Is there any well 
>> established code style that has something like this?
>
> Yes, we should adhere to the policy 
> 
>  when adding new options.

Understood. I'll close this out, then, as I don't know of a major project that 
needs this.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D116229

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