r237263 - clang-format: Improve nested block / lambda indentation when wrapping

Daniel Jasper djasper at google.com
Wed May 13 09:09:21 PDT 2015


Author: djasper
Date: Wed May 13 11:09:21 2015
New Revision: 237263

URL: http://llvm.org/viewvc/llvm-project?rev=237263&view=rev
Log:
clang-format: Improve nested block / lambda indentation when wrapping
before binary/ternary operators.

Basically, it doesn't seem right to indent a nested block aligned to a
binary or ternary operator.

Before:
  int i = aaaaaa ? 1  //
                 : [] {
                   return 2;  //
                 }();
  llvm::errs() << "number of twos is "
               << std::count_if(v.begin(), v.end(), [](int x) {
                 return x == 2;  // force break
               });

After:
  int i = aaaaaa ? 1  //
                 : [] {
                     return 2;  //
                   }();
  llvm::errs() << "number of twos is "
               << std::count_if(v.begin(), v.end(), [](int x) {
                    return x == 2;  // force break
                  });

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=237263&r1=237262&r2=237263&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed May 13 11:09:21 2015
@@ -667,6 +667,9 @@ unsigned ContinuationIndenter::moveState
       State.Stack.back().AvoidBinPacking = true;
     State.Stack.back().BreakBeforeParameter = false;
   }
+  if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
+    State.Stack.back().NestedBlockIndent =
+        State.Column + Current.ColumnWidth + 1;
 
   // Insert scopes created by fake parenthesis.
   const FormatToken *Previous = Current.getPreviousNonComment();

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=237263&r1=237262&r2=237263&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed May 13 11:09:21 2015
@@ -9992,6 +9992,14 @@ TEST_F(FormatTest, FormatsLambdas) {
   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
                "  return some_parameter.size();\n"
                "};");
+  verifyFormat("int i = aaaaaa ? 1 //\n"
+               "               : [] {\n"
+               "                   return 2; //\n"
+               "                 }();");
+  verifyFormat("llvm::errs() << \"number of twos is \"\n"
+               "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
+               "                  return x == 2; // force break\n"
+               "                });");
 
   // Lambdas with return types.
   verifyFormat("int c = []() -> int { return 2; }();\n");





More information about the cfe-commits mailing list