Skip to content

Commit dba2aa2

Browse files
authored
[clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (#93402)
Closes #92999.
1 parent 434ee06 commit dba2aa2

File tree

7 files changed

+62
-17
lines changed

7 files changed

+62
-17
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,13 +1421,21 @@ the configuration (without a prefix: ``Auto``).
14211421

14221422
.. code-block:: c++
14231423

1424-
true:
14251424
#define A \
14261425
int aaaa; \
14271426
int b; \
14281427
int dddddddddd;
14291428

1430-
false:
1429+
* ``ENAS_LeftWithLastLine`` (in configuration: ``LeftWithLastLine``)
1430+
Align escaped newlines as far left as possible, using the last line of
1431+
the preprocessor directive as the reference if it's the longest.
1432+
1433+
.. code-block:: c++
1434+
1435+
#define A \
1436+
int aaaa; \
1437+
int b; \
1438+
int dddddddddd;
14311439

14321440
* ``ENAS_Right`` (in configuration: ``Right``)
14331441
Align escaped newlines in the right-most column.

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,10 @@ clang-format
964964
``BreakTemplateDeclarations``.
965965
- ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
966966
``BreakAfterReturnType``.
967-
- Handles Java ``switch`` expressions.
967+
- Handles Java switch expressions.
968968
- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
969969
- Adds ``AlignCaseArrows`` suboption to ``AlignConsecutiveShortCaseStatements``.
970+
- Adds ``LeftWithLastLine`` suboption to ``AlignEscapedNewlines``.
970971

971972
libclang
972973
--------

clang/include/clang/Format/Format.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,15 +480,21 @@ struct FormatStyle {
480480
ENAS_DontAlign,
481481
/// Align escaped newlines as far left as possible.
482482
/// \code
483-
/// true:
484483
/// #define A \
485484
/// int aaaa; \
486485
/// int b; \
487486
/// int dddddddddd;
488-
///
489-
/// false:
490487
/// \endcode
491488
ENAS_Left,
489+
/// Align escaped newlines as far left as possible, using the last line of
490+
/// the preprocessor directive as the reference if it's the longest.
491+
/// \code
492+
/// #define A \
493+
/// int aaaa; \
494+
/// int b; \
495+
/// int dddddddddd;
496+
/// \endcode
497+
ENAS_LeftWithLastLine,
492498
/// Align escaped newlines in the right-most column.
493499
/// \code
494500
/// #define A \

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> {
308308
FormatStyle::EscapedNewlineAlignmentStyle &Value) {
309309
IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign);
310310
IO.enumCase(Value, "Left", FormatStyle::ENAS_Left);
311+
IO.enumCase(Value, "LeftWithLastLine", FormatStyle::ENAS_LeftWithLastLine);
311312
IO.enumCase(Value, "Right", FormatStyle::ENAS_Right);
312313

313314
// For backward compatibility.

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,22 +1245,29 @@ void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
12451245
}
12461246

12471247
void WhitespaceManager::alignEscapedNewlines() {
1248-
if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
1248+
const auto Align = Style.AlignEscapedNewlines;
1249+
if (Align == FormatStyle::ENAS_DontAlign)
12491250
return;
12501251

1251-
bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
1252-
unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1252+
const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
1253+
const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
1254+
const auto MaxColumn = Style.ColumnLimit;
1255+
unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
12531256
unsigned StartOfMacro = 0;
12541257
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
12551258
Change &C = Changes[i];
1256-
if (C.NewlinesBefore > 0) {
1257-
if (C.ContinuesPPDirective) {
1258-
MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
1259-
} else {
1260-
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1261-
MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1262-
StartOfMacro = i;
1263-
}
1259+
if (C.NewlinesBefore == 0 && (!WithLastLine || C.Tok->isNot(tok::eof)))
1260+
continue;
1261+
const bool InPPDirective = C.ContinuesPPDirective;
1262+
const auto BackslashColumn = C.PreviousEndOfTokenColumn + 2;
1263+
if (InPPDirective ||
1264+
(WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
1265+
MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
1266+
}
1267+
if (!InPPDirective) {
1268+
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1269+
MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1270+
StartOfMacro = i;
12641271
}
12651272
}
12661273
alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
480480
FormatStyle::ENAS_DontAlign);
481481
CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
482482
FormatStyle::ENAS_Left);
483+
CHECK_PARSE("AlignEscapedNewlines: LeftWithLastLine", AlignEscapedNewlines,
484+
FormatStyle::ENAS_LeftWithLastLine);
483485
CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
484486
FormatStyle::ENAS_Right);
485487
// For backward compatibility:

clang/unittests/Format/FormatTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6636,6 +6636,26 @@ TEST_F(FormatTest, EscapedNewlines) {
66366636
" int x(int a);",
66376637
AlignLeft);
66386638

6639+
constexpr StringRef Code{"#define A \\\n"
6640+
" int a123; \\\n"
6641+
" int a; \\\n"
6642+
" int a1234;"};
6643+
verifyFormat(Code, AlignLeft);
6644+
6645+
constexpr StringRef Code2{"#define A \\\n"
6646+
" int a123; \\\n"
6647+
" int a; \\\n"
6648+
" int a1234;"};
6649+
auto LastLine = getLLVMStyle();
6650+
LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
6651+
verifyFormat(Code2, LastLine);
6652+
6653+
LastLine.ColumnLimit = 13;
6654+
verifyFormat(Code, LastLine);
6655+
6656+
LastLine.ColumnLimit = 0;
6657+
verifyFormat(Code2, LastLine);
6658+
66396659
FormatStyle DontAlign = getLLVMStyle();
66406660
DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
66416661
DontAlign.MaxEmptyLinesToKeep = 3;

0 commit comments

Comments
 (0)