]> granicus.if.org Git - clang/commitdiff
Fix clang-format's expression parser for leading }s.
authorDaniel Jasper <djasper@google.com>
Thu, 6 Jun 2013 09:11:58 +0000 (09:11 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 6 Jun 2013 09:11:58 +0000 (09:11 +0000)
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
unittests/Format/FormatTest.cpp

index fc53681394e68f8daf58dc755c5e560f93e48847..71a1a28c85fda6be2ffda5178d9ed9b8adfe08f6 100644 (file)
@@ -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) {
index 1c2a5404091a836782f6c1678196d43abdcde3e8..19f702f06a67eef2b16c97a5112e7fb26244b31a 100644 (file)
@@ -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) {