[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare marked an inline comment as done.
gedare added inline comments.



Comment at: clang/lib/Format/ContinuationIndenter.cpp:773-792
   if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
   (Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
(Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||

owenpan wrote:
> This is getting out of hand. Consider writing a lambda with early returns.
I would probably not get to refactoring this until January.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 558069.
gedare added a comment.

Use cameLCAse in test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25789,8 +25789,8 @@
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
@@ -25803,12 +25803,44 @@
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (camelCaseName < alsoLongName ||\n"
+   "  anotherEvenLongerName <=\n"
+   "  thisReallyReallyReallyReallyReallyReallyLongerName 
||"
+   "\n"
+   "  otherName < thisLastName) {\n"
+   "return;\n"
+   "  } else if (quiteLongName < alsoLongName ||\n"
+   " anotherEvenLongerName <=\n"
+   " 
thisReallyReallyReallyReallyReallyReallyLonger"
+   "Name ||\n"
+   " otherName < thisLastName) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) 
{\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/ContinuationIndenter.cpp
===
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -777,8 +777,10 @@
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+   !(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+tok::kw_switch) ||
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+  Previous.Previous->isIf( &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25789,8 +25789,8 @@
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
@@ -25803,12 +25803,44 @@
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (camelCaseName < alsoLongName ||\n"
+   "  anotherEvenLongerName <=\n"
+   "  thisReallyReallyReallyReallyReallyReallyLongerName ||"
+   "\n"
+   "  otherName < thisLastName) {\n"
+   "return;\n"
+   "  } else if (qui

[PATCH] D156360: [clang-format] Support function and overloaded operator SpacesInParensOption

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

ping


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156360

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


[PATCH] D156360: [clang-format] Support function and overloaded operator SpacesInParensOption

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 558066.
gedare added a comment.

Rebase to D155529 .


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156360

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11064,7 +11064,8 @@
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
-  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InFunctionDeclarations = true;
+  Spaces.SpacesInParensOptions.InOverloadedOperators = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13631,6 +13632,7 @@
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
   SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
   SpaceBetweenBraces.SpacesInParensOptions.Other = true;
+  SpaceBetweenBraces.SpacesInParensOptions.InFunctionCalls = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -16685,6 +16687,7 @@
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
   Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  Spaces.SpacesInParensOptions.InFunctionCalls = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16712,7 +16715,7 @@
"}",
Spaces);
   verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
-  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
+  verifyFormat("void __attribute__( ( x ) ) foo(int y) { return; }", Spaces);
   verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
 
   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
@@ -16727,6 +16730,142 @@
   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
   verifyFormat("#define x (( int )-1)", Spaces);
 
+  // Run the first set of tests again with:
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InFunctionDeclarations = true;
+  verifyFormat("do_something(::globalVar);", Spaces);
+  verifyFormat("call(x, y, z);", Spaces);
+  verifyFormat("call();", Spaces);
+  verifyFormat("std::function callback;", Spaces);
+  verifyFormat("void inFunction() { std::function fct; }",
+   Spaces);
+  verifyFormat("while ((bool)1)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("for (;;)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("if (true)\n"
+   "  f();\n"
+   "else if (true)\n"
+   "  f();",
+   Spaces);
+  verifyFormat("do {\n"
+   "  do_something((int)i);\n"
+   "} while (something());",
+   Spaces);
+  verifyFormat("switch (x) {\n"
+   "default:\n"
+   "  break;\n"
+   "}",
+   Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo( int bar );", Spaces);
+  verifyFormat("void f( int g ) __attribute__((asdf));", Spaces);
+  verifyFormat("int f();", Spaces);
+  verifyFormat("void f(int a, T b) {}", Spaces);
+  verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", Spaces);
+  verifyFormat("A::A() : a(1) {}", Spaces);
+  verifyFormat("void f( int bar ) __attribute__((asdf));", Spaces);
+  verifyFormat("void __attribute__((asdf)) f( int bar );", Spaces);
+  verifyFormat("#define A(x) x", Spaces);
+  verifyFormat("#define A (x) x", Spaces);
+  verifyFormat("#if defined(x)\n"
+   "#endif",
+   Spaces);
+  verifyFormat("auto i = std::make_unique(5);", Spaces);
+  verifyFormat("size_t x = sizeof(x);", Spaces);
+  verifyFormat("auto f( int x ) -> decltype(x);", Spaces);
+  verifyFormat("auto f( int x ) -> typeof(x);", Spaces);
+  verifyFormat("auto f( int x ) -> _Atomic(x);", Spaces);
+  verifyFormat("auto f( int x ) -> __underlying_type(x);", Spaces);
+  verifyFormat("int f( T x ) noexcept(x.create());", Spaces);
+  verifyFormat("alignas(128) char a[128];", Spaces);
+  verifyFormat("size_t x = alignof(MyType);", Spaces);
+  verifyFormat("

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 558064.
gedare added a comment.

Rebase to main. Fix up use of TT_AttributeParen.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16680,6 +16680,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16754,6 +16755,12 @@
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  verifyFormat("void f( ) __attribute__( ( asdf ) );", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -230,6 +230,7 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
@@ -625,19 +626,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3967,10 +3967,19 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.isOneOf(TT_AttributeLParen, TT_AttributeRParen) ||
+Right.isOneOf(TT_AttributeLParen, TT_AttributeRParen) ||
+(Left.Previous && Left.Previous->isOneOf(TT_AttributeLParen,
+ TT_AttributeRParen)) ||
+(Right.Next && Right.Next->isOneOf(TT_AttributeLParen,
+

[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

@MyDeveloperDay ping

I have rebased to main without any problems. This patch fixes the bug.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 558063.
gedare added a comment.

Rebase to main


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25809,6 +25809,38 @@
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername 
||"
+   "\n"
+   "  othername < thislastname) {\n"
+   "return;\n"
+   "  } else if (quitelongname < alsolongname ||\n"
+   " anotherevenlongername <=\n"
+   " 
thisreallyreallyreallyreallyreallyreallylonger"
+   "name ||\n"
+   " othername < thislastname) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) 
{\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/ContinuationIndenter.cpp
===
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -777,8 +777,10 @@
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+   !(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+tok::kw_switch) ||
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+  Previous.Previous->isIf( &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25809,6 +25809,38 @@
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername ||"
+   "\n"
+   "  othername < thislastname) {\n"
+   "return;\n"
+   "  } else if (quitelongname < alsolongname ||\n"
+   " anotherevenlongername <=\n"
+   " thisreallyreallyreallyreallyreallyreallylonger"
+   "name ||\n"
+   " othername < thislastname) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/ContinuationIndenter.cpp
===
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -777,8 +777,10 @@
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
- 

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare abandoned this revision.
gedare added a comment.

This is not needed due to https://github.com/llvm/llvm-project/pull/70768


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D156370: [clang-format] Fix bug with parsing of function/variable names.

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 558061.
gedare added a comment.

Add TokenAnnotator tests and light refactoring.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156370

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
@@ -2313,6 +2313,7 @@
 TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
   auto Tokens = annotate("bool foo __attribute__((unused));");
   ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
   EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
   EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_Unknown);
   EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_Unknown);
@@ -2323,6 +2324,22 @@
   EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
   EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
 
+  Tokens = annotate("bool __attribute__((unused)) foo;");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_AttributeLParen);
+  EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_AttributeRParen);
+  EXPECT_TOKEN(Tokens[7], tok::identifier, TT_StartOfName);
+
+  Tokens = annotate("void __attribute__((x)) Foo();");
+  ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+  EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_AttributeLParen);
+  EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_AttributeRParen);
+  EXPECT_TOKEN(Tokens[7], tok::identifier, TT_FunctionDeclarationName);
+
   FormatStyle Style = getLLVMStyle();
   Style.AttributeMacros.push_back("FOO");
   Tokens = annotate("bool foo FOO(unused);", Style);
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16387,8 +16387,10 @@
 
   verifyFormat("int f ();", SpaceFuncDecl);
   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
+  verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
+  verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
   verifyFormat("#define A(x) x", SpaceFuncDecl);
   verifyFormat("#define A (x) x", SpaceFuncDecl);
   verifyFormat("#if defined(x)\n"
@@ -16422,8 +16424,10 @@
 
   verifyFormat("int f();", SpaceFuncDef);
   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
+  verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
+  verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
   verifyFormat("#define A(x) x", SpaceFuncDef);
   verifyFormat("#define A (x) x", SpaceFuncDef);
   verifyFormat("#if defined(x)\n"
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2220,7 +2220,8 @@
 }
 
 if (PreviousNotConst->is(tok::r_paren) &&
-PreviousNotConst->is(TT_TypeDeclarationParen)) {
+(PreviousNotConst->isOneOf(TT_TypeDeclarationParen,
+   TT_AttributeRParen))) {
   return true;
 }
 


Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2313,6 +2313,7 @@
 TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
   auto Tokens = annotate("bool foo __attribute__((unused));");
   ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
   EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
   EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_Unknown);
   EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_Unknown);
@@ -2323,6 +2324,22 @@
   EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
   EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
 
+  Tokens = annotate("bool __attribute__((unused)) foo;");
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_AttributeLParen);
+  EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_AttributeRParen);
+  EXPECT_TOKEN(Tokens[7], to

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-08-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D154550#4562061 , @owenpan wrote:

> In D154550#4529346 , @gedare wrote:
>
>> In D154550#4526386 , @owenpan 
>> wrote:
>>
>>> Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
>>> misleading if it excludes the former but not the latter. IMO we should just 
>>> fix the bug without extending the option because any loop with a 
>>> single-statement body is a short loop.
>>
>> Agreed, except that many style guides (notably, K&R, LLVM, and Google) treat 
>> these two cases differently.
>
> LLVM doesn't merge short loops. Google uses `{}` instead of `;` whereas AFAIK 
> K&R does the opposite. I don't know of any major style that requires breaking 
> before the null statement and merging the empty block.

Got it. I can prepare a change to merge null as a short loop. I will get back 
to this in a couple weeks.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-08-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D154550#4562061 , @owenpan wrote:

> In D154550#4529346 , @gedare wrote:
>
>> In D154550#4526386 , @owenpan 
>> wrote:
>>
>>> Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
>>> misleading if it excludes the former but not the latter. IMO we should just 
>>> fix the bug without extending the option because any loop with a 
>>> single-statement body is a short loop.
>>
>> Agreed, except that many style guides (notably, K&R, LLVM, and Google) treat 
>> these two cases differently.
>
> LLVM doesn't merge short loops. Google uses `{}` instead of `;` whereas AFAIK 
> K&R does the opposite. I don't know of any major style that requires breaking 
> before the null statement and merging the empty block.

https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching

Empty loop bodies should use either an empty pair of braces or continue with no 
braces, rather than a single semicolon.

  while (condition) {}  // Good - `{}` indicates no logic.
  while (condition) {
// Comments are okay, too
  }
  while (condition) continue;  // Good - `continue` indicates no logic.
  while (condition);  // Bad - looks like part of `do-while` loop.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D156360: [clang-format] Support function and overloaded operator SpacesInParensOption

2023-08-01 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:16786
   verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
-  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
+  verifyFormat("void __attribute__( ( x ) ) foo(int y) { return; }", Spaces);
   verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);

HazardyKnusperkeks wrote:
> Why change this?
The original test is incomplete/ambiguous. It's either a declaration missing a 
semicolon, or it's the start of a definition. I made it a definition.



Comment at: clang/unittests/Format/FormatTest.cpp:16904
+  verifyFormat("call( x, y, z );", Spaces);
+  verifyFormat("call();", Spaces);
+  verifyFormat("std::function callback;", Spaces);

HazardyKnusperkeks wrote:
> Should here be a space?
That's controlled by space in empty parens.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156360

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


[PATCH] D156370: [clang-format] Fix bug with parsing of function/variable names.

2023-07-26 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D156370#4536793 , 
@HazardyKnusperkeks wrote:

> Does this result in a different annotation? Could you add a test for that?

If I understand you correctly, it does, for example:
`$ echo "void __attribute__((naked)) foo(int bar);" | clang-format -style=llvm 
-debug 2>&1 | grep \'foo\'`

Without the change:
 M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=23 Name=identifier L=31 PPK=2 
FakeLParens= FakeRParens=0 II=0x555d17efdce0 Text='foo'

With the change:
 M=0 C=1 T=FunctionDeclarationName S=1 F=0 B=0 BK=0 P=80 Name=identifier L=31 
PPK=2 FakeLParens= FakeRParens=0 II=0x559855502ce0 Text='foo'

I don't how to test for that.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156370

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


[PATCH] D156360: [clang-format] Support function and overloaded operator SpacesInParensOption

2023-07-26 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 544544.
gedare added a comment.

Add tests and correct function call detection.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156360

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11040,7 +11040,8 @@
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
-  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InFunctionDeclarations = true;
+  Spaces.SpacesInParensOptions.InOverloadedOperators = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13678,6 +13679,7 @@
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
   SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
   SpaceBetweenBraces.SpacesInParensOptions.Other = true;
+  SpaceBetweenBraces.SpacesInParensOptions.InFunctionCalls = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -16753,6 +16755,7 @@
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
   Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  Spaces.SpacesInParensOptions.InFunctionCalls = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16780,7 +16783,7 @@
"}",
Spaces);
   verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
-  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
+  verifyFormat("void __attribute__( ( x ) ) foo(int y) { return; }", Spaces);
   verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
 
   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
@@ -16795,6 +16798,142 @@
   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
   verifyFormat("#define x (( int )-1)", Spaces);
 
+  // Run the first set of tests again with:
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InFunctionDeclarations = true;
+  verifyFormat("do_something(::globalVar);", Spaces);
+  verifyFormat("call(x, y, z);", Spaces);
+  verifyFormat("call();", Spaces);
+  verifyFormat("std::function callback;", Spaces);
+  verifyFormat("void inFunction() { std::function fct; }",
+   Spaces);
+  verifyFormat("while ((bool)1)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("for (;;)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("if (true)\n"
+   "  f();\n"
+   "else if (true)\n"
+   "  f();",
+   Spaces);
+  verifyFormat("do {\n"
+   "  do_something((int)i);\n"
+   "} while (something());",
+   Spaces);
+  verifyFormat("switch (x) {\n"
+   "default:\n"
+   "  break;\n"
+   "}",
+   Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo( int bar );", Spaces);
+  verifyFormat("void f( int g ) __attribute__((asdf));", Spaces);
+  verifyFormat("int f();", Spaces);
+  verifyFormat("void f(int a, T b) {}", Spaces);
+  verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", Spaces);
+  verifyFormat("A::A() : a(1) {}", Spaces);
+  verifyFormat("void f( int bar ) __attribute__((asdf));", Spaces);
+  verifyFormat("void __attribute__((asdf)) f( int bar );", Spaces);
+  verifyFormat("#define A(x) x", Spaces);
+  verifyFormat("#define A (x) x", Spaces);
+  verifyFormat("#if defined(x)\n"
+   "#endif",
+   Spaces);
+  verifyFormat("auto i = std::make_unique(5);", Spaces);
+  verifyFormat("size_t x = sizeof(x);", Spaces);
+  verifyFormat("auto f( int x ) -> decltype(x);", Spaces);
+  verifyFormat("auto f( int x ) -> typeof(x);", Spaces);
+  verifyFormat("auto f( int x ) -> _Atomic(x);", Spaces);
+  verifyFormat("auto f( int x ) -> __underlying_type(x);", Spaces);
+  verifyFormat("int f( T x ) noexcept(x.create());", Spaces);
+  verifyFormat("alignas(128) char a[128];", Spaces);
+  verifyFormat("size_t x = alignof(MyType);", Spaces);
+  verifyFormat("static_

[PATCH] D156370: [clang-format] Fix bug with parsing of function/variable names.

2023-07-26 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

Function and variable names are not detected correctly when there is an
__attribute__((x)) preceding the name.

Fixes Github Issue 64137


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156370

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16455,8 +16455,10 @@
 
   verifyFormat("int f ();", SpaceFuncDecl);
   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
+  verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
+  verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
   verifyFormat("#define A(x) x", SpaceFuncDecl);
   verifyFormat("#define A (x) x", SpaceFuncDecl);
   verifyFormat("#if defined(x)\n"
@@ -16490,8 +16492,10 @@
 
   verifyFormat("int f();", SpaceFuncDef);
   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
+  verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
+  verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
   verifyFormat("#define A(x) x", SpaceFuncDef);
   verifyFormat("#define A (x) x", SpaceFuncDef);
   verifyFormat("#if defined(x)\n"
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2208,7 +2208,8 @@
 }
 
 if (PreviousNotConst->is(tok::r_paren) &&
-PreviousNotConst->is(TT_TypeDeclarationParen)) {
+(PreviousNotConst->is(TT_TypeDeclarationParen) ||
+ PreviousNotConst->is(TT_AttributeParen))) {
   return true;
 }
 


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16455,8 +16455,10 @@
 
   verifyFormat("int f ();", SpaceFuncDecl);
   verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
+  verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
   verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
   verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
+  verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
   verifyFormat("#define A(x) x", SpaceFuncDecl);
   verifyFormat("#define A (x) x", SpaceFuncDecl);
   verifyFormat("#if defined(x)\n"
@@ -16490,8 +16492,10 @@
 
   verifyFormat("int f();", SpaceFuncDef);
   verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
+  verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
   verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
   verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
+  verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
   verifyFormat("#define A(x) x", SpaceFuncDef);
   verifyFormat("#define A (x) x", SpaceFuncDef);
   verifyFormat("#if defined(x)\n"
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2208,7 +2208,8 @@
 }
 
 if (PreviousNotConst->is(tok::r_paren) &&
-PreviousNotConst->is(TT_TypeDeclarationParen)) {
+(PreviousNotConst->is(TT_TypeDeclarationParen) ||
+ PreviousNotConst->is(TT_AttributeParen))) {
   return true;
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156360: [clang-format] Support function and overloaded operator SpacesInParensOption

2023-07-26 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

This change separates function calls, declarations, definitions, and
overloaded operators from `SpacesInParensOptions.Other` to allow control
over each independently.

Fixes Github Issue \#55428.

Depends on D155529 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156360

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11040,7 +11040,8 @@
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
-  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InFunctionDeclarations = true;
+  Spaces.SpacesInParensOptions.InOverloadedOperators = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13678,6 +13679,7 @@
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
   SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
   SpaceBetweenBraces.SpacesInParensOptions.Other = true;
+  SpaceBetweenBraces.SpacesInParensOptions.InFunctionCalls = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -16749,6 +16751,7 @@
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
   Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  Spaces.SpacesInParensOptions.InFunctionCalls = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -23819,6 +23822,7 @@
   Style.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
   Style.SpacesInParensOptions.InCStyleCasts = false;
+  Style.SpacesInParensOptions.InFunctionCalls = true;
   Style.SpacesInParensOptions.Other = true;
   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -226,6 +226,10 @@
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionCalls);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionDeclarations);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InFunctionDefinitions);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InOverloadedOperators);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other);
 }
 
@@ -600,23 +604,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, true, false, false,
-  true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false, true,
+true, true, true, true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
   FormatStyle::SpacesInParensCustom(false, true, false, false,
-  false));
+false, false, false, false, false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
   FormatStyle::SpacesInParensCustom(false, false, true, false,
-  false));
+false, false, false, false, false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
   FormatStyle::SpacesInParensCustom(false, false, false, true,
-  false));
+false, false, false, false, false));
   Style.SpacesInParens = F

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-26 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 544466.
gedare added a comment.

Rebase to 18


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16748,6 +16748,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16822,6 +16823,12 @@
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  verifyFormat("void f( ) __attribute__( ( asdf ) );", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -222,6 +222,7 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
@@ -599,19 +600,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3813,10 +3813,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-24 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 543729.
gedare added a comment.

Add a release note.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16748,6 +16748,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16822,6 +16823,12 @@
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  verifyFormat("void f( ) __attribute__( ( asdf ) );", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -217,6 +217,7 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
@@ -594,19 +595,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index

[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-24 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 543727.
gedare added a comment.

Add a release note.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/FormatTestVerilog.cpp

Index: clang/unittests/Format/FormatTestVerilog.cpp
===
--- clang/unittests/Format/FormatTestVerilog.cpp
+++ clang/unittests/Format/FormatTestVerilog.cpp
@@ -807,7 +807,8 @@
"  if (x)\n"
"x = x;",
Style);
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("if ( x )\n"
"  x = x;\n"
"else if ( x )\n"
@@ -903,7 +904,8 @@
   verifyFormat("repeat (x) begin\n"
"end");
   auto Style = getDefaultStyle();
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("foreach ( x[x] )\n"
"  x = x;",
Style);
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11031,14 +11031,16 @@
   verifyFormat("template  void operator=(T) && {}", AlignMiddle);
 
   FormatStyle Spaces = getLLVMStyle();
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
-  Spaces.SpacesInCStyleCastParentheses = false;
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParensOptions.InCStyleCasts = false;
+  Spaces.SpacesInParensOptions.Other = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13674,7 +13676,8 @@
 
   FormatStyle SpaceBetweenBraces = getLLVMStyle();
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
-  SpaceBetweenBraces.SpacesInParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.Other = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -13697,7 +13700,8 @@
 "};",
 format("vectorx{1,2,3,4,};", SpaceBetweenBraces));
   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
-  SpaceBetweenBraces.SpaceInEmptyParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
 }
 
@@ -16707,10 +16711,43 @@
   verifyFormat("! ! x", Spaces);
 }
 
-TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+TEST_F(FormatTest, ConfigurableSpacesInParens) {
   FormatStyle Spaces = getLLVMStyle();
 
-  Spaces.SpacesInParentheses = true;
+  verifyFormat("do_something(::globalVar);", Spaces);
+  verifyFormat("call(x, y, z);", Spaces);
+  verifyFormat("call();", Spaces);
+  verifyFormat("std::function callback;", Spaces);
+  verifyFormat("void inFunction() { std::function fct; }",
+   Spaces);
+  verifyFormat("while ((bool)1)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("for (;;)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("if (true)\n"
+   "  f();\n"
+   "else if (true)\n"
+   "  f();",
+   Spaces);
+  verifyFormat("do {\n"
+   "  do_something((int)i);\n"
+   "} while (something());",
+   Spaces);
+  verifyFormat("switch (x) {\n"
+   "default:\n"
+   "  break;\n"
+   "}",
+   Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
+  verifyFormat("void f() __attribute__((asdf));", Spaces);
+
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-24 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 543721.
gedare added a comment.
This revision is now accepted and ready to land.

Change option name from NonEmpty to NonNullStatement. Fix space before semi.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1380,7 +1380,8 @@
 
 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
   FormatStyle AllowsMergedLoops = getLLVMStyle();
-  AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_NonNullStatement;
   verifyFormat("while (true) continue;", AllowsMergedLoops);
   verifyFormat("for (;;) continue;", AllowsMergedLoops);
   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
@@ -1436,6 +1437,66 @@
"  while (true);\n"
"}",
AllowsMergedLoops);
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_All;
+  verifyFormat("while (true) continue;", AllowsMergedLoops);
+  verifyFormat("for (;;) continue;", AllowsMergedLoops);
+  verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("while (true);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  while (true);",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int &v, vec)\n"
+   "  for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  BOOST_FOREACH (int &v, vec);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) // Can't merge this\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) /* still don't merge */\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("do a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do /* Don't merge */\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do\n"
+   "  // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  // Without braces labels are interpreted differently.
+  verifyFormat("{\n"
+   "  do\n"
+   "  label:\n"
+   "a++;\n"
+   "  while (true);\n"
+   "}",
+   AllowsMergedLoops);
 }
 
 TEST_F(FormatTest, FormatShortBracedStatements) {
@@ -1443,7 +1504,8 @@
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
 FormatStyle::SIS_Never);
-  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
+  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine,
+FormatStyle::SWFLS_Never);
   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
   verifyFormat("for (;;) {\n"
"  f();\n"
@@ -1488,7 +1550,8 @@
   AllowSimpleBracedStatements.ColumnLimit = 40;
   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
   FormatStyle::SBS_Always;
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_NonNullStatement;
   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
@@ -1605,7 +1668,8 @@
"}",
AllowSimpleBracedStatements);
 
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  Form

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-24 Thread Gedare Bloom via Phabricator via cfe-commits
gedare planned changes to this revision.
gedare added a comment.

In D154550#4526386 , @owenpan wrote:

> Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
> misleading if it excludes the former but not the latter. IMO we should just 
> fix the bug without extending the option because any loop with a 
> single-statement body is a short loop.

Agreed, except that many style guides (notably, K&R, LLVM, and Google) treat 
these two cases differently. The loop with only a semi-colon is a special case, 
and is what I'm looking to support. K&R defines the semi-colon terminated 
for/while loop as a body composed of a //null statement//. I believe that `{ }` 
is instead an empty body, as it does not parse as a statement, despite 
generating identical code. Maybe`NonNullStatement` is precise and better than 
`NonEmpty`?  I will prepare that change, and fix the spacing before the `;`.

Whether someone thinks collapsing bracketed bodies that are short should be 
supported is a separate problem, and even if so, it would require a similar 
style option change as I suggest to allow for backward compatibility. I'm not 
personally interested in that case, as the code base I work with uses the 
isolated semicolon.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542655.
gedare added a comment.

