From 9acb8b4355028887e8cc4aa8f683aceee021a62b Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 6 Jun 2013 09:11:58 +0000 Subject: [PATCH] Fix clang-format's expression parser for leading }s. The leading "}" in the construct "} else if (..) {" was confusing the expression parser. Thus, no fake parentheses were generated and the indentation was broken in some cases. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183393 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 6 +++++- unittests/Format/FormatTest.cpp | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index fc53681394..71a1a28c85 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -773,7 +773,11 @@ private: /// operator precedence. class ExpressionParser { public: - ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {} + ExpressionParser(AnnotatedLine &Line) : Current(Line.First) { + // Skip leading "}", e.g. in "} else if (...) {". + if (Current->is(tok::r_brace)) + next(); + } /// \brief Parse expressions with the given operatore precedence. void parse(int Precedence = 0) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 1c2a540409..19f702f06a 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2073,6 +2073,10 @@ TEST_F(FormatTest, ExpressionIndentation) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); + verifyFormat("if () {\n" + "} else if (aaaaa && bbbbb > // break\n" + " ccccc) {\n" + "}"); } TEST_F(FormatTest, ConstructorInitializers) { -- 2.40.0