clang-format: Fix crasher caused by not properly setting dry-run.
authorDaniel Jasper <djasper@google.com>
Thu, 29 Jan 2015 10:47:14 +0000 (10:47 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 29 Jan 2015 10:47:14 +0000 (10:47 +0000)
llvm-svn: 227427

clang/lib/Format/UnwrappedLineFormatter.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp

index 6c4aadc..4844b66 100644 (file)
@@ -409,7 +409,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
           TheLine.Type == LT_ImportStatement) {
         LineState State = Indenter->getInitialState(Indent, &TheLine, DryRun);
         while (State.NextToken) {
-          formatChildren(State, /*Newline=*/false, /*DryRun=*/false, Penalty);
+          formatChildren(State, /*Newline=*/false, DryRun, Penalty);
           Indenter->addTokenToState(State, /*Newline=*/false, DryRun);
         }
       } else if (Style.ColumnLimit == 0) {
@@ -657,8 +657,8 @@ void UnwrappedLineFormatter::addNextStateToQueue(unsigned Penalty,
 
 bool UnwrappedLineFormatter::formatChildren(LineState &State, bool NewLine,
                                             bool DryRun, unsigned &Penalty) {
-  FormatToken &Previous = *State.NextToken->Previous;
   const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
+  FormatToken &Previous = *State.NextToken->Previous;
   if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->BlockKind != BK_Block ||
       Previous.Children.size() == 0)
     // The previous token does not open a block. Nothing to do. We don't
index bf1207e..44e5f69 100644 (file)
@@ -264,6 +264,11 @@ void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
 void WhitespaceManager::generateChanges() {
   for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
     const Change &C = Changes[i];
+    if (i > 0) {
+      assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
+                 C.OriginalWhitespaceRange.getBegin() &&
+             "Generating two replacements for the same location");
+    }
     if (C.CreateReplacement) {
       std::string ReplacementText = C.PreviousLinePostfix;
       if (C.ContinuesPPDirective)
index 65a7bff..f099f20 100644 (file)
@@ -3081,6 +3081,8 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
                "  if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
                "}, a);",
                Style);
+
+  verifyNoCrash("^{v^{a}}");
 }
 
 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {