From: Jan Korous Date: Tue, 5 Mar 2019 19:27:24 +0000 (+0000) Subject: [clang-format] Fix lambdas returning template specialization that contains operator... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=88e15140ee54dab2b29940b514534df954e7da28;p=platform%2Fupstream%2Fllvm.git [clang-format] Fix lambdas returning template specialization that contains operator in parameter A template specialization of a template foo can contain integer constants and a whole bunch of operators - e. g. foo< 1 ? !0 : (3+1)%4 > Inspired by https://reviews.llvm.org/D58922 Differential Revision: https://reviews.llvm.org/D58934 llvm-svn: 355434 --- diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 5e41564..4bc624d 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1423,6 +1423,25 @@ bool UnwrappedLineParser::tryToParseLambda() { case tok::coloncolon: case tok::kw_mutable: case tok::kw_noexcept: + // Specialization of a template with an integer parameter can contain + // arithmetic, logical, comparison and ternary operators. + case tok::plus: + case tok::minus: + case tok::exclaim: + case tok::tilde: + case tok::slash: + case tok::percent: + case tok::lessless: + case tok::pipe: + case tok::pipepipe: + case tok::ampamp: + case tok::caret: + case tok::equalequal: + case tok::exclaimequal: + case tok::greaterequal: + case tok::lessequal: + case tok::question: + case tok::colon: nextToken(); break; case tok::arrow: diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 0385665..094db34 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -11846,6 +11846,96 @@ TEST_F(FormatTest, FormatsLambdas) { verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); verifyFormat("[a, a]() -> a<1> {};"); + verifyFormat("[]() -> foo<5 + 2> { return {}; };"); + verifyFormat("[]() -> foo<5 - 2> { return {}; };"); + verifyFormat("[]() -> foo<5 / 2> { return {}; };"); + verifyFormat("[]() -> foo<5 * 2> { return {}; };"); + verifyFormat("[]() -> foo<5 % 2> { return {}; };"); + verifyFormat("[]() -> foo<5 << 2> { return {}; };"); + verifyFormat("[]() -> foo { return {}; };"); + verifyFormat("[]() -> foo<~5> { return {}; };"); + verifyFormat("[]() -> foo<5 | 2> { return {}; };"); + verifyFormat("[]() -> foo<5 || 2> { return {}; };"); + verifyFormat("[]() -> foo<5 & 2> { return {}; };"); + verifyFormat("[]() -> foo<5 && 2> { return {}; };"); + verifyFormat("[]() -> foo<5 == 2> { return {}; };"); + verifyFormat("[]() -> foo<5 != 2> { return {}; };"); + verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); + verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); + verifyFormat("[]() -> foo<5 < 2> { return {}; };"); + verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<~5> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" + "} // namespace bar"); + verifyFormat("namespace bar {\n" + "// broken:\n" + "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" + "} // namespace bar"); verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" " int j) -> int {\n" " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"