Add missing parser check.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16748,6 +16748,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16822,6 +16823,12 @@
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  verifyFormat("void f( ) __attribute__( ( asdf ) );", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -217,6 +217,7 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
@@ -594,19 +595,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Forma

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542651.
gedare added a comment.

Add one more attribute test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16748,6 +16748,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16822,6 +16823,12 @@
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  verifyFormat("void f( ) __attribute__( ( asdf ) );", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -594,19 +594,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -713,6 +713,7 @@
 
 template <> struct MappingTraits {
   static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) {
+IO.mapOptional("InAttributeSpecifiers", Spaces.InAttributeSpecifiers);
 IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
 IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
 IO.mapOptional("InEmptyParentheses", Spa

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542650.
gedare added a comment.

Rebase onto D155239 .


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16748,6 +16748,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16822,6 +16823,11 @@
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
   verifyFormat("void f( ) __attribute__((asdf));", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -594,19 +594,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -713,6 +713,7 @@
 
 template <> struct MappingTraits {
   static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) {
+IO.mapOptional("InAttributeSpecifiers", Spaces.InAttributeSpecifiers);
 IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
 IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
 IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses);
@@ -1140,6 +1

[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D155239#4520067 , 
@HazardyKnusperkeks wrote:

> Everything is fine, I just need to know how the attribute stuff is formatted 
> with plain LLVM style.

`__attribute__` is formatted with plain LLVM style in 5-10 other test cases.  
For plain llvm style probably the best is `UnderstandsAttributes`. I did add 
the test cases at the start of `ConfigurableSpacesInParens` with plain llvm 
style to establish a baseline.

In reviewing those test cases I did see a good reason to add another test of 
attribute placement. I have now covered function decl attributes before and 
after the function name, and variable attributes.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

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


[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542649.
gedare added a comment.

Add more __attribute__ test cases.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/FormatTestVerilog.cpp

Index: clang/unittests/Format/FormatTestVerilog.cpp
===
--- clang/unittests/Format/FormatTestVerilog.cpp
+++ clang/unittests/Format/FormatTestVerilog.cpp
@@ -807,7 +807,8 @@
"  if (x)\n"
"x = x;",
Style);
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("if ( x )\n"
"  x = x;\n"
"else if ( x )\n"
@@ -903,7 +904,8 @@
   verifyFormat("repeat (x) begin\n"
"end");
   auto Style = getDefaultStyle();
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("foreach ( x[x] )\n"
"  x = x;",
Style);
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11031,14 +11031,16 @@
   verifyFormat("template  void operator=(T) && {}", AlignMiddle);
 
   FormatStyle Spaces = getLLVMStyle();
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
-  Spaces.SpacesInCStyleCastParentheses = false;
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParensOptions.InCStyleCasts = false;
+  Spaces.SpacesInParensOptions.Other = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13674,7 +13676,8 @@
 
   FormatStyle SpaceBetweenBraces = getLLVMStyle();
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
-  SpaceBetweenBraces.SpacesInParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.Other = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -13697,7 +13700,8 @@
 "};",
 format("vectorx{1,2,3,4,};", SpaceBetweenBraces));
   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
-  SpaceBetweenBraces.SpaceInEmptyParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
 }
 
@@ -16707,10 +16711,43 @@
   verifyFormat("! ! x", Spaces);
 }
 
-TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+TEST_F(FormatTest, ConfigurableSpacesInParens) {
   FormatStyle Spaces = getLLVMStyle();
 
-  Spaces.SpacesInParentheses = true;
+  verifyFormat("do_something(::globalVar);", Spaces);
+  verifyFormat("call(x, y, z);", Spaces);
+  verifyFormat("call();", Spaces);
+  verifyFormat("std::function callback;", Spaces);
+  verifyFormat("void inFunction() { std::function fct; }",
+   Spaces);
+  verifyFormat("while ((bool)1)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("for (;;)\n"
+   "  continue;",
+   Spaces);
+  verifyFormat("if (true)\n"
+   "  f();\n"
+   "else if (true)\n"
+   "  f();",
+   Spaces);
+  verifyFormat("do {\n"
+   "  do_something((int)i);\n"
+   "} while (something());",
+   Spaces);
+  verifyFormat("switch (x) {\n"
+   "default:\n"
+   "  break;\n"
+   "}",
+   Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
+  verifyFormat("void f() __attribute__((asdf));", Spaces);
+
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensO

[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542632.
gedare added a comment.

fix typo


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/FormatTestVerilog.cpp

Index: clang/unittests/Format/FormatTestVerilog.cpp
===
--- clang/unittests/Format/FormatTestVerilog.cpp
+++ clang/unittests/Format/FormatTestVerilog.cpp
@@ -807,7 +807,8 @@
"  if (x)\n"
"x = x;",
Style);
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("if ( x )\n"
"  x = x;\n"
"else if ( x )\n"
@@ -903,7 +904,8 @@
   verifyFormat("repeat (x) begin\n"
"end");
   auto Style = getDefaultStyle();
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("foreach ( x[x] )\n"
"  x = x;",
Style);
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11031,14 +11031,16 @@
   verifyFormat("template  void operator=(T) && {}", AlignMiddle);
 
   FormatStyle Spaces = getLLVMStyle();
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
-  Spaces.SpacesInCStyleCastParentheses = false;
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParensOptions.InCStyleCasts = false;
+  Spaces.SpacesInParensOptions.Other = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13674,7 +13676,8 @@
 
   FormatStyle SpaceBetweenBraces = getLLVMStyle();
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
-  SpaceBetweenBraces.SpacesInParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.Other = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -13697,7 +13700,8 @@
 "};",
 format("vectorx{1,2,3,4,};", SpaceBetweenBraces));
   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
-  SpaceBetweenBraces.SpaceInEmptyParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
 }
 
@@ -16707,10 +16711,13 @@
   verifyFormat("! ! x", Spaces);
 }
 
-TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+TEST_F(FormatTest, ConfigurableSpacesInParens) {
   FormatStyle Spaces = getLLVMStyle();
 
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16737,9 +16744,12 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
 
-  Spaces.SpacesInParentheses = false;
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Type *A = ( Type * )P;", Spaces);
   verifyFormat("Type *A = ( vector )P;", Spaces);
   verifyFormat("x = ( int32 )y;", Spaces);
@@ -16750,9 +16760,10 @@
   verifyFormat("#define x (( int )-1)", Spaces);
 
   // Run the first set of tests again with:
-  Spaces.SpacesInParentheses = false;
-  Spaces.SpaceInEmptyParentheses = true

[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D155239#4520067 , 
@HazardyKnusperkeks wrote:

> Everything is fine, I just need to know how the attribute stuff is formatted 
> with plain LLVM style.

oh, sorry. plain llvm style. I will look for a good place to add that.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

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


[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:16748
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
 

and here


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

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


[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:16791
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
 

@HazardyKnusperkeks here.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare marked 3 inline comments as done.
gedare added a comment.

I have fixed the bug in a different way that avoids using `BlockIndent` on `if` 
conditionals. `AlwaysBreak` will continue to apply to `if` conditionals by 
default. I may propose a style option to control that behavior later.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542625.
gedare added a comment.

Do not apply BlockIndent to if conditionals. Address other comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25501,6 +25501,38 @@
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername 
||"
+   "\n"
+   "  othername < thislastname) {\n"
+   "return;\n"
+   "  } else if (quitelongname < alsolongname ||\n"
+   " anotherevenlongername <=\n"
+   " 
thisreallyreallyreallyreallyreallyreallylonger"
+   "name ||\n"
+   " othername < thislastname) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) 
{\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/ContinuationIndenter.cpp
===
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -727,8 +727,10 @@
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+   !(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+tok::kw_switch) ||
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+  Previous.Previous->isIf( &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25501,6 +25501,38 @@
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername ||"
+   "\n"
+   "  othername < thislastname) {\n"
+   "return;\n"
+   "  } else if (quitelongname < alsolongname ||\n"
+   " anotherevenlongername <=\n"
+   " thisreallyreallyreallyreallyreallyreallylonger"
+   "name ||\n"
+   " othername < thislastname) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/ContinuationIndenter.cpp
===
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -727,8 +727,10 @@
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNe

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-19 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542254.
gedare added a comment.

remove blank line


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16718,6 +16718,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16790,6 +16791,11 @@
   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
@@ -16821,6 +16827,8 @@
   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
   verifyFormat("throw ( int32 ) x;", Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
 
   // Run subset of tests again with:
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -594,19 +594,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -713,6 +713,7 @@
 
 template <> s

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-19 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542253.
gedare added a comment.

Rebase onto D155239 .


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16718,6 +16718,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16790,6 +16791,11 @@
   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
   verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
 
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
+
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
   verifyFormat("call(x, y, z);", Spaces);
@@ -16821,6 +16827,8 @@
   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
   verifyFormat("throw ( int32 ) x;", Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
 
   // Run subset of tests again with:
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -594,19 +594,23 @@
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, true));
+  FormatStyle::SpacesInParensCustom(true, true, false, false,
+  true));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(true, false, false, false));
+  FormatStyle::SpacesInParensCustom(false, true, false, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, true, false, false));
+  FormatStyle::SpacesInParensCustom(false, false, true, false,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
   CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
-  FormatStyle::SpacesInParensCustom(false, false, true, false));
+  FormatStyle::SpacesInParensCustom(false, false, false, true,
+  false));
   Style.SpacesInParens = FormatStyle::SIPO_Never;
   Style.SpacesInParensOptions = {};
 
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cp

[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-19 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542249.
gedare added a comment.

Add config parse tests for deprecated options, and add tests for __attribute__


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/FormatTestVerilog.cpp

Index: clang/unittests/Format/FormatTestVerilog.cpp
===
--- clang/unittests/Format/FormatTestVerilog.cpp
+++ clang/unittests/Format/FormatTestVerilog.cpp
@@ -807,7 +807,8 @@
"  if (x)\n"
"x = x;",
Style);
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("if ( x )\n"
"  x = x;\n"
"else if ( x )\n"
@@ -903,7 +904,8 @@
   verifyFormat("repeat (x) begin\n"
"end");
   auto Style = getDefaultStyle();
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("foreach ( x[x] )\n"
"  x = x;",
Style);
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11031,14 +11031,16 @@
   verifyFormat("template  void operator=(T) && {}", AlignMiddle);
 
   FormatStyle Spaces = getLLVMStyle();
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
-  Spaces.SpacesInCStyleCastParentheses = false;
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParensOptions.InCStyleCasts = false;
+  Spaces.SpacesInParensOptions.Other = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13674,7 +13676,8 @@
 
   FormatStyle SpaceBetweenBraces = getLLVMStyle();
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
-  SpaceBetweenBraces.SpacesInParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.Other = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -13697,7 +13700,8 @@
 "};",
 format("vectorx{1,2,3,4,};", SpaceBetweenBraces));
   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
-  SpaceBetweenBraces.SpaceInEmptyParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
 }
 
@@ -16707,10 +16711,13 @@
   verifyFormat("! ! x", Spaces);
 }
 
-TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+TEST_F(FormatTest, ConfigurableSpacesInParens) {
   FormatStyle Spaces = getLLVMStyle();
 
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16737,9 +16744,12 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
 
-  Spaces.SpacesInParentheses = false;
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Type *A = ( Type * )P;", Spaces);
   verifyFormat("Type *A = ( vector )P;", Spaces);
   verifyFormat("x = ( int32 )y;", Spaces);
@@ -16750,9 +16760,10 @@
   verifyFormat("#define x (( int )-1)", Spaces);
 
   // Run the first set of tests again with:
-  Spaces

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-19 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 542090.
gedare added a comment.

Rebase onto D155239  and correctly handle 
deprecated SpacesInParentheses option.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16718,6 +16718,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16744,6 +16745,8 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
 
   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
   Spaces.SpacesInParensOptions = {};
@@ -16785,6 +16788,12 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
 
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
@@ -16817,6 +16826,8 @@
   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
   verifyFormat("throw ( int32 ) x;", Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
 
   // Run subset of tests again with:
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -217,6 +217,7 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -713,6 +713,7 @@
 
 template <> struct MappingTraits {
   static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) {
+IO.mapOptional("InAttributeSpecifiers", Spaces.InAttributeSpecifiers);
 IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
 IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
 IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses);
@@ -1140,6 +1141,7 @@
   if (SpacesInParentheses) {
 // set all options except InCStyleCasts and InEmptyParentheses
 // to true for backw

[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-19 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

I guess since the current behavior does not treat `if` with block indentation, 
this bug fix should instead prefer to maintain the current behavior, and if 
someone wants to have block indentation of `if` it would need to be added as a 
new style option. Otherwise, this will introduce regressions like that test 
case.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-19 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:25504
+
+#if 0
   verifyFormat("if (quitelongarg !=\n"

MyDeveloperDay wrote:
> we don't do this.
OK, I'll move that other bug up my priority list, as the behavior of this test 
case involves both. That bug prevents breaking the line after the operand, I 
think there's some off-by-1 error somewhere in calculating the columns for 
blockindent breaks. Otherwise, this patch reveals that bug as a regression in 
this test case.



Comment at: clang/unittests/Format/FormatTest.cpp:25505-25510
   verifyFormat("if (quitelongarg !=\n"
"(alsolongarg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);

MyDeveloperDay wrote:
> you can't remove a test and just call it good... the original author put this 
> test in for a reason I assume?
understood, although it seems that the original author may not have considered 
`if` statements as legitimately to be used in BlockIndented formats, since this 
test case does not block indent. So, the test case will have to change anyway, 
if the `if` should be blockindented.



Comment at: clang/unittests/Format/FormatTest.cpp:25514
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"

MyDeveloperDay wrote:
> isn't the breaking of `if (` and `} else if (\n` inconsistent?
The breaking is dependent on what ends up exceeding the ColumnLimit. since ` if 
(` is shorter than ` } else if (` by about 5 characters, you can get different 
breaks. As far as I can tell, the behavior of `AlwaysBreak` and `BlockIndent` 
will only break after the opening parens if the first "argument" doesn't fit. 
From what I understand, the penalties and desirable breaks come into play then, 
based on breaking around commas or boolean operators.  With this patch, the 
behavior here is now consistent for both `AlwaysBreak` and `BlockIndent`. 

This behavior is undocumented and I do not know the intention. I would think it 
is better for the two options to behave consistently, but I'm also willing to 
treat them differently for backward compatibility while fixing this bug.



Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-18 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:25486
"comment\n"
-   "  return;\n"
"}",

MyDeveloperDay wrote:
> Why remove?
oops. that's a spurious change. I have reverted it.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-18 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 541813.
gedare marked an inline comment as done.
gedare added a comment.

Revert deleted line in test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25494,12 +25494,56 @@
"}",
Style);
 
+  // Treating if clauses as block indents causes a known bug (#54808, #63383)
+  // breaking the following test. It gets formatted instead as:
+  // "if (quitelongarg != (alsolongarg - 1)\n"
+  // ") { // ABC is a very long comment"
+  // "return;"
+  // "}"
+
+#if 0
   verifyFormat("if (quitelongarg !=\n"
"(alsolongarg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+#endif
+
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername 
||"
+   "\n"
+   "  othername < thislastname) {\n"
+   "return;\n"
+   "  } else if (\n"
+   "  quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername 
||"
+   "\n"
+   "  othername < thislastname\n"
+   "  ) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (\n"
+   "ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "ontoMultipleLines && whenFormattedCorrectly\n"
+   "  ) {\n"
+   "if (false) {\n"
+   "} else if (\n"
+   "  thisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly\n"
+   ") {\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -5499,7 +5499,7 @@
 if (Next && Next->is(tok::l_paren))
   return false;
 const FormatToken *Previous = Right.MatchingParen->Previous;
-return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
+return !(Previous && Previous->is(tok::kw_for));
   }
 
   // Allow breaking after a trailing annotation, e.g. after a method


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25494,12 +25494,56 @@
"}",
Style);
 
+  // Treating if clauses as block indents causes a known bug (#54808, #63383)
+  // breaking the following test. It gets formatted instead as:
+  // "if (quitelongarg != (alsolongarg - 1)\n"
+  // ") { // ABC is a very long comment"
+  // "return;"
+  // "}"
+
+#if 0
   verifyFormat("if (quitelongarg !=\n"
"(alsolongarg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+#endif
+
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername ||"
+   "\n"
+   "  othername < thislastname) {\n"
+   "return;\n"
+   "  } else if (\n"
+   "  quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername ||"
+   "\n"
+   "  othername < thislastname\n"
+   "  ) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (\n"
+   "ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "ontoMultipleLines && whenFormattedCorrectly\n"
+   "  ) {\n"
+   "if (false) {\n"
+   

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-18 Thread Gedare Bloom via Phabricator via cfe-commits
gedare planned changes to this revision.
gedare added a comment.

I need to fix this to reflect the changes in the parent rev.




Comment at: clang/include/clang/Format/Format.h:4208-4213
+/// Put a space in parentheses inside attribute specifier lists.
+/// \code
+///true:  false:
+///__attribute__(( noreturn ))vs. __attribute__((noreturn))
+/// \endcode
+bool InAttributeSpecifierLists;

HazardyKnusperkeks wrote:
> gedare wrote:
> > owenpan wrote:
> > > This should be covered by `SpacesInParetheses`, so we really should not 
> > > have a special option for `__attribute__`.
> > Currently, the behavior of `SpacesInParentheses` does this:
> > ```
> > __attribute__( ( noreturn ) )
> > ```
> > In order to prevent this from happening, it is necessary to add an option 
> > to disable it somehow, because I don't see that this kind of spacing should 
> > ever be used by anyone, but probably someone does it, and it should be 
> > maintained for backward compatibility anyway.
> > Currently, the behavior of `SpacesInParentheses` does this:
> > ```
> > __attribute__( ( noreturn ) )
> > ```
> > In order to prevent this from happening, it is necessary to add an option 
> > to disable it somehow, because I don't see that this kind of spacing should 
> > ever be used by anyone, but probably someone does it, and it should be 
> > maintained for backward compatibility anyway.
> 
> And what does clang-format do before your `SpacesInParentheses`? You should 
> expand the tests to cover the attributes (if they aren't in there already).
`SpacesInParentheses` formats like this: `__attribute__( ( ... ) )`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

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


[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-18 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D155239#4509921 , 
@HazardyKnusperkeks wrote:

> If you limit it to `Never` I don't see any value in the differentiation. You 
> could just always use `Custom` (by dropping the custom and only having the 
> nested options).
>
> But I think having at least the `Always` option would be nice. If you want 
> **always** to have a space and you set everything by hand to true, someone 
> comes along and adds a new option (which then is defaulted to `false`) you 
> don't get what you want.

Having `Custom` simplifies detecting if the new options are being used. It is 
possible to add an `Always` option if someone wants it, but that option has not 
existed yet for `clang-format`.




Comment at: clang/lib/Format/Format.cpp:1035
 IO.mapOptional("SpaceInEmptyBlock", Style.SpaceInEmptyBlock);
-IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
 IO.mapOptional("SpacesBeforeTrailingComments",

HazardyKnusperkeks wrote:
> MyDeveloperDay wrote:
> > By removing the old options don’t you break everyone’s clang format file
> You need to parse all of the old options, and map them to the new one, if and 
> only if the old one(s) is/are set and the new is not! See below for the other 
> deprecated options.
Got it, I misunderstood how to handle deprecated options (twice). I think I 
have that sorted now.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

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


[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-18 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 541770.
gedare added a comment.

Parse deprecated options and map to new ones.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/FormatTestVerilog.cpp

Index: clang/unittests/Format/FormatTestVerilog.cpp
===
--- clang/unittests/Format/FormatTestVerilog.cpp
+++ clang/unittests/Format/FormatTestVerilog.cpp
@@ -807,7 +807,8 @@
"  if (x)\n"
"x = x;",
Style);
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("if ( x )\n"
"  x = x;\n"
"else if ( x )\n"
@@ -903,7 +904,8 @@
   verifyFormat("repeat (x) begin\n"
"end");
   auto Style = getDefaultStyle();
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("foreach ( x[x] )\n"
"  x = x;",
Style);
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11031,14 +11031,16 @@
   verifyFormat("template  void operator=(T) && {}", AlignMiddle);
 
   FormatStyle Spaces = getLLVMStyle();
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
-  Spaces.SpacesInCStyleCastParentheses = false;
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParensOptions.InCStyleCasts = false;
+  Spaces.SpacesInParensOptions.Other = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13674,7 +13676,8 @@
 
   FormatStyle SpaceBetweenBraces = getLLVMStyle();
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
-  SpaceBetweenBraces.SpacesInParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.Other = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -13697,7 +13700,8 @@
 "};",
 format("vectorx{1,2,3,4,};", SpaceBetweenBraces));
   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
-  SpaceBetweenBraces.SpaceInEmptyParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
 }
 
@@ -16707,10 +16711,13 @@
   verifyFormat("! ! x", Spaces);
 }
 
-TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+TEST_F(FormatTest, ConfigurableSpacesInParens) {
   FormatStyle Spaces = getLLVMStyle();
 
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16738,8 +16745,9 @@
"}",
Spaces);
 
-  Spaces.SpacesInParentheses = false;
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Type *A = ( Type * )P;", Spaces);
   verifyFormat("Type *A = ( vector )P;", Spaces);
   verifyFormat("x = ( int32 )y;", Spaces);
@@ -16750,9 +16758,10 @@
   verifyFormat("#define x (( int )-1)", Spaces);
 
   // Run the first set of tests again with:
-  Spaces.SpacesInParentheses = false;
-  Spaces.SpaceInEmptyParentheses = true;
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+ 

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare marked an inline comment as done.
gedare added a comment.

I simplified this to treat the double parens identically, so that it will 
either inject spaces inside both parens, or not.

Note: This option is necessary to disable ``SpacesInParens.Other`` from adding 
spaces inside of ``__attribute__((...))``.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

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


[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 541322.
gedare added a comment.

Merge the two options to just one for attribute parens.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16718,6 +16718,7 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16744,6 +16745,8 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
 
   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
   Spaces.SpacesInParensOptions = {};
@@ -16785,6 +16788,12 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo(int bar)", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = false;
 
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
@@ -16817,6 +16826,8 @@
   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
   verifyFormat("throw ( int32 ) x;", Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
 
   // Run subset of tests again with:
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -217,6 +217,7 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,16 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Left.is(TT_AttributeParen) || Right.is(TT_AttributeParen) ||
+(Left.Previous && Left.Previous->is(TT_AttributeParen)) ||
+(Right.Next && Right.Next->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -713,6 +713,7 @@
 
 template <> struct MappingTraits {
   static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) {
+IO.mapOptional("InAttributeSpecifiers", Spaces.InAttributeSpecifiers);
 IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
 IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
 IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses);
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -4205,6 +4205,12 @@
   

[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare planned changes to this revision.
gedare added inline comments.



Comment at: clang/include/clang/Format/Format.h:4208-4213
+/// Put a space in parentheses inside attribute specifier lists.
+/// \code
+///true:  false:
+///__attribute__(( noreturn ))vs. __attribute__((noreturn))
+/// \endcode
+bool InAttributeSpecifierLists;

owenpan wrote:
> This should be covered by `SpacesInParetheses`, so we really should not have 
> a special option for `__attribute__`.
Currently, the behavior of `SpacesInParentheses` does this:
```
__attribute__( ( noreturn ) )
```
In order to prevent this from happening, it is necessary to add an option to 
disable it somehow, because I don't see that this kind of spacing should ever 
be used by anyone, but probably someone does it, and it should be maintained 
for backward compatibility anyway.



Comment at: clang/include/clang/Format/Format.h:4214-4220
+/// Put a space in parentheses around the outside of attribute specifier
+/// lists.
+/// \code
+///true:  false:
+///__attribute__( (noreturn) )vs. __attribute__((noreturn))
+/// \endcode
+bool InAttributeSpecifiers;

owenpan wrote:
> I'm against this as the double parens, IMO, should never be separated.
I'm happy to consolidate these to a single option, that only allows to toggle 
between all spaces and no spaces, i.e.,
```
__attribute__( ( noreturn ) )  // enabled
__attribute__((noreturn)) // disabled
```
The style I aim for, uses no spaces around attributes, but likes to have spaces 
in other places. I will prepare a revision.



Comment at: clang/lib/Format/TokenAnnotator.cpp:3818
+if (Right.is(TT_AttributeParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;

This logic can be simplified, since the parser labels both the left and right 
parentheses (unlike with the ``CastRParen`` case.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155529

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

Ping.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D155529: [clang-format] Add SpaceInParensOption for __attribute__ keyword

2023-07-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

The __attribute((specifier-list)) currently is formatted based on the
SpacesInParensOptions.Other (previously, SpacesInParentheses). This change
allows finer control over addition of spaces between the consecutive parens,
and between the inner parens and the list of attribute specifiers.

DependsOn: D155239 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155529

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16718,6 +16718,8 @@
   Spaces.SpacesInParensOptions = {};
   Spaces.SpacesInParensOptions.Other = true;
   Spaces.SpacesInParensOptions.InConditionalStatements = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifierLists = true;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16744,6 +16746,8 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
+  verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
 
   Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
   Spaces.SpacesInParensOptions = {};
@@ -16785,6 +16789,13 @@
"  break;\n"
"}",
Spaces);
+  verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
+
+  Spaces.SpacesInParensOptions.InAttributeSpecifierLists = true;
+  verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
+  Spaces.SpacesInParensOptions.InAttributeSpecifierLists = false;
+  Spaces.SpacesInParensOptions.InAttributeSpecifiers = true;
+  verifyFormat("SomeType *__attribute__( (attr) ) *a = NULL;", Spaces);
 
   // Run the first set of tests again with:
   Spaces.SpaceAfterCStyleCast = true;
@@ -16817,6 +16828,7 @@
   verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
   verifyFormat("bool *y = ( bool * ) (x);", Spaces);
   verifyFormat("throw ( int32 ) x;", Spaces);
+  verifyFormat("SomeType *__attribute__( (attr) ) *a = NULL;", Spaces);
 
   // Run subset of tests again with:
   Spaces.SpacesInParensOptions.InCStyleCasts = false;
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -217,6 +217,8 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifierLists);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InAttributeSpecifiers);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
   CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3810,10 +3810,20 @@
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-return (Right.is(TT_CastRParen) ||
-(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-   ? Style.SpacesInParensOptions.InCStyleCasts
-   : Style.SpacesInParensOptions.Other;
+if (Right.is(TT_CastRParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
+  return Style.SpacesInParensOptions.InCStyleCasts;
+}
+if (Right.is(TT_AttributeParen) ||
+(Left.MatchingParen && Left.MatchingParen->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifiers;
+}
+if ((Right.Next && Right.Next->is(TT_AttributeParen)) ||
+(Left.Previous && Left.Previous->MatchingParen &&
+ Left.Previous->MatchingParen->is(TT_AttributeParen))) {
+  return Style.SpacesInParensOptions.InAttributeSpecifierLists;
+}
+return Style.SpacesInParensOptions.Other;
   }
   if (Right.isOneOf(tok::semi, tok::comma))
 return false;
Index: clang/lib/Format/Format.cpp
=

[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

I addressed the comments, and I have redesigned this option to simplify it 
further. Now there are only two options at the top for `SpacesInParens` to be 
either `Never` or `Custom`. Then within `Custom` the individual behavior of the 
spaces can be controlled. This allows, for example, someone to actually set 
every option `true` and really get a space in every parens that is supported. 
Meanwhile, to get the behavior of what was previously called 
`SpacesInParentheses` a user would set the spaces in parens options for 
`InConditionalStatements` and `Other` to be `true`. If `Other` gets further 
divided in the future, the previous behavior can be retained by simply setting 
its options to `true` as well.




Comment at: clang/include/clang/Format/Format.h:4220
   /// \version 3.7
   bool SpacesInParentheses;
 

HazardyKnusperkeks wrote:
> The deprecated options should be removed from the struct, see 
> `AllowAllConstructorInitializersOnNextLine` for an example.
> 
> You also need to adapt the parsing logic a bit.
Got it, thanks for the hint



Comment at: clang/lib/Format/TokenAnnotator.cpp:4046
+   ? (Style.SpacesInParens == FormatStyle::SIPO_Always ||
+  Style.SpacesInParentheses)
+   : true;

MyDeveloperDay wrote:
> isn't SpacesInParentheses mapped to Style.SpacesInParens == 
> FormatStyle::SIPO_Always?
Yes it was, but I misunderstood how to deprecate options. This is fixed now 
that I removed the deprecated SpacesInParentheses, but it's also been changed 
to map `SpacesInParentheses` to `Style.SpacesInParens == 
FormatStyle::SIPO_Custom`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

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


[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 541219.
gedare marked 3 inline comments as done.
gedare added a comment.

Properly deprecate old options, and simplify top-level to Never and Custom


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/FormatTestVerilog.cpp

Index: clang/unittests/Format/FormatTestVerilog.cpp
===
--- clang/unittests/Format/FormatTestVerilog.cpp
+++ clang/unittests/Format/FormatTestVerilog.cpp
@@ -807,7 +807,8 @@
"  if (x)\n"
"x = x;",
Style);
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("if ( x )\n"
"  x = x;\n"
"else if ( x )\n"
@@ -903,7 +904,8 @@
   verifyFormat("repeat (x) begin\n"
"end");
   auto Style = getDefaultStyle();
-  Style.SpacesInConditionalStatement = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("foreach ( x[x] )\n"
"  x = x;",
Style);
Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11031,14 +11031,16 @@
   verifyFormat("template  void operator=(T) && {}", AlignMiddle);
 
   FormatStyle Spaces = getLLVMStyle();
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
-  Spaces.SpacesInCStyleCastParentheses = false;
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParensOptions.InCStyleCasts = false;
+  Spaces.SpacesInParensOptions.Other = true;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13674,7 +13676,8 @@
 
   FormatStyle SpaceBetweenBraces = getLLVMStyle();
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
-  SpaceBetweenBraces.SpacesInParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.Other = true;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -13697,7 +13700,8 @@
 "};",
 format("vectorx{1,2,3,4,};", SpaceBetweenBraces));
   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
-  SpaceBetweenBraces.SpaceInEmptyParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
+  SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
 }
 
@@ -16707,10 +16711,13 @@
   verifyFormat("! ! x", Spaces);
 }
 
-TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+TEST_F(FormatTest, ConfigurableSpacesInParens) {
   FormatStyle Spaces = getLLVMStyle();
 
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.Other = true;
+  Spaces.SpacesInParensOptions.InConditionalStatements = true;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16738,8 +16745,9 @@
"}",
Spaces);
 
-  Spaces.SpacesInParentheses = false;
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
+  Spaces.SpacesInParensOptions = {};
+  Spaces.SpacesInParensOptions.InCStyleCasts = true;
   verifyFormat("Type *A = ( Type * )P;", Spaces);
   verifyFormat("Type *A = ( vector )P;", Spaces);
   verifyFormat("x = ( int32 )y;", Spaces);
@@ -16750,9 +16758,10 @@
   verifyFormat("#define x (( int )-1)", Spaces);
 
   // Run the first set of tests again with:
-  Spaces.SpacesInParentheses = false;
-  Spaces.SpaceInEmptyParentheses = true;
-  Spaces.SpacesInCStyleCastParentheses = true;
+  Spaces.SpacesInParen

[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-13 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

After doing this, I'm not too sure about the value of keeping the higher-level 
`CStyleCasts`, `ConditionalStatements`, and `EmptyParentheses` as options to 
`SpacesInParens`. However, the behavior of `Always` is actually "Always except 
for `CStyleCasts` and `EmptyParentheses`, which is consistent with how 
`SpacesInParentheses` currently works. I think that is a bit buggy/brittle, but 
I kept the behavior as-is, and modeled the design of this after 
`SpaceBeforeParens`.

I might prefer to make `Always` really mean every single parens, and simply have
`Never`, `Always`, and `Custom`. Inside Custom, there can be a `Other` option 
to catch anything that isn't explicitly controlled.

I could make this work, mapping the previous behavior of `SpacesInParenthesis` 
to Custom with `Options = {ConditionalStatements: true, Other: true}`. I'll sit 
on this a bit, but if I don't hear objections (to the entire point of this rev, 
or to my next step), I will redo the options like this.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155239

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


[PATCH] D155239: [clang-format] Add SpacesInParens with SpacesInParensOptions

2023-07-13 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

This is a refactoring of:

- SpacesInConditionalStatement
- SpacesInCStyleCastParentheses
- SpaceInEmptyParentheses
- SpacesInParentheses

These are now options under the new Style Option: SpacesInParens. The existing
options are maintained for backward compatibility.

Within SpacesInParens, there are currently options for:

- Never
- ConditionalStatements
- CStyleCasts
- EmptyParentheses
- Always
- Custom

Always enables the same space additions as SpacesInParentheses. The currently
available options for Custom are:

- InConditionalStatements
- InCStyleCasts
- InEmptyParentheses

This refactoring does not add or remove any existing features, but it makes it
possible to more easily extend and maintain the addition of spaces within
parentheses.

This rev is related to Github Issue 55428.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155239

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -11038,7 +11038,7 @@
   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
 
   Spaces.SpacesInCStyleCastParentheses = false;
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Always;
   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@@ -13674,7 +13674,7 @@
 
   FormatStyle SpaceBetweenBraces = getLLVMStyle();
   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
-  SpaceBetweenBraces.SpacesInParentheses = true;
+  SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Always;
   SpaceBetweenBraces.SpacesInSquareBrackets = true;
   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@@ -16707,10 +16707,10 @@
   verifyFormat("! ! x", Spaces);
 }
 
-TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+TEST_F(FormatTest, ConfigurableSpacesInParens) {
   FormatStyle Spaces = getLLVMStyle();
 
-  Spaces.SpacesInParentheses = true;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Always;
   verifyFormat("do_something( ::globalVar );", Spaces);
   verifyFormat("call( x, y, z );", Spaces);
   verifyFormat("call();", Spaces);
@@ -16738,7 +16738,7 @@
"}",
Spaces);
 
-  Spaces.SpacesInParentheses = false;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Never;
   Spaces.SpacesInCStyleCastParentheses = true;
   verifyFormat("Type *A = ( Type * )P;", Spaces);
   verifyFormat("Type *A = ( vector )P;", Spaces);
@@ -16750,7 +16750,7 @@
   verifyFormat("#define x (( int )-1)", Spaces);
 
   // Run the first set of tests again with:
-  Spaces.SpacesInParentheses = false;
+  Spaces.SpacesInParens = FormatStyle::SIPO_Never;
   Spaces.SpaceInEmptyParentheses = true;
   Spaces.SpacesInCStyleCastParentheses = true;
   verifyFormat("call(x, y, z);", Spaces);
@@ -23523,10 +23523,10 @@
   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
 
   Style.SpacesInCStyleCastParentheses = true;
-  Style.SpacesInParentheses = false;
+  Style.SpacesInParens = FormatStyle::SIPO_Never;
   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
   Style.SpacesInCStyleCastParentheses = false;
-  Style.SpacesInParentheses = true;
+  Style.SpacesInParens = FormatStyle::SIPO_Always;
   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
 }
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -175,7 +175,6 @@
   CHECK_PARSE_BOOL(ReflowComments);
   CHECK_PARSE_BOOL(RemoveBracesLLVM);
   CHECK_PARSE_BOOL(RemoveSemicolon);
-  CHECK_PARSE_BOOL(SpacesInParentheses);
   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
@@ -221,6 +220,9 @@
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
   CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
+  CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
+  CHECK_PARSE_NESTED_BO

[PATCH] D154552: [clang-format] Add note directive generation for docs

2023-07-07 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

It looks like this:
F28163425: image.png 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154552

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-07 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

Although I chose to preserve the existing behavior of `AlwaysBreak` and to 
mimic it for `BlockIndent`, I think it would be best to choose a consistent 
approach and document it. I also have a patch that does not apply the 
AlignAfterOpenBracket for if clauses, which could be done for only 
`BlockIndent`, or it could be done for `AlwaysBreak` too but that will cause 
some backward compatibility problems.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154755

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


[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

2023-07-07 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

A bug with BlockIndent prevents line breaks within if (and else if) clauses.
While fixing this bug, it appears that AlignAfterOpenBracket is not designed
to work with loop and if statements, but AlwaysBreak works on if clauses.
The documentation and tests are not clear on whether or not this is intended.
This patch preserves the AlwaysBreak behavior and supports BlockIndent on if
clauses while fixing the bug.

It may be reasonable to go the other way and create an explicit option for
alignment of if (and loop) clauses intentionally.

Fixes #54663.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154755

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25483,7 +25483,6 @@
   verifyFormat("if (quitelongarg !=\n"
"(alsolongarg - 1)) { // ABC is a very long "
"comment\n"
-   "  return;\n"
"}",
Style);
 
@@ -25494,12 +25493,56 @@
"}",
Style);
 
+  // Treating if clauses as block indents causes a known bug (#54808, #63383)
+  // breaking the following test. It gets formatted instead as:
+  // "if (quitelongarg != (alsolongarg - 1)\n"
+  // ") { // ABC is a very long comment"
+  // "return;"
+  // "}"
+
+#if 0
   verifyFormat("if (quitelongarg !=\n"
"(alsolongarg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+#endif
+
+  verifyFormat("void foo() {\n"
+   "  if (quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername 
||"
+   "\n"
+   "  othername < thislastname) {\n"
+   "return;\n"
+   "  } else if (\n"
+   "  quitelongname < alsolongname ||\n"
+   "  anotherevenlongername <=\n"
+   "  thisreallyreallyreallyreallyreallyreallylongername 
||"
+   "\n"
+   "  othername < thislastname\n"
+   "  ) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (\n"
+   "ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "ontoMultipleLines && whenFormattedCorrectly\n"
+   "  ) {\n"
+   "if (false) {\n"
+   "} else if (\n"
+   "  thisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly\n"
+   ") {\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -5499,7 +5499,7 @@
 if (Next && Next->is(tok::l_paren))
   return false;
 const FormatToken *Previous = Right.MatchingParen->Previous;
-return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
+return !(Previous && Previous->is(tok::kw_for));
   }
 
   // Allow breaking after a trailing annotation, e.g. after a method


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25483,7 +25483,6 @@
   verifyFormat("if (quitelongarg !=\n"
"(alsolongarg - 1)) { // ABC is a very long "
"comment\n"
-   "  return;\n"
"}",
Style);
 
@@ -25494,12 +25493,56 @@
"}",
Style);
 
+  // Treating if clauses as block indents causes a known bug (#54808, #63383)
+  // breaking the following test. It gets formatted instead as:
+  // "if (quitelongarg != (alsolongarg - 1)\n"
+  // ") { // ABC is a very long comment"
+  // "return;"
+  // "}"
+
+#if 0
   verifyFormat("if (quitelongarg !=\n"
"(alsolongarg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style)

[PATCH] D154552: [clang-format] Add note directive generation for docs

2023-07-06 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537872.
gedare added a comment.

Update from D153205 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154552

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/tools/dump_format_style.py
  clang/include/clang/Format/Format.h

Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -92,10 +92,10 @@
 ///   )
 /// \endcode
 ///
-/// \warning
+/// \note
 ///  This currently only applies to braced initializer lists (when
 ///  ``Cpp11BracedListStyle`` is ``true``) and parentheses.
-/// \endwarning
+/// \endnote
 BAS_BlockIndent,
   };
 
@@ -134,8 +134,10 @@
   /// if not ``None``, when using initialization for an array of structs
   /// aligns the fields into columns.
   ///
-  /// NOTE: As of clang-format 15 this option only applied to arrays with equal
-  /// number of columns per row.
+  /// \note
+  ///  As of clang-format 15 this option only applied to arrays with equal
+  ///  number of columns per row.
+  /// \endnote
   ///
   /// \version 13
   ArrayInitializerAlignmentStyle AlignArrayOfStructures;
@@ -440,8 +442,10 @@
 
   /// Control of trailing comments.
   ///
-  /// NOTE: As of clang-format 16 this option is not a bool but can be set
-  /// to the options. Conventional bool options still can be parsed as before.
+  /// \note
+  ///  As of clang-format 16 this option is not a bool but can be set
+  ///  to the options. Conventional bool options still can be parsed as before.
+  /// \endnote
   ///
   /// \code{.yaml}
   ///   # Example of usage:
@@ -1094,8 +1098,10 @@
 /// \endcode
 bool AfterNamespace;
 /// Wrap ObjC definitions (interfaces, implementations...).
-/// \note @autoreleasepool and @synchronized blocks are wrapped
-/// according to `AfterControlStatement` flag.
+/// \note
+///  @autoreleasepool and @synchronized blocks are wrapped
+///  according to `AfterControlStatement` flag.
+/// \endnote
 bool AfterObjCDeclaration;
 /// Wrap struct definitions.
 /// \code
@@ -1290,7 +1296,9 @@
   /// otherwise it will scan until the closing `]` to determine if it should add
   /// newlines between elements (prettier compatible).
   ///
-  /// NOTE: This is currently only for formatting JSON.
+  /// \note
+  ///  This is currently only for formatting JSON.
+  /// \endnote
   /// \code
   ///true:  false:
   ///[  vs.  [1, 2, 3, 4]
@@ -2180,8 +2188,10 @@
   /// made, clang-format analyzes whether there are other bin-packed cases in
   /// the input file and act accordingly.
   ///
-  /// NOTE: This is an experimental flag, that might go away or be renamed. Do
-  /// not use this in config files, etc. Use at your own risk.
+  /// \note
+  ///  This is an experimental flag, that might go away or be renamed. Do
+  ///  not use this in config files, etc. Use at your own risk.
+  /// \endnote
   /// \version 3.7
   bool ExperimentalAutoDetectBinPacking;
 
@@ -3200,10 +3210,13 @@
   ///   * restrict
   ///   * type
   ///
-  /// Note: it MUST contain 'type'.
+  /// \note
+  ///  it MUST contain 'type'.
+  /// \endnote
+  ///
   /// Items to the left of 'type' will be placed to the left of the type and
-  /// aligned in the order supplied. Items to the right of 'type' will be placed
-  /// to the right of the type and aligned in the order supplied.
+  /// aligned in the order supplied. Items to the right of 'type' will be
+  /// placed to the right of the type and aligned in the order supplied.
   ///
   /// \code{.yaml}
   ///   QualifierOrder: ['inline', 'static', 'type', 'const', 'volatile' ]
Index: clang/docs/tools/dump_format_style.py
===
--- clang/docs/tools/dump_format_style.py
+++ clang/docs/tools/dump_format_style.py
@@ -257,6 +257,14 @@
 endwarning_match = re.match(r"^/// +\\endwarning$", line)
 if endwarning_match:
 return ""
+
+match = re.match(r"^/// \\note$", line)
+if match:
+return "\n.. note::\n\n"
+
+endnote_match = re.match(r"^/// +\\endnote$", line)
+if endnote_match:
+return ""
 return line[4:] + "\n"
 
 def read_options(self):
Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -239,7 +239,7 @@
   )
 
 
-.. warning::
+.. note::
 
  This currently only applies to braced initializer lists (when
  ``Cpp11BracedListStyle`` is ``true``) and parentheses.
@@ -252,8 +252,11 @@
   if not ``None``, when using initial

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-06 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537859.
gedare added a comment.

Regenerate complete diff


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1380,7 +1380,7 @@
 
 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
   FormatStyle AllowsMergedLoops = getLLVMStyle();
-  AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_NonEmpty;
   verifyFormat("while (true) continue;", AllowsMergedLoops);
   verifyFormat("for (;;) continue;", AllowsMergedLoops);
   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
@@ -1436,6 +1436,66 @@
"  while (true);\n"
"}",
AllowsMergedLoops);
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_All;
+  verifyFormat("while (true) continue;", AllowsMergedLoops);
+  verifyFormat("for (;;) continue;", AllowsMergedLoops);
+  verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int &v, vec)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  BOOST_FOREACH (int &v, vec) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) // Can't merge this\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) /* still don't merge */\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("do a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do /* Don't merge */\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do\n"
+   "  // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  // Without braces labels are interpreted differently.
+  verifyFormat("{\n"
+   "  do\n"
+   "  label:\n"
+   "a++;\n"
+   "  while (true);\n"
+   "}",
+   AllowsMergedLoops);
 }
 
 TEST_F(FormatTest, FormatShortBracedStatements) {
@@ -1443,7 +1503,8 @@
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
 FormatStyle::SIS_Never);
-  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
+  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine,
+FormatStyle::SWFLS_Never);
   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
   verifyFormat("for (;;) {\n"
"  f();\n"
@@ -1488,7 +1549,8 @@
   AllowSimpleBracedStatements.ColumnLimit = 40;
   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
   FormatStyle::SBS_Always;
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_NonEmpty;
   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
@@ -1605,7 +1667,8 @@
"}",
AllowSimpleBracedStatements);
 
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_Never;
   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
   verifyFormat("while (true) 

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-06 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537857.
gedare added a comment.

Reorder traits and add comment about backward compatibility.


Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

It looks like your clang-format review does not contain any unit tests, please 
try to ensure all code changes have a unit test (unless this is an `NFC` or 
refactoring, adding documentation etc..)

Add your unit tests in `clang/unittests/Format` and you can build with `ninja 
FormatTests`.  We recommend using the `verifyFormat(xxx)` format of unit tests 
rather than `EXPECT_EQ` as this will ensure you change is tolerant to random 
whitespace changes (see FormatTest.cpp as an example)

For situations where your change is altering the TokenAnnotator.cpp which can 
happen if you are trying to improve the annotation phase to ensure we are 
correctly identifying the type of a token, please add a token annotator test in 
`TokenAnnotatorTest.cpp`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

Files:
  clang/lib/Format/Format.cpp


Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -593,9 +593,11 @@
 template <> struct 
ScalarEnumerationTraits {
   static void enumeration(IO &IO, FormatStyle::ShortWhileForLoopStyle &Value) {
 IO.enumCase(Value, "Never", FormatStyle::SWFLS_Never);
-IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "NonEmpty", FormatStyle::SWFLS_NonEmpty);
 IO.enumCase(Value, "All", FormatStyle::SWFLS_All);
+
+// For backward compatibility.
+IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "true", FormatStyle::SWFLS_NonEmpty);
   }
 };


Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -593,9 +593,11 @@
 template <> struct ScalarEnumerationTraits {
   static void enumeration(IO &IO, FormatStyle::ShortWhileForLoopStyle &Value) {
 IO.enumCase(Value, "Never", FormatStyle::SWFLS_Never);
-IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "NonEmpty", FormatStyle::SWFLS_NonEmpty);
 IO.enumCase(Value, "All", FormatStyle::SWFLS_All);
+
+// For backward compatibility.
+IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "true", FormatStyle::SWFLS_NonEmpty);
   }
 };
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153205: [clang-format] Support block indenting array/struct list initializers

2023-07-06 Thread Gedare Bloom via Phabricator via cfe-commits
gedare marked an inline comment as done.
gedare added a comment.

In D153205#4475836 , @owenpan wrote:

> Do you need to update the current diff to show the net change? For example, 
> the unit tests you added don't show up as a diff right now.

Done. Not sure what happened there. But it looks right now.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

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


[PATCH] D153205: [clang-format] Support block indenting array/struct list initializers

2023-07-06 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537761.
gedare added a comment.

Update and refresh diff


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/FormatToken.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -4944,7 +4944,7 @@
"  \"\"};\n",
Style);
   // Designated initializers.
-  verifyFormat("int LongVariable[1] = {\n"
+  verifyFormat("int LongVariable[2] = {\n"
"  [0] = 1000, [1] = 2000};",
Style);
   verifyFormat("SomeStruct s{\n"
@@ -5054,7 +5054,7 @@
"bar,\n"
"  },\n"
"  SomeArrayT{},\n"
-   "}\n",
+   "};",
Style);
   verifyFormat("SomeArrayT a[3] = {\n"
"  {foo},\n"
@@ -5071,7 +5071,7 @@
"},\n"
"  },\n"
"  {baz},\n"
-   "}\n",
+   "};",
Style);
 
   // Aligning after open braces unaffected by BracedInitializerIndentWidth.
@@ -25513,6 +25513,155 @@
Style);
 }
 
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
+  auto Style = getLLVMStyleWithColumns(60);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  // Aggregate initialization.
+  verifyFormat("int LongVariable[2] = {\n"
+   "1000, 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   "\"\", \"\",\n"
+   "\"\"\n"
+   "};",
+   Style);
+  // Designated initializers.
+  verifyFormat("int LongVariable[2] = {\n"
+   "[0] = 1000, [1] = 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   ".foo = \"x\",\n"
+   ".bar = \"y\",\n"
+   ".baz = \"z\"\n"
+   "};",
+   Style);
+  // List initialization.
+  verifyFormat("SomeStruct s{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};",
+   Style);
+  verifyFormat("new SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};",
+   Style);
+  // Member initializer.
+  verifyFormat("class SomeClass {\n"
+   "  SomeStruct s{\n"
+   "  \"x\",\n"
+   "  \"y\",\n"
+   "  \"z\",\n"
+   "  };\n"
+   "};",
+   Style);
+  // Constructor member initializer.
+  verifyFormat("SomeClass::SomeClass : strct{\n"
+   "   \"x\",\n"
+   "   \"y\",\n"
+   "   \"z\",\n"
+   "   } {}",
+   Style);
+  // Copy initialization.
+  verifyFormat("SomeStruct s = SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};",
+   Style);
+  // Copy list initialization.
+  verifyFormat("SomeStruct s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};",
+   Style);
+  // Assignment operand initialization.
+  verifyFormat("s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};",
+   Style);
+  // Returned object initialization.
+  verifyFormat("return {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};",
+   Style);
+  // Initializer list.
+  verifyForm

[PATCH] D153205: [clang-format] Support block indenting array/struct list initializers

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537531.
gedare added a comment.

Address comments


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/FormatToken.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25535,26 +25535,26 @@
".foo = \"x\",\n"
".bar = \"y\",\n"
".baz = \"z\"\n"
-   "};\n",
+   "};",
Style);
   // List initialization.
   verifyFormat("SomeStruct s{\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   verifyFormat("SomeStruct{\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   verifyFormat("new SomeStruct{\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   // Member initializer.
   verifyFormat("class SomeClass {\n"
@@ -25563,62 +25563,62 @@
"  \"y\",\n"
"  \"z\",\n"
"  };\n"
-   "};\n",
+   "};",
Style);
   // Constructor member initializer.
   verifyFormat("SomeClass::SomeClass : strct{\n"
"   \"x\",\n"
"   \"y\",\n"
"   \"z\",\n"
-   "   } {}\n",
+   "   } {}",
Style);
   // Copy initialization.
   verifyFormat("SomeStruct s = SomeStruct{\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   // Copy list initialization.
   verifyFormat("SomeStruct s = {\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   // Assignment operand initialization.
   verifyFormat("s = {\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   // Returned object initialization.
   verifyFormat("return {\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   // Initializer list.
   verifyFormat("auto initializerList = {\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "};\n",
+   "};",
Style);
   // Function parameter initialization.
   verifyFormat("func({\n"
"\"x\",\n"
"\"y\",\n"
"\"z\",\n"
-   "});\n",
+   "});",
Style);
   // Nested init lists.
   verifyFormat("SomeStruct s = {\n"
"{{init1, init2, init3, init4, init5},\n"
" {init1, init2, init3, init4, init5}}\n"
-   "};\n",
+   "};",
Style);
   verifyFormat("SomeStruct s = {\n"
"{{\n"
@@ -25629,7 +25629,7 @@
" .init5 = 5,\n"
" },\n"
" {init1, init2, init3, init4, init5}}\n"
-   "};\n",
+   "};",
Style);
   verifyFormat("SomeArrayT a[3] = {\n"
"{\n"
@@ -25641,7 +25641,7 @@
"bar,\n"
"},\n"
"SomeArrayT{},\n"
-   "};\n",
+   "};",
Style);
   verifyFormat("SomeArrayT a[3] = {\n"
"{foo},\n"
@@ -25658,7 +25658,7 @@
"},\n"
"},\n"
"{baz},\n"
-   "};\n",
+   "};",
Style);
 }
 
Index: clang/lib/Format/FormatToken.cpp
=

[PATCH] D153205: [clang-format] Support block indenting array/struct list initializers

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/include/clang/Format/Format.h:95-98
 /// \warning
-///  Note: This currently only applies to parentheses.
+///  Note: This currently only applies to parentheses and braced list
+///  initializers when ``Cpp11BracedListStyle`` is ``True``.
 /// \endwarning

owenpan wrote:
> 
This `\note` is not currently supported? I have submitted a rev D154552 to add 
such ability.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

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


[PATCH] D154552: [clang-format] Add note directive generation for docs

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.
Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

It looks like your clang-format review does not contain any unit tests, please 
try to ensure all code changes have a unit test (unless this is an `NFC` or 
refactoring, adding documentation etc..)

Add your unit tests in `clang/unittests/Format` and you can build with `ninja 
FormatTests`.  We recommend using the `verifyFormat(xxx)` format of unit tests 
rather than `EXPECT_EQ` as this will ensure you change is tolerant to random 
whitespace changes (see FormatTest.cpp as an example)

For situations where your change is altering the TokenAnnotator.cpp which can 
happen if you are trying to improve the annotation phase to ensure we are 
correctly identifying the type of a token, please add a token annotator test in 
`TokenAnnotatorTest.cpp`


Generate a ..note:: directive when the \note is encountered in Format.h


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154552

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/tools/dump_format_style.py
  clang/include/clang/Format/Format.h

Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -133,8 +133,10 @@
   /// if not ``None``, when using initialization for an array of structs
   /// aligns the fields into columns.
   ///
-  /// NOTE: As of clang-format 15 this option only applied to arrays with equal
-  /// number of columns per row.
+  /// \note
+  ///  As of clang-format 15 this option only applied to arrays with equal
+  ///  number of columns per row.
+  /// \endnote
   ///
   /// \version 13
   ArrayInitializerAlignmentStyle AlignArrayOfStructures;
@@ -439,8 +441,10 @@
 
   /// Control of trailing comments.
   ///
-  /// NOTE: As of clang-format 16 this option is not a bool but can be set
-  /// to the options. Conventional bool options still can be parsed as before.
+  /// \note
+  ///  As of clang-format 16 this option is not a bool but can be set
+  ///  to the options. Conventional bool options still can be parsed as before.
+  /// \endnote
   ///
   /// \code{.yaml}
   ///   # Example of usage:
@@ -1093,8 +1097,10 @@
 /// \endcode
 bool AfterNamespace;
 /// Wrap ObjC definitions (interfaces, implementations...).
-/// \note @autoreleasepool and @synchronized blocks are wrapped
-/// according to `AfterControlStatement` flag.
+/// \note
+///  @autoreleasepool and @synchronized blocks are wrapped
+///  according to `AfterControlStatement` flag.
+/// \endnote
 bool AfterObjCDeclaration;
 /// Wrap struct definitions.
 /// \code
@@ -1289,7 +1295,9 @@
   /// otherwise it will scan until the closing `]` to determine if it should add
   /// newlines between elements (prettier compatible).
   ///
-  /// NOTE: This is currently only for formatting JSON.
+  /// \note
+  ///  This is currently only for formatting JSON.
+  /// \endnote
   /// \code
   ///true:  false:
   ///[  vs.  [1, 2, 3, 4]
@@ -2179,8 +2187,10 @@
   /// made, clang-format analyzes whether there are other bin-packed cases in
   /// the input file and act accordingly.
   ///
-  /// NOTE: This is an experimental flag, that might go away or be renamed. Do
-  /// not use this in config files, etc. Use at your own risk.
+  /// \note
+  ///  This is an experimental flag, that might go away or be renamed. Do
+  ///  not use this in config files, etc. Use at your own risk.
+  /// \endnote
   /// \version 3.7
   bool ExperimentalAutoDetectBinPacking;
 
@@ -3199,10 +3209,12 @@
   ///   * restrict
   ///   * type
   ///
-  /// Note: it MUST contain 'type'.
-  /// Items to the left of 'type' will be placed to the left of the type and
-  /// aligned in the order supplied. Items to the right of 'type' will be placed
-  /// to the right of the type and aligned in the order supplied.
+  /// \note
+  ///  it MUST contain 'type'.
+  ///  Items to the left of 'type' will be placed to the left of the type and
+  ///  aligned in the order supplied. Items to the right of 'type' will be
+  ///  placed to the right of the type and aligned in the order supplied.
+  /// \endnote
   ///
   /// \code{.yaml}
   ///   QualifierOrder: ['inline', 'static', 'type', 'const', 'volatile' ]
Index: clang/docs/tools/dump_format_style.py
===
--- clang/docs/tools/dump_format_style.py
+++ clang/docs/tools/dump_format_style.py
@@ -257,6 +257,14 @@
 endwarning_match = re.match(r"^/// +\\endwarning$", line)
 if endwarning_match:
 return ""
+
+match = 

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

Changes the AllowShortLoopsOnASingleLine from a boolean to a style option
consisting of three choices: Never, NonEmpty, and All. Never does not merge a
loop body back to the header's line. NonEmpty allows for merging a loop body
only if it has an expression. The new option is All, which will merge empty
loop bodies, i.e., a semi-colon.

The options of true and false are maintained for backward compatibility to mean
NonEmpty and Never, respectively.

See also Github Issue 61708.
https://github.com/llvm/llvm-project/issues/61708


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154550

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1380,7 +1380,7 @@
 
 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
   FormatStyle AllowsMergedLoops = getLLVMStyle();
-  AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_NonEmpty;
   verifyFormat("while (true) continue;", AllowsMergedLoops);
   verifyFormat("for (;;) continue;", AllowsMergedLoops);
   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
@@ -1436,6 +1436,66 @@
"  while (true);\n"
"}",
AllowsMergedLoops);
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_All;
+  verifyFormat("while (true) continue;", AllowsMergedLoops);
+  verifyFormat("for (;;) continue;", AllowsMergedLoops);
+  verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int &v, vec)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  BOOST_FOREACH (int &v, vec) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) // Can't merge this\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) /* still don't merge */\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("do a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do /* Don't merge */\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do\n"
+   "  // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  // Without braces labels are interpreted differently.
+  verifyFormat("{\n"
+   "  do\n"
+   "  label:\n"
+   "a++;\n"
+   "  while (true);\n"
+   "}",
+   AllowsMergedLoops);
 }
 
 TEST_F(FormatTest, FormatShortBracedStatements) {
@@ -1443,7 +1503,8 @@
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
 FormatStyle::SIS_Never);
-  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
+  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine,
+FormatStyle::SWFLS_Never);
   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
   verifyFormat("for (;;) {\n"
"  f();\n"
@@ -1488,7 +1549,8 @@
   AllowSimpleBracedStatements.ColumnLimit = 40;
   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
   FormatStyle::SBS_Always;
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASi

[PATCH] D153205: [clang-format] Support block indenting array/struct list initializers

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare requested review of this revision.
gedare marked 8 inline comments as done.
gedare added inline comments.



Comment at: clang/lib/Format/ContinuationIndenter.cpp:365-367
+   (Current.is(tok::r_brace) && Style.Cpp11BracedListStyle &&
+Current.MatchingParen->isOneOf(BK_BracedInit, BK_ListInit) &&
+Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent))) {

owenpan wrote:
> And `isBlockIndentedInitRBrace()` returns true only if the matching `l_brace` 
> is of `BK_BracedInit` or preceded by an `=`.
yes, this works nicely. thanks for spotting it. 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

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


[PATCH] D153205: [clang-format] Support block indenting array/struct list initializers

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537402.
gedare added a comment.

Regenerate docs


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/FormatToken.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -4929,7 +4929,7 @@
"  \"\"};\n",
Style);
   // Designated initializers.
-  verifyFormat("int LongVariable[1] = {\n"
+  verifyFormat("int LongVariable[2] = {\n"
"  [0] = 1000, [1] = 2000};",
Style);
   verifyFormat("SomeStruct s{\n"
@@ -5039,7 +5039,7 @@
"bar,\n"
"  },\n"
"  SomeArrayT{},\n"
-   "}\n",
+   "};",
Style);
   verifyFormat("SomeArrayT a[3] = {\n"
"  {foo},\n"
@@ -5056,7 +5056,7 @@
"},\n"
"  },\n"
"  {baz},\n"
-   "}\n",
+   "};",
Style);
 
   // Aligning after open braces unaffected by BracedInitializerIndentWidth.
@@ -25494,6 +25494,155 @@
Style);
 }
 
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
+  auto Style = getLLVMStyleWithColumns(60);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  // Aggregate initialization.
+  verifyFormat("int LongVariable[2] = {\n"
+   "1000, 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   "\"\", \"\",\n"
+   "\"\"\n"
+   "};",
+   Style);
+  // Designated initializers.
+  verifyFormat("int LongVariable[2] = {\n"
+   "[0] = 1000, [1] = 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   ".foo = \"x\",\n"
+   ".bar = \"y\",\n"
+   ".baz = \"z\"\n"
+   "};\n",
+   Style);
+  // List initialization.
+  verifyFormat("SomeStruct s{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("new SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Member initializer.
+  verifyFormat("class SomeClass {\n"
+   "  SomeStruct s{\n"
+   "  \"x\",\n"
+   "  \"y\",\n"
+   "  \"z\",\n"
+   "  };\n"
+   "};\n",
+   Style);
+  // Constructor member initializer.
+  verifyFormat("SomeClass::SomeClass : strct{\n"
+   "   \"x\",\n"
+   "   \"y\",\n"
+   "   \"z\",\n"
+   "   } {}\n",
+   Style);
+  // Copy initialization.
+  verifyFormat("SomeStruct s = SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Copy list initialization.
+  verifyFormat("SomeStruct s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Assignment operand initialization.
+  verifyFormat("s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Returned object initialization.
+  verifyFormat("return {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Initializer list.
+

[PATCH] D153205: [clang-format] Add new block type ListInit

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537399.
gedare added a comment.

Address comments from owenpan.

Do not add the new block type, and instead check explicitly for the equal sign
before an opening right brace.


Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

Your review contains a change to ClangFormatStyleOptions.rst but not a change 
to clang/include/clang/Format/Format.h

ClangFormatStyleOptions.rst is auto generated from Format.h via 
clang/docs/tools/dump_format_style.py,  please run this to regenerate the .rst

You can validate that the rst is valid by running.

  ./docs/tools/dump_format_style.py
  mkdir -p html
  /usr/bin/sphinx-build -n ./docs ./html


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/FormatToken.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -4929,7 +4929,7 @@
"  \"\"};\n",
Style);
   // Designated initializers.
-  verifyFormat("int LongVariable[1] = {\n"
+  verifyFormat("int LongVariable[2] = {\n"
"  [0] = 1000, [1] = 2000};",
Style);
   verifyFormat("SomeStruct s{\n"
@@ -5039,7 +5039,7 @@
"bar,\n"
"  },\n"
"  SomeArrayT{},\n"
-   "}\n",
+   "};",
Style);
   verifyFormat("SomeArrayT a[3] = {\n"
"  {foo},\n"
@@ -5056,7 +5056,7 @@
"},\n"
"  },\n"
"  {baz},\n"
-   "}\n",
+   "};",
Style);
 
   // Aligning after open braces unaffected by BracedInitializerIndentWidth.
@@ -25494,6 +25494,155 @@
Style);
 }
 
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
+  auto Style = getLLVMStyleWithColumns(60);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  // Aggregate initialization.
+  verifyFormat("int LongVariable[2] = {\n"
+   "1000, 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   "\"\", \"\",\n"
+   "\"\"\n"
+   "};",
+   Style);
+  // Designated initializers.
+  verifyFormat("int LongVariable[2] = {\n"
+   "[0] = 1000, [1] = 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   ".foo = \"x\",\n"
+   ".bar = \"y\",\n"
+   ".baz = \"z\"\n"
+   "};\n",
+   Style);
+  // List initialization.
+  verifyFormat("SomeStruct s{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("new SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Member initializer.
+  verifyFormat("class SomeClass {\n"
+   "  SomeStruct s{\n"
+   "  \"x\",\n"
+   "  \"y\",\n"
+   "  \"z\",\n"
+   "  };\n"
+   "};\n",
+   Style);
+  // Constructor member initializer.
+  verifyFormat("SomeClass::SomeClass : strct{\n"
+   "   \"x\",\n"
+   "   \"y\",\n"
+   "   \"z\",\n"
+   "   } {}\n",
+   Style);
+  // Copy initialization.
+  verifyFormat("SomeStruct s = SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Copy list initialization.
+  verifyFormat("SomeStruct s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"zzz

[PATCH] D153579: [clang-format] Fix RAS reference alignment when PAS is left or middle

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537363.
gedare edited the summary of this revision.
gedare added a comment.

Use suggested shorter logic


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153579

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2189,6 +2189,15 @@
   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
 
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("Const unsigned int * c;\n"
+   "const unsigned int * d;\n"
+   "Const unsigned int  &e;\n"
+   "const unsigned int  &f;\n"
+   "const unsigned &&g;\n"
+   "Const unsigned   h;",
+   Style);
+
   // FIXME: we don't handle this yet, so output may be arbitrary until it's
   // specifically handled
   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -453,12 +453,24 @@
   Changes[i + 1].PreviousEndOfTokenColumn += Shift;
 
 // If PointerAlignment is PAS_Right, keep *s or &s next to the token
-if (Style.PointerAlignment == FormatStyle::PAS_Right &&
+if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
+ Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
 Changes[i].Spaces != 0) {
+  const bool ReferenceNotRightAligned =
+  Style.ReferenceAlignment != FormatStyle::RAS_Right &&
+  Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
   for (int Previous = i - 1;
Previous >= 0 &&
Changes[Previous].Tok->getType() == TT_PointerOrReference;
--Previous) {
+assert(
+Changes[Previous].Tok->isOneOf(tok::star, tok::amp, tok::ampamp));
+if (Changes[Previous].Tok->isNot(tok::star)) {
+  if (ReferenceNotRightAligned)
+continue;
+} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
+  continue;
+}
 Changes[Previous + 1].Spaces -= Shift;
 Changes[Previous].Spaces += Shift;
 Changes[Previous].StartOfTokenColumn += Shift;


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2189,6 +2189,15 @@
   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
 
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("Const unsigned int * c;\n"
+   "const unsigned int * d;\n"
+   "Const unsigned int  &e;\n"
+   "const unsigned int  &f;\n"
+   "const unsigned &&g;\n"
+   "Const unsigned   h;",
+   Style);
+
   // FIXME: we don't handle this yet, so output may be arbitrary until it's
   // specifically handled
   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -453,12 +453,24 @@
   Changes[i + 1].PreviousEndOfTokenColumn += Shift;
 
 // If PointerAlignment is PAS_Right, keep *s or &s next to the token
-if (Style.PointerAlignment == FormatStyle::PAS_Right &&
+if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
+ Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
 Changes[i].Spaces != 0) {
+  const bool ReferenceNotRightAligned =
+  Style.ReferenceAlignment != FormatStyle::RAS_Right &&
+  Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
   for (int Previous = i - 1;
Previous >= 0 &&
Changes[Previous].Tok->getType() == TT_PointerOrReference;
--Previous) {
+assert(
+Changes[Previous].Tok->isOneOf(tok::star, tok::amp, tok::ampamp));
+if (Changes[Previous].Tok->isNot(tok::star)) {
+  if (ReferenceNotRightAligned)
+continue;
+} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
+  continue;
+}
 Changes[Previous + 1].Spaces -= Shift;
 Changes[Previous].Spaces += Shift;
 Changes[Previous].StartOfTokenColumn += Shift;
___
cfe-commits mailing list
cfe-

[PATCH] D153585: [clang-format] Fix align consecutive declarations over function pointers

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537359.
gedare added a comment.

Merge the two if statements.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153585

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2059,6 +2059,8 @@
"const unsigned int *d;\n"
"Const unsigned int &e;\n"
"const unsigned int &f;\n"
+   "int*f1(int *a, int &b, int &&c);\n"
+   "double *(*f2)(int *a, double &&b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2104,6 +2106,8 @@
"const unsigned int* d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int* a, int& b, int&& c);\n"
+   "double* (*f2)(int* a, double&& b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2129,6 +2133,8 @@
"const unsigned int *d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int *a, int& b, int&& c);\n"
+   "double *(*f2)(int *a, double&& b);\n"
"const unsigned  g;\n"
"Const unsigned  h;",
Style);
@@ -2169,6 +2175,8 @@
"const unsigned int*  d;\n"
"Const unsigned int & e;\n"
"const unsigned int & f;\n"
+   "int* f1(int* a, int & b, int && c);\n"
+   "double*  (*f2)(int* a, double && b);\n"
"const unsigned &&g;\n"
"Const unsigned   h;",
Style);
@@ -2189,6 +2197,17 @@
   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
 
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("Const unsigned int * c;\n"
+   "const unsigned int * d;\n"
+   "Const unsigned int  &e;\n"
+   "const unsigned int  &f;\n"
+   "int *f1(int * a, int &b, int &&c);\n"
+   "double * (*f2)(int * a, double &&b);\n"
+   "const unsigned &&g;\n"
+   "Const unsigned   h;",
+   Style);
+
   // FIXME: we don't handle this yet, so output may be arbitrary until it's
   // specifically handled
   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -866,7 +866,7 @@
   AlignTokens(
   Style,
   [](Change const &C) {
-if (C.Tok->is(TT_FunctionDeclarationName))
+if (C.Tok->isOneOf(TT_FunctionDeclarationName, TT_FunctionTypeLParen))
   return true;
 if (C.Tok->isNot(TT_StartOfName))
   return false;


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2059,6 +2059,8 @@
"const unsigned int *d;\n"
"Const unsigned int &e;\n"
"const unsigned int &f;\n"
+   "int*f1(int *a, int &b, int &&c);\n"
+   "double *(*f2)(int *a, double &&b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2104,6 +2106,8 @@
"const unsigned int* d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int* a, int& b, int&& c);\n"
+   "double* (*f2)(int* a, double&& b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2129,6 +2133,8 @@
"const unsigned int *d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int *a, int& b, int&& c);\n"
+   "double *(*f2)(int *a, double&& b);\n"
"const unsigned  g;\n"
"Const unsigned  h;",
Style);
@@ -2169,6 +2175,8 @@
"const unsigned int*  d;\n"
"Const unsigned int & e;\n"
"const unsigned int & f;\n"

[PATCH] D153585: [clang-format] Fix align consecutive declarations over function pointers

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

I'm not sure how to properly stack dependent revs.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153585

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


[PATCH] D153585: [clang-format] Fix align consecutive declarations over function pointers

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

Split this out from D153579 . This requires 
D153579  first, for the test cases to pass.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153585

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


[PATCH] D153585: [clang-format] Fix align consecutive declarations over function pointers

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

Fixes a bug that prevents alignment from proceeding through a function pointer 
in a list of declarations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153585

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2059,6 +2059,8 @@
"const unsigned int *d;\n"
"Const unsigned int &e;\n"
"const unsigned int &f;\n"
+   "int*f1(int *a, int &b, int &&c);\n"
+   "double *(*f2)(int *a, double &&b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2104,6 +2106,8 @@
"const unsigned int* d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int* a, int& b, int&& c);\n"
+   "double* (*f2)(int* a, double&& b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2129,6 +2133,8 @@
"const unsigned int *d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int *a, int& b, int&& c);\n"
+   "double *(*f2)(int *a, double&& b);\n"
"const unsigned  g;\n"
"Const unsigned  h;",
Style);
@@ -2169,6 +2175,8 @@
"const unsigned int*  d;\n"
"Const unsigned int & e;\n"
"const unsigned int & f;\n"
+   "int* f1(int* a, int & b, int && c);\n"
+   "double*  (*f2)(int* a, double && b);\n"
"const unsigned &&g;\n"
"Const unsigned   h;",
Style);
@@ -2189,6 +2197,17 @@
   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
 
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("Const unsigned int * c;\n"
+   "const unsigned int * d;\n"
+   "Const unsigned int  &e;\n"
+   "const unsigned int  &f;\n"
+   "int *f1(int * a, int &b, int &&c);\n"
+   "double * (*f2)(int * a, double &&b);\n"
+   "const unsigned &&g;\n"
+   "Const unsigned   h;",
+   Style);
+
   // FIXME: we don't handle this yet, so output may be arbitrary until it's
   // specifically handled
   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -868,6 +868,8 @@
   [](Change const &C) {
 if (C.Tok->is(TT_FunctionDeclarationName))
   return true;
+if (C.Tok->is(TT_FunctionTypeLParen))
+  return true;
 if (C.Tok->isNot(TT_StartOfName))
   return false;
 if (C.Tok->Previous &&


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2059,6 +2059,8 @@
"const unsigned int *d;\n"
"Const unsigned int &e;\n"
"const unsigned int &f;\n"
+   "int*f1(int *a, int &b, int &&c);\n"
+   "double *(*f2)(int *a, double &&b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2104,6 +2106,8 @@
"const unsigned int* d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int* a, int& b, int&& c);\n"
+   "double* (*f2)(int* a, double&& b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2129,6 +2133,8 @@
"const unsigned int *d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int *a, int& b, int&& c);\n"
+   "double *(*f2)(int *a, double&& b);\n"
"const unsigned  g;\n"
"Const unsigned  h;",

[PATCH] D153579: [clang-format] Fix RAS reference alignment when PAS is left or middle

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D153579#4442346 , 
@HazardyKnusperkeks wrote:

> Could you please split this into two reviews?

yes, done, will stage the other review soon. The function pointer alignment 
requires this fix for the formatting to be correct inside of the nested scope 
of the function pointer parameters.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153579

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


[PATCH] D153579: [clang-format] Align consecutive function pointers and references

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 533745.
gedare edited the summary of this revision.
gedare added a comment.

Split and keep right-aligned references, will put function pointers in new rev


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153579

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2189,6 +2189,15 @@
   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
 
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("Const unsigned int * c;\n"
+   "const unsigned int * d;\n"
+   "Const unsigned int  &e;\n"
+   "const unsigned int  &f;\n"
+   "const unsigned &&g;\n"
+   "Const unsigned   h;",
+   Style);
+
   // FIXME: we don't handle this yet, so output may be arbitrary until it's
   // specifically handled
   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -453,12 +453,22 @@
   Changes[i + 1].PreviousEndOfTokenColumn += Shift;
 
 // If PointerAlignment is PAS_Right, keep *s or &s next to the token
-if (Style.PointerAlignment == FormatStyle::PAS_Right &&
+if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
+ Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
 Changes[i].Spaces != 0) {
   for (int Previous = i - 1;
Previous >= 0 &&
Changes[Previous].Tok->getType() == TT_PointerOrReference;
--Previous) {
+if (Changes[Previous].Tok->is(tok::amp) ||
+Changes[Previous].Tok->is(tok::ampamp)) {
+  if (Style.ReferenceAlignment != FormatStyle::RAS_Right &&
+  Style.ReferenceAlignment != FormatStyle::RAS_Pointer) {
+continue;
+  }
+} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
+  continue;
+}
 Changes[Previous + 1].Spaces -= Shift;
 Changes[Previous].Spaces += Shift;
 Changes[Previous].StartOfTokenColumn += Shift;


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2189,6 +2189,15 @@
   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
 
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("Const unsigned int * c;\n"
+   "const unsigned int * d;\n"
+   "Const unsigned int  &e;\n"
+   "const unsigned int  &f;\n"
+   "const unsigned &&g;\n"
+   "Const unsigned   h;",
+   Style);
+
   // FIXME: we don't handle this yet, so output may be arbitrary until it's
   // specifically handled
   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -453,12 +453,22 @@
   Changes[i + 1].PreviousEndOfTokenColumn += Shift;
 
 // If PointerAlignment is PAS_Right, keep *s or &s next to the token
-if (Style.PointerAlignment == FormatStyle::PAS_Right &&
+if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
+ Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
 Changes[i].Spaces != 0) {
   for (int Previous = i - 1;
Previous >= 0 &&
Changes[Previous].Tok->getType() == TT_PointerOrReference;
--Previous) {
+if (Changes[Previous].Tok->is(tok::amp) ||
+Changes[Previous].Tok->is(tok::ampamp)) {
+  if (Style.ReferenceAlignment != FormatStyle::RAS_Right &&
+  Style.ReferenceAlignment != FormatStyle::RAS_Pointer) {
+continue;
+  }
+} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
+  continue;
+}
 Changes[Previous + 1].Spaces -= Shift;
 Changes[Previous].Spaces += Shift;
 Changes[Previous].StartOfTokenColumn += Shift;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153579: [clang-format] Align consecutive function pointers and references

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

Allows alignment of consecutive declarations that include function pointers. 
Also fixes a latent bug with the handling of right aligned references with 
left/middle alignment pointers.

Addresses Issues 63451 and 63452.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153579

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2059,6 +2059,8 @@
"const unsigned int *d;\n"
"Const unsigned int &e;\n"
"const unsigned int &f;\n"
+   "int*f1(int *a, int &b, int &&c);\n"
+   "double *(*f2)(int *a, double &&b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2104,6 +2106,8 @@
"const unsigned int* d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int* a, int& b, int&& c);\n"
+   "double* (*f2)(int* a, double&& b);\n"
"const unsigned&&g;\n"
"Const unsigned  h;",
Style);
@@ -2129,6 +2133,8 @@
"const unsigned int *d;\n"
"Const unsigned int& e;\n"
"const unsigned int& f;\n"
+   "int*f1(int *a, int& b, int&& c);\n"
+   "double *(*f2)(int *a, double&& b);\n"
"const unsigned  g;\n"
"Const unsigned  h;",
Style);
@@ -2169,6 +2175,8 @@
"const unsigned int*  d;\n"
"Const unsigned int & e;\n"
"const unsigned int & f;\n"
+   "int* f1(int* a, int & b, int && c);\n"
+   "double*  (*f2)(int* a, double && b);\n"
"const unsigned &&g;\n"
"Const unsigned   h;",
Style);
@@ -2189,6 +2197,17 @@
   verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
   verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
 
+  Style.AlignConsecutiveDeclarations.Enabled = true;
+  verifyFormat("Const unsigned int * c;\n"
+   "const unsigned int * d;\n"
+   "Const unsigned int  &e;\n"
+   "const unsigned int  &f;\n"
+   "int *f1(int * a, int &b, int &&c);\n"
+   "double * (*f2)(int * a, double &&b);\n"
+   "const unsigned &&g;\n"
+   "Const unsigned   h;",
+   Style);
+
   // FIXME: we don't handle this yet, so output may be arbitrary until it's
   // specifically handled
   // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -453,12 +453,22 @@
   Changes[i + 1].PreviousEndOfTokenColumn += Shift;
 
 // If PointerAlignment is PAS_Right, keep *s or &s next to the token
-if (Style.PointerAlignment == FormatStyle::PAS_Right &&
+if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
+ Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
 Changes[i].Spaces != 0) {
   for (int Previous = i - 1;
Previous >= 0 &&
Changes[Previous].Tok->getType() == TT_PointerOrReference;
--Previous) {
+if (Changes[Previous].Tok->is(tok::amp) ||
+Changes[Previous].Tok->is(tok::ampamp)) {
+  if (Style.ReferenceAlignment != FormatStyle::RAS_Right &&
+  Style.ReferenceAlignment != FormatStyle::RAS_Pointer) {
+continue;
+  }
+} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
+  continue;
+}
 Changes[Previous + 1].Spaces -= Shift;
 Changes[Previous].Spaces += Shift;
 Changes[Previous].StartOfTokenColumn += Shift;
@@ -868,6 +878,8 @@
   [](Change const &C) {
 if (C.Tok->is(TT_FunctionDeclarationName))
   return true;
+if (C.Tok->is(TT_FunctionTypeLParen))
+  return true;
 if (C.Tok->isNot(TT_StartOfName))
   return false;
 if (C.Tok->Previous &&


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/For

[PATCH] D153205: [clang-format] Add new block type ListInit

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

Can someone land for me? thanks.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

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


[PATCH] D152975: [clang-format] Allow break after return keyword

2023-06-22 Thread Gedare Bloom via Phabricator via cfe-commits
gedare abandoned this revision.
gedare added a comment.

I don't like the approach I took. It will be hard (impossible) to tune a 
default return penalty that ensures backward compatibility. The only way I see 
to make this work is to add an option to enable breaks after the return 
keyword. This is low on my priorities however, so I am abandoning this.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152975

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


[PATCH] D153205: [clang-format] Add new block type ListInit

2023-06-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 532967.
gedare added a comment.

Do not use temporary variable for getBlockKind().
Fix test case syntax errors.
Fix formatting.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/FormatToken.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -4929,7 +4929,7 @@
"  \"\"};\n",
Style);
   // Designated initializers.
-  verifyFormat("int LongVariable[1] = {\n"
+  verifyFormat("int LongVariable[2] = {\n"
"  [0] = 1000, [1] = 2000};",
Style);
   verifyFormat("SomeStruct s{\n"
@@ -5039,7 +5039,7 @@
"bar,\n"
"  },\n"
"  SomeArrayT{},\n"
-   "}\n",
+   "};\n",
Style);
   verifyFormat("SomeArrayT a[3] = {\n"
"  {foo},\n"
@@ -5056,7 +5056,7 @@
"},\n"
"  },\n"
"  {baz},\n"
-   "}\n",
+   "};\n",
Style);
 
   // Aligning after open braces unaffected by BracedInitializerIndentWidth.
@@ -25494,6 +25494,155 @@
Style);
 }
 
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
+  auto Style = getLLVMStyleWithColumns(60);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  // Aggregate initialization.
+  verifyFormat("int LongVariable[2] = {\n"
+   "1000, 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   "\"\", \"\",\n"
+   "\"\"\n"
+   "};",
+   Style);
+  // Designated initializers.
+  verifyFormat("int LongVariable[2] = {\n"
+   "[0] = 1000, [1] = 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   ".foo = \"x\",\n"
+   ".bar = \"y\",\n"
+   ".baz = \"z\"\n"
+   "};\n",
+   Style);
+  // List initialization.
+  verifyFormat("SomeStruct s{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("new SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Member initializer.
+  verifyFormat("class SomeClass {\n"
+   "  SomeStruct s{\n"
+   "  \"x\",\n"
+   "  \"y\",\n"
+   "  \"z\",\n"
+   "  };\n"
+   "};\n",
+   Style);
+  // Constructor member initializer.
+  verifyFormat("SomeClass::SomeClass : strct{\n"
+   "   \"x\",\n"
+   "   \"y\",\n"
+   "   \"z\",\n"
+   "   } {}\n",
+   Style);
+  // Copy initialization.
+  verifyFormat("SomeStruct s = SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Copy list initialization.
+  verifyFormat("SomeStruct s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Assignment operand initialization.
+  verifyFormat("s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Returned object initialization.
+  verifyFormat("return {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+ 

[PATCH] D153205: [clang-format] Add new block type ListInit

2023-06-20 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D153205#4431235 , 
@HazardyKnusperkeks wrote:

> You add a lot of checks and I honestly can't say if it does not affect other 
> code that is not covered by the tests.

Not much I can do about that. I guess it is worth noting that all the checks I 
add are predicated on either BK_ListInit or BK_BracedInit.  I have reduced the 
checks to what minimally works to address the Issue.  Technically, this has two 
distinct changes included:

- Allow BlockIndent to work with Braces.
- Allow BlockIndent to work with array/struct initializers.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

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


[PATCH] D153205: [clang-format] Add new block type ListInit

2023-06-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D153205#4430528 , @owenpan wrote:

> It seems to me that there has been a proliferation of new options being 
> proposed and/or accepted recently. I'd like to remind everyone of the 
> long-standing policy 
> 
>  of adding new options. That being said, I wonder if we should add a new 
> language `LK_C` along with its variants C78 (i.e. K&R C), C89 (i.e. ANSI C), 
> GNU extensions (e.g. #55745 
>  and #62755 
> ), etc.

Understood. In this case, the `BK_ListInit` is not visible at the API / Style 
level. It is an internal categorization to help distinguish different kinds of 
blocks.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153205

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


[PATCH] D153205: [clang-format] Add new block type ListInit

2023-06-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

C89 and C90 list initializers are treated differently than Cpp11 braced
initializers. This patch introduces a new block type (BK_ListInit) that
is used to identify the C array/struct initializer lists and to support
formatting rules for BracketAlignmentStyle.BlockIndent applied to them.

Addresses Issue #57878.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153205

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/FormatToken.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25494,6 +25494,155 @@
Style);
 }
 
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
+  auto Style = getLLVMStyleWithColumns(60);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  // Aggregate initialization.
+  verifyFormat("int LongVariable[2] = {\n"
+   "1000, 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   "\"\", \"\",\n"
+   "\"\"\n"
+   "};",
+   Style);
+  // Designated initializers.
+  verifyFormat("int LongVariable[1] = {\n"
+   "[0] = 1000, [1] = 2000\n"
+   "};",
+   Style);
+  verifyFormat("SomeStruct s{\n"
+   ".foo = \"x\",\n"
+   ".bar = \"y\",\n"
+   ".baz = \"z\"\n"
+   "};\n",
+   Style);
+  // List initialization.
+  verifyFormat("SomeStruct s{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  verifyFormat("new SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Member initializer.
+  verifyFormat("class SomeClass {\n"
+   "  SomeStruct s{\n"
+   "  \"x\",\n"
+   "  \"y\",\n"
+   "  \"z\",\n"
+   "  };\n"
+   "};\n",
+   Style);
+  // Constructor member initializer.
+  verifyFormat("SomeClass::SomeClass : strct{\n"
+   "   \"x\",\n"
+   "   \"y\",\n"
+   "   \"z\",\n"
+   "   } {}\n",
+   Style);
+  // Copy initialization.
+  verifyFormat("SomeStruct s = SomeStruct{\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Copy list initialization.
+  verifyFormat("SomeStruct s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Assignment operand initialization.
+  verifyFormat("s = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Returned object initialization.
+  verifyFormat("return {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Initializer list.
+  verifyFormat("auto initializerList = {\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "};\n",
+   Style);
+  // Function parameter initialization.
+  verifyFormat("func({\n"
+   "\"x\",\n"
+   "\"y\",\n"
+   "\"z\",\n"
+   "});\n",
+   Style);
+  // Nested init lists.
+  verifyFormat("SomeStruct s = {\n"
+   "{{i

[PATCH] D152975: [clang-format] Allow break after return keyword

2023-06-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D152975#4425932 , 
@HazardyKnusperkeks wrote:

> I'd like more tests (and examples how it is before the change), some 
> operations (+, *, etc.), your stated string literal with and without many 
> spaces, call chains.

Ok. Since it applies by default, all the existing unittests use it. I will see 
about fabricating some more interesting tests for it.

> I have nothing against the introduction, I'm just not sure about the default 
> value and the position of the check in `splitPenalty`.

Yes the default was chosen arbitrarily.  Adding more tests to exercise return 
value lengths should help tune defaults.

> And please add a remark in the changelog.

Is there a separate changelog file?  I don't understand this request.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152975

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


[PATCH] D152975: [clang-format] Allow break after return keyword

2023-06-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 531561.
gedare added a comment.

- Regenerate ClangFormatStyleOptions.rst


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152975

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

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -8916,6 +8916,9 @@
   verifyFormat("return aaa(\n"
"   aa) &&\n"
"   aaa;");
+  verifyFormat("return\n"
+   "aaa"
+   "();");
   verifyFormat("return\n"
"// true if code is one of a or b.\n"
"code == a || code == b;");
@@ -22179,9 +22182,9 @@
   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
"[]()\n"
"{\n"
-   "  return "
-   "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
-   "eConsiderAsInline;\n"
+   "  return\n"
+   "  HereAVeryLongLineThatWillBeFormattedOnMultipleLineAnd"
+   "ShouldNotBeConsiderAsInline;\n"
"});",
LLVMWithBeforeLambdaBody);
   verifyFormat(
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3525,6 +3525,8 @@
 }
   }
 
+  if (Left.is(tok::kw_return))
+return Style.PenaltyBreakReturn;
   if (Left.is(tok::coloncolon))
 return 500;
   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
@@ -5526,7 +5528,8 @@
   }
 
   return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
-  tok::kw_class, tok::kw_struct, tok::comment) ||
+  tok::kw_class, tok::kw_struct, tok::comment,
+  tok::kw_return) ||
  Right.isMemberAccess() ||
  Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
tok::colon, tok::l_square, tok::at) ||
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -967,6 +967,7 @@
Style.PenaltyBreakFirstLessLess);
 IO.mapOptional("PenaltyBreakOpenParenthesis",
Style.PenaltyBreakOpenParenthesis);
+IO.mapOptional("PenaltyBreakReturn", Style.PenaltyBreakReturn);
 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
 IO.mapOptional("PenaltyBreakTemplateDeclaration",
Style.PenaltyBreakTemplateDeclaration);
@@ -1480,6 +1481,7 @@
   LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
   LLVMStyle.PenaltyBreakComment = 300;
   LLVMStyle.PenaltyBreakFirstLessLess = 120;
+  LLVMStyle.PenaltyBreakReturn = 100;
   LLVMStyle.PenaltyBreakString = 1000;
   LLVMStyle.PenaltyExcessCharacter = 100;
   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3082,6 +3082,10 @@
   /// \version 14
   unsigned PenaltyBreakOpenParenthesis;
 
+  /// The penalty for breaking after ``return``.
+  /// \version 18
+  unsigned PenaltyBreakReturn;
+
   /// The penalty for each line break introduced inside a string literal.
   /// \version 3.7
   unsigned PenaltyBreakString;
@@ -4391,6 +4395,7 @@
PenaltyBreakComment == R.PenaltyBreakComment &&
PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess &&
PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis &&
+   PenaltyBreakReturn == R.PenaltyBreakReturn &&
PenaltyBreakString == R.PenaltyBreakString &&
PenaltyBreakTemplateDeclaration ==
R.PenaltyBreakTemplateDeclaration &&
Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4048,6 +4048,11 @@
 **PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` :ref:`¶ `
   The penalty for breaking after ``(``.
 
+.. _PenaltyBreakReturn:
+
+**PenaltyBreakReturn** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ `
+  The penalty for breaking after ``return``.
+
 .. _P

[PATCH] D152975: [clang-format] Allow break after return keyword

2023-06-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 531557.
gedare added a comment.

Regenerate docs


Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

Your review contains a change to ClangFormatStyleOptions.rst but not a change 
to clang/include/clang/Format/Format.h

ClangFormatStyleOptions.rst is auto generated from Format.h via 
clang/docs/tools/dump_format_style.py,  please run this to regenerate the .rst

You can validate that the rst is valid by running.

  ./docs/tools/dump_format_style.py
  mkdir -p html
  /usr/bin/sphinx-build -n ./docs ./html


Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

It looks like your clang-format review does not contain any unit tests, please 
try to ensure all code changes have a unit test (unless this is an `NFC` or 
refactoring, adding documentation etc..)

Add your unit tests in `clang/unittests/Format` and you can build with `ninja 
FormatTests`.  We recommend using the `verifyFormat(xxx)` format of unit tests 
rather than `EXPECT_EQ` as this will ensure you change is tolerant to random 
whitespace changes (see FormatTest.cpp as an example)

For situations where your change is altering the TokenAnnotator.cpp which can 
happen if you are trying to improve the annotation phase to ensure we are 
correctly identifying the type of a token, please add a token annotator test in 
`TokenAnnotatorTest.cpp`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152975

Files:
  clang/docs/ClangFormatStyleOptions.rst


Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4048,6 +4048,11 @@
 **PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` 
:ref:`¶ `
   The penalty for breaking after ``(``.
 
+.. _PenaltyBreakReturn:
+
+**PenaltyBreakReturn** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ 
`
+  The penalty for breaking after ``return``.
+
 .. _PenaltyBreakString:
 
 **PenaltyBreakString** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ 
`


Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4048,6 +4048,11 @@
 **PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` :ref:`¶ `
   The penalty for breaking after ``(``.
 
+.. _PenaltyBreakReturn:
+
+**PenaltyBreakReturn** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ `
+  The penalty for breaking after ``return``.
+
 .. _PenaltyBreakString:
 
 **PenaltyBreakString** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ `
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152976: Regenerate ClangFormatStyleOptions.rst

2023-06-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added a project: All.
Herald added a comment.
gedare requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

NOTE: Clang-Format Team Automated Review Comment

Your review contains a change to ClangFormatStyleOptions.rst but not a change 
to clang/include/clang/Format/Format.h

ClangFormatStyleOptions.rst is auto generated from Format.h via 
clang/docs/tools/dump_format_style.py,  please run this to regenerate the .rst

You can validate that the rst is valid by running.

  ./docs/tools/dump_format_style.py
  mkdir -p html
  /usr/bin/sphinx-build -n ./docs ./html


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152976

Files:
  clang/docs/ClangFormatStyleOptions.rst


Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4048,6 +4048,11 @@
 **PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` 
:ref:`¶ `
   The penalty for breaking after ``(``.
 
+.. _PenaltyBreakReturn:
+
+**PenaltyBreakReturn** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ 
`
+  The penalty for breaking after ``return``.
+
 .. _PenaltyBreakString:
 
 **PenaltyBreakString** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ 
`


Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4048,6 +4048,11 @@
 **PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` :ref:`¶ `
   The penalty for breaking after ``(``.
 
+.. _PenaltyBreakReturn:
+
+**PenaltyBreakReturn** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ `
+  The penalty for breaking after ``return``.
+
 .. _PenaltyBreakString:
 
 **PenaltyBreakString** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ `
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152975: [clang-format] Allow break after return keyword

2023-06-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.
Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

Your review contains a change to clang/include/clang/Format/Format.h but does 
not contain an update to ClangFormatStyleOptions.rst

ClangFormatStyleOptions.rst is generated via 
clang/docs/tools/dump_format_style.py,  please run this to regenerate the .rst

You can validate that the rst is valid by running.

  ./docs/tools/dump_format_style.py
  mkdir -p html
  /usr/bin/sphinx-build -n ./docs ./html


Currently new line breaks are not added between the `return` keyword
and the return value. With long, singleton return values, it can be
preferred to break before the return value. An example of this would be
a lengthy string return value.

Adds a new style option PenaltyBreakReturn to control when breaks are
preferred. With the current setting of 100, most existing unit tests pass.
One unit test needed to be tweaked, as it assumes very long return values
do not get broken from the return keyword. Added a new unit test to exercise
the long return value.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152975

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


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -8916,6 +8916,9 @@
   verifyFormat("return aaa(\n"
"   aa) 
&&\n"
"   aaa;");
+  verifyFormat("return\n"
+   "
aaa"
+   "();");
   verifyFormat("return\n"
"// true if code is one of a or b.\n"
"code == a || code == b;");
@@ -22179,9 +22182,9 @@
   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
"[]()\n"
"{\n"
-   "  return "
-   
"HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
-   "eConsiderAsInline;\n"
+   "  return\n"
+   "  
HereAVeryLongLineThatWillBeFormattedOnMultipleLineAnd"
+   "ShouldNotBeConsiderAsInline;\n"
"});",
LLVMWithBeforeLambdaBody);
   verifyFormat(
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3525,6 +3525,8 @@
 }
   }
 
+  if (Left.is(tok::kw_return))
+return Style.PenaltyBreakReturn;
   if (Left.is(tok::coloncolon))
 return 500;
   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
@@ -5526,7 +5528,8 @@
   }
 
   return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
-  tok::kw_class, tok::kw_struct, tok::comment) ||
+  tok::kw_class, tok::kw_struct, tok::comment,
+  tok::kw_return) ||
  Right.isMemberAccess() ||
  Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
tok::colon, tok::l_square, tok::at) ||
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -967,6 +967,7 @@
Style.PenaltyBreakFirstLessLess);
 IO.mapOptional("PenaltyBreakOpenParenthesis",
Style.PenaltyBreakOpenParenthesis);
+IO.mapOptional("PenaltyBreakReturn", Style.PenaltyBreakReturn);
 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
 IO.mapOptional("PenaltyBreakTemplateDeclaration",
Style.PenaltyBreakTemplateDeclaration);
@@ -1480,6 +1481,7 @@
   LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
   LLVMStyle.PenaltyBreakComment = 300;
   LLVMStyle.PenaltyBreakFirstLessLess = 120;
+  LLVMStyle.PenaltyBreakReturn = 100;
   LLVMStyle.PenaltyBreakString = 1000;
   LLVMStyle.PenaltyExcessCharacter = 100;
   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3082,6 +3082,10 @@
   /// \version 14
   unsigned PenaltyBreakOpenParenthesis;
 
+  /// The penalty for breaking after ``return``.
+  /// \version 18
+  unsigned PenaltyBreakReturn;
+
   /// The penalty for each 

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-12-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D137762#3969770 , @owenpan wrote:

> @gedare do you need us to commit this patch on your behalf (using "Gedare 
> Bloom ")?

Yes, please. I don't have any llvm permissions, this is my first patch set for 
llvm actually. Anything else I need to do?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

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


[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

I have changed the code as recommended by @owenpan with new unit tests passing, 
and reverting the modifications to existing unit tests. Someone else may 
janitor the EXPECT_EQ blocks separately.

I will not be able to look at this again for ~2 weeks. with luck it is ready to 
review and land, if not I'll deal with any further requests in December. Thanks 
for all the feedback and guidance so far!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

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


[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 476196.
gedare added a comment.

- fixup: use simpler logic for handling rparens
- Revert "fixup: port other EXPECT_EQ to verifyFormat"


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7225,7 +7225,7 @@
   "void functionDecl(int A, int B,\n"
   "  int C);"),
 format(Input, Style));
-  // However, BAS_AlwaysBreak should take precedence over
+  // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
   // AllowAllArgumentsOnNextLine.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
   EXPECT_EQ(StringRef("functionCall(\n"
@@ -7233,6 +7233,14 @@
   "void functionDecl(\n"
   "int A, int B, int C);"),
 format(Input, Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC\n"
+   ");\n"
+   "void functionDecl(\n"
+   "int A, int B, int C\n"
+   ");",
+   Input, Style);
 
   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
   // first argument.
@@ -8435,6 +8443,52 @@
   "(a, )) &&\n"
   ");",
   Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  Style.BinPackArguments = false;
+  Style.BinPackParameters = false;
+  verifyFormat("void aa(\n"
+   "aaa ,\n"
+   "a aaa,\n"
+   "a\n"
+   ") {}",
+   Style);
+  verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
+   "aaa a,\n"
+   "aaa a,\n"
+   "a\n"
+   ");",
+   Style);
+  verifyFormat("SomeLongVariableName->someFunction(f(\n"
+   "aaa,\n"
+   "a,\n"
+   "a\n"
+   "));",
+   Style);
+  verifyFormat("(a(\n"
+   "(a, )\n"
+   "));",
+   Style);
+  verifyFormat("(aa.aa(\n"
+   "(a, )\n"
+   "));",
+   Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  "),\n"
+  "\n"
+  ");",
+  Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  ") &&\n"
+  "\n"
+  ");",
+  Style);
 }
 
 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
@@ -14354,6 +14408,13 @@
 "\"long\",\n"
 "a);",
 format("someFunction(\"long long long long\", a);", Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("someFunction(\n"
+   "\"long long long \"\n"
+   "\"long\",\n"
+   "a\n"
+   ");",
+   Style);
 }
 
 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
@@ -16075,6 +16136,23 @@
"FoLong);\n"
"}",
Spaces);
+
+  Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("void foo( ) {\n"
+   "size_t foo = (*(function))(\n"
+   "F, Bar, F, Ba, FoLong, "
+   "BaLong,\n"
+   "FoLong\n"
+   ");\n"
+   "}",
+   Spaces);
+  verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
+  verifyFormat("size_t idx = (size_t)a;", Spaces);
+  verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx 

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-17 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/lib/Format/TokenAnnotator.cpp:5007
   return false;
 }
 const FormatToken *Previous = Right.MatchingParen->Previous;

owenpan wrote:
> Can we simply add the above and leave the original `return` statement on line 
> 5009 unchanged?
Yes this seems to work. I'll push a diff shortly along with reverting the 
EXPECT_EQ outside of my new tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

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


[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:7215
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
-  "paramC);\n"
-  "void functionDecl(int A, int B,\n"
-  "int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(paramA, paramB,\n"
+   "paramC);\n"

HazardyKnusperkeks wrote:
> Sorry for the confusion, you can (and should?) keep the `Input` variable. 
> There is a verifyFormat with 4 arguments. So that the input in the formatting 
> doesn't have to be the expected.
Ok, I restored it in this function. I didn't add it to the other functions 
since they weren't using it already.  The vast majority of the calls to 
verifyFormat are using the 3-argument version. It's unclear what is preferred.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

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


[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 475313.
gedare added a comment.

- FormatTest: restore Input variable to one test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7214,49 +7214,57 @@
 "void functionDecl(int A, int B, int C);";
   Style.AllowAllArgumentsOnNextLine = false;
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
-  "paramC);\n"
-  "void functionDecl(int A, int B,\n"
-  "int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(paramA, paramB,\n"
+   "paramC);\n"
+   "void functionDecl(int A, int B,\n"
+   "int C);",
+   Input, Style);
   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
-  " paramC);\n"
-  "void functionDecl(int A, int B,\n"
-  "  int C);"),
-format(Input, Style));
-  // However, BAS_AlwaysBreak should take precedence over
+  verifyFormat("functionCall(paramA, paramB,\n"
+   " paramC);\n"
+   "void functionDecl(int A, int B,\n"
+   "  int C);",
+   Input, Style);
+  // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
   // AllowAllArgumentsOnNextLine.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
