[PATCH] D152443: Add SpaceAfterOperatorKeyword style option for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex created this revision.
KitsuneAlex added reviewers: lattner, craig.topper, RKSimon, respindola.
KitsuneAlex added a project: clang-format.
Herald added projects: All, clang.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
KitsuneAlex requested review of this revision.

First of all, i apologize in advance if anything in the submission proccess was 
messed up, this is my first time contributon to the LLVM project.

This patch adds a new option 'SpaceAfterOperatorKeyword' to clang-format. 
As the name implies, this will insert a space after every operator keyword, 
effectively splitting the actual operator from the keyword, which i found is a 
missing option for my private and professional work.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152443

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
@@ -22907,6 +22907,12 @@
   verifyFormat("template void foo();", Style);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SpaceAfterOperatorKeyword = false;
+  verifyFormat("bool operator==(void*);", Style);
+}
+
 TEST_F(FormatTest, TripleAngleBrackets) {
   verifyFormat("f<<<1, 1>>>();");
   verifyFormat("f<<<1, 1, 1, s>>>();");
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -184,6 +184,7 @@
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3781,6 +3781,8 @@
   }
   if (Right.is(tok::less) && Left.is(tok::kw_template))
 return Style.SpaceAfterTemplateKeyword;
+  if (Left.is(tok::kw_operator))
+return Style.SpaceAfterOperatorKeyword;
   if (Left.isOneOf(tok::exclaim, tok::tilde))
 return false;
   if (Left.is(tok::at) &&
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1002,6 +1002,8 @@
 IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
 IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
+IO.mapOptional("SpaceAfterOperatorKeyword",
+   Style.SpaceAfterOperatorKeyword);
 IO.mapOptional("SpaceAroundPointerQualifiers",
Style.SpaceAroundPointerQualifiers);
 IO.mapOptional("SpaceBeforeAssignmentOperators",
@@ -1440,6 +1442,7 @@
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
   LLVMStyle.SpaceBeforeCtorInitializerColon = true;
@@ -1731,6 +1734,7 @@
   MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
   MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
   MozillaStyle.SpaceAfterTemplateKeyword = false;
+  MozillaStyle.SpaceAfterOperatorKeyword = false;
   return MozillaStyle;
 }
 
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3700,6 +3700,14 @@
   /// \version 4
   bool SpaceAfterTemplateKeyword;
 
+  /// If \c true, a space will be inserted after the 'operator' keyword.
+  /// \code
+  ///true:  false:
+  ///bool operator ==(...); vs. bool operator==(...);
+  /// \endcode
+  /// \version 17
+  bool SpaceAfterOperatorKeyword;
+
   /// Different ways to put a space before opening parentheses.
   enum SpaceAroundPointerQualifiersStyle : int8_t {
 /// Don't ensure spaces around pointer qualifiers and use PointerAlignment
@@ -4412,6 +4420,7 @@
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
+   SpaceAfterOperat

[PATCH] D152443: Add SpaceAfterOperatorKeyword style option for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex added a comment.

I made a stupid mistake and messed up the formatting of the anonymous parameter 
in the unit test code.
Fixing this right now, don't know why it slipped through.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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


[PATCH] D152443: Add SpaceAfterOperatorKeyword style option for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 529587.
KitsuneAlex added a comment.

I removed the redundant anonymous parameter in the unit test source string. 
Locally all formatting tests passed fine.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22907,6 +22907,12 @@
   verifyFormat("template void foo();", Style);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SpaceAfterOperatorKeyword = false;
+  verifyFormat("bool operator==();", Style);
+}
+
 TEST_F(FormatTest, TripleAngleBrackets) {
   verifyFormat("f<<<1, 1>>>();");
   verifyFormat("f<<<1, 1, 1, s>>>();");
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -184,6 +184,7 @@
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3781,6 +3781,8 @@
   }
   if (Right.is(tok::less) && Left.is(tok::kw_template))
 return Style.SpaceAfterTemplateKeyword;
+  if (Left.is(tok::kw_operator))
+return Style.SpaceAfterOperatorKeyword;
   if (Left.isOneOf(tok::exclaim, tok::tilde))
 return false;
   if (Left.is(tok::at) &&
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1002,6 +1002,8 @@
 IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
 IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
+IO.mapOptional("SpaceAfterOperatorKeyword",
+   Style.SpaceAfterOperatorKeyword);
 IO.mapOptional("SpaceAroundPointerQualifiers",
Style.SpaceAroundPointerQualifiers);
 IO.mapOptional("SpaceBeforeAssignmentOperators",
@@ -1440,6 +1442,7 @@
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
   LLVMStyle.SpaceBeforeCtorInitializerColon = true;
@@ -1731,6 +1734,7 @@
   MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
   MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
   MozillaStyle.SpaceAfterTemplateKeyword = false;
+  MozillaStyle.SpaceAfterOperatorKeyword = false;
   return MozillaStyle;
 }
 
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3700,6 +3700,14 @@
   /// \version 4
   bool SpaceAfterTemplateKeyword;
 
+  /// If \c true, a space will be inserted after the 'operator' keyword.
+  /// \code
+  ///true:  false:
+  ///bool operator ==(...); vs. bool operator==(...);
+  /// \endcode
+  /// \version 17
+  bool SpaceAfterOperatorKeyword;
+
   /// Different ways to put a space before opening parentheses.
   enum SpaceAroundPointerQualifiersStyle : int8_t {
 /// Don't ensure spaces around pointer qualifiers and use PointerAlignment
@@ -4412,6 +4420,7 @@
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
+   SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword &&
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
SpaceBeforeCaseColon == R.SpaceBeforeCaseColon &&
SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList &&
Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4714,6 +4714,16 @@
  true:   

[PATCH] D152443: Add SpaceAfterOperatorKeyword style option for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 529635.
KitsuneAlex added a comment.

I took the liberty and extended the unit tests for SpaceAfterTemplateKeyword 
and SpaceAfterOperator keyword to cover both possible cases, which lead to me 
discovering that my code was not quite right because of an early-out return 
path in a calling function.

Everything is fixed and tested now.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22905,6 +22905,16 @@
   FormatStyle Style = getLLVMStyle();
   Style.SpaceAfterTemplateKeyword = false;
   verifyFormat("template void foo();", Style);
+  Style.SpaceAfterTemplateKeyword = true;
+  verifyFormat("template  void foo();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SpaceAfterOperatorKeyword = false;
+  verifyFormat("bool operator==();", Style);
+  Style.SpaceAfterOperatorKeyword = true;
+  verifyFormat("bool operator ==();", Style);
 }
 
 TEST_F(FormatTest, TripleAngleBrackets) {
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -184,6 +184,7 @@
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3759,6 +3759,7 @@
   Left.Previous->is(tok::kw_operator)) {
 return false;
   }
+
   // co_await (x), co_yield (x), co_return (x)
   if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
   !Right.isOneOf(tok::semi, tok::r_paren)) {
@@ -4202,7 +4203,7 @@
   return true;
 
 if (Left.is(tok::kw_operator))
-  return Right.is(tok::coloncolon);
+  return Style.SpaceAfterOperatorKeyword || Right.is(tok::coloncolon);
 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
   return true;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1002,6 +1002,8 @@
 IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
 IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
+IO.mapOptional("SpaceAfterOperatorKeyword",
+   Style.SpaceAfterOperatorKeyword);
 IO.mapOptional("SpaceAroundPointerQualifiers",
Style.SpaceAroundPointerQualifiers);
 IO.mapOptional("SpaceBeforeAssignmentOperators",
@@ -1440,6 +1442,7 @@
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
   LLVMStyle.SpaceBeforeCtorInitializerColon = true;
@@ -1731,6 +1734,7 @@
   MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
   MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
   MozillaStyle.SpaceAfterTemplateKeyword = false;
+  MozillaStyle.SpaceAfterOperatorKeyword = false;
   return MozillaStyle;
 }
 
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3700,6 +3700,14 @@
   /// \version 4
   bool SpaceAfterTemplateKeyword;
 
+  /// If \c true, a space will be inserted after the 'operator' keyword.
+  /// \code
+  ///true:  false:
+  ///bool operator ==(...); vs. bool operator==(...);
+  /// \endcode
+  /// \version 17
+  bool SpaceAfterOperatorKeyword;
+
   /// Different ways to put a space before opening parentheses.
   enum SpaceAroundPointerQualifiersStyle : int8_t {
 /// Don't ensure spaces around pointer qualifiers and use PointerAlignment
@@ -4412,6 +4420,7 @@
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
 

[PATCH] D152443: Add SpaceAfterOperatorKeyword style option for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex added a comment.

In D152443#4406923 , 
@HazardyKnusperkeks wrote:

> Can you add a note to the release notes also please?

Of course! Doing that right now :)

Another thing i was wondering was if it would make sense to add a separate 
option for deciding whether to insert a space between the operator keyword
specifically in the case of a call expression.

Meaning operator overload definitions would use **SpaceAfterOperatorKeyword** 
while operator call expressions would use
something like **SpaceAfterOperatorCall** maybe. In the case this sounds like a 
good idea to you, i'd also suggest to have **SpaceAfterOperatorCall**
use **SpaceAfterOperatorKeyword** as its default value.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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


[PATCH] D152443: Add SpaceAfterOperatorKeyword style option for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 529788.
KitsuneAlex added a comment.

Applied all suggested changes and added a suiting option for aformentioned 
edge-case for call expressions.
Also added the missing release notes to the apropriate section.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22901,8 +22901,27 @@
Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+
+  Style.SpaceAfterOperatorKeyword = true;
+  verifyFormat("bool operator ==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorKeywordInCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  
+  Style.SpaceAfterOperatorKeywordInCall = true;
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+}
+
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
   FormatStyle Style = getLLVMStyle();
+  verifyFormat("template  void foo();", Style);
   Style.SpaceAfterTemplateKeyword = false;
   verifyFormat("template void foo();", Style);
 }
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -183,6 +183,8 @@
   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeywordInCall);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3759,6 +3759,7 @@
   Left.Previous->is(tok::kw_operator)) {
 return false;
   }
+
   // co_await (x), co_yield (x), co_return (x)
   if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
   !Right.isOneOf(tok::semi, tok::r_paren)) {
@@ -4201,8 +4202,12 @@
 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
   return true;
 
-if (Left.is(tok::kw_operator))
-  return Right.is(tok::coloncolon);
+if (Left.is(tok::kw_operator)) {
+  if (Left.Previous && Left.Previous->isOneOf(tok::coloncolon, tok::period))
+return Style.SpaceAfterOperatorKeywordInCall || Right.is(tok::coloncolon);
+  return Style.SpaceAfterOperatorKeyword || Right.is(tok::coloncolon);
+}
+
 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
   return true;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1000,6 +1000,10 @@
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
 IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
+IO.mapOptional("SpaceAfterOperatorKeyword",
+   Style.SpaceAfterOperatorKeyword);
+IO.mapOptional("SpaceAfterOperatorKeywordInCall",
+   Style.SpaceAfterOperatorKeywordInCall);
 IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
 IO.mapOptional("SpaceAroundPointerQualifiers",
@@ -1439,6 +1443,8 @@
   LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
+  LLVMStyle.SpaceAfterOperatorKeywordInCall = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3692,6 +3692,24 @@
   /// \version 9
   bool SpaceAfterLogicalNot;
 
+  /// If \c true, a space will be inserted after the 'operator' keyword.

[PATCH] D152443: Add SpaceAfterOperatorKeyword & SpaceAfterOperatorKeywordInCall style options for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 529798.
KitsuneAlex added a comment.

Some additional changes because i exceeded the maximum line width on multiple 
occasions.


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


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

Files:
  clang/include/clang/Format/Format.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
@@ -22904,7 +22904,6 @@
 TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
   FormatStyle Style = getLLVMStyle();
   verifyFormat("bool operator==();", Style);
-
   Style.SpaceAfterOperatorKeyword = true;
   verifyFormat("bool operator ==();", Style);
 }
@@ -22913,7 +22912,6 @@
   FormatStyle Style = getLLVMStyle();
   verifyFormat("foo.operator==();", Style);
   verifyFormat("foo.Foo::operator==();", Style);
-  
   Style.SpaceAfterOperatorKeywordInCall = true;
   verifyFormat("foo.operator ==();", Style);
   verifyFormat("foo.Foo::operator ==();", Style);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -4201,13 +4201,13 @@
 // Space in __attribute__((attr)) ::type.
 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
   return true;
-
 if (Left.is(tok::kw_operator)) {
-  if (Left.Previous && Left.Previous->isOneOf(tok::coloncolon, 
tok::period))
-return Style.SpaceAfterOperatorKeywordInCall || 
Right.is(tok::coloncolon);
+  if (Left.Previous && 
+  Left.Previous->isOneOf(tok::coloncolon, tok::period))
+return Style.SpaceAfterOperatorKeywordInCall ||
+   Right.is(tok::coloncolon);
   return Style.SpaceAfterOperatorKeyword || Right.is(tok::coloncolon);
 }
-
 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
   return true;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3701,7 +3701,6 @@
   bool SpaceAfterOperatorKeyword;
 
   /// If \c true, a space will be inserted after the 'operator' keyword in a 
call expression.
-  /// This option defaults to SpaceAfterOperatorKeyword.
   /// \code
   ///true:  false:
   ///foo.operator ==(...); vs.  foo.operator==(...);
@@ -4430,7 +4429,8 @@
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword &&
-   SpaceAfterOperatorKeywordInCall == 
R.SpaceAfterOperatorKeywordInCall &&
+   SpaceAfterOperatorKeywordInCall == 
+   R.SpaceAfterOperatorKeywordInCall &&
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators 
&&
SpaceBeforeCaseColon == R.SpaceBeforeCaseColon &&


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -22904,7 +22904,6 @@
 TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
   FormatStyle Style = getLLVMStyle();
   verifyFormat("bool operator==();", Style);
-
   Style.SpaceAfterOperatorKeyword = true;
   verifyFormat("bool operator ==();", Style);
 }
