From 5c2e7c9ca043d92bed75b08e653fb47c384edd13 Mon Sep 17 00:00:00 2001 From: mydeveloperday Date: Mon, 10 Jan 2022 08:28:42 +0000 Subject: [clang-format] Ensure we can correctly parse lambda in the template argument list https://github.com/llvm/llvm-project/issues/46505 The presence of a lambda in an argument template list ignored the [] as a lambda at all, this caused the contents of the <> to be incorrectly analyzed. ``` struct Y : X < [] { return 0; } > {}; ``` Fixes: #46505 Reviewed By: curdeius Differential Revision: https://reviews.llvm.org/D116806 --- clang/lib/Format/UnwrappedLineParser.cpp | 2 ++ clang/unittests/Format/FormatTest.cpp | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index c293c236193c..410cce79a087 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2874,6 +2874,8 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { if (!tryToParseBracedList()) break; } + if (FormatTok->is(tok::l_square) && !tryToParseLambda()) + break; if (FormatTok->Tok.is(tok::semi)) return; if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 85ce3171bbc8..d3e4479a5aa8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -23212,6 +23212,15 @@ TEST_F(FormatTest, EmptyShortBlock) { Style); } +TEST_F(FormatTest, ShortTemplatedArgumentLists) { + auto Style = getLLVMStyle(); + + verifyFormat("struct Y : X<[] { return 0; }> {};", Style); + verifyFormat("struct Y<[] { return 0; }> {};", Style); + + verifyFormat("struct Z : X {};", Style); +} + } // namespace } // namespace format } // namespace clang -- cgit v1.2.1