From 03bf8df72b0db5fee9c4f08805e84a538fdf2267 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 1 Feb 2016 11:21:02 +0000 Subject: [PATCH] clang-format: Add option to disable string literal formatting. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@259352 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Format/Format.h | 4 ++++ lib/Format/ContinuationIndenter.cpp | 3 ++- lib/Format/Format.cpp | 6 +++++- unittests/Format/FormatTest.cpp | 6 ++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 6d051e09cb..9f3f0326d4 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -285,6 +285,9 @@ struct FormatStyle { /// \brief Break after each annotation on a field in Java files. bool BreakAfterJavaFieldAnnotations; + /// \brief Allow breaking string literals when formatting. + bool BreakStringLiterals; + /// \brief The column limit. /// /// A column limit of \c 0 means that there is no column limit. In this case, @@ -619,6 +622,7 @@ struct FormatStyle { BreakConstructorInitializersBeforeComma == R.BreakConstructorInitializersBeforeComma && BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations && + BreakStringLiterals == R.BreakStringLiterals && ColumnLimit == R.ColumnLimit && CommentPragmas == R.CommentPragmas && ConstructorInitializerAllOnOneLineOrOnePerLine == R.ConstructorInitializerAllOnOneLineOrOnePerLine && diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 07b90090ac..76a458db9b 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -1061,7 +1061,8 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, // FIXME: String literal breaking is currently disabled for Java and JS, as // it requires strings to be merged using "+" which we don't support. if (Style.Language == FormatStyle::LK_Java || - Style.Language == FormatStyle::LK_JavaScript) + Style.Language == FormatStyle::LK_JavaScript || + !Style.BreakStringLiterals) return 0; // Don't break string literals inside preprocessor directives (except for diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 2689368da5..acd520ef54 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -275,6 +275,9 @@ template <> struct MappingTraits { Style.BreakBeforeTernaryOperators); IO.mapOptional("BreakConstructorInitializersBeforeComma", Style.BreakConstructorInitializersBeforeComma); + IO.mapOptional("BreakAfterJavaFieldAnnotations", + Style.BreakAfterJavaFieldAnnotations); + IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals); IO.mapOptional("ColumnLimit", Style.ColumnLimit); IO.mapOptional("CommentPragmas", Style.CommentPragmas); IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", @@ -488,8 +491,9 @@ FormatStyle getLLVMStyle() { LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; LLVMStyle.BraceWrapping = {false, false, false, false, false, false, false, false, false, false, false}; - LLVMStyle.BreakConstructorInitializersBeforeComma = false; LLVMStyle.BreakAfterJavaFieldAnnotations = false; + LLVMStyle.BreakConstructorInitializersBeforeComma = false; + LLVMStyle.BreakStringLiterals = true; LLVMStyle.ColumnLimit = 80; LLVMStyle.CommentPragmas = "^ IWYU pragma:"; LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 057871b702..17db98fe79 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -7958,6 +7958,10 @@ TEST_F(FormatTest, BreaksStringLiterals) { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", getGoogleStyle())); + FormatStyle Style = getLLVMStyleWithColumns(12); + Style.BreakStringLiterals = false; + EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); + FormatStyle AlignLeft = getLLVMStyleWithColumns(12); AlignLeft.AlignEscapedNewlinesLeft = true; EXPECT_EQ("#define A \\\n" @@ -9823,8 +9827,10 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); CHECK_PARSE_BOOL(BinPackArguments); CHECK_PARSE_BOOL(BinPackParameters); + CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); + CHECK_PARSE_BOOL(BreakStringLiterals); CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); CHECK_PARSE_BOOL(DerivePointerAlignment); CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); -- 2.50.1