MyDeveloperDay updated this revision to Diff 352193.
MyDeveloperDay added a comment.
Add support for TemplateCloser before LambdaBrace
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D104222/new/
https://reviews.llvm.org/D104222
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -18856,8 +18856,7 @@
" });\n"
" });",
LLVMWithBeforeLambdaBody);
- verifyFormat("void Fct()\n"
- "{\n"
+ verifyFormat("void Fct() {\n"
" return {[]()\n"
" {\n"
" return 17;\n"
@@ -19061,6 +19060,35 @@
" });\n"
" });",
LLVMWithBeforeLambdaBody);
+
+ LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
+ FormatStyle::ShortLambdaStyle::SLS_None;
+
+ verifyFormat("auto select = [this]() -> const Library::Object *\n"
+ "{\n"
+ " return MyAssignment::SelectFromList(this);\n"
+ "};\n",
+ LLVMWithBeforeLambdaBody);
+
+ verifyFormat("auto select = [this]() -> const Library::Object &\n"
+ "{\n"
+ " return MyAssignment::SelectFromList(this);\n"
+ "};\n",
+ LLVMWithBeforeLambdaBody);
+
+ verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
+ "{\n"
+ " return MyAssignment::SelectFromList(this);\n"
+ "};\n",
+ LLVMWithBeforeLambdaBody);
+
+ verifyFormat("namespace test {\n"
+ "class Test {\n"
+ "public:\n"
+ " Test() = default;\n"
+ "};\n"
+ "} // namespace test",
+ LLVMWithBeforeLambdaBody);
}
TEST_F(FormatTest, LambdaWithLineComments) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3557,42 +3557,11 @@
return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
}
-static bool
-isItAInlineLambdaAllowed(const FormatToken &Tok,
- FormatStyle::ShortLambdaStyle ShortLambdaOption) {
- return (ShortLambdaOption == FormatStyle::SLS_Inline &&
- IsFunctionArgument(Tok)) ||
- (ShortLambdaOption == FormatStyle::SLS_All);
-}
-
-static bool isOneChildWithoutMustBreakBefore(const FormatToken &Tok) {
- if (Tok.Children.size() != 1)
- return false;
- FormatToken *curElt = Tok.Children[0]->First;
- while (curElt) {
- if (curElt->MustBreakBefore)
- return false;
- curElt = curElt->Next;
- }
- return true;
-}
static bool isAllmanLambdaBrace(const FormatToken &Tok) {
return (Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral));
}
-static bool isAllmanBraceIncludedBreakableLambda(
- const FormatToken &Tok, FormatStyle::ShortLambdaStyle ShortLambdaOption) {
- if (!isAllmanLambdaBrace(Tok))
- return false;
-
- if (isItAnEmptyLambdaAllowed(Tok, ShortLambdaOption))
- return false;
-
- return !isItAInlineLambdaAllowed(Tok, ShortLambdaOption) ||
- !isOneChildWithoutMustBreakBefore(Tok);
-}
-
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
const FormatToken &Right) {
const FormatToken &Left = *Right.Previous;
@@ -3754,13 +3723,6 @@
if (Right.is(TT_InlineASMBrace))
return Right.HasUnescapedNewline;
- auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
- if (Style.BraceWrapping.BeforeLambdaBody &&
- (isAllmanBraceIncludedBreakableLambda(Left, ShortLambdaOption) ||
- isAllmanBraceIncludedBreakableLambda(Right, ShortLambdaOption))) {
- return true;
- }
-
if (isAllmanBrace(Left) || isAllmanBrace(Right))
return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
(Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
@@ -3783,6 +3745,11 @@
return true;
}
+ if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
+ Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
+ return true;
+ }
+
// Put multiple Java annotation on a new line.
if ((Style.Language == FormatStyle::LK_Java ||
Style.Language == FormatStyle::LK_JavaScript) &&
@@ -4186,7 +4153,7 @@
return false;
auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
- if (Style.BraceWrapping.BeforeLambdaBody) {
+ if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
if (isAllmanLambdaBrace(Left))
return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
if (isAllmanLambdaBrace(Right))
@@ -4198,7 +4165,6 @@
Right.isMemberAccess() ||
Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
tok::colon, tok::l_square, tok::at) ||
- (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) ||
(Left.is(tok::r_paren) &&
Right.isOneOf(tok::identifier, tok::kw_const)) ||
(Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits