summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2013-06-07 16:02:52 +0000
committerAlexander Kornienko <alexfh@google.com>2013-06-07 16:02:52 +0000
commitdd7ece53a2958ab3910007f6521bbb078b33505c (patch)
tree8b16bfa4d9034dc88a102d1683c1ef6d1f937cba /clang/lib/Format/Format.cpp
parentcead69d4a3f80fa8684f5636a9469d043f4f7285 (diff)
downloadbcm5719-llvm-dd7ece53a2958ab3910007f6521bbb078b33505c.tar.gz
bcm5719-llvm-dd7ece53a2958ab3910007f6521bbb078b33505c.zip
Fixed calculation of penalty when breaking tokens.
Summary: Introduced two new style parameters: PenaltyBreakComment and PenaltyBreakString. Add penalty for each character of a breakable token beyond the column limit (this relates mainly to comments, as they are broken only on whitespace). Tuned PenaltyBreakComment to prefer comment breaking over breaking inside most binary expressions. Fixed a bug that prevented *, & and && from being considered TT_BinaryOperator in the presense of adjacent comments. Reviewers: klimek, djasper Reviewed By: klimek CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D933 llvm-svn: 183530
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r--clang/lib/Format/Format.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 9dd5e4a0f21..f61f1fbb3fb 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -96,6 +96,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
IO.mapOptional("ObjCSpaceBeforeProtocolList",
Style.ObjCSpaceBeforeProtocolList);
+ IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
+ IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
Style.PenaltyReturnTypeOnItsOwnLine);
@@ -130,6 +132,8 @@ FormatStyle getLLVMStyle() {
LLVMStyle.IndentCaseLabels = false;
LLVMStyle.MaxEmptyLinesToKeep = 1;
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
+ LLVMStyle.PenaltyBreakComment = 45;
+ LLVMStyle.PenaltyBreakString = 1000;
LLVMStyle.PenaltyExcessCharacter = 1000000;
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 75;
LLVMStyle.PointerBindsToType = false;
@@ -157,6 +161,8 @@ FormatStyle getGoogleStyle() {
GoogleStyle.IndentCaseLabels = true;
GoogleStyle.MaxEmptyLinesToKeep = 1;
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
+ GoogleStyle.PenaltyBreakComment = 45;
+ GoogleStyle.PenaltyBreakString = 1000;
GoogleStyle.PenaltyExcessCharacter = 1000000;
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
GoogleStyle.PointerBindsToType = true;
@@ -840,8 +846,8 @@ private:
Whitespaces);
}
unsigned TailOffset = 0;
- unsigned RemainingTokenColumns =
- Token->getLineLengthAfterSplit(LineIndex, TailOffset);
+ unsigned RemainingTokenColumns = Token->getLineLengthAfterSplit(
+ LineIndex, TailOffset, StringRef::npos);
while (RemainingTokenColumns > RemainingSpace) {
BreakableToken::Split Split =
Token->getSplit(LineIndex, TailOffset, getColumnLimit());
@@ -849,15 +855,23 @@ private:
break;
assert(Split.first != 0);
unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
- LineIndex, TailOffset + Split.first + Split.second);
+ LineIndex, TailOffset + Split.first + Split.second,
+ StringRef::npos);
assert(NewRemainingTokenColumns < RemainingTokenColumns);
if (!DryRun) {
Token->insertBreak(LineIndex, TailOffset, Split, Line.InPPDirective,
Whitespaces);
}
+ Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
+ : Style.PenaltyBreakComment;
+ unsigned ColumnsUsed =
+ Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
+ if (ColumnsUsed > getColumnLimit()) {
+ Penalty +=
+ Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit());
+ }
TailOffset += Split.first + Split.second;
RemainingTokenColumns = NewRemainingTokenColumns;
- Penalty += Style.PenaltyExcessCharacter;
BreakInserted = true;
}
PositionAfterLastLineInToken = RemainingTokenColumns;
OpenPOWER on IntegriCloud