From: Alexander Kornienko Date: Thu, 4 Jul 2013 12:02:44 +0000 (+0000) Subject: Added AlwaysBreakBeforeMultilineStrings option. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=56312023bf62cb40d2e33ea5e14d55c1e42303a7;p=clang 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185622 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index d03b8796fe..ab3c1aa332 100644 --- a/include/clang/Format/Format.h +++ b/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/lib/Format/Format.cpp b/lib/Format/Format.cpp index ef871389fb..a4b5f4197d 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -87,6 +87,8 @@ template <> struct MappingTraits { 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/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 04f1c7f8a3..806c805208 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 324f797e6d..c1fdab7cfc 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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"