diff options
author | Owen Pan <owenpiano@gmail.com> | 2024-05-27 15:20:58 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-27 15:20:58 -0700 |
commit | dba2aa265c5f2ef774c2812cf6ffdea8dd784e09 (patch) | |
tree | 6989e0b77f93696551f39d5d0d86bedd35321b77 | |
parent | 434ee06d77613df23a2ece62a62bcd62c13121d2 (diff) | |
download | llvm-dba2aa265c5f2ef774c2812cf6ffdea8dd784e09.zip llvm-dba2aa265c5f2ef774c2812cf6ffdea8dd784e09.tar.gz llvm-dba2aa265c5f2ef774c2812cf6ffdea8dd784e09.tar.bz2 |
[clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (#93402)
Closes #92999.
-rw-r--r-- | clang/docs/ClangFormatStyleOptions.rst | 12 | ||||
-rw-r--r-- | clang/docs/ReleaseNotes.rst | 3 | ||||
-rw-r--r-- | clang/include/clang/Format/Format.h | 12 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 1 | ||||
-rw-r--r-- | clang/lib/Format/WhitespaceManager.cpp | 29 | ||||
-rw-r--r-- | clang/unittests/Format/ConfigParseTest.cpp | 2 | ||||
-rw-r--r-- | 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 6d09221..1a7d0e6 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 37a664b..182f8b5 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -964,9 +964,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 274b45d..eb66470 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 9cba0c2..c015e03 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 ed06d609..50531ae 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 82e72f0..ff3ced3 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 a9df994..76eb2b6 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; |