From: Daniel Jasper Date: Fri, 6 Mar 2015 10:57:12 +0000 (+0000) Subject: clang-format: Slightly change indentation rules in for loops. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=36d0c630ba37b418a2b4244a1bbff413a5388e92;p=clang clang-format: Slightly change indentation rules in for loops. There was already a TODO to double-check whether the extra indenation makes sense. A slightly different case reveals that it is actively harmful: for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc; ++i) { } Here (and it is probably not a totally infrequent case, it just works out that "i < " is four spaces and so the four space extra indentation makes the operator precedence confusing. So, this will now instead be formatted as: for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc; ++i) { } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@231461 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 7c2d916ac5..17a007f8b2 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -722,7 +722,8 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State, // 'return', assignments or opening <({[. The indentation for these cases // is special cased. bool SkipFirstExtraIndent = - (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) || + (Previous && (Previous->opensScope() || + Previous->isOneOf(tok::semi, tok::kw_return) || (Previous->getPrecedence() == prec::Assignment && Style.AlignOperands) || Previous->is(TT_ObjCMethodExpr))); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index d49250391c..fcfe9778f3 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -529,13 +529,15 @@ TEST_F(FormatTest, FormatsForLoop) { " E = FD->getDeclsInPrototypeScope().end();\n" " I != E; ++I) {\n}"); - // FIXME: Not sure whether we want extra identation in line 3 here: verifyFormat( "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" " ++aaaaaaaaaaa) {\n}"); + verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" + " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" + " ++i) {\n}"); verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" "}");