[clang] [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (PR #93402)
via cfe-commits
cfe-commits at lists.llvm.org
Sun May 26 00:29:09 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Closes #<!-- -->92999.
---
Full diff: https://github.com/llvm/llvm-project/pull/93402.diff
7 Files Affected:
- (modified) clang/docs/ClangFormatStyleOptions.rst (+10-2)
- (modified) clang/docs/ReleaseNotes.rst (+2-1)
- (modified) clang/include/clang/Format/Format.h (+9-3)
- (modified) clang/lib/Format/Format.cpp (+1)
- (modified) clang/lib/Format/WhitespaceManager.cpp (+18-11)
- (modified) clang/unittests/Format/ConfigParseTest.cpp (+2)
- (modified) clang/unittests/Format/FormatTest.cpp (+20)
``````````diff
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 6d092219877f9..1a7d0e6a05e31 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -1421,13 +1421,21 @@ the configuration (without a prefix: ``Auto``).
.. code-block:: c++
- true:
#define A \
int aaaa; \
int b; \
int dddddddddd;
- false:
+ * ``ENAS_LeftWithLastLine`` (in configuration: ``LeftWithLastLine``)
+ Align escaped newlines as far left as possible, using the last line of
+ the preprocessor directive as the reference if it's the longest.
+
+ .. code-block:: c++
+
+ #define A \
+ int aaaa; \
+ int b; \
+ int dddddddddd;
* ``ENAS_Right`` (in configuration: ``Right``)
Align escaped newlines in the right-most column.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 81e9d0423f96a..56bf78d3298de 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -902,9 +902,10 @@ clang-format
``BreakTemplateDeclarations``.
- ``AlwaysBreakAfterReturnType`` is deprecated and renamed to
``BreakAfterReturnType``.
-- Handles Java ``switch`` expressions.
+- Handles Java switch expressions.
- Adds ``AllowShortCaseExpressionOnASingleLine`` option.
- Adds ``AlignCaseArrows`` suboption to ``AlignConsecutiveShortCaseStatements``.
+- Adds ``LeftWithLastLine`` suboption to ``AlignEscapedNewlines``.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 274b45d1bc586..eb6647038403d 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -480,15 +480,21 @@ struct FormatStyle {
ENAS_DontAlign,
/// Align escaped newlines as far left as possible.
/// \code
- /// true:
/// #define A \
/// int aaaa; \
/// int b; \
/// int dddddddddd;
- ///
- /// false:
/// \endcode
ENAS_Left,
+ /// Align escaped newlines as far left as possible, using the last line of
+ /// the preprocessor directive as the reference if it's the longest.
+ /// \code
+ /// #define A \
+ /// int aaaa; \
+ /// int b; \
+ /// int dddddddddd;
+ /// \endcode
+ ENAS_LeftWithLastLine,
/// Align escaped newlines in the right-most column.
/// \code
/// #define A \
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 9cba0c2614eef..c015e03fa15e7 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -308,6 +308,7 @@ struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> {
FormatStyle::EscapedNewlineAlignmentStyle &Value) {
IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign);
IO.enumCase(Value, "Left", FormatStyle::ENAS_Left);
+ IO.enumCase(Value, "LeftWithLastLine", FormatStyle::ENAS_LeftWithLastLine);
IO.enumCase(Value, "Right", FormatStyle::ENAS_Right);
// For backward compatibility.
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index ed06d6098a9f2..50531aee9d597 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1245,22 +1245,29 @@ void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
}
void WhitespaceManager::alignEscapedNewlines() {
- if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
+ const auto Align = Style.AlignEscapedNewlines;
+ if (Align == FormatStyle::ENAS_DontAlign)
return;
- bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
- unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
+ const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
+ const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
+ const auto MaxColumn = Style.ColumnLimit;
+ unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
unsigned StartOfMacro = 0;
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
Change &C = Changes[i];
- if (C.NewlinesBefore > 0) {
- if (C.ContinuesPPDirective) {
- MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
- } else {
- alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
- MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
- StartOfMacro = i;
- }
+ if (C.NewlinesBefore == 0 && (!WithLastLine || C.Tok->isNot(tok::eof)))
+ continue;
+ const bool InPPDirective = C.ContinuesPPDirective;
+ const auto BackslashColumn = C.PreviousEndOfTokenColumn + 2;
+ if (InPPDirective ||
+ (WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
+ MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
+ }
+ if (!InPPDirective) {
+ alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
+ MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
+ StartOfMacro = i;
}
}
alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 82e72f08ffb5e..ff3ced38a1f31 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -480,6 +480,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle::ENAS_DontAlign);
CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
FormatStyle::ENAS_Left);
+ CHECK_PARSE("AlignEscapedNewlines: LeftWithLastLine", AlignEscapedNewlines,
+ FormatStyle::ENAS_LeftWithLastLine);
CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
FormatStyle::ENAS_Right);
// For backward compatibility:
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a9df994189f00..76eb2b6cd994c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6636,6 +6636,26 @@ TEST_F(FormatTest, EscapedNewlines) {
" int x(int a);",
AlignLeft);
+ constexpr StringRef Code{"#define A \\\n"
+ " int a123; \\\n"
+ " int a; \\\n"
+ " int a1234;"};
+ verifyFormat(Code, AlignLeft);
+
+ constexpr StringRef Code2{"#define A \\\n"
+ " int a123; \\\n"
+ " int a; \\\n"
+ " int a1234;"};
+ auto LastLine = getLLVMStyle();
+ LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
+ verifyFormat(Code2, LastLine);
+
+ LastLine.ColumnLimit = 13;
+ verifyFormat(Code, LastLine);
+
+ LastLine.ColumnLimit = 0;
+ verifyFormat(Code2, LastLine);
+
FormatStyle DontAlign = getLLVMStyle();
DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
DontAlign.MaxEmptyLinesToKeep = 3;
``````````
</details>
https://github.com/llvm/llvm-project/pull/93402
More information about the cfe-commits
mailing list