From dc837b1722d8e071d9ec0434aea487dbdfd57853 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 30 Oct 2013 14:04:10 +0000 Subject: [PATCH] clang-format: Fix indenting corner case with comment and else. Before: if (a) { f(); } // or else .. else { g(); } After: if (a) { f(); } // or else .. else { g(); } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193684 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 3 ++- unittests/Format/FormatTest.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index c1f448be56..74cfbf0e90 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -391,7 +391,8 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, State.Column = State.Stack.back().Indent; // Ensure that we fall back to the continuation indent width instead of just // flushing continuations left. - if (State.Column == State.FirstIndent) + if (State.Column == State.FirstIndent && + PreviousNonComment->isNot(tok::r_brace)) State.Column += Style.ContinuationIndentWidth; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 639b8c89d9..5852d1c0ed 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -374,6 +374,13 @@ TEST_F(FormatTest, ElseIf) { " g();\n" "else\n" " h();"); + verifyFormat("if (a) {\n" + " f();\n" + "}\n" + "// or else ..\n" + "else {\n" + " g()\n" + "}"); } TEST_F(FormatTest, FormatsForLoop) { -- 2.40.0