summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/ContinuationIndenter.cpp
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2017-10-16 09:08:53 +0000
committerKrasimir Georgiev <krasimir@google.com>2017-10-16 09:08:53 +0000
commit35599fdfa034fcc8506986ad3d83fc3bf581014c (patch)
tree1d71413e23349a26cc255bae37c49bb85d780b23 /clang/lib/Format/ContinuationIndenter.cpp
parentb583a7ead49d6a9ab8de2d2e8e17a8e8fe106ac1 (diff)
downloadbcm5719-llvm-35599fdfa034fcc8506986ad3d83fc3bf581014c.tar.gz
bcm5719-llvm-35599fdfa034fcc8506986ad3d83fc3bf581014c.zip
[clang-format] Break non-trailing comments, try 2
Summary: This patch enables `BreakableToken` to manage the formatting of non-trailing block comments. It is a refinement of https://reviews.llvm.org/D37007. We discovered that the optimizer outsmarts us on cases where breaking the comment costs considerably less than breaking after the comment. This patch addresses this by ensuring that a newline is inserted between a block comment and the next token. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D37695 llvm-svn: 315893
Diffstat (limited to 'clang/lib/Format/ContinuationIndenter.cpp')
-rw-r--r--clang/lib/Format/ContinuationIndenter.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index b438a3f64e7..b57b8de2e70 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -106,6 +106,7 @@ LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
/*AvoidBinPacking=*/false,
/*NoLineBreak=*/false));
State.LineContainsContinuedForLoopSection = false;
+ State.NoContinuation = false;
State.StartOfStringLiteral = 0;
State.StartOfLineLevel = 0;
State.LowestLevelOnLine = 0;
@@ -322,6 +323,12 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
Previous.TokenText == "\'\\n\'"))))
return true;
+ if (Previous.is(TT_BlockComment) && Previous.IsMultiline)
+ return true;
+
+ if (State.NoContinuation)
+ return true;
+
return false;
}
@@ -331,6 +338,8 @@ unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
const FormatToken &Current = *State.NextToken;
assert(!State.Stack.empty());
+ State.NoContinuation = false;
+
if ((Current.is(TT_ImplicitStringLiteral) &&
(Current.Previous->Tok.getIdentifierInfo() == nullptr ||
Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
@@ -1286,7 +1295,7 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
return 0;
}
} else if (Current.is(TT_BlockComment)) {
- if (!Current.isTrailingComment() || !Style.ReflowComments ||
+ if (!Style.ReflowComments ||
// If a comment token switches formatting, like
// /* clang-format on */, we don't want to break it further,
// but we may still want to adjust its indentation.
@@ -1332,6 +1341,7 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
ReflowInProgress = SplitBefore.first != StringRef::npos;
TailOffset =
ReflowInProgress ? (SplitBefore.first + SplitBefore.second) : 0;
+ BreakInserted = BreakInserted || Token->introducesBreakBefore(LineIndex);
if (!DryRun)
Token->replaceWhitespaceBefore(LineIndex, RemainingTokenColumns,
RemainingSpace, SplitBefore, Whitespaces);
@@ -1408,6 +1418,9 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
State.Stack[i].BreakBeforeParameter = true;
}
+ if (Current.is(TT_BlockComment))
+ State.NoContinuation = true;
+
Penalty += Current.isStringLiteral() ? Style.PenaltyBreakString
: Style.PenaltyBreakComment;
OpenPOWER on IntegriCloud