From: Marek Kurdej Date: Wed, 26 Jan 2022 08:11:21 +0000 (+0100) Subject: [clang-format] Fix regression in parsing pointers to arrays. X-Git-Tag: upstream/15.0.7~19122 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=72e29caf039fd81bc6948e16d6f71d1581615469;p=platform%2Fupstream%2Fllvm.git [clang-format] Fix regression in parsing pointers to arrays. Fixes https://github.com/llvm/llvm-project/issues/53293. After commit 5c2e7c9, the code: ``` template <> struct S : Template {}; ``` was misformatted as: ``` template <> struct S : Template{}; ``` Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D118106 --- diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 96d227b..ff3e791 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -3081,8 +3081,15 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { if (!tryToParseBracedList()) break; } - if (FormatTok->is(tok::l_square) && !tryToParseLambda()) - break; + if (FormatTok->is(tok::l_square)) { + FormatToken *Previous = FormatTok->Previous; + if (!Previous || Previous->isNot(tok::r_paren)) { + // Don't try parsing a lambda if we had a closing parenthesis before, + // it was probably a pointer to an array: int (*)[]. + if (!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 c4e0e14..c45869f 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -23483,6 +23483,8 @@ TEST_F(FormatTest, EmptyShortBlock) { TEST_F(FormatTest, ShortTemplatedArgumentLists) { auto Style = getLLVMStyle(); + verifyFormat("template <> struct S : Template {};\n", Style); + verifyFormat("template <> struct S : Template {};\n", Style); verifyFormat("struct Y : X<[] { return 0; }> {};", Style); verifyFormat("struct Y<[] { return 0; }> {};", Style);