[PATCH] D102730: [clang-format] Support custom If macros

2021-06-23 Thread Vitali Lovich via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbe9a87fe9bc3: [clang-format] Add IfMacros option (authored 
by vlovich).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -525,6 +525,7 @@
"}");
 
   FormatStyle AllowsMergedIf = getLLVMStyle();
+  AllowsMergedIf.IfMacros.push_back("MYIF");
   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_WithoutElse;
@@ -564,17 +565,62 @@
"  f();\n"
"}",
AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("#define A  \\\n"
+   "  MYIF (a) \\\n"
+   "  label:   \\\n"
+   "f()",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  ;",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;",
+   AllowsMergedIf);
+
+  verifyFormat("MYIF (a) // Can't merge this\n"
+   "  f();\n",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) /* still don't merge */\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { // Never merge this\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { /* Never merge this */\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 14;
+  // Where line-lengths matter, a 2-letter synonym that maintains line length.
+  // Not IF to avoid any confusion that IF is somehow special.
+  AllowsMergedIf.IfMacros.push_back("FI");
   verifyFormat("if (a) return;", AllowsMergedIf);
   verifyFormat("if (a)\n"
"  return;",
AllowsMergedIf);
+  verifyFormat("FI (a) return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n"
+   "  return;",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 13;
   verifyFormat("if (a)\n  return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n  return;", AllowsMergedIf);
 
   FormatStyle AllowsMergedIfElse = getLLVMStyle();
+  AllowsMergedIfElse.IfMacros.push_back("MYIF");
   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_AllIfsAndElse;
   verifyFormat("if (a)\n"
@@ -626,10 +672,60 @@
"  else if constexpr (c) return;\n"
"  else return;",
AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();\n"
+   "else\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIfElse);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "  else\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  ;\n"
+   "else\n"
+   "  ;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else {\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;\n"
+   "  else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF constexpr (a)\n"
+   "  MYIF constexpr (b) return;\n"
+   "  else MYIF constexpr (c) return;\n"
+   "  else return;",
+ 

[PATCH] D102730: [clang-format] Support custom If macros

2021-06-22 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 353823.
vlovich added a comment.

Rebase onto main.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -525,6 +525,7 @@
"}");
 
   FormatStyle AllowsMergedIf = getLLVMStyle();
+  AllowsMergedIf.IfMacros.push_back("MYIF");
   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_WithoutElse;
@@ -564,17 +565,62 @@
"  f();\n"
"}",
AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("#define A  \\\n"
+   "  MYIF (a) \\\n"
+   "  label:   \\\n"
+   "f()",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  ;",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;",
+   AllowsMergedIf);
+
+  verifyFormat("MYIF (a) // Can't merge this\n"
+   "  f();\n",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) /* still don't merge */\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { // Never merge this\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { /* Never merge this */\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 14;
+  // Where line-lengths matter, a 2-letter synonym that maintains line length.
+  // Not IF to avoid any confusion that IF is somehow special.
+  AllowsMergedIf.IfMacros.push_back("FI");
   verifyFormat("if (a) return;", AllowsMergedIf);
   verifyFormat("if (a)\n"
"  return;",
AllowsMergedIf);
+  verifyFormat("FI (a) return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n"
+   "  return;",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 13;
   verifyFormat("if (a)\n  return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n  return;", AllowsMergedIf);
 
   FormatStyle AllowsMergedIfElse = getLLVMStyle();
+  AllowsMergedIfElse.IfMacros.push_back("MYIF");
   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_AllIfsAndElse;
   verifyFormat("if (a)\n"
@@ -626,10 +672,60 @@
"  else if constexpr (c) return;\n"
"  else return;",
AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();\n"
+   "else\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIfElse);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "  else\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  ;\n"
+   "else\n"
+   "  ;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else {\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;\n"
+   "  else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF constexpr (a)\n"
+   "  MYIF constexpr (b) return;\n"
+   "  else MYIF constexpr (c) return;\n"
+   "  else return;",
+   AllowsMergedIfElse);
 }
 
 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
   FormatStyle 

[PATCH] D102730: [clang-format] Support custom If macros

2021-06-18 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 353150.
vlovich marked an inline comment as done.
vlovich added a comment.

Fix clang-format issue


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -525,6 +525,7 @@
"}");
 
   FormatStyle AllowsMergedIf = getLLVMStyle();
+  AllowsMergedIf.IfMacros.push_back("MYIF");
   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_WithoutElse;
@@ -564,17 +565,62 @@
"  f();\n"
"}",
AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("#define A  \\\n"
+   "  MYIF (a) \\\n"
+   "  label:   \\\n"
+   "f()",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  ;",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;",
+   AllowsMergedIf);
+
+  verifyFormat("MYIF (a) // Can't merge this\n"
+   "  f();\n",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) /* still don't merge */\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { // Never merge this\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { /* Never merge this */\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 14;
+  // Where line-lengths matter, a 2-letter synonym that maintains line length.
+  // Not IF to avoid any confusion that IF is somehow special.
+  AllowsMergedIf.IfMacros.push_back("FI");
   verifyFormat("if (a) return;", AllowsMergedIf);
   verifyFormat("if (a)\n"
"  return;",
AllowsMergedIf);
+  verifyFormat("FI (a) return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n"
+   "  return;",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 13;
   verifyFormat("if (a)\n  return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n  return;", AllowsMergedIf);
 
   FormatStyle AllowsMergedIfElse = getLLVMStyle();
+  AllowsMergedIfElse.IfMacros.push_back("MYIF");
   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_AllIfsAndElse;
   verifyFormat("if (a)\n"
@@ -626,10 +672,60 @@
"  else if constexpr (c) return;\n"
"  else return;",
AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();\n"
+   "else\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIfElse);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "  else\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  ;\n"
+   "else\n"
+   "  ;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else {\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;\n"
+   "  else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF constexpr (a)\n"
+   "  MYIF constexpr (b) return;\n"
+   "  else MYIF constexpr (c) return;\n"
+   "  else return;",
+   AllowsMergedIfElse);
 }
 
 TEST_F(FormatTest, 

[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-06-18 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

I think you can go ahead & commit it for me. I don't know when I'll get commit 
access.


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

https://reviews.llvm.org/D102706

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-06-16 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 352573.
vlovich added a comment.

Review response

I've updated coverage for AllowShortIfStatementsOnASingleLine and
BreakBeforeBraces. SpacesInConditionalStatement is what the existing
tests were covering so I don't think there's anything there to update.
I looked & couldn't find any tests for
BraceWrappingAfterControlStatementStyle so my (admittedly lazy)
preference is to skip that for now if possible.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -525,6 +525,7 @@
"}");
 
   FormatStyle AllowsMergedIf = getLLVMStyle();
+  AllowsMergedIf.IfMacros.push_back("MYIF");
   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_WithoutElse;
@@ -564,17 +565,62 @@
"  f();\n"
"}",
AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("#define A  \\\n"
+   "  MYIF (a) \\\n"
+   "  label:   \\\n"
+   "f()",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  ;",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;",
+   AllowsMergedIf);
+
+  verifyFormat("MYIF (a) // Can't merge this\n"
+   "  f();\n",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) /* still don't merge */\n"
+   "  f();",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { // Never merge this\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
+  verifyFormat("MYIF (a) { /* Never merge this */\n"
+   "  f();\n"
+   "}",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 14;
+  // Where line-lengths matter, a 2-letter synonym that maintains line length.
+  // Not IF to avoid any confusion that IF is somehow special.
+  AllowsMergedIf.IfMacros.push_back("FI");
   verifyFormat("if (a) return;", AllowsMergedIf);
   verifyFormat("if (a)\n"
"  return;",
AllowsMergedIf);
+  verifyFormat("FI (a) return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n"
+   "  return;",
+   AllowsMergedIf);
 
   AllowsMergedIf.ColumnLimit = 13;
   verifyFormat("if (a)\n  return;", AllowsMergedIf);
+  verifyFormat("FI (a)\n  return;", AllowsMergedIf);
 
   FormatStyle AllowsMergedIfElse = getLLVMStyle();
+  AllowsMergedIfElse.IfMacros.push_back("MYIF");
   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
   FormatStyle::SIS_AllIfsAndElse;
   verifyFormat("if (a)\n"
@@ -626,10 +672,60 @@
"  else if constexpr (c) return;\n"
"  else return;",
AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  // comment\n"
+   "  f();\n"
+   "else\n"
+   "  // comment\n"
+   "  f();",
+   AllowsMergedIfElse);
+  verifyFormat("{\n"
+   "  MYIF (a)\n"
+   "  label:\n"
+   "f();\n"
+   "  else\n"
+   "  label:\n"
+   "f();\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  ;\n"
+   "else\n"
+   "  ;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else {\n"
+   "}",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) {\n"
+   "} else MYIF (b) return;\n"
+   "else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a) return;\n"
+   "else MYIF (b) {\n"
+   "} else return;",
+   AllowsMergedIfElse);
+  verifyFormat("MYIF (a)\n"
+   "  MYIF (b) return;\n"
+   "  else 

[PATCH] D102730: [clang-format] Support custom If macros

2021-06-16 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

Just back from vacation so will try to polish this off today.


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

https://reviews.llvm.org/D102730

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


[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-06-16 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

I don't know how to commit. Happy to do it if I have the permissions & 
appropriate instructions.

Name: Vitali Lovich
E-mail: vlov...@gmail.com


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

https://reviews.llvm.org/D102706

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


[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-27 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich marked an inline comment as done.
vlovich added a comment.

I think all review comments have been addressed. Please let me know if there's 
anything else blocking merge.


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

https://reviews.llvm.org/D102706

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-05-27 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added inline comments.



Comment at: clang/include/clang/Format/Format.h:2983
 /// \endcode
-SBPO_ControlStatementsExceptForEachMacros,
+SBPO_ControlStatementsExceptControlMacros,
 /// Put a space before opening parentheses only if the parentheses are not

HazardyKnusperkeks wrote:
> Why did you change this?
Per the discussion below.

> MyDeveloperDay
> I'll let you decide if you think we need another SBPO_XXX style?

> Me
> I thought about it but I wasn't was really sure how to add it in a way that 
> would make sense. Do you think people would want to apply consistent SBPO 
> styling for IF & FOREACH macros or want fine-grained control? If the former, 
> then I can just check the foreach macro & maybe rename it to 
> SBPO_ControlStatementsExceptMacros (maintaining the old name for back 
> compat). If the latter, then it would seem like we need a separate boolean 
> that controls whether SBPO_ControlStatements would apply?
> My gut is probably the "maintain consistency" option is fine for now so I've 
> gone ahead & applied that change in the latest diff.

This felt like a simpler solution because otherwise you would either end up 
with SBPO_ControlStatementsForEachMacros, 
SBPO_ControlStatementsExceptIfAndForEachMacros, 
SBPO_ControlStatementsExceptIfMacros which just feels extremely confusing (& 
for now I'm assuming you'll want a similar style for ForEach & If macros). 
Arguably at the point where you want distinct SBPO styling of these 
control-like macros, you would be moved them out into a separate option since 
it's really orthogonal to the other settings.

Open to suggestions of course.


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

https://reviews.llvm.org/D102730

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


[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-21 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 347152.
vlovich added a comment.

Regen documentation & clang-format test cases.


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

https://reviews.llvm.org/D102706

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/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -17871,6 +17871,7 @@
" aaa;\n"
"});",
getLLVMStyleWithColumns(60));
+
   verifyFormat("SomeFunction({[&] {\n"
"// comment\n"
"  },\n"
@@ -18423,6 +18424,117 @@
"  });\n"
"});",
LLVMWithBeforeLambdaBody);
+
+  // Lambdas with different indentation styles.
+  Style = getLLVMStyleWithColumns(100);
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then(\n"
+"  [this, , someObject = "
+"std::mv(s)](std::vector evaluated) mutable {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) "
+"mutable { result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, , someObject = "
+   "std::mv(s)](std::vector evaluated) mutable {\n"
+   "return someObject.startAsyncAction().then([this, "
+   "](AsyncActionResult result) mutable {\n"
+   "  result.processMore();\n"
+   "});\n"
+   "  });\n"
+   "}\n",
+   Style));
+  Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
+  verifyFormat("test() {\n"
+   "  ([]() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  }).foo();\n"
+   "}",
+   Style);
+  verifyFormat("test() {\n"
+   "  []() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  }\n"
+   "}",
+   Style);
+  verifyFormat("std::sort(v.begin(), v.end(),\n"
+   "  [](const auto , const auto "
+   ") {\n"
+   "  return someLongArgumentName.someMemberVariable < "
+   "someOtherLongArgumentName.someMemberVariable;\n"
+   "});",
+   Style);
+  verifyFormat("test() {\n"
+   "  (\n"
+   "  []() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  },\n"
+   "  foo, bar)\n"
+   "  .foo();\n"
+   "}",
+   Style);
+  verifyFormat("test() {\n"
+   "  ([]() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  })\n"
+   "  .foo()\n"
+   "  .bar();\n"
+   "}",
+   Style);
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then(\n"
+"  [this, , someObject = "
+"std::mv(s)](std::vector evaluated) mutable {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { "
+"result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, , someObject = "
+   "std::mv(s)](std::vector evaluated) mutable {\n"
+   "return someObject.startAsyncAction().then([this, "
+   "](AsyncActionResult result) mutable {\n"
+   "  result.processMore();\n"
+   "});\n"
+   "  });\n"
+   "}\n",
+   Style));
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then([this, ] {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { "
+"result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, ] {\n"
+   "return someObject.startAsyncAction().then([this, "
+   "](AsyncActionResult result) 

[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-21 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

In D102706#2774010 , @MyDeveloperDay 
wrote:

> Is ```Signature``` effectively as is?

Yes. I noted that it's the default behavior. Please let me know if I can 
clarify the documentation wording somehow (or if you have preferred wording you 
want me to use)


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

https://reviews.llvm.org/D102706

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-05-21 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added inline comments.



Comment at: clang/docs/ReleaseNotes.rst:252
+- Option ``IfMacros`` has been added. This lets you define macros that get
+  formatted like conditionals much like ``ForEachMacros`` get stiled like
+  foreach loops.

MyDeveloperDay wrote:
> stiled? did you mean styled?
Whoops. Yes I did.



Comment at: clang/lib/Format/FormatTokenLexer.cpp:1019
   FormatTok->setType(it->second);
+  if (it->second == TT_IfMacro) {
+FormatTok->Tok.setKind(tok::kw_if);

MyDeveloperDay wrote:
> Is there any chance you could leave a comment here as to why this is needed, 
> I can't work it out?
Will do my best.  TLDR: The token is otherwise "unknown" & the conditional 
formatting logic depends on the lexer token value. This avoids having to 
rewrite all the existing code & seemed to make sense since we want this token 
treated like an `if`.

I don't of course know if this is The Best Way (tm). It's just the minimal 
change I figured out would get the formatting to work properly but happy to 
apply feedback from someone more intimately familiar with the internals of the 
formatter as I'm a complete novice here.



Comment at: clang/lib/Format/TokenAnnotator.cpp:3025
   return false;
+if (Left.is(TT_IfMacro)) {
+  return false;

MyDeveloperDay wrote:
> I'll let you decide if you think we need another SBPO_XXX style?
I thought about it but I wasn't was really sure how to add it in a way that 
would make sense. Do you think people would want to apply consistent SBPO 
styling for IF & FOREACH macros or want fine-grained control? If the former, 
then I can just check the foreach macro & maybe rename it to 
`SBPO_ControlStatementsExceptMacros` (maintaining the old name for back 
compat). If the latter, then it would seem like we need a separate boolean that 
controls whether SBPO_ControlStatements would apply?

My gut is probably the "maintain consistency" option is fine for now so I've 
gone ahead & applied that change in the latest diff.


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

https://reviews.llvm.org/D102730

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-05-21 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 347150.
vlovich marked 3 inline comments as done.
vlovich added a comment.

Review response. Updated the options I changed via the dump_format_style.py 
script but didn't ingest all the other changes it generated.
Changed the SBPO option to have a more generic name that also applies to IF 
macros.
Fix typo in release notes.


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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -1335,7 +1335,7 @@
 
   FormatStyle Style = getLLVMStyle();
   Style.SpaceBeforeParens =
-  FormatStyle::SBPO_ControlStatementsExceptForEachMacros;
+  FormatStyle::SBPO_ControlStatementsExceptControlMacros;
   verifyFormat("void f() {\n"
"  foreach(Item *item, itemlist) {}\n"
"  Q_FOREACH(Item *item, itemlist) {}\n"
@@ -16957,6 +16957,8 @@
   FormatStyle::SBPO_Always);
   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
   FormatStyle::SBPO_ControlStatements);
+  CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", SpaceBeforeParens,
+  FormatStyle::SBPO_ControlStatementsExceptControlMacros);
   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
   FormatStyle::SBPO_NonEmptyParentheses);
   // For backward compatibility:
@@ -16964,6 +16966,8 @@
   FormatStyle::SBPO_Never);
   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
   FormatStyle::SBPO_ControlStatements);
+  CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", SpaceBeforeParens,
+  FormatStyle::SBPO_ControlStatementsExceptControlMacros);
 
   Style.ColumnLimit = 123;
   FormatStyle BaseStyle = getLLVMStyle();
@@ -17111,6 +17115,11 @@
   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
   BoostAndQForeach);
 
+  Style.IfMacros.clear();
+  std::vector CustomIfs;
+  CustomIfs.push_back("MYIF");
+  CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
+
   Style.AttributeMacros.clear();
   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
   std::vector{"__capability"});
@@ -19684,11 +19693,16 @@
 
 TEST_F(FormatTest, SpacesInConditionalStatement) {
   FormatStyle Spaces = getLLVMStyle();
+  Spaces.IfMacros.clear();
+  Spaces.IfMacros.push_back("MYIF");
   Spaces.SpacesInConditionalStatement = true;
   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
   verifyFormat("if ( !a )\n  return;", Spaces);
   verifyFormat("if ( a )\n  return;", Spaces);
   verifyFormat("if constexpr ( a )\n  return;", Spaces);
+  verifyFormat("MYIF ( a )\n  return;", Spaces);
+  verifyFormat("MYIF ( a )\n  return;\nelse MYIF ( b )\n  return;", Spaces);
+  verifyFormat("MYIF ( a )\n  return;\nelse\n  return;", Spaces);
   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
   verifyFormat("while ( a )\n  return;", Spaces);
   verifyFormat("while ( (a && b) )\n  return;", Spaces);
@@ -19697,6 +19711,12 @@
   // Check that space on the left of "::" is inserted as expected at beginning
   // of condition.
   verifyFormat("while ( ::func() )\n  return;", Spaces);
+
+  // Check impact of ControlStatementsExceptControlMacros is honored.
+  Spaces.SpaceBeforeParens = FormatStyle::SBPO_ControlStatementsExceptControlMacros;
+  verifyFormat("MYIF( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
 }
 
 TEST_F(FormatTest, AlternativeOperators) {
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1363,7 +1363,7 @@
 // Reset token type in case we have already looked at it and then
 // recovered from an error (e.g. failure to find the matching >).
 if (!CurrentToken->isOneOf(
-TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro,
+TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
@@ -3019,9 +3019,13 @@
 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare)))
   return true;
 if (Style.SpaceBeforeParens ==
-

[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-21 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

In D102706#2774018 , @MyDeveloperDay 
wrote:

> Could you clang-format the tests please so I can more easily read them.

Sure. I was just taking my queue from the rest of the file which appeared to 
intentionally not clang-format the test strings.




Comment at: clang/docs/ClangFormatStyleOptions.rst:2790
 
+**LambdaBodyIndentation** (``LambdaBodyIndentationKind``)
+  What is the lambda body indented relative to (default is ``Signature``).

MyDeveloperDay wrote:
> Did you generate this by hand or using the clang/doc/tools/dump_style.py?
By hand. `dump_format_style` generates other changes (although I wasn't aware 
of it at the time). I'll regenerate just this section using that tool.


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

https://reviews.llvm.org/D102706

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-05-20 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 346870.
vlovich added a comment.

Fixed missing _ after the hyperlink to the KJ link in the Style options 
documentation. I'm assuming that's required formatting for hyperlinks by what 
renders the markdown.


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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -17111,6 +17111,11 @@
   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
   BoostAndQForeach);
 
+  Style.IfMacros.clear();
+  std::vector CustomIfs;
+  CustomIfs.push_back("MYIF");
+  CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
+
   Style.AttributeMacros.clear();
   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
   std::vector{"__capability"});
@@ -19684,11 +19689,16 @@
 
 TEST_F(FormatTest, SpacesInConditionalStatement) {
   FormatStyle Spaces = getLLVMStyle();
+  Spaces.IfMacros.clear();
+  Spaces.IfMacros.push_back("MYIF");
   Spaces.SpacesInConditionalStatement = true;
   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
   verifyFormat("if ( !a )\n  return;", Spaces);
   verifyFormat("if ( a )\n  return;", Spaces);
   verifyFormat("if constexpr ( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
   verifyFormat("while ( a )\n  return;", Spaces);
   verifyFormat("while ( (a && b) )\n  return;", Spaces);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1363,7 +1363,7 @@
 // Reset token type in case we have already looked at it and then
 // recovered from an error (e.g. failure to find the matching >).
 if (!CurrentToken->isOneOf(
-TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro,
+TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
@@ -3022,6 +3022,9 @@
 FormatStyle::SBPO_ControlStatementsExceptForEachMacros &&
 Left.is(TT_ForEachMacro))
   return false;
+if (Left.is(TT_IfMacro)) {
+  return false;
+}
 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
(Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
 (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
Index: clang/lib/Format/FormatTokenLexer.cpp
===
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -39,6 +39,8 @@
 
   for (const std::string  : Style.ForEachMacros)
 Macros.insert({(ForEachMacro), TT_ForEachMacro});
+  for (const std::string  : Style.IfMacros)
+Macros.insert({(IfMacro), TT_IfMacro});
   for (const std::string  : Style.AttributeMacros)
 Macros.insert({(AttributeMacro), TT_AttributeMacro});
   for (const std::string  : Style.StatementMacros)
@@ -1014,6 +1016,9 @@
   tok::pp_define) &&
 it != Macros.end()) {
   FormatTok->setType(it->second);
+  if (it->second == TT_IfMacro) {
+FormatTok->Tok.setKind(tok::kw_if);
+  }
 } else if (FormatTok->is(tok::identifier)) {
   if (MacroBlockBeginRegex.match(Text)) {
 FormatTok->setType(TT_MacroBlockBegin);
Index: clang/lib/Format/FormatToken.h
===
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -52,6 +52,7 @@
   TYPE(FunctionDeclarationName)\
   TYPE(FunctionLBrace) \
   TYPE(FunctionTypeLParen) \
+  TYPE(IfMacro)\
   TYPE(ImplicitStringLiteral)  \
   TYPE(InheritanceColon)   \
   TYPE(InheritanceComma)   \
Index: clang/lib/Format/Format.cpp

[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-20 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 346869.
vlovich added a comment.
Herald added a subscriber: mgrang.

Review response

- Add examples to documentation + links to KJ style guide.
- Add better header doc
- Add more test cases
- Add release notes
- Note current "problem" test with a TODO (corner case that's not critical for 
KJ).


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

https://reviews.llvm.org/D102706

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/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -17871,6 +17871,7 @@
" aaa;\n"
"});",
getLLVMStyleWithColumns(60));
+
   verifyFormat("SomeFunction({[&] {\n"
"// comment\n"
"  },\n"
@@ -18423,6 +18424,105 @@
"  });\n"
"});",
LLVMWithBeforeLambdaBody);
+
+  // Lambdas with different indentation styles.
+  Style = getLLVMStyleWithColumns(100);
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then(\n"
+"  [this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+   "return someObject.startAsyncAction().then([this, ](AsyncActionResult result) mutable {\n"
+   "  result.processMore();\n"
+   "});\n"
+   "  });\n"
+   "}\n",
+   Style));
+  Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
+  verifyFormat("test() {\n"
+   "  ([]() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  }).foo();\n"
+   "}",
+   Style);
+  verifyFormat("test() {\n"
+   "  []() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  }\n"
+   "}",
+   Style);
+  verifyFormat("std::sort(v.begin(), v.end(),\n"
+   "  [](const auto , const auto ) {\n"
+   "  return someLongArgumentName.someMemberVariable < someOtherLongArgumentName.someMemberVariable;\n"
+   "});",
+   Style);
+  verifyFormat("test() {\n"
+   "  (\n"
+   "  []() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  },\n"
+   "  foo, bar)\n"
+   "  .foo();\n"
+   "}",
+   Style);
+  verifyFormat("test() {\n"
+   "  ([]() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  })\n"
+   "  .foo()\n"
+   "  .bar();\n"
+   "}",
+   Style);
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then(\n"
+"  [this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+   "return someObject.startAsyncAction().then([this, ](AsyncActionResult result) mutable {\n"
+   "  result.processMore();\n"
+   "});\n"
+   "  });\n"
+   "}\n",
+   Style));
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then([this, ] {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, ] {\n"
+   "return someObject.startAsyncAction().then([this, ](AsyncActionResult result) mutable {\n"
+  

[PATCH] D102730: [clang-format] Support custom If macros

2021-05-20 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 346864.
vlovich marked an inline comment as done.
vlovich added a comment.

Updated release notes.


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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -17111,6 +17111,11 @@
   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
   BoostAndQForeach);
 
+  Style.IfMacros.clear();
+  std::vector CustomIfs;
+  CustomIfs.push_back("MYIF");
+  CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
+
   Style.AttributeMacros.clear();
   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
   std::vector{"__capability"});
@@ -19684,11 +19689,16 @@
 
 TEST_F(FormatTest, SpacesInConditionalStatement) {
   FormatStyle Spaces = getLLVMStyle();
+  Spaces.IfMacros.clear();
+  Spaces.IfMacros.push_back("MYIF");
   Spaces.SpacesInConditionalStatement = true;
   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
   verifyFormat("if ( !a )\n  return;", Spaces);
   verifyFormat("if ( a )\n  return;", Spaces);
   verifyFormat("if constexpr ( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
   verifyFormat("while ( a )\n  return;", Spaces);
   verifyFormat("while ( (a && b) )\n  return;", Spaces);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1363,7 +1363,7 @@
 // Reset token type in case we have already looked at it and then
 // recovered from an error (e.g. failure to find the matching >).
 if (!CurrentToken->isOneOf(
-TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro,
+TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
@@ -3022,6 +3022,9 @@
 FormatStyle::SBPO_ControlStatementsExceptForEachMacros &&
 Left.is(TT_ForEachMacro))
   return false;
+if (Left.is(TT_IfMacro)) {
+  return false;
+}
 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
(Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
 (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
Index: clang/lib/Format/FormatTokenLexer.cpp
===
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -39,6 +39,8 @@
 
   for (const std::string  : Style.ForEachMacros)
 Macros.insert({(ForEachMacro), TT_ForEachMacro});
+  for (const std::string  : Style.IfMacros)
+Macros.insert({(IfMacro), TT_IfMacro});
   for (const std::string  : Style.AttributeMacros)
 Macros.insert({(AttributeMacro), TT_AttributeMacro});
   for (const std::string  : Style.StatementMacros)
@@ -1014,6 +1016,9 @@
   tok::pp_define) &&
 it != Macros.end()) {
   FormatTok->setType(it->second);
+  if (it->second == TT_IfMacro) {
+FormatTok->Tok.setKind(tok::kw_if);
+  }
 } else if (FormatTok->is(tok::identifier)) {
   if (MacroBlockBeginRegex.match(Text)) {
 FormatTok->setType(TT_MacroBlockBegin);
Index: clang/lib/Format/FormatToken.h
===
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -52,6 +52,7 @@
   TYPE(FunctionDeclarationName)\
   TYPE(FunctionLBrace) \
   TYPE(FunctionTypeLParen) \
+  TYPE(IfMacro)\
   TYPE(ImplicitStringLiteral)  \
   TYPE(InheritanceColon)   \
   TYPE(InheritanceComma)   \
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -616,6 

[PATCH] D102730: [clang-format] Support custom If macros

2021-05-20 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich marked an inline comment as done.
vlovich added inline comments.



Comment at: clang/include/clang/Format/Format.h:2098
+  ///
+  /// For example: KJ_IF_MAYBE.
+  std::vector IfMacros;

HazardyKnusperkeks wrote:
> vlovich wrote:
> > Should I put a hyperlink to 
> > https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes?
> Maybe. *scnr*
Well done. Took me a second.


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

https://reviews.llvm.org/D102730

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-05-20 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich updated this revision to Diff 346863.
vlovich added a comment.

Review response:

- Added additional tests.
- Added `KJ_IF_MAYBE` to default `IfMacros`.
- Added links to KJ documentation.


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

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -17111,6 +17111,11 @@
   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
   BoostAndQForeach);
 
+  Style.IfMacros.clear();
+  std::vector CustomIfs;
+  CustomIfs.push_back("MYIF");
+  CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
+
   Style.AttributeMacros.clear();
   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
   std::vector{"__capability"});
@@ -19684,11 +19689,16 @@
 
 TEST_F(FormatTest, SpacesInConditionalStatement) {
   FormatStyle Spaces = getLLVMStyle();
+  Spaces.IfMacros.clear();
+  Spaces.IfMacros.push_back("MYIF");
   Spaces.SpacesInConditionalStatement = true;
   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
   verifyFormat("if ( !a )\n  return;", Spaces);
   verifyFormat("if ( a )\n  return;", Spaces);
   verifyFormat("if constexpr ( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse MYIF( b )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;\nelse\n  return;", Spaces);
   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
   verifyFormat("while ( a )\n  return;", Spaces);
   verifyFormat("while ( (a && b) )\n  return;", Spaces);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1363,7 +1363,7 @@
 // Reset token type in case we have already looked at it and then
 // recovered from an error (e.g. failure to find the matching >).
 if (!CurrentToken->isOneOf(
-TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro,
+TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
@@ -3022,6 +3022,9 @@
 FormatStyle::SBPO_ControlStatementsExceptForEachMacros &&
 Left.is(TT_ForEachMacro))
   return false;
+if (Left.is(TT_IfMacro)) {
+  return false;
+}
 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
(Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
 (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
Index: clang/lib/Format/FormatTokenLexer.cpp
===
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -39,6 +39,8 @@
 
   for (const std::string  : Style.ForEachMacros)
 Macros.insert({(ForEachMacro), TT_ForEachMacro});
+  for (const std::string  : Style.IfMacros)
+Macros.insert({(IfMacro), TT_IfMacro});
   for (const std::string  : Style.AttributeMacros)
 Macros.insert({(AttributeMacro), TT_AttributeMacro});
   for (const std::string  : Style.StatementMacros)
@@ -1014,6 +1016,9 @@
   tok::pp_define) &&
 it != Macros.end()) {
   FormatTok->setType(it->second);
+  if (it->second == TT_IfMacro) {
+FormatTok->Tok.setKind(tok::kw_if);
+  }
 } else if (FormatTok->is(tok::identifier)) {
   if (MacroBlockBeginRegex.match(Text)) {
 FormatTok->setType(TT_MacroBlockBegin);
Index: clang/lib/Format/FormatToken.h
===
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -52,6 +52,7 @@
   TYPE(FunctionDeclarationName)\
   TYPE(FunctionLBrace) \
   TYPE(FunctionTypeLParen) \
+  TYPE(IfMacro)\
   TYPE(ImplicitStringLiteral)  \
   TYPE(InheritanceColon)   \
   TYPE(InheritanceComma)   \
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ 

[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-20 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

> verifyFormat("test() {\n"
>
>   "  ([]() -> {\n"
>   "int b = 32;\n"
>   "return 3;\n"
>   "  }).foo();\n"
>   "}",
>   Style);
>
>   There you have parenthesis around the lambda, how about without?
>   Maybe just something like
>
> std::sort(v.begin(), v.end(), [](const auto& lhs, const auto& rhs) { return 
> lhs.Foo < rhs.Foo; });
>
>   

Ah. Ok. What I had done was run all the tests with this flag & note the ones 
where the formatting changed & copied those. The one without parenthesis didn't 
do anything interesting which is why I omitted it originally. I'll add it back 
+ your sort example too. Thanks!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102706

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-05-18 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added inline comments.



Comment at: clang/include/clang/Format/Format.h:2098
+  ///
+  /// For example: KJ_IF_MAYBE.
+  std::vector IfMacros;

Should I put a hyperlink to 
https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102730

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


[PATCH] D102730: [clang-format] Support custom If macros

2021-05-18 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich created this revision.
vlovich added reviewers: curdeius, MyDeveloperDay, HazardyKnusperkeks.
vlovich added a project: clang-format.
vlovich requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Like with ForEachMacros, support user to provide additional macros that act 
like `if` statements.

This makes it possible to support KJ_IF_MAYBE from the cap'n'proto library:
https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes.

https://bugs.llvm.org/show_bug.cgi?id=49354


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102730

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/FormatTokenLexer.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
@@ -17111,6 +17111,11 @@
   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
   BoostAndQForeach);
 
+  Style.IfMacros.clear();
+  std::vector CustomIfs;
+  CustomIfs.push_back("MYIF");
+  CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs);
+
   Style.AttributeMacros.clear();
   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
   std::vector{"__capability"});
@@ -19684,11 +19689,14 @@
 
 TEST_F(FormatTest, SpacesInConditionalStatement) {
   FormatStyle Spaces = getLLVMStyle();
+  Spaces.IfMacros.clear();
+  Spaces.IfMacros.push_back("MYIF");
   Spaces.SpacesInConditionalStatement = true;
   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
   verifyFormat("if ( !a )\n  return;", Spaces);
   verifyFormat("if ( a )\n  return;", Spaces);
   verifyFormat("if constexpr ( a )\n  return;", Spaces);
+  verifyFormat("MYIF( a )\n  return;", Spaces);
   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
   verifyFormat("while ( a )\n  return;", Spaces);
   verifyFormat("while ( (a && b) )\n  return;", Spaces);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1363,7 +1363,7 @@
 // Reset token type in case we have already looked at it and then
 // recovered from an error (e.g. failure to find the matching >).
 if (!CurrentToken->isOneOf(
-TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro,
+TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
@@ -3022,6 +3022,9 @@
 FormatStyle::SBPO_ControlStatementsExceptForEachMacros &&
 Left.is(TT_ForEachMacro))
   return false;
+if (Left.is(TT_IfMacro)) {
+  return false;
+}
 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
(Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
 (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
Index: clang/lib/Format/FormatTokenLexer.cpp
===
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -39,6 +39,8 @@
 
   for (const std::string  : Style.ForEachMacros)
 Macros.insert({(ForEachMacro), TT_ForEachMacro});
+  for (const std::string  : Style.IfMacros)
+Macros.insert({(IfMacro), TT_IfMacro});
   for (const std::string  : Style.AttributeMacros)
 Macros.insert({(AttributeMacro), TT_AttributeMacro});
   for (const std::string  : Style.StatementMacros)
@@ -1014,6 +1016,9 @@
   tok::pp_define) &&
 it != Macros.end()) {
   FormatTok->setType(it->second);
+  if (it->second == TT_IfMacro) {
+FormatTok->Tok.setKind(tok::kw_if);
+  }
 } else if (FormatTok->is(tok::identifier)) {
   if (MacroBlockBeginRegex.match(Text)) {
 FormatTok->setType(TT_MacroBlockBegin);
Index: clang/lib/Format/FormatToken.h
===
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -52,6 +52,7 @@
   TYPE(FunctionDeclarationName)\
   TYPE(FunctionLBrace) \
   TYPE(FunctionTypeLParen) \
+  TYPE(IfMacro)\
   TYPE(ImplicitStringLiteral)  \
   TYPE(InheritanceColon)   \
   TYPE(InheritanceComma) 

[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-18 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

In D102706#2766680 , 
@HazardyKnusperkeks wrote:

> Maybe a bit more test cases with smaller lambdas? Or without the outer 
> parenthesis?

I'm not sure I understand this comment. Which test case are you referring to by 
"or without the outer parenthesis"?




Comment at: clang/include/clang/Format/Format.h:2456
+  enum LambdaBodyIndentationKind : unsigned char {
+/// Align lambda body relative to the lambda signature. This is the 
default.
+LBI_Signature,

HazardyKnusperkeks wrote:
> I think you should add examples, because for me it is not 100% what's the 
> difference is.
Good idea. Meant to do that & forgot. In terms of an example, do  you find the 
test case or description in the phabricator message useful or are you looking 
for something else?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102706

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


[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-18 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich added a comment.

I'm not 100% certain if the implementation is absolutely correct. For example, 
the `#define A` test case is broken (the closing brace is on the wrong line). 
Additionally, the `pop_back` feels weird but seems to fix all the other cases 
to make the closing brace aligned properly. Maybe some/all of the 
implementation belongs in ContinuationIndenter.cpp instead but I couldn't 
really follow where the best place to do that might be. I can also just leave 
the preprocessor macro case as a known issue for now.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102706

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


[PATCH] D102706: [clang-format] Add new LambdaBodyIndentation option

2021-05-18 Thread Vitali Lovich via Phabricator via cfe-commits
vlovich created this revision.
vlovich added reviewers: MyDeveloperDay, curdeius.
vlovich added a project: clang-format.
vlovich requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Currently the lambda body indents relative to where the lambda signature is 
located. This instead lets the user
choose to align the lambda body relative to the parent scope that contains the 
lambda declaration. Thus:

  someFunction([] {
lambdaBody();
  });

will always have the same indentation of the body even when the lambda 
signature goes on a new line:

  someFunction(
  [] {
lambdaBody();
  });

whereas before `lambdaBody` would be indented 6 spaces.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102706

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

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -17871,6 +17871,7 @@
" aaa;\n"
"});",
getLLVMStyleWithColumns(60));
+
   verifyFormat("SomeFunction({[&] {\n"
"// comment\n"
"  },\n"
@@ -18423,6 +18424,90 @@
"  });\n"
"});",
LLVMWithBeforeLambdaBody);
+
+  // Lambdas with different indentation styles.
+  Style = getLLVMStyleWithColumns(100);
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then(\n"
+"  [this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+   "return someObject.startAsyncAction().then([this, ](AsyncActionResult result) mutable {\n"
+   "  result.processMore();\n"
+   "});\n"
+   "  });\n"
+   "}\n",
+   Style));
+  Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
+  verifyFormat("test() {\n"
+   "  ([]() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  }).foo();\n"
+   "}",
+   Style);
+  verifyFormat("test() {\n"
+   "  (\n"
+   "  []() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  },\n"
+   "  foo, bar)\n"
+   "  .foo();\n"
+   "}",
+   Style);
+  verifyFormat("test() {\n"
+   "  ([]() -> {\n"
+   "int b = 32;\n"
+   "return 3;\n"
+   "  })\n"
+   "  .foo()\n"
+   "  .bar();\n"
+   "}",
+   Style);
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then(\n"
+"  [this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, , someObject = std::mv(s)](std::vector evaluated) mutable {\n"
+   "return someObject.startAsyncAction().then([this, ](AsyncActionResult result) mutable {\n"
+   "  result.processMore();\n"
+   "});\n"
+   "  });\n"
+   "}\n",
+   Style));
+  EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n"
+"  return promise.then([this, ] {\n"
+"return someObject.startAsyncAction().then(\n"
+"[this, ](AsyncActionResult result) mutable { result.processMore(); });\n"
+"  });\n"
+"}\n",
+format("SomeResult doSomething(SomeObject promise) {\n"
+   "  return promise.then([this, ] {\n"
+   "return someObject.startAsyncAction().then([this, ](AsyncActionResult result) mutable {\n"
+   "  result.processMore();\n"
+   "});\n"
+   "  });\n"
+   "}\n",
+