diff options
| author | Daniel Jasper <djasper@google.com> | 2014-05-22 08:36:53 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2014-05-22 08:36:53 +0000 |
| commit | 5f3ea477cf3b48710a5ef839624606311f0c4ab7 (patch) | |
| tree | cc197a160a9f1eabd3b911f487212cf6cdd04829 /clang/lib/Format/TokenAnnotator.cpp | |
| parent | 0f6272271e77b3b2b6325141c9798aed6ac966b8 (diff) | |
| download | bcm5719-llvm-5f3ea477cf3b48710a5ef839624606311f0c4ab7.tar.gz bcm5719-llvm-5f3ea477cf3b48710a5ef839624606311f0c4ab7.zip | |
clang-format: Correctly calculate line lenghts for nest blocks.
If simple (one-statement) blocks can be inlined, the length needs to be
calculated correctly.
Before (in JavaScript but this also affects lambdas, etc.):
var x = {
valueOf: function() { return 1; }
};
After:
var x = {valueOf: function() { return 1; }};
llvm-svn: 209410
Diffstat (limited to 'clang/lib/Format/TokenAnnotator.cpp')
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 47ecd6a4dc3..dca20d2d75d 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1178,6 +1178,12 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { } void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { + for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), + E = Line.Children.end(); + I != E; ++I) { + calculateFormattingInformation(**I); + } + Line.First->TotalLength = Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth; if (!Line.First->Next) @@ -1222,12 +1228,18 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { Current->CanBreakBefore = Current->MustBreakBefore || canBreakBefore(Line, *Current); - if (Current->MustBreakBefore || !Current->Children.empty() || + unsigned ChildSize = 0; + if (Current->Previous->Children.size() == 1) { + FormatToken &LastOfChild = *Current->Previous->Children[0]->Last; + ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit + : LastOfChild.TotalLength + 1; + } + if (Current->MustBreakBefore || Current->Previous->Children.size() > 1 || Current->IsMultiline) Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit; else Current->TotalLength = Current->Previous->TotalLength + - Current->ColumnWidth + + Current->ColumnWidth + ChildSize + Current->SpacesRequiredBefore; if (Current->Type == TT_CtorInitializerColon) @@ -1249,12 +1261,6 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { } DEBUG({ printDebugInfo(Line); }); - - for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), - E = Line.Children.end(); - I != E; ++I) { - calculateFormattingInformation(**I); - } } void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) { |

