[PATCH] D130299: [clang-format] FIX: Misformatting lambdas with trailing return type 'auto' in braced lists
Denis Fatkulin via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 21 12:42:43 PDT 2022
denis-fatkulin created this revision.
denis-fatkulin added a reviewer: rsmith.
denis-fatkulin added a project: clang-format.
Herald added a project: All.
denis-fatkulin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Lambdas with trailing return type 'auto' are formatted incorrectly in braced lists. The simpliest reproduction code is:
cpp
auto list = {[]() -> auto { return 0; }};
Was reported at https://github.com/llvm/llvm-project/issues/54798
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D130299
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -23546,6 +23546,13 @@
verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
}
+TEST_F(FormatTest, LambdaWithTrailingAutoInBracedList) {
+ FormatStyle Style = getLLVMStyle();
+ verifyFormat("auto list = {[]() -> auto { return Val; }};", Style);
+ verifyFormat("auto list = {[]() -> auto * { return Ptr; }};", Style);
+ verifyFormat("auto list = {[]() -> auto & { return Ref; }};", Style);
+}
+
TEST_F(FormatTest, SpacesInConditionalStatement) {
FormatStyle Spaces = getLLVMStyle();
Spaces.IfMacros.clear();
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2108,6 +2108,7 @@
case tok::l_square:
parseSquare();
break;
+ case tok::kw_auto:
case tok::kw_class:
case tok::kw_template:
case tok::kw_typename:
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3310,6 +3310,10 @@
}
}
+ // Lambda with trailing return type 'auto': []() -> auto { return T; }
+ if (Left.is(tok::kw_auto) && Right.getType() == TT_LambdaLBrace)
+ return true;
+
// auto{x} auto(x)
if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
return false;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130299.446600.patch
Type: text/x-patch
Size: 1635 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220721/6af6023b/attachment.bin>
More information about the cfe-commits
mailing list