@@ -22913,7 +22912,6 @@
   FormatStyle Style = getLLVMStyle();
   verifyFormat("foo.operator==();", Style);
   verifyFormat("foo.Foo::operator==();", Style);
-  
   Style.SpaceAfterOperatorKeywordInCall = true;
   verifyFormat("foo.operator ==();", Style);
   verifyFormat("foo.Foo::operator ==();", Style);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -4201,13 +4201,13 @@
 // Space in __attribute__((attr)) ::type.
 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
   return true;
-
 if (Left.is(tok::kw_operator)) {
-  if (Left.Previous &&

[PATCH] D152443: Add SpaceAfterOperatorKeyword & SpaceAfterOperatorKeywordInCall style options for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 529799.
KitsuneAlex added a comment.

I had some issues with Git there because i messed up a merge, so the diff was 
bad. Here's the proper diff.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22901,8 +22901,25 @@
Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  Style.SpaceAfterOperatorKeyword = true;
+  verifyFormat("bool operator ==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorKeywordInCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  Style.SpaceAfterOperatorKeywordInCall = true;
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+}
+
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
   FormatStyle Style = getLLVMStyle();
+  verifyFormat("template  void foo();", Style);
   Style.SpaceAfterTemplateKeyword = false;
   verifyFormat("template void foo();", Style);
 }
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -183,6 +183,8 @@
   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeywordInCall);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3759,6 +3759,7 @@
   Left.Previous->is(tok::kw_operator)) {
 return false;
   }
+
   // co_await (x), co_yield (x), co_return (x)
   if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
   !Right.isOneOf(tok::semi, tok::r_paren)) {
@@ -4200,9 +4201,13 @@
 // Space in __attribute__((attr)) ::type.
 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
   return true;
-
-if (Left.is(tok::kw_operator))
-  return Right.is(tok::coloncolon);
+if (Left.is(tok::kw_operator)) {
+  if (Left.Previous && 
+  Left.Previous->isOneOf(tok::coloncolon, tok::period))
+return Style.SpaceAfterOperatorKeywordInCall || 
+   Right.is(tok::coloncolon);
+  return Style.SpaceAfterOperatorKeyword || Right.is(tok::coloncolon);
+}
 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
   return true;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1000,6 +1000,10 @@
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
 IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
+IO.mapOptional("SpaceAfterOperatorKeyword",
+   Style.SpaceAfterOperatorKeyword);
+IO.mapOptional("SpaceAfterOperatorKeywordInCall",
+   Style.SpaceAfterOperatorKeywordInCall);
 IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
 IO.mapOptional("SpaceAroundPointerQualifiers",
@@ -1439,6 +1443,8 @@
   LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
+  LLVMStyle.SpaceAfterOperatorKeywordInCall = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3692,6 +3692,24 @@
   /// \version 9
   bool SpaceAfterLogicalNot;
 
+  /// If \c true, a space will be inserted after the 'operator' keyw

[PATCH] D152443: Add SpaceAfterOperatorKeyword & SpaceAfterOperatorKeywordInCall style options for clang-format

2023-06-08 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 529804.
KitsuneAlex added a comment.

Clean up the formatting with clang-format.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22901,8 +22901,25 @@
Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  Style.SpaceAfterOperatorKeyword = true;
+  verifyFormat("bool operator ==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorKeywordInCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  Style.SpaceAfterOperatorKeywordInCall = true;
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+}
+
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
   FormatStyle Style = getLLVMStyle();
+  verifyFormat("template  void foo();", Style);
   Style.SpaceAfterTemplateKeyword = false;
   verifyFormat("template void foo();", Style);
 }
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -183,6 +183,8 @@
   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorKeywordInCall);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3759,6 +3759,7 @@
   Left.Previous->is(tok::kw_operator)) {
 return false;
   }
+
   // co_await (x), co_yield (x), co_return (x)
   if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
   !Right.isOneOf(tok::semi, tok::r_paren)) {
@@ -4200,9 +4201,14 @@
 // Space in __attribute__((attr)) ::type.
 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
   return true;
-
-if (Left.is(tok::kw_operator))
-  return Right.is(tok::coloncolon);
+if (Left.is(tok::kw_operator)) {
+  if (Left.Previous &&
+  Left.Previous->isOneOf(tok::coloncolon, tok::period)) {
+return Style.SpaceAfterOperatorKeywordInCall ||
+   Right.is(tok::coloncolon);
+  }
+  return Style.SpaceAfterOperatorKeyword || Right.is(tok::coloncolon);
+}
 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
   return true;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1000,6 +1000,10 @@
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
 IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
+IO.mapOptional("SpaceAfterOperatorKeyword",
+   Style.SpaceAfterOperatorKeyword);
+IO.mapOptional("SpaceAfterOperatorKeywordInCall",
+   Style.SpaceAfterOperatorKeywordInCall);
 IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
 IO.mapOptional("SpaceAroundPointerQualifiers",
@@ -1439,6 +1443,8 @@
   LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceAfterLogicalNot = false;
+  LLVMStyle.SpaceAfterOperatorKeyword = false;
+  LLVMStyle.SpaceAfterOperatorKeywordInCall = false;
   LLVMStyle.SpaceAfterTemplateKeyword = true;
   LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
   LLVMStyle.SpaceBeforeCaseColon = false;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3692,6 +3692,23 @@
   /// \version 9
   bool SpaceAfterLogicalNot;
 
+  /// If \c true, a space will be inserted after the 'operator' keyword.
+  /// \code
+  ///true:   

[PATCH] D152443: Add SpaceAfterOperatorKeyword & SpaceAfterOperatorKeywordInCall style options for clang-format

2023-06-09 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex added a comment.

In D152443#4408991 , @MyDeveloperDay 
wrote:

> In D152443#4407438 , @KitsuneAlex 
> wrote:
>
>> I had some issues with Git there because i messed up a merge, so the diff 
>> was bad. Here's the proper diff.
>
>
>
>> NOTE: Clang-Format Team Automated Review Comment
>
> keeps you honest right!

Yes, i was referring to the fact that the previous diff was only 30% of my 
intended changes if you look at the changed files, which was caused by an 
unsmart local merge on my end.
That's why my following diff corrected the formatting.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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


[PATCH] D152443: Add SpaceAfterOperatorKeyword & SpaceAfterOperatorKeywordInCall style options for clang-format

2023-06-11 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 530303.
KitsuneAlex marked 2 inline comments as done.
KitsuneAlex removed reviewers: lattner, craig.topper, RKSimon, respindola, 
rymiel, owenpan.
KitsuneAlex added a comment.
Herald added reviewers: rymiel, owenpan.

This revision of the diff changes the names of the new style options to 
**SpaceAfterOperatorOverload** and **SpaceAfterOperatorCall**, which clarifies 
their scope a lot better i think.
I also rewrote the logic to apply formatting that fits exactly what their name 
implies respectively,
the new tests i added should cover all common use cases (except i forgot some 
again).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22901,8 +22901,43 @@
Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorOverload) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  Style.SpaceAfterOperatorOverload = true;
+  verifyFormat("bool operator ==();", Style);
+  verifyFormat("bool Foo::operator ==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  Style.SpaceAfterOperatorCall = true;
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+  verifyFormat("foo->operator ==();", Style);
+  verifyFormat("foo->Foo::operator ==();", Style);
+}
+
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
   FormatStyle Style = getLLVMStyle();
+  verifyFormat("template  void foo();", Style);
   Style.SpaceAfterTemplateKeyword = false;
   verifyFormat("template void foo();", Style);
 }
