[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 9 05:20:51 PST 2024
https://github.com/NorthBlue333 updated https://github.com/llvm/llvm-project/pull/77456
>From 897c90c09f24b6fd8b0c84b57a284cabb77c9e80 Mon Sep 17 00:00:00 2001
From: NorthBlue333 <north333 at 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 | 14 +++++++---
clang/unittests/Format/SortIncludesTest.cpp | 31 +++++++++++++++++++++
2 files changed, 41 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f798d555bf9929..a91f6a639fb00b 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,14 @@ 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..652cd842e6f3f5 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -821,6 +821,37 @@ TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
}
+TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
+ Style.IncludeBlocks = Style.IBS_Regroup;
+ FmtStyle.LineEnding = FormatStyle::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
+ std::string Expected = "#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(Expected, 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"
More information about the cfe-commits
mailing list