[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
Fri Jul 22 11:58:10 PDT 2022
denis-fatkulin updated this revision to Diff 446916.
denis-fatkulin added a comment.
> Could you please add full git context?
I updated the patch with properly git context. Thanks!
> Was the problem due to misannotation of auto? If so, could you add an annotator test?
I'm not sure about the questions. I will try to explain my patch purpose. Actually there was two problems:
1. `auto` wasn't detected as properly type keyword in lambda's trailing return type. So, formatting was completely wrong for this case (fixed in `clang/lib/Format/UnwrappedLineParser.cpp`)
2. The keyword `auto` and left brace `{` was interpreted as declaration `auto{}`. So, formatting delete a space symbol between them. (fixed in `clang/lib/Format/TokenAnnotator.cpp`)
Both cases are checked in changes at `clang/unittests/Format/FormatTest.cpp` and I think the unit test is sufficient.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D130299/new/
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
@@ -23550,6 +23550,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.446916.patch
Type: text/x-patch
Size: 1635 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220722/77000a17/attachment.bin>
More information about the cfe-commits
mailing list