diff options
author | Krasimir Georgiev <krasimir@google.com> | 2018-01-23 11:26:19 +0000 |
---|---|---|
committer | Krasimir Georgiev <krasimir@google.com> | 2018-01-23 11:26:19 +0000 |
commit | 55c23a10c29d352a20f9dc27cff9056706e248af (patch) | |
tree | ff4a36030962994dd8dad890a3ff505481b65f04 /clang | |
parent | 27f77b4300a9060579a14a190ae1fe123468a1f1 (diff) | |
download | bcm5719-llvm-55c23a10c29d352a20f9dc27cff9056706e248af.tar.gz bcm5719-llvm-55c23a10c29d352a20f9dc27cff9056706e248af.zip |
[clang-format] Ignore UnbreakableTailLength sometimes during breaking
Summary:
This patch fixes an issue where the UnbreakableTailLength would be counted towards
the length of a token during breaking, even though we can break after the token.
For example, this proto text with column limit 20
```
# ColumnLimit: 20 V
foo: {
bar: {
bazoo: "aaaaaaa"
}
}
```
was broken:
```
# ColumnLimit: 20 V
foo: {
bar: {
bazoo:
"aaaaaaa"
}
}
```
because the 2 closing `}` were counted towards the string literal's `UnbreakableTailLength`.
Reviewers: djasper
Reviewed By: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D42376
llvm-svn: 323188
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Format/BreakableToken.cpp | 6 | ||||
-rw-r--r-- | clang/lib/Format/BreakableToken.h | 4 | ||||
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 11 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestTextProto.cpp | 12 |
4 files changed, 26 insertions, 7 deletions
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index 4735ab3564f..72d2078797e 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -214,11 +214,11 @@ unsigned BreakableStringLiteral::getContentStartColumn(unsigned LineIndex, BreakableStringLiteral::BreakableStringLiteral( const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, - StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding, - const FormatStyle &Style) + StringRef Postfix, unsigned UnbreakableTailLength, bool InPPDirective, + encoding::Encoding Encoding, const FormatStyle &Style) : BreakableToken(Tok, InPPDirective, Encoding, Style), StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix), - UnbreakableTailLength(Tok.UnbreakableTailLength) { + UnbreakableTailLength(UnbreakableTailLength) { assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix)); Line = Tok.TokenText.substr( Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); diff --git a/clang/lib/Format/BreakableToken.h b/clang/lib/Format/BreakableToken.h index 8ef26ef464d..eba48f75e88 100644 --- a/clang/lib/Format/BreakableToken.h +++ b/clang/lib/Format/BreakableToken.h @@ -238,8 +238,8 @@ public: /// after formatting. BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, StringRef Postfix, - bool InPPDirective, encoding::Encoding Encoding, - const FormatStyle &Style); + unsigned UnbreakableTailLength, bool InPPDirective, + encoding::Encoding Encoding, const FormatStyle &Style); Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit, unsigned ReflowColumn, diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index f7472bcd083..3711ee01c48 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1576,9 +1576,16 @@ std::unique_ptr<BreakableToken> ContinuationIndenter::createBreakableToken( Text.startswith(Prefix = "u8\"") || Text.startswith(Prefix = "L\""))) || (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) { + // We need this to address the case where there is an unbreakable tail + // only if certain other formatting decisions have been taken. The + // UnbreakableTailLength of Current is an overapproximation is that case + // and we need to be correct here. + unsigned UnbreakableTailLength = (State.NextToken && canBreak(State)) + ? 0 + : Current.UnbreakableTailLength; return llvm::make_unique<BreakableStringLiteral>( - Current, StartColumn, Prefix, Postfix, State.Line->InPPDirective, - Encoding, Style); + Current, StartColumn, Prefix, Postfix, UnbreakableTailLength, + State.Line->InPPDirective, Encoding, Style); } } else if (Current.is(TT_BlockComment)) { if (!Style.ReflowComments || diff --git a/clang/unittests/Format/FormatTestTextProto.cpp b/clang/unittests/Format/FormatTestTextProto.cpp index 0a7bcdd8236..82da8737a37 100644 --- a/clang/unittests/Format/FormatTestTextProto.cpp +++ b/clang/unittests/Format/FormatTestTextProto.cpp @@ -290,5 +290,17 @@ TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) { " product_data <product {1}>\n" ">"); } + +TEST_F(FormatTestTextProto, DiscardsUnbreakableTailIfCanBreakAfter) { + // The two closing braces count towards the string UnbreakableTailLength, but + // since we have broken after the corresponding opening braces, we don't + // consider that length for string breaking. + verifyFormat( + "foo: {\n" + " bar: {\n" + " text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" + " }\n" + "}"); +} } // end namespace tooling } // end namespace clang |