Index: clang/unittests/Format/ConfigParseTest.cpp
===
--- clang/unittests/Format/ConfigParseTest.cpp
+++ clang/unittests/Format/ConfigParseTest.cpp
@@ -183,6 +183,8 @@
   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorOverload);
+  CHECK_PARSE_BOOL(SpaceAfterOperatorCall);
   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -4200,9 +4200,21 @@
 // Space in __attribute__((attr)) ::type.
 if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
   return true;
-
-if (Left.is(tok::kw_operator))
-  return Right.is(tok::coloncolon);
+if (Left.is(tok::kw_operator)) {
+  if (Left.hasWhitespaceBefore())
+return Style.SpaceAfterOperatorOverload || Right.is(tok::coloncolon);
+  if (!Left.Previous)
+return Right.is(tok::coloncolon);
+  FormatToken const *Previous = Left.Previous;
+  while (Previous) {
+if (!Previous->hasWhitespaceBefore()) {
+  Previous = Previous->Previous;
+  continue;
+}
+return Style.SpaceAfterOperatorOverload || Right.is(tok::coloncolon);
+  }
+  return Style.SpaceAfterOperatorCall || Right.is(tok::coloncolon);
+}
 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
   return true;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1000,6 +1000,9 @@
 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDecla

[PATCH] D152443: Add operator style options to clang-format

2023-06-11 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 530343.
KitsuneAlex added a comment.

