summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-11-01 00:27:35 +0000
committerDaniel Jasper <djasper@google.com>2015-11-01 00:27:35 +0000
commitf67c32466d782824c67a3f31196eb31d22faa7fa (patch)
treef75ff2d76d478140aa2a1e8c4ba7bfdda4e0a7b2 /clang/lib/Format
parent2f5e8874f3a973eb9aec8fd92e7f1cbbc7c00494 (diff)
downloadbcm5719-llvm-f67c32466d782824c67a3f31196eb31d22faa7fa.tar.gz
bcm5719-llvm-f67c32466d782824c67a3f31196eb31d22faa7fa.zip
clang-format: Be slightly more cautious when formatting subsequent lines after a change. With r251474, clang-format could indent the entire rest of the file, if there is a missing closing brace, e.g. while writing code in an editor.
Summary: With this change, clang-format stops formatting when either it leaves the current scope or when it comes back to the initial scope after going into a nested one. Reviewers: klimek Subscribers: cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D14213 llvm-svn: 251760
Diffstat (limited to 'clang/lib/Format')
-rw-r--r--clang/lib/Format/UnwrappedLineFormatter.cpp23
1 files changed, 19 insertions, 4 deletions
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index fcf45c9286f..5b4f5d5b09c 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -812,13 +812,28 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
AdditionalIndent);
const AnnotatedLine *PreviousLine = nullptr;
const AnnotatedLine *NextLine = nullptr;
- bool PreviousLineFormatted = false;
+
+ // The minimum level of consecutive lines that have been formatted.
+ unsigned RangeMinLevel = UINT_MAX;
+ // The level of the previous line.
+ unsigned PreviousLineLevel = Lines.front()->Level;
+
for (const AnnotatedLine *Line =
Joiner.getNextMergedLine(DryRun, IndentTracker);
Line; Line = NextLine) {
const AnnotatedLine &TheLine = *Line;
unsigned Indent = IndentTracker.getIndent();
- bool FixIndentation = (FixBadIndentation || PreviousLineFormatted) &&
+
+ // We continue formatting unchanged lines to adjust their indent, e.g. if a
+ // scope was added. However, we need to carefully stop doing this when we
+ // exit the scope of affected lines to prevent indenting a the entire
+ // remaining file if it currently missing a closing brace.
+ bool ContinueFormatting =
+ TheLine.Level > RangeMinLevel ||
+ (TheLine.Level == RangeMinLevel && PreviousLineLevel <= TheLine.Level);
+ PreviousLineLevel = TheLine.Level;
+
+ bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
Indent != TheLine.First->OriginalColumn;
bool ShouldFormat = TheLine.Affected || FixIndentation;
// We cannot format this line; if the reason is that the line had a
@@ -846,7 +861,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
else
Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
.formatLine(TheLine, Indent, DryRun);
- PreviousLineFormatted = true;
+ RangeMinLevel = std::min(RangeMinLevel, TheLine.Level);
} else {
// If no token in the current line is affected, we still need to format
// affected children.
@@ -877,7 +892,7 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
}
NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
- PreviousLineFormatted = false;
+ RangeMinLevel = UINT_MAX;
}
if (!DryRun)
markFinalized(TheLine.First);
OpenPOWER on IntegriCloud