]> granicus.if.org Git - clang/commitdiff
clang-format: Improve braced-list detection.
authorDaniel Jasper <djasper@google.com>
Tue, 13 Dec 2016 10:05:03 +0000 (10:05 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 13 Dec 2016 10:05:03 +0000 (10:05 +0000)
Before:
  vector<int> v { 12 }
      GUARDED_BY(mutex);

After:
  vector<int> v{12} GUARDED_BY(mutex);

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289525 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp

index 52278d151e782bb4d3097c041b3713785111426b..84e06d05c739f851b796fa4fc6912c1d870b62f4 100644 (file)
@@ -360,8 +360,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
           // BlockKind later if we parse a braced list (where all blocks
           // inside are by default braced lists), or when we explicitly detect
           // blocks (for example while parsing lambdas).
-          //
-          // We exclude + and - as they can be ObjC visibility modifiers.
           ProbablyBracedList =
               (Style.Language == FormatStyle::LK_JavaScript &&
                NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
@@ -369,6 +367,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
               NextTok->isOneOf(tok::comma, tok::period, tok::colon,
                                tok::r_paren, tok::r_square, tok::l_brace,
                                tok::l_square, tok::l_paren, tok::ellipsis) ||
+              (NextTok->is(tok::identifier) &&
+               !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
               (NextTok->is(tok::semi) &&
                (!ExpectClassBody || LBraceStack.size() != 1)) ||
               (NextTok->isBinaryOperator() && !NextIsObjCMethod);
index fe2d470420748ddbacb00b9b6c484c7dc8d5b06c..5bed7a81de1907b18dbd0d3d2421b40fff56dab4 100644 (file)
@@ -6504,6 +6504,19 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
                "};");
   verifyFormat("#define A {a, a},");
 
+  // Cases where distinguising braced lists and blocks is hard.
+  verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
+  verifyFormat("void f() {\n"
+               "  return; // comment\n"
+               "}\n"
+               "SomeType t;");
+  verifyFormat("void f() {\n"
+               "  if (a) {\n"
+               "    f();\n"
+               "  }\n"
+               "}\n"
+               "SomeType t;");
+
   // In combination with BinPackArguments = false.
   FormatStyle NoBinPacking = getLLVMStyle();
   NoBinPacking.BinPackArguments = false;