]> granicus.if.org Git - clang/commitdiff
clang-format: Don't accidentally move tokens into preprocessor directive.
authorDaniel Jasper <djasper@google.com>
Tue, 17 Sep 2013 09:52:48 +0000 (09:52 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 17 Sep 2013 09:52:48 +0000 (09:52 +0000)
This fixes llvm.org/PR17265.

Before:
  Foo::Foo()
  #ifdef BAR
      : baz(0)
  #endif {
  }

After:
  Foo::Foo()
  #ifdef BAR
      : baz(0)
  #endif
  {
  }

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

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

index 58a1eeb5a6d0957fd90bb4f38d3ea780da10e047..0f9f57802452059dfbd69a3f19f1abc41f7f1f54 100644 (file)
@@ -1039,35 +1039,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
       Current->SpacesRequiredBefore =
           spaceRequiredBefore(Line, *Current) ? 1 : 0;
 
-    if (Current->is(tok::comment)) {
-      Current->MustBreakBefore = Current->NewlinesBefore > 0;
-    } else if (Current->Previous->isTrailingComment() ||
-               (Current->is(tok::string_literal) &&
-                Current->Previous->is(tok::string_literal))) {
-      Current->MustBreakBefore = true;
-    } else if (Current->Previous->IsUnterminatedLiteral) {
-      Current->MustBreakBefore = true;
-    } else if (Current->is(tok::lessless) && Current->Next &&
-               Current->Previous->is(tok::string_literal) &&
-               Current->Next->is(tok::string_literal)) {
-      Current->MustBreakBefore = true;
-    } else if (Current->Previous->ClosesTemplateDeclaration &&
-               Current->Previous->MatchingParen &&
-               Current->Previous->MatchingParen->BindingStrength == 1 &&
-               Style.AlwaysBreakTemplateDeclarations) {
-      // FIXME: Fix horrible hack of using BindingStrength to find top-level <>.
-      Current->MustBreakBefore = true;
-    } else if (Current->Type == TT_CtorInitializerComma &&
-               Style.BreakConstructorInitializersBeforeComma) {
-      Current->MustBreakBefore = true;
-    } else if (Current->Previous->BlockKind == BK_Block &&
-               Current->Previous->isNot(tok::r_brace) &&
-               Current->isNot(tok::r_brace)) {
-      Current->MustBreakBefore = true;
-    } else if (Current->is(tok::l_brace) && (Current->BlockKind == BK_Block)) {
-      Current->MustBreakBefore =
-          Style.BreakBeforeBraces == FormatStyle::BS_Allman;
-    }
+    Current->MustBreakBefore =
+        Current->MustBreakBefore || mustBreakBefore(Line, *Current);
+
     Current->CanBreakBefore =
         Current->MustBreakBefore || canBreakBefore(Line, *Current);
     if (Current->MustBreakBefore || !Current->Children.empty() ||
@@ -1357,6 +1331,39 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
   return spaceRequiredBetween(Line, *Tok.Previous, Tok);
 }
 
+bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
+                                     const FormatToken &Right) {
+  if (Right.is(tok::comment)) {
+    return Right.NewlinesBefore > 0;
+  } else if (Right.Previous->isTrailingComment() ||
+             (Right.is(tok::string_literal) &&
+              Right.Previous->is(tok::string_literal))) {
+    return true;
+  } else if (Right.Previous->IsUnterminatedLiteral) {
+    return true;
+  } else if (Right.is(tok::lessless) && Right.Next &&
+             Right.Previous->is(tok::string_literal) &&
+             Right.Next->is(tok::string_literal)) {
+    return true;
+  } else if (Right.Previous->ClosesTemplateDeclaration &&
+             Right.Previous->MatchingParen &&
+             Right.Previous->MatchingParen->BindingStrength == 1 &&
+             Style.AlwaysBreakTemplateDeclarations) {
+    // FIXME: Fix horrible hack of using BindingStrength to find top-level <>.
+    return true;
+  } else if (Right.Type == TT_CtorInitializerComma &&
+             Style.BreakConstructorInitializersBeforeComma) {
+    return true;
+  } else if (Right.Previous->BlockKind == BK_Block &&
+             Right.Previous->isNot(tok::r_brace) &&
+             Right.isNot(tok::r_brace)) {
+    return true;
+  } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
+    return Style.BreakBeforeBraces == FormatStyle::BS_Allman;
+  }
+  return false;
+}
+
 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
                                     const FormatToken &Right) {
   const FormatToken &Left = *Right.Previous;
index 06f335215d120823eaae3335bbcc024c84e57950..91f1b4678a14ace3d323dc3337c77e2f944cf136 100644 (file)
@@ -110,6 +110,8 @@ private:
 
   bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
 
+  bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
+
   bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
 
   void printDebugInfo(const AnnotatedLine &Line);
index c5d0171a710732a31b71768b24e77bc14a880760..fc13ad150ba0dcda2308f1ff29b5f4cadcc47963 100644 (file)
@@ -2226,6 +2226,12 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
                "        andMoreParameters),\n"
                "    trailing);",
                getLLVMStyleWithColumns(69));
+  verifyFormat("Foo::Foo()\n"
+               "#ifdef BAR\n"
+               "    : baz(0)\n"
+               "#endif\n"
+               "{\n"
+               "}");
 }
 
 TEST_F(FormatTest, LayoutBlockInsideParens) {