diff options
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 7 | ||||
| -rw-r--r-- | clang/lib/Format/ContinuationIndenter.h | 2 | ||||
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 6 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 12 |
4 files changed, 22 insertions, 5 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index c2122ffb7d3..e63f72d65e6 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -136,7 +136,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { if (Style.AlwaysBreakBeforeMultilineStrings && State.Column > State.Stack.back().Indent && // Breaking saves columns. !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) && - Previous.Type != TT_InlineASMColon && NextIsMultilineString(State)) + Previous.Type != TT_InlineASMColon && nextIsMultilineString(State)) return true; if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) || Previous.Type == TT_ArrayInitializerLSquare) && @@ -865,12 +865,13 @@ unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const { return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0); } -bool ContinuationIndenter::NextIsMultilineString(const LineState &State) { +bool ContinuationIndenter::nextIsMultilineString(const LineState &State) { const FormatToken &Current = *State.NextToken; if (!Current.is(tok::string_literal)) return false; // We never consider raw string literals "multiline" for the purpose of - // AlwaysBreakBeforeMultilineStrings implementation. + // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased + // (see TokenAnnotator::mustBreakBefore(). if (Current.TokenText.startswith("R\"")) return false; if (Current.IsMultiline) diff --git a/clang/lib/Format/ContinuationIndenter.h b/clang/lib/Format/ContinuationIndenter.h index b3175658338..7eab6d5456f 100644 --- a/clang/lib/Format/ContinuationIndenter.h +++ b/clang/lib/Format/ContinuationIndenter.h @@ -115,7 +115,7 @@ private: /// /// This includes implicitly concatenated strings, strings that will be broken /// by clang-format and string literals with escaped newlines. - bool NextIsMultilineString(const LineState &State); + bool nextIsMultilineString(const LineState &State); FormatStyle Style; SourceManager &SourceMgr; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 37ad7336513..9132ba43770 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1412,6 +1412,12 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) { return Style.BreakBeforeBraces == FormatStyle::BS_Allman || Style.BreakBeforeBraces == FormatStyle::BS_GNU; + } else if (Right.is(tok::string_literal) && + Right.TokenText.startswith("R\"")) { + // Raw string literals are special wrt. line breaks. The author has made a + // deliberate choice and might have aligned the contents of the string + // literal accordingly. Thus, we try keep existing line breaks. + return Right.NewlinesBefore > 0; } return false; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 940f60df3c4..e34ebeb98cd 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -6187,7 +6187,7 @@ TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", - format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); + format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); EXPECT_EQ("fffffffffff(g(R\"x(\n" "multiline raw string literal xxxxxxxxxxxxxx\n" ")x\",\n" @@ -6223,6 +6223,16 @@ TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { "multiline raw string literal xxxxxxxxxxxxxx\n" ")x\" + bbbbbb);", getGoogleStyleWithColumns(20))); + EXPECT_EQ("fffffffffff(\n" + " R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" +\n" + " bbbbbb);", + format("fffffffffff(\n" + " R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" + bbbbbb);", + getGoogleStyleWithColumns(20))); } TEST_F(FormatTest, SkipsUnknownStringLiterals) { |

