r355434 - [clang-format] Fix lambdas returning template specialization that contains operator in parameter
Jan Korous via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 5 11:27:24 PST 2019
Author: jkorous
Date: Tue Mar 5 11:27:24 2019
New Revision: 355434
URL: http://llvm.org/viewvc/llvm-project?rev=355434&view=rev
Log:
[clang-format] Fix lambdas returning template specialization that contains operator in parameter
A template specialization of a template foo<int N> 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
Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=355434&r1=355433&r2=355434&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Tue Mar 5 11:27:24 2019
@@ -1423,6 +1423,25 @@ bool UnwrappedLineParser::tryToParseLamb
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:
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=355434&r1=355433&r2=355434&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Mar 5 11:27:24 2019
@@ -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<!5> { 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<!5> { 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"
More information about the cfe-commits
mailing list