https://github.com/NorthBlue333 updated https://github.com/llvm/llvm-project/pull/77456
>From 18163c8cad017274adaf8c4e23c0121dc1f3c844 Mon Sep 17 00:00:00 2001 From: NorthBlue333 <north...@free.fr> Date: Tue, 9 Jan 2024 14:01:14 +0100 Subject: [PATCH] [clang-format] Do not update cursor pos if no includes replacement --- clang/lib/Format/Format.cpp | 13 +++++++---- clang/unittests/Format/SortIncludesTest.cpp | 24 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index f798d555bf9929..2bc336e046aa94 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3119,6 +3119,7 @@ static void sortCppIncludes(const FormatStyle &Style, return; } + unsigned NewCursor = UINT_MAX; std::string result; for (unsigned Index : Indices) { if (!result.empty()) { @@ -3131,13 +3132,10 @@ static void sortCppIncludes(const FormatStyle &Style, } result += Includes[Index].Text; if (Cursor && CursorIndex == Index) - *Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset; + NewCursor = IncludesBeginOffset + result.size() - CursorToEOLOffset; CurrentCategory = Includes[Index].Category; } - if (Cursor && *Cursor >= IncludesEndOffset) - *Cursor += result.size() - IncludesBlockSize; - // If the #includes are out of order, we generate a single replacement fixing // the entire range of blocks. Otherwise, no replacement is generated. if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr( @@ -3145,6 +3143,13 @@ static void sortCppIncludes(const FormatStyle &Style, return; } + if (Cursor) { + if (UINT_MAX != NewCursor) + *Cursor = NewCursor; + else if (*Cursor >= IncludesEndOffset) + *Cursor += result.size() - IncludesBlockSize; + } + auto Err = Replaces.add(tooling::Replacement( FileName, Includes.front().Offset, IncludesBlockSize, result)); // FIXME: better error handling. For now, just skip the replacement for the diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp index ec142e03b12854..dc1fadc61b3f93 100644 --- a/clang/unittests/Format/SortIncludesTest.cpp +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -821,6 +821,30 @@ TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) { EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line } +TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) { + Style.IncludeBlocks = Style.IBS_Regroup; + FmtStyle.LineEnding = FormatStyle::LE_CRLF; + Style.IncludeCategories = { + {"^\"aa\"", 1, 0, false}, + {"^\"b\"", 1, 1, false}, + {".*", 2, 2, false}}; + std::string Code = "#include \"aa\"\r\n" // Start of line: 0 + "\r\n" // Start of line: 15 + "#include \"b\"\r\n" // Start of line: 17 + "\r\n" // Start of line: 31 + "#include \"c\"\r\n" // Start of line: 33 + "\r\n" // Start of line: 47 + "int i;"; // Start of line: 49 + EXPECT_EQ(Code, sort(Code)); + EXPECT_EQ(0u, newCursor(Code, 0)); + EXPECT_EQ(15u, newCursor(Code, 15)); + EXPECT_EQ(17u, newCursor(Code, 17)); + EXPECT_EQ(31u, newCursor(Code, 31)); + EXPECT_EQ(33u, newCursor(Code, 33)); + EXPECT_EQ(47u, newCursor(Code, 47)); + EXPECT_EQ(49u, newCursor(Code, 49)); +} + TEST_F(SortIncludesTest, DeduplicateIncludes) { EXPECT_EQ("#include <a>\n" "#include <b>\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits