Skip to content

[clang-format] Break after string literals with trailing line breaks #76795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5151,6 +5151,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
if (Left.IsUnterminatedLiteral)
return true;
// FIXME: Breaking after newlines seems useful in general. Turn this into an
// option and recognize more cases like endl etc, and break independent of
// what comes after operator lessless.
if (Right.is(tok::lessless) && Right.Next &&
Right.Next->is(tok::string_literal) && Left.is(tok::string_literal) &&
Left.TokenText.ends_with("\\n\"")) {
Comment on lines +5158 to +5159
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also handle \n and endl now. For example:

Suggested change
Right.Next->is(tok::string_literal) && Left.is(tok::string_literal) &&
Left.TokenText.ends_with("\\n\"")) {
Right.Next->is(tok::string_literal) &&
((Left.is(tok::string_literal) && Left.TokenText.ends_with("\\n\"")) ||
Left.TokenText == "'\\n'" || Left.TokenText == "endl")) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also handle \n and endl now. For example:

yes, but that'll be "new" behavior, and even if we do that, i'd rather do that in a separate patch that can be reviewed/reverted on it's own.

as mentioned, this is mostly to restore some of the "incidental" behavior that was relied on by codebases in the wild. so that changes in the new clang-format release will be minimal.

return true;
}
if (Right.is(TT_RequiresClause)) {
switch (Style.RequiresClausePosition) {
case FormatStyle::RCPS_OwnLine:
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26708,6 +26708,8 @@ TEST_F(FormatTest, PPBranchesInBracedInit) {

TEST_F(FormatTest, StreamOutputOperator) {
verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
verifyFormat("std::cout << \"foo\\n\"\n"
" << \"bar\";");
}

TEST_F(FormatTest, BreakAdjacentStringLiterals) {
Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,15 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
}

TEST_F(TokenAnnotatorTest, StreamOperator) {
auto Tokens = annotate("\"foo\\n\" << aux << \"foo\\n\" << \"foo\";");
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_FALSE(Tokens[1]->MustBreakBefore);
EXPECT_FALSE(Tokens[3]->MustBreakBefore);
// Only break between string literals if the former ends with \n.
EXPECT_TRUE(Tokens[5]->MustBreakBefore);
}

} // namespace
} // namespace format
} // namespace clang