[clang] [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (PR #93402)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/93402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (PR #93402)
https://github.com/HazardyKnusperkeks approved this pull request. Nice https://github.com/llvm/llvm-project/pull/93402 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (PR #93402)
llvmbot wrote: @llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) Changes 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 ; \ int b;\ int dd; - 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 ; \ +int b; \ +int dd; * ``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 ; \ /// int b;\ /// int dd; -/// -/// 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 ; \ +/// int b; \ +/// int dd; +/// \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 &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.PreviousEndOfTokenColum
[clang] [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (PR #93402)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Owen Pan (owenca) Changes 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 ; \ int b;\ int dd; - 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 ; \ +int b; \ +int dd; * ``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 ; \ /// int b;\ /// int dd; -/// -/// 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 ; \ +/// int b; \ +/// int dd; +/// \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 &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,
[clang] [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (PR #93402)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/93402 Closes #92999. >From fc9097e064e2d64832acc611b2a8d50d332119d6 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 26 May 2024 00:23:35 -0700 Subject: [PATCH] [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option Closes #92999. --- clang/docs/ClangFormatStyleOptions.rst | 12 +++-- clang/docs/ReleaseNotes.rst| 3 ++- clang/include/clang/Format/Format.h| 12 ++--- clang/lib/Format/Format.cpp| 1 + clang/lib/Format/WhitespaceManager.cpp | 29 ++ clang/unittests/Format/ConfigParseTest.cpp | 2 ++ clang/unittests/Format/FormatTest.cpp | 20 +++ 7 files changed, 62 insertions(+), 17 deletions(-) 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 ; \ int b;\ int dd; - 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 ; \ +int b; \ +int dd; * ``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 ; \ /// int b;\ /// int dd; -/// -/// 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 ; \ +/// int b; \ +/// int dd; +/// \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 &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 = C