]> granicus.if.org Git - clang/commitdiff
clang-format: Fix corner cases for comments in if conditions.
authorDaniel Jasper <djasper@google.com>
Wed, 7 May 2014 09:23:05 +0000 (09:23 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 7 May 2014 09:23:05 +0000 (09:23 +0000)
Before:
  if ( // a
          x + 3) { ..

After:
  if ( // a
      x + 3) { ..

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

lib/Format/ContinuationIndenter.cpp
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index f18984bb15bd6a19667f8f404dca14584fbf02ec..d225bce8bfb6a20695080d0ca65c84f45b7eddd5 100644 (file)
@@ -290,10 +290,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
     State.Stack.back().ContainsUnwrappedBuilder = true;
 
   State.Column += Spaces;
-  if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
+  if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
+      Previous.Previous && Previous.Previous->isOneOf(tok::kw_if, tok::kw_for))
     // Treat the condition inside an if as if it was a second function
     // parameter, i.e. let nested calls have a continuation indent.
-    State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
+    State.Stack.back().LastSpace = State.Column;
   else if (Current.isNot(tok::comment) &&
            (Previous.is(tok::comma) ||
             (Previous.is(tok::colon) && Previous.Type == TT_ObjCMethodExpr)))
index a41992679ad6a6451d2eeb8db4f576bf2222c34e..21cec905a8e4c24b0a9f2aa47750bc60a231eb7e 100644 (file)
@@ -1667,7 +1667,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
     return false;
   if (Left.is(tok::l_paren) && Left.Previous &&
       (Left.Previous->Type == TT_BinaryOperator ||
-       Left.Previous->Type == TT_CastRParen))
+       Left.Previous->Type == TT_CastRParen || Left.Previous->is(tok::kw_if)))
     return false;
   if (Right.Type == TT_ImplicitStringLiteral)
     return false;
index bd7e23f60b86b899085aefefbe8960a34c2d7fa4..75f1854666cd3e45d77566d5c6a032f9a6e6060e 100644 (file)
@@ -390,6 +390,11 @@ TEST_F(FormatTest, ElseIf) {
                "else {\n"
                "  g()\n"
                "}");
+
+  verifyFormat("if (a) {\n"
+               "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+               "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
+               "}");
 }
 
 TEST_F(FormatTest, FormatsForLoop) {
@@ -847,6 +852,18 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
       getLLVMStyleWithColumns(61));
+
+  verifyFormat("if ( // This is some comment\n"
+               "    x + 3) {\n"
+               "}");
+  EXPECT_EQ("if ( // This is some comment\n"
+            "     // spanning two lines\n"
+            "    x + 3) {\n"
+            "}",
+            format("if( // This is some comment\n"
+                   "     // spanning two lines\n"
+                   " x + 3) {\n"
+                   "}"));
 }
 
 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {