[PATCH] D79320: [clang-format [PR45791] BeforeLambdaBody is confused by comment inside lambda
MyDeveloperDay via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon May 4 04:14:27 PDT 2020
MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: krasimir, sammccall, Wawha, duncan-llvm.
MyDeveloperDay added projects: clang, clang-format.
https://bugs.llvm.org/show_bug.cgi?id=45791
Lambda with line comment is incorrectly formatted
auto k = []() // comment
{ return; };
`
auto k = []() // comment { return; };
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D79320
Files:
clang/lib/Format/ContinuationIndenter.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -14576,6 +14576,30 @@
LLVMWithBeforeLambdaBody);
}
+TEST_F(FormatTest, LambdaWithLineComments) {
+ FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
+ LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
+ LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
+ LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
+ FormatStyle::ShortLambdaStyle::SLS_All;
+
+ verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
+ verifyFormat("auto k = []() // comment\n"
+ "{ return; }",
+ LLVMWithBeforeLambdaBody);
+ verifyFormat("auto k = []() /* comment */ { return; }",
+ LLVMWithBeforeLambdaBody);
+ verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
+ LLVMWithBeforeLambdaBody);
+ verifyFormat("auto k = []() // X\n"
+ "{ return; }",
+ LLVMWithBeforeLambdaBody);
+ verifyFormat(
+ "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
+ "{ return; }",
+ LLVMWithBeforeLambdaBody);
+}
+
TEST_F(FormatTest, EmptyLinesInLambdas) {
verifyFormat("auto lambda = []() {\n"
" x(); //\n"
Index: clang/lib/Format/ContinuationIndenter.cpp
===================================================================
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -330,7 +330,7 @@
const FormatToken &Current = *State.NextToken;
const FormatToken &Previous = *Current.Previous;
if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore &&
- Current.is(TT_LambdaLBrace)) {
+ Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) {
auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack);
return (LambdaBodyLength > getColumnLimit(State));
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79320.261776.patch
Type: text/x-patch
Size: 2066 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200504/b7f65b8b/attachment.bin>
More information about the cfe-commits
mailing list