Added AlwaysBreakBeforeMultilineStrings option.

Summary:
Always breaking before multiline strings can help format complex
expressions containing multiline strings more consistently, and avoid consuming
too much horizontal space.

Reviewers: djasper

Reviewed By: djasper

CC: cfe-commits, klimek

Differential Revision: http://llvm-reviews.chandlerc.com/D1097

llvm-svn: 185622
This commit is contained in:
Alexander Kornienko 2013-07-04 12:02:44 +00:00
parent d0c91acb58
commit 5861171893
4 changed files with 44 additions and 0 deletions

View File

@ -110,6 +110,9 @@ struct FormatStyle {
/// declaration.
bool AlwaysBreakTemplateDeclarations;
/// \brief If \c true, always break before multiline string literals.
bool AlwaysBreakBeforeMultilineStrings;
/// \brief If true, \c IndentWidth consecutive spaces will be replaced with
/// tab characters.
bool UseTab;
@ -144,6 +147,8 @@ struct FormatStyle {
R.AllowShortIfStatementsOnASingleLine &&
AlwaysBreakTemplateDeclarations ==
R.AlwaysBreakTemplateDeclarations &&
AlwaysBreakBeforeMultilineStrings ==
R.AlwaysBreakBeforeMultilineStrings &&
BinPackParameters == R.BinPackParameters &&
BreakBeforeBraces == R.BreakBeforeBraces &&
ColumnLimit == R.ColumnLimit &&

View File

@ -87,6 +87,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
Style.AllowShortLoopsOnASingleLine);
IO.mapOptional("AlwaysBreakTemplateDeclarations",
Style.AlwaysBreakTemplateDeclarations);
IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
Style.AlwaysBreakBeforeMultilineStrings);
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
IO.mapOptional("ColumnLimit", Style.ColumnLimit);
IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
@ -127,6 +129,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
LLVMStyle.AllowShortLoopsOnASingleLine = false;
LLVMStyle.AlwaysBreakTemplateDeclarations = false;
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
LLVMStyle.BinPackParameters = true;
LLVMStyle.ColumnLimit = 80;
LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
@ -157,6 +160,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
GoogleStyle.AllowShortLoopsOnASingleLine = true;
GoogleStyle.AlwaysBreakTemplateDeclarations = true;
GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
GoogleStyle.BinPackParameters = true;
GoogleStyle.ColumnLimit = 80;
GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;

View File

@ -936,6 +936,13 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
} else if (Current->Previous->ClosesTemplateDeclaration &&
Style.AlwaysBreakTemplateDeclarations) {
Current->MustBreakBefore = true;
} else if (Style.AlwaysBreakBeforeMultilineStrings &&
Current->is(tok::string_literal) &&
Current->Previous->isNot(tok::lessless) &&
Current->Previous->Type != TT_InlineASMColon &&
Current->getNextNoneComment() &&
Current->getNextNoneComment()->is(tok::string_literal)) {
Current->MustBreakBefore = true;
} else {
Current->MustBreakBefore = false;
}

View File

@ -2878,6 +2878,34 @@ TEST_F(FormatTest, AlignsStringLiterals) {
" \"jkl\");");
}
TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
FormatStyle NoBreak = getLLVMStyle();
NoBreak.AlwaysBreakBeforeMultilineStrings = false;
FormatStyle Break = getLLVMStyle();
Break.AlwaysBreakBeforeMultilineStrings = true;
EXPECT_EQ("aaaa = \"bbbb\"\n"
" \"cccc\";",
format("aaaa=\"bbbb\" \"cccc\";", NoBreak));
EXPECT_EQ("aaaa =\n"
" \"bbbb\"\n"
" \"cccc\";",
format("aaaa=\"bbbb\" \"cccc\";", Break));
EXPECT_EQ("aaaa(\"bbbb\"\n"
" \"cccc\");",
format("aaaa(\"bbbb\" \"cccc\");", NoBreak));
EXPECT_EQ("aaaa(\n"
" \"bbbb\"\n"
" \"cccc\");",
format("aaaa(\"bbbb\" \"cccc\");", Break));
EXPECT_EQ("aaaa(qqq, \"bbbb\"\n"
" \"cccc\");",
format("aaaa(qqq, \"bbbb\" \"cccc\");", NoBreak));
EXPECT_EQ("aaaa(qqq,\n"
" \"bbbb\"\n"
" \"cccc\");",
format("aaaa(qqq, \"bbbb\" \"cccc\");", Break));
}
TEST_F(FormatTest, AlignsPipes) {
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"