Skip to content

Commit 39faf13

Browse files
authored
[clang-format] Add BreakAdjacentStringLiterals option (llvm#73432)
Closes llvm#70451.
1 parent 9e86919 commit 39faf13

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,21 @@ the configuration (without a prefix: ``Auto``).
20462046
};
20472047
}
20482048

2049+
.. _BreakAdjacentStringLiterals:
2050+
2051+
**BreakAdjacentStringLiterals** (``Boolean``) :versionbadge:`clang-format 18` :ref:`<BreakAdjacentStringLiterals>`
2052+
Break between adjacent string literals.
2053+
2054+
.. code-block:: c++
2055+
2056+
true:
2057+
return "Code"
2058+
"\0\52\26\55\55\0"
2059+
"x013"
2060+
"\02\xBA";
2061+
false:
2062+
return "Code" "\0\52\26\55\55\0" "x013" "\02\xBA";
2063+
20492064
.. _BreakAfterAttributes:
20502065

20512066
**BreakAfterAttributes** (``AttributeBreakingStyle``) :versionbadge:`clang-format 16` :ref:`<BreakAfterAttributes>`

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ clang-format
956956
- Add ``AllowBreakBeforeNoexceptSpecifier`` option.
957957
- Add ``AllowShortCompoundRequirementOnASingleLine`` option.
958958
- Change ``BreakAfterAttributes`` from ``Never`` to ``Leave`` in LLVM style.
959+
- Add ``BreakAdjacentStringLiterals`` option.
959960

960961
libclang
961962
--------

clang/include/clang/Format/Format.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,19 @@ struct FormatStyle {
14241424
/// \version 3.8
14251425
BraceWrappingFlags BraceWrapping;
14261426

1427+
/// Break between adjacent string literals.
1428+
/// \code
1429+
/// true:
1430+
/// return "Code"
1431+
/// "\0\52\26\55\55\0"
1432+
/// "x013"
1433+
/// "\02\xBA";
1434+
/// false:
1435+
/// return "Code" "\0\52\26\55\55\0" "x013" "\02\xBA";
1436+
/// \endcode
1437+
/// \version 18
1438+
bool BreakAdjacentStringLiterals;
1439+
14271440
/// Different ways to break after attributes.
14281441
enum AttributeBreakingStyle : int8_t {
14291442
/// Always break after attributes.
@@ -4745,6 +4758,7 @@ struct FormatStyle {
47454758
BinPackParameters == R.BinPackParameters &&
47464759
BitFieldColonSpacing == R.BitFieldColonSpacing &&
47474760
BracedInitializerIndentWidth == R.BracedInitializerIndentWidth &&
4761+
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
47484762
BreakAfterAttributes == R.BreakAfterAttributes &&
47494763
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations &&
47504764
BreakArrays == R.BreakArrays &&

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,8 @@ template <> struct MappingTraits<FormatStyle> {
965965
IO.mapOptional("BracedInitializerIndentWidth",
966966
Style.BracedInitializerIndentWidth);
967967
IO.mapOptional("BraceWrapping", Style.BraceWrapping);
968+
IO.mapOptional("BreakAdjacentStringLiterals",
969+
Style.BreakAdjacentStringLiterals);
968970
IO.mapOptional("BreakAfterAttributes", Style.BreakAfterAttributes);
969971
IO.mapOptional("BreakAfterJavaFieldAnnotations",
970972
Style.BreakAfterJavaFieldAnnotations);
@@ -1476,6 +1478,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14761478
/*SplitEmptyFunction=*/true,
14771479
/*SplitEmptyRecord=*/true,
14781480
/*SplitEmptyNamespace=*/true};
1481+
LLVMStyle.BreakAdjacentStringLiterals = true;
14791482
LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave;
14801483
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
14811484
LLVMStyle.BreakArrays = true;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5077,11 +5077,10 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
50775077
// it is hard to identify them in UnwrappedLineParser.
50785078
if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
50795079
return true;
5080-
} else if (Style.Language == FormatStyle::LK_Cpp ||
5081-
Style.Language == FormatStyle::LK_ObjC ||
5082-
Style.Language == FormatStyle::LK_Proto ||
5083-
Style.Language == FormatStyle::LK_TableGen ||
5084-
Style.Language == FormatStyle::LK_TextProto) {
5080+
} else if (Style.BreakAdjacentStringLiterals &&
5081+
(Style.isCpp() || Style.isProto() ||
5082+
Style.Language == FormatStyle::LK_TableGen ||
5083+
Style.Language == FormatStyle::LK_TextProto)) {
50855084
if (Left.isStringLiteral() && Right.isStringLiteral())
50865085
return true;
50875086
}

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
159159
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
160160
CHECK_PARSE_BOOL(BinPackArguments);
161161
CHECK_PARSE_BOOL(BinPackParameters);
162+
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
162163
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
163164
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
164165
CHECK_PARSE_BOOL(BreakStringLiterals);

clang/unittests/Format/FormatTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26644,6 +26644,20 @@ TEST_F(FormatTest, StreamOutputOperator) {
2664426644
verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
2664526645
}
2664626646

26647+
TEST_F(FormatTest, BreakAdjacentStringLiterals) {
26648+
constexpr StringRef Code{
26649+
"return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
26650+
26651+
verifyFormat("return \"Code\"\n"
26652+
" \"\\0\\52\\26\\55\\55\\0\"\n"
26653+
" \"x013\"\n"
26654+
" \"\\02\\xBA\";",
26655+
Code);
26656+
26657+
auto Style = getLLVMStyle();
26658+
Style.BreakAdjacentStringLiterals = false;
26659+
verifyFormat(Code, Style);
26660+
}
2664726661
} // namespace
2664826662
} // namespace test
2664926663
} // namespace format

0 commit comments

Comments
 (0)