Revamped the formatting logic again and added the requested unit tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22885,6 +22885,120 @@
Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  verifyFormat("operator==();", Style);
+  verifyFormat("Foo::operator==();", Style);
+  verifyFormat("Bar{var, operator==()};", Style);
+  verifyFormat("Bar{var, Foo::operator==()};", Style);
+  verifyFormat("Bar(var, operator==());", Style);
+  verifyFormat("Bar(var, Foo::operator==());", Style);
+  verifyFormat("&operator==();", Style);
+  verifyFormat("&Foo::operator==();", Style);
+  verifyFormat("&foo.operator==();", Style);
+  verifyFormat("&foo.Foo::operator==();", Style);
+  verifyFormat("&foo->operator==();", Style);
+  verifyFormat("&foo->Foo::operator==();", Style);
+  Style.SpaceAfterOperatorCall = true;
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+  verifyFormat("foo->operator ==();", Style);
+  verifyFormat("foo->Foo::operator ==();", Style);
+  verifyFormat("operator ==();", Style);
+  verifyFormat("Foo::operator ==();", Style);
+  verifyFormat("Bar{var, operator ==()};", Style);
+  verifyFormat("Bar{var, Foo::operator ==()};", Style);
+  verifyFormat("Bar(var, operator ==());", Style);
+  verifyFormat("Bar(var, Foo::operator ==());", Style);
+  verifyFormat("&operator ==();", Style);
+  verifyFormat("&Foo::operator ==();", Style);
+  verifyFormat("&foo.operator ==();", Style);
+  verifyFormat("&foo.Foo::operator ==();", Style);
+  verifyFormat("&foo->operator ==();", Style);
+  verifyFormat("&foo->Foo::operator ==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorOverload) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  verifyFormat("operator==();", Style);
+  verifyFormat("Foo::operator==();", Style);
+  verifyFormat("Bar{var, operator==()};", Style);
+  verifyFormat("Bar{var, Foo::operator==()};", Style);
+  verifyFormat("Bar(var, operator==());", Style);
+  verifyFormat("Bar(var, Foo::operator==());", Style);
+  verifyFormat("&operator==();", Style);
+  verifyFormat("&Foo::operator==();", Style);
+  verifyFormat("&foo.operator==();", Style);
+  verifyFormat("&foo.Foo::operator==();", Style);
+  verifyFormat("&foo->operator==();", Style);
+  verifyFormat("&foo->Foo::operator==();", Style);
+  Style.SpaceAfterOperatorOverload = true;
+  verifyFormat("bool operator ==();", Style);
+

[PATCH] D152443: Add operator style options to clang-format

2023-06-11 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 530346.
KitsuneAlex marked 4 inline comments as done.
KitsuneAlex added a comment.

Added missing test case for default operator declarations as requested, as well 
as blank lines between the blocks inside the test functions
as visual separators.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22885,6 +22885,130 @@
Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool operator==() = default;", Style);
+  verifyFormat("bool Foo::operator==() = default;", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  verifyFormat("operator==();", Style);
+  verifyFormat("Foo::operator==();", Style);
+  verifyFormat("Bar{var, operator==()};", Style);
+  verifyFormat("Bar{var, Foo::operator==()};", Style);
+  verifyFormat("Bar(var, operator==());", Style);
+  verifyFormat("Bar(var, Foo::operator==());", Style);
+  verifyFormat("&operator==();", Style);
+  verifyFormat("&Foo::operator==();", Style);
+  verifyFormat("&foo.operator==();", Style);
+  verifyFormat("&foo.Foo::operator==();", Style);
+  verifyFormat("&foo->operator==();", Style);
+  verifyFormat("&foo->Foo::operator==();", Style);
+
+  Style.SpaceAfterOperatorCall = true;
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool operator==() = default;", Style);
+  verifyFormat("bool Foo::operator==() = default;", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+  verifyFormat("foo->operator ==();", Style);
+  verifyFormat("foo->Foo::operator ==();", Style);
+  verifyFormat("operator ==();", Style);
+  verifyFormat("Foo::operator ==();", Style);
+  verifyFormat("Bar{var, operator ==()};", Style);
+  verifyFormat("Bar{var, Foo::operator ==()};", Style);
+  verifyFormat("Bar(var, operator ==());", Style);
+  verifyFormat("Bar(var, Foo::operator ==());", Style);
+  verifyFormat("&operator ==();", Style);
+  verifyFormat("&Foo::operator ==();", Style);
+  verifyFormat("&foo.operator ==();", Style);
+  verifyFormat("&foo.Foo::operator ==();", Style);
+  verifyFormat("&foo->operator ==();", Style);
+  verifyFormat("&foo->Foo::operator ==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorOverload) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool operator==() = default;", Style);
+  verifyFormat("bool Foo::operator==() = default;", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  verifyFormat("operator==();", Style);
+  verifyFormat("Foo::operator==();", Style);
+  verifyFormat("Bar{var, operator==()};", Style);
+  verifyFormat("Bar{var, Foo::operator==()};", St

[PATCH] D152443: Add operator style options to clang-format

2023-06-11 Thread Alexander Hinze via Phabricator via cfe-commits
KitsuneAlex updated this revision to Diff 530348.
KitsuneAlex marked 2 inline comments as done.
KitsuneAlex added a comment.

Fix broken style in TokenAnnotator.cpp. I don't know how i keep missing these, 
trying to get better.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152443

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
@@ -22885,6 +22885,130 @@
Spaces);
 }
 
+TEST_F(FormatTest, SpaceAfterOperatorCall) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool operator==() = default;", Style);
+  verifyFormat("bool Foo::operator==() = default;", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  verifyFormat("operator==();", Style);
+  verifyFormat("Foo::operator==();", Style);
+  verifyFormat("Bar{var, operator==()};", Style);
+  verifyFormat("Bar{var, Foo::operator==()};", Style);
+  verifyFormat("Bar(var, operator==());", Style);
+  verifyFormat("Bar(var, Foo::operator==());", Style);
+  verifyFormat("&operator==();", Style);
+  verifyFormat("&Foo::operator==();", Style);
+  verifyFormat("&foo.operator==();", Style);
+  verifyFormat("&foo.Foo::operator==();", Style);
+  verifyFormat("&foo->operator==();", Style);
+  verifyFormat("&foo->Foo::operator==();", Style);
+
+  Style.SpaceAfterOperatorCall = true;
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool operator==() = default;", Style);
+  verifyFormat("bool Foo::operator==() = default;", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator ==();", Style);
+  verifyFormat("foo.Foo::operator ==();", Style);
+  verifyFormat("foo->operator ==();", Style);
+  verifyFormat("foo->Foo::operator ==();", Style);
+  verifyFormat("operator ==();", Style);
+  verifyFormat("Foo::operator ==();", Style);
+  verifyFormat("Bar{var, operator ==()};", Style);
+  verifyFormat("Bar{var, Foo::operator ==()};", Style);
+  verifyFormat("Bar(var, operator ==());", Style);
+  verifyFormat("Bar(var, Foo::operator ==());", Style);
+  verifyFormat("&operator ==();", Style);
+  verifyFormat("&Foo::operator ==();", Style);
+  verifyFormat("&foo.operator ==();", Style);
+  verifyFormat("&foo.Foo::operator ==();", Style);
+  verifyFormat("&foo->operator ==();", Style);
+  verifyFormat("&foo->Foo::operator ==();", Style);
+}
+
+TEST_F(FormatTest, SpaceAfterOperatorOverload) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("bool operator==();", Style);
+  verifyFormat("bool Foo::operator==();", Style);
+  verifyFormat("bool operator==() = default;", Style);
+  verifyFormat("bool Foo::operator==() = default;", Style);
+  verifyFormat("bool *operator==();", Style);
+  verifyFormat("bool *Foo::operator==();", Style);
+  verifyFormat("bool &operator==();", Style);
+  verifyFormat("bool &Foo::operator==();", Style);
+  verifyFormat("bool &&operator==();", Style);
+  verifyFormat("bool &&Foo::operator==();", Style);
+  verifyFormat("std::vector operator==();", Style);
+  verifyFormat("std::vector Foo::operator==();", Style);
+  verifyFormat("foo.operator==();", Style);
+  verifyFormat("foo.Foo::operator==();", Style);
+  verifyFormat("foo->operator==();", Style);
+  verifyFormat("foo->Foo::operator==();", Style);
+  verifyFormat("operator==();", Style);
+  verifyFormat("Foo::operator==();", Style);
+  verifyFormat("Bar{var, operator==()};", Style);
+  verifyFormat("Bar{var, Foo::operator==()};", Style);
+  verifyFormat("Bar(var, operator==());", Style);
+  v