diff options
| -rw-r--r-- | clang/include/clang/Format/Format.h | 5 | ||||
| -rw-r--r-- | clang/lib/Format/Format.cpp | 4 | ||||
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 7 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 28 |
4 files changed, 44 insertions, 0 deletions
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index d03b8796feb..ab3c1aa3328 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -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 && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index ef871389fb6..a4b5f4197d4 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -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; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 04f1c7f8a39..806c805208b 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 324f797e6da..c1fdab7cfca 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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" |

