summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2014-05-22 08:36:53 +0000
committerDaniel Jasper <djasper@google.com>2014-05-22 08:36:53 +0000
commit5f3ea477cf3b48710a5ef839624606311f0c4ab7 (patch)
treecc197a160a9f1eabd3b911f487212cf6cdd04829 /clang/lib/Format
parent0f6272271e77b3b2b6325141c9798aed6ac966b8 (diff)
downloadbcm5719-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')
-rw-r--r--clang/lib/Format/Format.cpp4
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp22
2 files changed, 17 insertions, 9 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 891a7188bba..03177e586ad 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -828,8 +828,10 @@ public:
if (TheLine.Last->TotalLength + Indent <= ColumnLimit) {
LineState State = Indenter->getInitialState(Indent, &TheLine, DryRun);
- while (State.NextToken)
+ while (State.NextToken) {
+ formatChildren(State, /*Newline=*/false, /*DryRun=*/false, Penalty);
Indenter->addTokenToState(State, /*Newline=*/false, DryRun);
+ }
} else if (Style.ColumnLimit == 0) {
// FIXME: Implement nested blocks for ColumnLimit = 0.
NoColumnLimitFormatter Formatter(Indenter);
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) {
OpenPOWER on IntegriCloud