Skip to content

Commit

Permalink
[clang-format] Add BreakAdjacentStringLiterals option (#73432)
Browse files Browse the repository at this point in the history
Closes #70451.
  • Loading branch information
owenca committed Nov 27, 2023
1 parent 9e86919 commit 39faf13
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
15 changes: 15 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,21 @@ the configuration (without a prefix: ``Auto``).
};
}

.. _BreakAdjacentStringLiterals:

**BreakAdjacentStringLiterals** (``Boolean``) :versionbadge:`clang-format 18` :ref:`<BreakAdjacentStringLiterals>`
Break between adjacent string literals.

.. code-block:: c++

true:
return "Code"
"\0\52\26\55\55\0"
"x013"
"\02\xBA";
false:
return "Code" "\0\52\26\55\55\0" "x013" "\02\xBA";

.. _BreakAfterAttributes:

**BreakAfterAttributes** (``AttributeBreakingStyle``) :versionbadge:`clang-format 16` :ref:`<BreakAfterAttributes>`
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ clang-format
- Add ``AllowBreakBeforeNoexceptSpecifier`` option.
- Add ``AllowShortCompoundRequirementOnASingleLine`` option.
- Change ``BreakAfterAttributes`` from ``Never`` to ``Leave`` in LLVM style.
- Add ``BreakAdjacentStringLiterals`` option.

libclang
--------
Expand Down
14 changes: 14 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,19 @@ struct FormatStyle {
/// \version 3.8
BraceWrappingFlags BraceWrapping;

/// Break between adjacent string literals.
/// \code
/// true:
/// return "Code"
/// "\0\52\26\55\55\0"
/// "x013"
/// "\02\xBA";
/// false:
/// return "Code" "\0\52\26\55\55\0" "x013" "\02\xBA";
/// \endcode
/// \version 18
bool BreakAdjacentStringLiterals;

/// Different ways to break after attributes.
enum AttributeBreakingStyle : int8_t {
/// Always break after attributes.
Expand Down Expand Up @@ -4745,6 +4758,7 @@ struct FormatStyle {
BinPackParameters == R.BinPackParameters &&
BitFieldColonSpacing == R.BitFieldColonSpacing &&
BracedInitializerIndentWidth == R.BracedInitializerIndentWidth &&
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
BreakAfterAttributes == R.BreakAfterAttributes &&
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations &&
BreakArrays == R.BreakArrays &&
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("BracedInitializerIndentWidth",
Style.BracedInitializerIndentWidth);
IO.mapOptional("BraceWrapping", Style.BraceWrapping);
IO.mapOptional("BreakAdjacentStringLiterals",
Style.BreakAdjacentStringLiterals);
IO.mapOptional("BreakAfterAttributes", Style.BreakAfterAttributes);
IO.mapOptional("BreakAfterJavaFieldAnnotations",
Style.BreakAfterJavaFieldAnnotations);
Expand Down Expand Up @@ -1476,6 +1478,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
/*SplitEmptyFunction=*/true,
/*SplitEmptyRecord=*/true,
/*SplitEmptyNamespace=*/true};
LLVMStyle.BreakAdjacentStringLiterals = true;
LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave;
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
LLVMStyle.BreakArrays = true;
Expand Down
9 changes: 4 additions & 5 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5077,11 +5077,10 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
// it is hard to identify them in UnwrappedLineParser.
if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
return true;
} else if (Style.Language == FormatStyle::LK_Cpp ||
Style.Language == FormatStyle::LK_ObjC ||
Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TableGen ||
Style.Language == FormatStyle::LK_TextProto) {
} else if (Style.BreakAdjacentStringLiterals &&
(Style.isCpp() || Style.isProto() ||
Style.Language == FormatStyle::LK_TableGen ||
Style.Language == FormatStyle::LK_TextProto)) {
if (Left.isStringLiteral() && Right.isStringLiteral())
return true;
}
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
CHECK_PARSE_BOOL(BinPackArguments);
CHECK_PARSE_BOOL(BinPackParameters);
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
CHECK_PARSE_BOOL(BreakStringLiterals);
Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26644,6 +26644,20 @@ TEST_F(FormatTest, StreamOutputOperator) {
verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
}

TEST_F(FormatTest, BreakAdjacentStringLiterals) {
constexpr StringRef Code{
"return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};

verifyFormat("return \"Code\"\n"
" \"\\0\\52\\26\\55\\55\\0\"\n"
" \"x013\"\n"
" \"\\02\\xBA\";",
Code);

auto Style = getLLVMStyle();
Style.BreakAdjacentStringLiterals = false;
verifyFormat(Code, Style);
}
} // namespace
} // namespace test
} // namespace format
Expand Down

0 comments on commit 39faf13

Please sign in to comment.