-  EXPECT_EQ(StringRef("functionCall(\n"
-  "paramA, paramB, paramC);\n"
-  "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC);\n"
+   "void functionDecl(\n"
+   "int A, int B, int C);",
+   Input, Style);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC\n"
+   ");\n"
+   "void functionDecl(\n"
+   "int A, int B, int C\n"
+   ");",
+   Input, Style);
 
   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
   // first argument.
   Style.AllowAllArgumentsOnNextLine = true;
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
-  EXPECT_EQ(StringRef("functionCall(\n"
-  "paramA, paramB, paramC);\n"
-  "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC);\n"
+   "void functionDecl(\n"
+   "int A, int B, int C);",
+   Input, Style);
   // It wouldn't fit on one line with aligned parameters so this setting
   // doesn't change anything for BAS_Align.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
-  " paramC);\n"
-  "void functionDecl(int A, int B,\n"
-  "  int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(paramA, paramB,\n"
+   " paramC);\n"
+   "void functionDecl(int A, int B,\n"
+   "  int C);",
+   Input, Style);
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
-  EXPECT_EQ(StringRef("functionCall(\n"
-  "paramA, paramB, paramC);\n"
-  "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC);\n"
+   "void functionDecl(\n"
+   "int A, int B, int C);",
+   Input, Style);
 }
 
 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
