diff options
author | Daniel Jasper <djasper@google.com> | 2013-01-09 10:16:05 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-01-09 10:16:05 +0000 |
commit | ceb99ab9d721848c0851c59f3fdd9c1ca85c58be (patch) | |
tree | b8c27a7b113012584050895e50454ddd113e47d6 | |
parent | d64f73812f3e6473ef8b6b6ec909bb9d8a510843 (diff) |
Don't simply give up when exceeding 80cols, choose an "ok" option.
This addresses llvm.org/PR14847.
We can now format something like:
int aaaaaaaaaaaaaaaaaaaaaaaaaaa =
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
clang-format unavoidably exceeds the column limit, but does not just
flush everything into a single line. Moreover, it tries to minimize the
number of characters beyond the column limit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171964 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Format/Format.cpp | 14 | ||||
-rw-r--r-- | unittests/Format/FormatTest.cpp | 9 |
2 files changed, 20 insertions, 3 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index fbcb228fbe..53dc97abec 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -116,6 +116,7 @@ FormatStyle getGoogleStyle() { struct OptimizationParameters { unsigned PenaltyIndentLevel; unsigned PenaltyLevelDecrease; + unsigned PenaltyExcessCharacter; }; class UnwrappedLineFormatter { @@ -131,6 +132,7 @@ public: Replaces(Replaces), StructuralError(StructuralError) { Parameters.PenaltyIndentLevel = 15; Parameters.PenaltyLevelDecrease = 30; + Parameters.PenaltyExcessCharacter = 1000000; } /// \brief Formats an \c UnwrappedLine. @@ -419,6 +421,10 @@ private: return 3; } + unsigned getColumnLimit() { + return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0); + } + /// \brief Calculate the number of lines needed to format the remaining part /// of the unwrapped line. /// @@ -454,9 +460,11 @@ private: addTokenToState(NewLine, true, State); - // Exceeding column limit is bad. - if (State.Column > Style.ColumnLimit - (Line.InPPDirective ? 1 : 0)) - return UINT_MAX; + // Exceeding column limit is bad, assign penalty. + if (State.Column > getColumnLimit()) { + unsigned ExcessCharacters = State.Column - getColumnLimit(); + CurrentPenalty += Parameters.PenaltyExcessCharacter * ExcessCharacters; + } if (StopAt <= CurrentPenalty) return UINT_MAX; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 56d4e17468..495fc7fee1 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1026,6 +1026,15 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { // Error recovery tests. //===----------------------------------------------------------------------===// +TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { + verifyFormat("int aaaaaaaa =\n" + " // Overly long comment\n" + " b;", getLLVMStyleWithColumns(20)); + verifyFormat("function(\n" + " ShortArgument,\n" + " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); +} + TEST_F(FormatTest, IncorrectAccessSpecifier) { verifyFormat("public:"); verifyFormat("class A {\n" |