clang-format: Fix comment formatting bugs in nested blocks.
This fixes two issues:
1) The indent of a line comment was not adapted to the subsequent
statement as it would be outside of a nested block.
2) A missing DryRun flag caused actualy breaks to be inserted in
overly long comments while trying to come up with the best line
breaking decisions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190123 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 46baab4..2f3be55 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -974,9 +974,26 @@
} // end anonymous namespace
+void
+TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
+ if (Lines.empty())
+ return;
+
+ const AnnotatedLine *NextNonCommentLine = NULL;
+ for (unsigned i = Lines.size() - 1; i > 0; --i) {
+ if (NextNonCommentLine && Lines[i]->First->is(tok::comment) &&
+ !Lines[i]->First->Next)
+ Lines[i]->Level = NextNonCommentLine->Level;
+ else
+ NextNonCommentLine =
+ Lines[i]->First->isNot(tok::r_brace) ? Lines[i] : NULL;
+ }
+}
+
void TokenAnnotator::annotate(AnnotatedLine &Line) {
- for (std::vector<AnnotatedLine *>::iterator I = Line.Children.begin(),
- E = Line.Children.end();
+ setCommentLineLevels(Line.Children);
+ for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
+ E = Line.Children.end();
I != E; ++I) {
annotate(**I);
}
@@ -1056,8 +1073,8 @@
DEBUG({ printDebugInfo(Line); });
- for (std::vector<AnnotatedLine *>::iterator I = Line.Children.begin(),
- E = Line.Children.end();
+ for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
+ E = Line.Children.end();
I != E; ++I) {
calculateFormattingInformation(**I);
}