@@ -8435,6 +8443,52 @@
   "(a, )) &&\n"
   ");",
   Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  Style.BinPackArguments = false;
+  Style.BinPackParameters = false;
+  verifyFormat("void aa(\n"
+   "aaa ,\n"
+   "a aaa,\n"
+ 

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 475165.
gedare added a comment.

- fixup: unit test formatting


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7210,53 +7210,59 @@
   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
   // and BAS_Align.
   FormatStyle Style = getLLVMStyleWithColumns(35);
-  StringRef Input = "functionCall(paramA, paramB, paramC);\n"
-"void functionDecl(int A, int B, int C);";
   Style.AllowAllArgumentsOnNextLine = false;
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
-  "paramC);\n"
-  "void functionDecl(int A, int B,\n"
-  "int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(paramA, paramB,\n"
+   "paramC);\n"
+   "void functionDecl(int A, int B,\n"
+   "int C);",
+   Style);
   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
-  " paramC);\n"
-  "void functionDecl(int A, int B,\n"
-  "  int C);"),
-format(Input, Style));
-  // However, BAS_AlwaysBreak should take precedence over
+  verifyFormat("functionCall(paramA, paramB,\n"
+   " paramC);\n"
+   "void functionDecl(int A, int B,\n"
+   "  int C);",
+   Style);
+  // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
   // AllowAllArgumentsOnNextLine.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
-  EXPECT_EQ(StringRef("functionCall(\n"
-  "paramA, paramB, paramC);\n"
-  "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC);\n"
+   "void functionDecl(\n"
+   "int A, int B, int C);",
+   Style);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC\n"
+   ");\n"
+   "void functionDecl(\n"
+   "int A, int B, int C\n"
+   ");",
+   Style);
 
   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
   // first argument.
   Style.AllowAllArgumentsOnNextLine = true;
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
-  EXPECT_EQ(StringRef("functionCall(\n"
-  "paramA, paramB, paramC);\n"
-  "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC);\n"
+   "void functionDecl(\n"
+   "int A, int B, int C);",
+   Style);
   // It wouldn't fit on one line with aligned parameters so this setting
   // doesn't change anything for BAS_Align.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
-  " paramC);\n"
-  "void functionDecl(int A, int B,\n"
-  "  int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(paramA, paramB,\n"
+   " paramC);\n"
+   "void functionDecl(int A, int B,\n"
+   "  int C);",
+   Style);
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
-  EXPECT_EQ(StringRef("functionCall(\n"
-  "paramA, paramB, paramC);\n"
-  "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  verifyFormat("functionCall(\n"
+   "paramA, paramB, paramC);\n"
+   "void functionDecl(\n"
+   "int A, int B, int C);",
+   Style);
 }
 
 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
@@ -8435,6 +8441,52 @@
   "(a, )) &&\n"
   ");",
   Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  Style.BinPackArguments = false;
+  Style.BinPackParameters = false;
+  verifyFormat("void a

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-14 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 475164.
gedare added a comment.

- fixup: replace EXPECT_EQ with verifyFormat
- fixup: port other EXPECT_EQ to verifyFormat


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7210,53 +7210,59 @@
   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
   // and BAS_Align.
   FormatStyle Style = getLLVMStyleWithColumns(35);
-  StringRef Input = "functionCall(paramA, paramB, paramC);\n"
-"void functionDecl(int A, int B, int C);";
   Style.AllowAllArgumentsOnNextLine = false;
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
+  verifyFormat("functionCall(paramA, paramB,\n"
   "paramC);\n"
   "void functionDecl(int A, int B,\n"
-  "int C);"),
-format(Input, Style));
+  "int C);",
+   Style);
   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
+  verifyFormat("functionCall(paramA, paramB,\n"
   " paramC);\n"
   "void functionDecl(int A, int B,\n"
-  "  int C);"),
-format(Input, Style));
-  // However, BAS_AlwaysBreak should take precedence over
+  "  int C);",
+   Style);
+  // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
   // AllowAllArgumentsOnNextLine.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
-  EXPECT_EQ(StringRef("functionCall(\n"
+  verifyFormat("functionCall(\n"
   "paramA, paramB, paramC);\n"
   "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  "int A, int B, int C);",
+   Style);
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("functionCall(\n"
+  "paramA, paramB, paramC\n"
+  ");\n"
+  "void functionDecl(\n"
+  "int A, int B, int C\n"
+  ");",
+   Style);
 
   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
   // first argument.
   Style.AllowAllArgumentsOnNextLine = true;
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
-  EXPECT_EQ(StringRef("functionCall(\n"
+  verifyFormat("functionCall(\n"
   "paramA, paramB, paramC);\n"
   "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  "int A, int B, int C);",
+   Style);
   // It wouldn't fit on one line with aligned parameters so this setting
   // doesn't change anything for BAS_Align.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
-  EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
+  verifyFormat("functionCall(paramA, paramB,\n"
   " paramC);\n"
   "void functionDecl(int A, int B,\n"
-  "  int C);"),
-format(Input, Style));
+  "  int C);",
+   Style);
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
-  EXPECT_EQ(StringRef("functionCall(\n"
+  verifyFormat("functionCall(\n"
   "paramA, paramB, paramC);\n"
   "void functionDecl(\n"
-  "int A, int B, int C);"),
-format(Input, Style));
+  "int A, int B, int C);",
+   Style);
 }
 
 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
@@ -8435,6 +8441,54 @@
   "(a, )) &&\n"
   ");",
   Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  Style.BinPackArguments = false;
+  Style.BinPackParameters = false;
+  verifyFormat("void aa(\n"
+   "aaa ,\n"
+   "a aaa,\n"
+   "a\n"
+   ") {}",
+   Style);
+  verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
+   "aaa a,\n"
+   "aaa aa

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-11 Thread Gedare Bloom via Phabricator via cfe-commits
gedare marked an inline comment as done.
gedare added a comment.

I've added unit tests and confirmed (1) the new tests fail on current `main` 
and (2) all unit tests pass with this revision applied.

  [100%] Running lit suite /llvm-project/clang/test/Unit
  
  Testing Time: 250.49s
Skipped:31
Passed : 15931
  [100%] Built target check-clang-unit

All pending comments have been addressed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

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


[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-11 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 474901.
gedare added a comment.

- fixup typo in unit test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7225,7 +7225,7 @@
   "void functionDecl(int A, int B,\n"
   "  int C);"),
 format(Input, Style));
-  // However, BAS_AlwaysBreak should take precedence over
+  // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
   // AllowAllArgumentsOnNextLine.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
   EXPECT_EQ(StringRef("functionCall(\n"
@@ -7233,6 +7233,14 @@
   "void functionDecl(\n"
   "int A, int B, int C);"),
 format(Input, Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  EXPECT_EQ(StringRef("functionCall(\n"
+  "paramA, paramB, paramC\n"
+  ");\n"
+  "void functionDecl(\n"
+  "int A, int B, int C\n"
+  ");"),
+format(Input, Style));
 
   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
   // first argument.
@@ -8435,6 +8443,54 @@
   "(a, )) &&\n"
   ");",
   Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  Style.BinPackArguments = false;
+  Style.BinPackParameters = false;
+  verifyFormat("void aa(\n"
+   "aaa ,\n"
+   "a aaa,\n"
+   "a\n"
+   ") {}",
+   Style);
+  verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
+   "aaa a,\n"
+   "aaa a,\n"
+   "a\n"
+   ");",
+   Style);
+  verifyFormat("SomeLongVariableName->someFunction(f(\n"
+   "aaa,\n"
+   "a,\n"
+   "a\n"
+   "));",
+   Style);
+  verifyFormat(
+  "(a(\n"
+  "(a, )\n"
+  "));",
+  Style);
+  verifyFormat(
+  "(aa.aa(\n"
+  "(a, )\n"
+  "));",
+  Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  "),\n"
+  "\n"
+  ");",
+  Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  ") &&\n"
+  "\n"
+  ");",
+  Style);
 }
 
 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
@@ -14354,6 +14410,13 @@
 "\"long\",\n"
 "a);",
 format("someFunction(\"long long long long\", a);", Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  EXPECT_EQ("someFunction(\n"
+"\"long long long \"\n"
+"\"long\",\n"
+"a\n"
+");",
+format("someFunction(\"long long long long\", a);", Style));
 }
 
 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
@@ -16075,6 +16138,23 @@
"FoLong);\n"
"}",
Spaces);
+
+  Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("void foo( ) {\n"
+   "size_t foo = (*(function))(\n"
+   "F, Bar, F, Ba, FoLong, "
+   "BaLong,\n"
+   "FoLong\n"
+   ");\n"
+   "}",
+   Spaces);
+  verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
+  verifyFormat("size_t idx = (size_t)a;", Spaces);
+  verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
+  v

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-11 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 474881.
gedare added a comment.

Refactor horrible to read logic


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7225,7 +7225,7 @@
   "void functionDecl(int A, int B,\n"
   "  int C);"),
 format(Input, Style));
-  // However, BAS_AlwaysBreak should take precedence over
+  // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
   // AllowAllArgumentsOnNextLine.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
   EXPECT_EQ(StringRef("functionCall(\n"
@@ -7233,6 +7233,14 @@
   "void functionDecl(\n"
   "int A, int B, int C);"),
 format(Input, Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  EXPECT_EQ(StringRef("functionCall(\n"
+  "paramA, paramB, paramC\n"
+  ");\n"
+  "void functionDecl(\n"
+  "int A, int B, int C\n"
+  ");"),
+format(Input, Style));
 
   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
   // first argument.
@@ -8435,6 +8443,54 @@
   "(a, )) &&\n"
   ");",
   Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+  Style.BinPackArguments = false;
+  Style.BinPackParameters = false;
+  verifyFormat("void aa(\n"
+   "aaa ,\n"
+   "a aaa,\n"
+   "a\n"
+   ") {}",
+   Style);
+  verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
+   "aaa a,\n"
+   "aaa a,\n"
+   "a\n"
+   ");",
+   Style);
+  verifyFormat("SomeLongVariableName->someFunction(f(\n"
+   "aaa,\n"
+   "a,\n"
+   "a\n"
+   "));",
+   Style);
+  verifyFormat(
+  "(a(\n"
+  "(a, )\n"
+  "));",
+  Style);
+  verifyFormat(
+  "(aa.aa(\n"
+  "(a, )\n"
+  "));",
+  Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  "),\n"
+  "\n"
+  ");",
+  Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  ") &&\n"
+  "\n"
+  ");",
+  Style);
 }
 
 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
@@ -14354,6 +14410,13 @@
 "\"long\",\n"
 "a);",
 format("someFunction(\"long long long long\", a);", Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  EXPECT_EQ("someFunction(\n"
+"\"long long long \"\n"
+"\"long\",\n"
+"a\n"
+");",
+format("someFunction(\"long long long long\", a);", Style));
 }
 
 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
@@ -16075,6 +16138,23 @@
"FoLong);\n"
"}",
Spaces);
+
+  Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("void foo( ) {\n"
+   "size_t foo = (*(function))(\n"
+   "F, Bar, F, Ba, FoLong, "
+   "BaLong,\n"
+   "FoLong\n"
+   ");\n"
+   "}",
+   Spaces);
+  verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
+  verifyFormat("size_t idx = (size_t)a;", Spaces);
+  verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (*foo)(a - 1);", Spaces)

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-11 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 474876.
gedare added a comment.

add unit tests for BlockIndent


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7225,7 +7225,7 @@
   "void functionDecl(int A, int B,\n"
   "  int C);"),
 format(Input, Style));
-  // However, BAS_AlwaysBreak should take precedence over
+  // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
   // AllowAllArgumentsOnNextLine.
   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
   EXPECT_EQ(StringRef("functionCall(\n"
@@ -7233,6 +7233,14 @@
   "void functionDecl(\n"
   "int A, int B, int C);"),
 format(Input, Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  EXPECT_EQ(StringRef("functionCall(\n"
+  "paramA, paramB, paramC\n"
+  ");\n"
+  "void functionDecl(\n"
+  "int A, int B, int C\n"
+  ");"),
+format(Input, Style));
 
   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
   // first argument.
@@ -8435,6 +8443,54 @@
   "(a, )) &&\n"
   ");",
   Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+  Style.BinPackArguments = false;
+  Style.BinPackParameters = false;
+  verifyFormat("void aa(\n"
+   "aaa ,\n"
+   "a aaa,\n"
+   "a\n"
+   ") {}",
+   Style);
+  verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
+   "aaa a,\n"
+   "aaa a,\n"
+   "a\n"
+   ");",
+   Style);
+  verifyFormat("SomeLongVariableName->someFunction(f(\n"
+   "aaa,\n"
+   "a,\n"
+   "a\n"
+   "));",
+   Style);
+  verifyFormat(
+  "(a(\n"
+  "(a, )\n"
+  "));",
+  Style);
+  verifyFormat(
+  "(aa.aa(\n"
+  "(a, )\n"
+  "));",
+  Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  "),\n"
+  "\n"
+  ");",
+  Style);
+  verifyFormat(
+  "(\n"
+  "a(\n"
+  "(a, )\n"
+  ") &&\n"
+  "\n"
+  ");",
+  Style);
 }
 
 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
@@ -14354,6 +14410,13 @@
 "\"long\",\n"
 "a);",
 format("someFunction(\"long long long long\", a);", Style));
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  EXPECT_EQ("someFunction(\n"
+"\"long long long \"\n"
+"\"long\",\n"
+"a\n"
+");",
+format("someFunction(\"long long long long\", a);", Style));
 }
 
 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
@@ -16075,6 +16138,23 @@
"FoLong);\n"
"}",
Spaces);
+
+  Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+  verifyFormat("void foo( ) {\n"
+   "size_t foo = (*(function))(\n"
+   "F, Bar, F, Ba, FoLong, "
+   "BaLong,\n"
+   "FoLong\n"
+   ");\n"
+   "}",
+   Spaces);
+  verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
+  verifyFormat("size_t idx = (size_t)a;", Spaces);
+  verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
+  verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);

[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-11 Thread Gedare Bloom via Phabricator via cfe-commits
gedare marked 2 inline comments as done.
gedare added inline comments.



Comment at: clang/lib/Format/TokenAnnotator.cpp:5006-5010
+return !((Previous && (Previous->is(tok::kw_for) || Previous->isIf())) ||
+ (Right.Next &&
+  (Right.Next->is(tok::l_paren) ||
+   (Right.Next->is(tok::r_paren) &&
+(Right.NestingLevel == 0 || Previous->is(tok::star));

HazardyKnusperkeks wrote:
> This is horrible to read.
> 
> Could you split this into multiple statements? Maybe with lambdas, I don't 
> know.
> 
> But I have no intention to ever understand that condition.
Sure. I was just following the prevailing style in this code base. I'll 
refactor.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

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


[PATCH] D137762: [clang-format] avoid breaking )( with BlockIndent

2022-11-11 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 474814.
gedare added a comment.

Address review comments


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137762

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/TokenAnnotator.cpp


Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -5001,7 +5001,13 @@
   return false;
 }
 const FormatToken *Previous = Right.MatchingParen->Previous;
-return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
+// Avoid breaking when there is another parens immediately following
+// a closing parens, such as in cast operators and indirect function calls.
+return !((Previous && (Previous->is(tok::kw_for) || Previous->isIf())) ||
+ (Right.Next &&
+  (Right.Next->is(tok::l_paren) ||
+   (Right.Next->is(tok::r_paren) &&
+(Right.NestingLevel == 0 || Previous->is(tok::star));
   }
 
   // Allow breaking after a trailing annotation, e.g. after a method
Index: clang/lib/Format/ContinuationIndenter.cpp
===
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -716,8 +716,9 @@
(Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
-  (!Previous.Previous || !Previous.Previous->isOneOf(
- tok::kw_for, tok::kw_while, tok::kw_switch)) 
&&
+  (!Previous.Previous ||
+   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+   tok::kw_switch)) &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //


Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -5001,7 +5001,13 @@
   return false;
 }
 const FormatToken *Previous = Right.MatchingParen->Previous;
-return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
+// Avoid breaking when there is another parens immediately following
+// a closing parens, such as in cast operators and indirect function calls.
+return !((Previous && (Previous->is(tok::kw_for) || Previous->isIf())) ||
+ (Right.Next &&
+  (Right.Next->is(tok::l_paren) ||
+   (Right.Next->is(tok::r_paren) &&
+(Right.NestingLevel == 0 || Previous->is(tok::star));
   }
 
   // Allow breaking after a trailing annotation, e.g. after a method
Index: clang/lib/Format/ContinuationIndenter.cpp
===
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -716,8 +716,9 @@
(Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
-  (!Previous.Previous || !Previous.Previous->isOneOf(
- tok::kw_for, tok::kw_while, tok::kw_switch)) &&
+  (!Previous.Previous ||
+   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+   tok::kw_switch)) &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   >