[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-25 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca approved this pull request.


https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/77456

>From 6c184f9714c94af94c7692e1264061b8dc14e912 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/3] [clang-format] Do not update cursor pos if no includes
 replacement

Signed-off-by: NorthBlue333 
---
 clang/lib/Format/Format.cpp |   3 +
 clang/unittests/Format/SortIncludesTest.cpp | 119 +++-
 2 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..e12ad2ced38285 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3114,6 +3114,7 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  const auto OldCursor = Cursor ? *Cursor : 0;
   std::string result;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
@@ -3137,6 +3138,8 @@ static void sortCppIncludes(const FormatStyle &Style,
   // the entire range of blocks. Otherwise, no replacement is generated.
   if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
  IncludesBeginOffset, IncludesBlockSize {
+if (Cursor)
+*Cursor = OldCursor;
 return;
   }
 
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 772eb53806b4b1..791ab7bb185ed9 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,119 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+ "#include \"b\"\r\n" // Start of line: 14
+ "#include \"c\"\r\n" // Start of line: 28
+ "\r\n"   // Start of line: 42
+ "int i;";// Start of line: 44
+  EXPECT_EQ(Expected, sort(Code));
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(
+  14u,
+  newCursor(Code, 14)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(14u, newCursor(Code, 16));
+  EXPECT_EQ(
+  30u,
+  newCursor(Code, 30)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(28u, newCursor(Code, 32));
+  EXPECT_EQ(42u, newCursor(Code, 46));
+  EXPECT_EQ(44u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
Calculates

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/77456

>From 6c184f9714c94af94c7692e1264061b8dc14e912 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/2] [clang-format] Do not update cursor pos if no includes
 replacement

Signed-off-by: NorthBlue333 
---
 clang/lib/Format/Format.cpp |   3 +
 clang/unittests/Format/SortIncludesTest.cpp | 119 +++-
 2 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..e12ad2ced38285 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3114,6 +3114,7 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  const auto OldCursor = Cursor ? *Cursor : 0;
   std::string result;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
@@ -3137,6 +3138,8 @@ static void sortCppIncludes(const FormatStyle &Style,
   // the entire range of blocks. Otherwise, no replacement is generated.
   if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
  IncludesBeginOffset, IncludesBlockSize {
+if (Cursor)
+*Cursor = OldCursor;
 return;
   }
 
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 772eb53806b4b1..791ab7bb185ed9 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,119 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+ "#include \"b\"\r\n" // Start of line: 14
+ "#include \"c\"\r\n" // Start of line: 28
+ "\r\n"   // Start of line: 42
+ "int i;";// Start of line: 44
+  EXPECT_EQ(Expected, sort(Code));
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(
+  14u,
+  newCursor(Code, 14)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(14u, newCursor(Code, 16));
+  EXPECT_EQ(
+  30u,
+  newCursor(Code, 30)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(28u, newCursor(Code, 32));
+  EXPECT_EQ(42u, newCursor(Code, 46));
+  EXPECT_EQ(44u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
Calculates

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-24 Thread Owen Pan via cfe-commits

owenca wrote:

> I have squashed the commits in only one. Note that I have left the failing 
> tests in the commit, I am not sure if I should remove them or not.

Unfortunately, this wiped out my updates that fixed a formatting error and 
added `#if 0` for the failing tests.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-19 Thread via cfe-commits

NorthBlue333 wrote:

Sure, that was was not very clear from my side I agree.

Yes, your implementation is cleaner (I don't know why I did not do that from 
the beginning, I think I was struggling to fix the other tests and ended up 
with something more complicated than necessary).

I have squashed the commits in only one.
Thanks for your time!

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-19 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 6c184f9714c94af94c7692e1264061b8dc14e912 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH] [clang-format] Do not update cursor pos if no includes
 replacement

Signed-off-by: NorthBlue333 
---
 clang/lib/Format/Format.cpp |   3 +
 clang/unittests/Format/SortIncludesTest.cpp | 119 +++-
 2 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..e12ad2ced38285 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3114,6 +3114,7 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  const auto OldCursor = Cursor ? *Cursor : 0;
   std::string result;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
@@ -3137,6 +3138,8 @@ static void sortCppIncludes(const FormatStyle &Style,
   // the entire range of blocks. Otherwise, no replacement is generated.
   if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
  IncludesBeginOffset, IncludesBlockSize {
+if (Cursor)
+*Cursor = OldCursor;
 return;
   }
 
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 772eb53806b4b1..791ab7bb185ed9 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,119 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+ "#include \"b\"\r\n" // Start of line: 14
+ "#include \"c\"\r\n" // Start of line: 28
+ "\r\n"   // Start of line: 42
+ "int i;";// Start of line: 44
+  EXPECT_EQ(Expected, sort(Code));
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(
+  14u,
+  newCursor(Code, 14)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(14u, newCursor(Code, 16));
+  EXPECT_EQ(
+  30u,
+  newCursor(Code, 30)); // cursor on empty line in include block is ignored
+  EXPECT_EQ(28u, newCursor(Code, 32));
+  EXPECT_EQ(42u, newCursor(Code, 46));
+  EXPECT_EQ(44u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
Calculat

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-18 Thread Owen Pan via cfe-commits

https://github.com/owenca approved this pull request.


https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-18 Thread Owen Pan via cfe-commits

owenca wrote:

> I've done all your changes in a second commit just to be able to keep track 
> of my previous changes; but I am actually not sure how your suggested 
> modifications are any different from mine? Well, I admit it looks a bit 
> better, but it works the same? Or am I missing something? You are just 
> storing the old value of Cursor and resetting it when no changes, while I was 
> doing it the other way around: storing the new value and updating it only if 
> no changes.

It's been a while, but IIRC your original fix was not equivalent to my 
suggestion, which IMO is simpler and cleaner.

> It does not fix the tests I mentioned.

My bad. I copy-pasted the two tests you said would fail and ran FormatTests 
with my suggested fix, and they passed. Of course, I missed the comments in 
your snippet, e.g.:
```
  EXPECT_EQ(15u, newCursor(Code, 14)); // FIXME: should expect 16, caused by \r
```

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-18 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/77456

>From d4fd95374a82361e3dbfcd7a5d87c37da4542d2b Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/4] [clang-format] Do not update cursor pos if no includes
 replacement

---
 clang/lib/Format/Format.cpp | 13 +++--
 clang/unittests/Format/SortIncludesTest.cpp | 61 -
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..17a3e0c9cfd733 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3115,6 +3115,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3126,13 +3127,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(
@@ -3140,6 +3138,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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 772eb53806b4b1..939ad181a9d707 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-18 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/77456

>From d4fd95374a82361e3dbfcd7a5d87c37da4542d2b Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/3] [clang-format] Do not update cursor pos if no includes
 replacement

---
 clang/lib/Format/Format.cpp | 13 +++--
 clang/unittests/Format/SortIncludesTest.cpp | 61 -
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..17a3e0c9cfd733 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3115,6 +3115,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3126,13 +3127,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(
@@ -3140,6 +3138,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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 772eb53806b4b1..939ad181a9d707 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-04-18 Thread via cfe-commits

NorthBlue333 wrote:

Friendly ping again here, as the status on this has been asked again by my 
team! :)
@owenca, @mydeveloperday, and maybe @HazardyKnusperkeks (about splitting the 
issue in 2).

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-03-26 Thread via cfe-commits

NorthBlue333 wrote:

Woops, sorry, I missed the update here.

I've done all your changes in a second commit just to be able to keep track of 
my previous changes; but I am actually not sure how your suggested 
modifications are any different from mine? Well, I admit it looks a bit better, 
but it works the same? Or am I missing something? You are just storing the old 
value of Cursor and resetting it when no changes, while I was doing it the 
other way around: storing the new value and updating it only if no changes.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-03-26 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From d4fd95374a82361e3dbfcd7a5d87c37da4542d2b Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/2] [clang-format] Do not update cursor pos if no includes
 replacement

---
 clang/lib/Format/Format.cpp | 13 +++--
 clang/unittests/Format/SortIncludesTest.cpp | 61 -
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..17a3e0c9cfd733 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3115,6 +3115,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3126,13 +3127,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(
@@ -3140,6 +3138,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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 772eb53806b4b1..939ad181a9d707 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of li

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-03-26 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From d4fd95374a82361e3dbfcd7a5d87c37da4542d2b Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/2] [clang-format] Do not update cursor pos if no includes
 replacement

---
 clang/lib/Format/Format.cpp | 13 +++--
 clang/unittests/Format/SortIncludesTest.cpp | 61 -
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 46ed5baaeacead..17a3e0c9cfd733 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3115,6 +3115,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3126,13 +3127,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(
@@ -3140,6 +3138,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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 772eb53806b4b1..939ad181a9d707 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of li

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-03-26 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From a3ddae11b909a319d73fccff409fa5c0648a234f Mon Sep 17 00:00:00 2001
From: NorthBlue333 
Date: Tue, 9 Jan 2024 14:01:14 +0100
Subject: [PATCH 1/2] [clang-format] Do not update cursor pos if no includes
 replacement

---
 clang/lib/Format/Format.cpp | 13 +++--
 clang/unittests/Format/SortIncludesTest.cpp | 61 -
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ff326dc784783b..3d8cffaca1ded3 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3123,6 +3123,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3134,13 +3135,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(
@@ -3148,6 +3146,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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..d180767b6ff9fa 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of li

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-22 Thread Owen Pan via cfe-commits

owenca wrote:

> As a side note, I tried adding these two tests but they fail, meaning the 
> cursor is still incorrectly computed with CRLF when replacing lines.
> 
> This is due to adding lines between the include groups (with Regroup option), 
> as this new line is added with only the character `\n` (while the line break 
> between include lines is right because the include line itself contains a 
> trailing `\r`). I am not sure how to fix this correctly because I do not want 
> to break things by trimming the include line and I do not think 
> `sortCppIncludes` should manage whitespaces (`WhitespaceManager` seems to be 
> the correct class to handle this?). Maybe the cursor should be updated when 
> replacing these whitespaces?
> 
> ```c++
> TEST_F(
>SortIncludesTest,
>
> CalculatesCorrectCursorPositionWhenNewLineReplacementsWithRegroupingAndCRLF) {
>   Style.IncludeBlocks   = Style.IBS_Regroup;
>   FmtStyle.LineEnding   = FormatStyle::LE_CRLF;
>   Style.IncludeCategories = {
>   {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
>   std::string Code = "#include \"a\"\r\n" // Start of line: 0
>  "#include \"b\"\r\n" // Start of line: 14
>  "#include \"c\"\r\n" // Start of line: 28
>  "\r\n"   // Start of line: 42
>  "int i;";// Start of line: 44
>   std::string Expected = "#include \"a\"\r\n" // Start of line: 0
>  "\r\n"   // Start of line: 14
>  "#include \"b\"\r\n" // Start of line: 16
>  "\r\n"   // Start of line: 30
>  "#include \"c\"\r\n" // Start of line: 32
>  "\r\n"   // Start of line: 46
>  "int i;";// Start of line: 48
>   EXPECT_EQ(Expected, sort(Code));
>   EXPECT_EQ(0u, newCursor(Code, 0));
>   EXPECT_EQ(15u, newCursor(Code, 14)); // FIXME: should expect 16, caused by 
> \r
>   EXPECT_EQ(30u, newCursor(Code, 28)); // FIXME: should expect 32, caused by 
> \r
>   EXPECT_EQ(44u, newCursor(Code, 42)); // FIXME: should expect 46, caused by 
> \r
>   EXPECT_EQ(46u, newCursor(Code, 44)); // FIXME: should expect 48, caused by 
> \r
> }
> 
> TEST_F(
>SortIncludesTest,
>
> CalculatesCorrectCursorPositionWhenNoNewLineReplacementsWithRegroupingAndCRLF)
>  {
>   Style.IncludeBlocks = Style.IBS_Regroup;
>   FmtStyle.LineEnding = FormatStyle::LE_CRLF;
>   Style.IncludeCategories = {
>   {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
>   std::string Code = "#include \"a\"\r\n" // Start of line: 0
>  "\r\n"   // Start of line: 14
>  "#include \"c\"\r\n" // Start of line: 16
>  "\r\n"   // Start of line: 30
>  "#include \"b\"\r\n" // Start of line: 32
>  "\r\n"   // Start of line: 46
>  "int i;";// Start of line: 48
>   std::string Expected = "#include \"a\"\r\n" // Start of line: 0
>  "\r\n"   // Start of line: 14
>  "#include \"b\"\r\n" // Start of line: 16
>  "\r\n"   // Start of line: 30
>  "#include \"c\"\r\n" // Start of line: 32
>  "\r\n"   // Start of line: 46
>  "int i;";// Start of line: 48
>   EXPECT_EQ(Expected, sort(Code));
>   EXPECT_EQ(0u, newCursor(Code, 0));
>   EXPECT_EQ(14u, newCursor(Code, 14));
>   EXPECT_EQ(30u, newCursor(Code, 16)); // FIXME: should expect 32, caused by 
> \r
>   EXPECT_EQ(30u, newCursor(Code, 30));
>   EXPECT_EQ(15u, newCursor(Code, 32)); // FIXME: should expect 15, caused by 
> \r
>   EXPECT_EQ(44u, newCursor(Code, 46)); // FIXME: should expect 46, caused by 
> \r
>   EXPECT_EQ(46u, newCursor(Code, 48)); // FIXME: should expect 48, caused by 
> \r
> }
> ```

With my suggested fix, these tests should also pass.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-22 Thread Owen Pan via cfe-commits


@@ -3134,20 +3135,24 @@ 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(
  IncludesBeginOffset, IncludesBlockSize {
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *Cursor = NewCursor;
+else if (*Cursor >= IncludesEndOffset)
+  *Cursor += result.size() - IncludesBlockSize;
+  }
+

owenca wrote:

```suggestion
```

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-22 Thread Owen Pan via cfe-commits

https://github.com/owenca commented:

`git diff Format.cpp` output:
```
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3116,6 +3116,7 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  const auto OldCursor = Cursor ? *Cursor : 0;
   std::string result;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
@@ -3139,6 +3140,8 @@ static void sortCppIncludes(const FormatStyle &Style,
   // the entire range of blocks. Otherwise, no replacement is generated.
   if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
  IncludesBeginOffset, IncludesBlockSize {
+if (Cursor)
+  *Cursor = OldCursor;
 return;
   }
 
```

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-22 Thread Owen Pan via cfe-commits


@@ -3134,20 +3135,24 @@ static void sortCppIncludes(const FormatStyle &Style,
 }
 result += Includes[Index].Text;
 if (Cursor && CursorIndex == Index)
-  *Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
+  NewCursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;

owenca wrote:

```suggestion
  *Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
```
i.e. undo the change.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-22 Thread Owen Pan via cfe-commits


@@ -3134,20 +3135,24 @@ 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(
  IncludesBeginOffset, IncludesBlockSize {
 return;

owenca wrote:

```suggestion
if (Cursor)
  *Cursor = OldCursor;
return;
```

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-22 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-22 Thread Owen Pan via cfe-commits


@@ -3123,6 +3123,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;

owenca wrote:

```suggestion
  const auto OldCursor = Cursor ? *Cursor : 0;
```

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-02-20 Thread via cfe-commits

NorthBlue333 wrote:

Hi there,
Any update on this?
Friendly ping @owenca or @mydeveloperday.
I think we could maybe divide the issue in two @HazardyKnusperkeks?

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-22 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From a3ddae11b909a319d73fccff409fa5c0648a234f Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 61 -
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ff326dc784783b2..3d8cffaca1ded3c 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3123,6 +3123,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3134,13 +3135,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(
@@ -3148,6 +3146,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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 ec142e03b12854e..d180767b6ff9fab 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of li

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-22 Thread via cfe-commits

NorthBlue333 wrote:

`clang-check` builds in local.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-20 Thread Björn Schäpers via cfe-commits

HazardyKnusperkeks wrote:

> Oh, sorry @HazardyKnusperkeks I missed this in your previous comment.
> 
> I've updated the PR.
> 
> As a side note, I tried adding these two tests but they fail, meaning the 
> cursor is still incorrectly computed with CRLF when replacing lines.
> 
> This is due to adding lines between the include groups (with Regroup option), 
> as this new line is added with only the character `\n` (while the line break 
> between include lines is right because the include line itself contains a 
> trailing `\r`). I am not sure how to fix this correctly because I do not want 
> to break things by trimming the include line and I do not think 
> `sortCppIncludes` should manage whitespaces (`WhitespaceManager` seems to be 
> the correct class to handle this?). Maybe the cursor should be updated when 
> replacing these whitespaces?
> 
> ```c++
> TEST_F(
>SortIncludesTest,
>
> CalculatesCorrectCursorPositionWhenNewLineReplacementsWithRegroupingAndCRLF) {
>   Style.IncludeBlocks   = Style.IBS_Regroup;
>   FmtStyle.LineEnding   = FormatStyle::LE_CRLF;
>   Style.IncludeCategories = {
>   {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
>   std::string Code = "#include \"a\"\r\n" // Start of line: 0
>  "#include \"b\"\r\n" // Start of line: 14
>  "#include \"c\"\r\n" // Start of line: 28
>  "\r\n"   // Start of line: 42
>  "int i;";// Start of line: 44
>   std::string Expected = "#include \"a\"\r\n" // Start of line: 0
>  "\r\n"   // Start of line: 14
>  "#include \"b\"\r\n" // Start of line: 16
>  "\r\n"   // Start of line: 30
>  "#include \"c\"\r\n" // Start of line: 32
>  "\r\n"   // Start of line: 46
>  "int i;";// Start of line: 48
>   EXPECT_EQ(Expected, sort(Code));
>   EXPECT_EQ(0u, newCursor(Code, 0));
>   EXPECT_EQ(15u, newCursor(Code, 14)); // FIXME: should expect 16, caused by 
> \r
>   EXPECT_EQ(30u, newCursor(Code, 28)); // FIXME: should expect 32, caused by 
> \r
>   EXPECT_EQ(44u, newCursor(Code, 42)); // FIXME: should expect 46, caused by 
> \r
>   EXPECT_EQ(46u, newCursor(Code, 44)); // FIXME: should expect 48, caused by 
> \r
> }
> 
> TEST_F(
>SortIncludesTest,
>
> CalculatesCorrectCursorPositionWhenNoNewLineReplacementsWithRegroupingAndCRLF)
>  {
>   Style.IncludeBlocks = Style.IBS_Regroup;
>   FmtStyle.LineEnding = FormatStyle::LE_CRLF;
>   Style.IncludeCategories = {
>   {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
>   std::string Code = "#include \"a\"\r\n" // Start of line: 0
>  "\r\n"   // Start of line: 14
>  "#include \"c\"\r\n" // Start of line: 16
>  "\r\n"   // Start of line: 30
>  "#include \"b\"\r\n" // Start of line: 32
>  "\r\n"   // Start of line: 46
>  "int i;";// Start of line: 48
>   std::string Expected = "#include \"a\"\r\n" // Start of line: 0
>  "\r\n"   // Start of line: 14
>  "#include \"b\"\r\n" // Start of line: 16
>  "\r\n"   // Start of line: 30
>  "#include \"c\"\r\n" // Start of line: 32
>  "\r\n"   // Start of line: 46
>  "int i;";// Start of line: 48
>   EXPECT_EQ(Expected, sort(Code));
>   EXPECT_EQ(0u, newCursor(Code, 0));
>   EXPECT_EQ(14u, newCursor(Code, 14));
>   EXPECT_EQ(30u, newCursor(Code, 16)); // FIXME: should expect 32, caused by 
> \r
>   EXPECT_EQ(30u, newCursor(Code, 30));
>   EXPECT_EQ(15u, newCursor(Code, 32)); // FIXME: should expect 15, caused by 
> \r
>   EXPECT_EQ(44u, newCursor(Code, 46)); // FIXME: should expect 46, caused by 
> \r
>   EXPECT_EQ(46u, newCursor(Code, 48)); // FIXME: should expect 48, caused by 
> \r
> }
> ```

That's outside my comfortable zone and I'll refer to @owenca or @mydeveloperday.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-20 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From ac5dccff5c1922912e88f687d2685d547f4bded9 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 61 -
 2 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7c2f4dcf3d2308..3157831ac44e7e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3121,6 +3121,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3132,13 +3133,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(
@@ -3146,6 +3144,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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..d180767b6ff9fa 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(
+SortIncludesTest,
+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-20 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From c3fa004487895acb96c7eeafbd27696c35f5148e Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 60 +++--
 2 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7c2f4dcf3d2308..3157831ac44e7e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3121,6 +3121,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3132,13 +3133,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(
@@ -3146,6 +3144,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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..6e64572d73975a 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,60 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(SortIncludesTest,
+   
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-20 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 86d92ae648c3d0cee305bdf0bcaadbb19a7f5f1c Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 60 +++--
 2 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7c2f4dcf3d23080..3157831ac44e7e9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3121,6 +3121,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3132,13 +3133,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(
@@ -3146,6 +3144,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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 ec142e03b12854e..4ed06bfb0b8cf67 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,60 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(SortIncludesTest,
+   
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-20 Thread via cfe-commits

NorthBlue333 wrote:

Oh, sorry @HazardyKnusperkeks I missed this in your previous comment.

I've updated the PR.

As a side note, I tried adding these two tests but they fail, meaning the 
cursor is still incorrectly computed with CRLF when replacing lines.

This is due to adding lines between the include groups (with Regroup option), 
as this new line is added with only the character `\n`. The line break between 
include lines is right because the include line itself contains a trailing `\r`.
I am not sure how to fix this correctly because I do not want to break things 
by trimming the include line and I do not think `sortCppIncludes` should manage 
whitespaces (`WhitespaceManager` seems to be the correct class to handle 
this?). Maybe the cursor should be updated when replacing these whitespaces?

```c++
TEST_F(SortIncludesTest,
   
CalculatesCorrectCursorPositionWhenNewLineReplacementsWithRegroupingAndCRLF) {
  Style.IncludeBlocks = Style.IBS_Regroup;
  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
  Style.IncludeCategories = {
  {"^\"a\"", 0, 0, false},
  {"^\"b\"", 1, 1, false},
  {".*", 2, 2, false}
 };
  std::string Code =
  "#include \"a\"\r\n"  // Start of line: 0
  "#include \"b\"\r\n"  // Start of line: 14
  "#include \"c\"\r\n"  // Start of line: 28
  "\r\n"// Start of line: 42
  "int i;"; // Start of line: 44
  std::string Expected =
  "#include \"a\"\r\n"  // Start of line: 0
  "\r\n"// Start of line: 14
  "#include \"b\"\r\n"  // Start of line: 16
  "\r\n"// Start of line: 30
  "#include \"c\"\r\n"  // Start of line: 32
  "\r\n"// Start of line: 46
  "int i;"; // Start of line: 48
  EXPECT_EQ(Expected, sort(Code));
  EXPECT_EQ(0u, newCursor(Code, 0));
  EXPECT_EQ(15u, newCursor(Code, 14)); // FIXME: should expect 16, caused by \r
  EXPECT_EQ(30u, newCursor(Code, 28)); // FIXME: should expect 32, caused by \r
  EXPECT_EQ(44u, newCursor(Code, 42)); // FIXME: should expect 46, caused by \r
  EXPECT_EQ(46u, newCursor(Code, 44)); // FIXME: should expect 48, caused by \r
}

TEST_F(SortIncludesTest,
   
CalculatesCorrectCursorPositionWhenNoNewLineReplacementsWithRegroupingAndCRLF) {
  Style.IncludeBlocks = Style.IBS_Regroup;
  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
  Style.IncludeCategories = {
  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
  std::string Code = "#include \"a\"\r\n" // Start of line: 0
 "\r\n"   // Start of line: 14
 "#include \"c\"\r\n" // Start of line: 16
 "\r\n"   // Start of line: 30
 "#include \"b\"\r\n" // Start of line: 32
 "\r\n"   // Start of line: 46
 "int i;";// Start of line: 48
  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
 "\r\n"   // Start of line: 14
 "#include \"b\"\r\n" // Start of line: 16
 "\r\n"   // Start of line: 30
 "#include \"c\"\r\n" // Start of line: 32
 "\r\n"   // Start of line: 46
 "int i;";// Start of line: 48
  EXPECT_EQ(Expected, sort(Code));
  EXPECT_EQ(0u, newCursor(Code, 0));
  EXPECT_EQ(14u, newCursor(Code, 14));
  EXPECT_EQ(30u, newCursor(Code, 16)); // FIXME: should expect 32, caused by \r
  EXPECT_EQ(30u, newCursor(Code, 30));
  EXPECT_EQ(15u, newCursor(Code, 32)); // FIXME: should expect 15, caused by \r
  EXPECT_EQ(44u, newCursor(Code, 46)); // FIXME: should expect 46, caused by \r
  EXPECT_EQ(46u, newCursor(Code, 48)); // FIXME: should expect 48, caused by \r
}
```

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-20 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 4dab4e9e082551a435b79f5d4647741f4b98168c Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 56 +++--
 2 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7c2f4dcf3d2308..3157831ac44e7e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3121,6 +3121,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3132,13 +3133,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(
@@ -3146,6 +3144,13 @@ static void sortCppIncludes(const FormatStyle &Style,
 return;
   }
 
+  if (Cursor) {
+if (NewCursor != UINT_MAX)
+  *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..7d2dd806b828f9 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,56 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
+TEST_F(SortIncludesTest,
+   
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF)
 {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
+  Style.IncludeCategories = {{".*", 0, 0, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  std::string Expected = "#include \"a\"\r\n" // Start of line: 0
+ "#i

[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-19 Thread Björn Schäpers via cfe-commits


@@ -3132,20 +3133,24 @@ 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(
  IncludesBeginOffset, IncludesBlockSize {
 return;
   }
 
+  if (Cursor) {
+if (UINT_MAX != NewCursor)

HazardyKnusperkeks wrote:

```suggestion
if (NewCursor != UINT_MAX)
```
Still that. :)

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-19 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 1c794e5544d5f4d1a73a4410a00412f445e5c1f9 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 29 ++---
 2 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7c2f4dcf3d2308..ca828c06531383 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3121,6 +3121,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3132,13 +3133,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(
@@ -3146,6 +3144,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..e5e91837bf9193 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,29 @@ 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 = {
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
 TEST_F(SortIncludesTest, DeduplicateIncludes) {
   EXPECT_EQ("#include \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-19 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 42b160356fe5d3b41bf07c428d0142d3721b1d44 
490343ec3461a8a3a5703198f6b9af57a853ef24 -- clang/lib/Format/Format.cpp 
clang/unittests/Format/SortIncludesTest.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 81ec3b245e..ca828c0653 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3140,7 +3140,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   // 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(
-IncludesBeginOffset, IncludesBlockSize {
+ IncludesBeginOffset, IncludesBlockSize {
 return;
   }
 
diff --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index e73770a21e..e5e91837bf 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -821,22 +821,19 @@ 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;
+TEST_F(SortIncludesTest,
+   CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) 
{
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  FmtStyle.LineEnding = FormatStyle::LE_CRLF;
   Style.IncludeCategories = {
- {"^\"a\"", 0, 0, false},
- {"^\"b\"", 1, 1, false},
- {".*", 2, 2, false}};
-  std::string Code =
- "#include \"a\"\r\n"  // Start of line: 0
- "\r\n"// Start of line: 14
- "#include \"b\"\r\n"  // Start of line: 16
- "\r\n"// Start of line: 30
- "#include \"c\"\r\n"  // Start of line: 32
- "\r\n"// Start of line: 46
- "int i;"; // Start of line: 48
+  {"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
+  std::string Code = "#include \"a\"\r\n" // Start of line: 0
+ "\r\n"   // Start of line: 14
+ "#include \"b\"\r\n" // Start of line: 16
+ "\r\n"   // Start of line: 30
+ "#include \"c\"\r\n" // Start of line: 32
+ "\r\n"   // Start of line: 46
+ "int i;";// Start of line: 48
   verifyNoChange(Code);
   EXPECT_EQ(0u, newCursor(Code, 0));
   EXPECT_EQ(14u, newCursor(Code, 14));

``




https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-19 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 7ee61d585faf393921f8964451ddc97e4cfb7444 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 32 +++--
 2 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7c2f4dcf3d2308..ca828c06531383 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3121,6 +3121,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3132,13 +3133,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(
@@ -3146,6 +3144,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..e73770a21e87b5 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,32 @@ 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 = {
+ {"^\"a\"", 0, 0, false},
+ {"^\"b\"", 1, 1, false},
+ {".*", 2, 2, false}};
+  std::string Code =
+ "#include \"a\"\r\n"  // Start of line: 0
+ "\r\n"// Start of line: 14
+ "#include \"b\"\r\n"  // Start of line: 16
+ "\r\n"// Start of line: 30
+ "#include \"c\"\r\n"  // Start of line: 32
+ "\r\n"// Start of line: 46
+ "int i;"; // Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
 TEST_F(SortIncludesTest, DeduplicateIncludes) {
   EXPECT_EQ("#include \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-19 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 490343ec3461a8a3a5703198f6b9af57a853ef24 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 15 ++
 clang/unittests/Format/SortIncludesTest.cpp | 32 +++--
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7c2f4dcf3d2308..81ec3b245e0d95 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3121,6 +3121,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   }
 
   std::string result;
+  unsigned NewCursor = UINT_MAX;
   for (unsigned Index : Indices) {
 if (!result.empty()) {
   result += "\n";
@@ -3132,20 +3133,24 @@ 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(
- IncludesBeginOffset, IncludesBlockSize {
+IncludesBeginOffset, IncludesBlockSize {
 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..e73770a21e87b5 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -6,19 +6,19 @@
 //
 
//===--===//
 
-#include "FormatTestUtils.h"
+#include "FormatTestBase.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 #include "gtest/gtest.h"
 
-#define DEBUG_TYPE "format-test"
+#define DEBUG_TYPE "sort-includes-test"
 
 namespace clang {
 namespace format {
 namespace {
 
-class SortIncludesTest : public ::testing::Test {
+class SortIncludesTest : public test::FormatTestBase {
 protected:
   std::vector GetCodeRange(StringRef Code) {
 return std::vector(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,32 @@ 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 = {
+ {"^\"a\"", 0, 0, false},
+ {"^\"b\"", 1, 1, false},
+ {".*", 2, 2, false}};
+  std::string Code =
+ "#include \"a\"\r\n"  // Start of line: 0
+ "\r\n"// Start of line: 14
+ "#include \"b\"\r\n"  // Start of line: 16
+ "\r\n"// Start of line: 30
+ "#include \"c\"\r\n"  // Start of line: 32
+ "\r\n"// Start of line: 46
+ "int i;"; // Start of line: 48
+  verifyNoChange(Code);
+  EXPECT_EQ(0u, newCursor(Code, 0));
+  EXPECT_EQ(14u, newCursor(Code, 14));
+  EXPECT_EQ(16u, newCursor(Code, 16));
+  EXPECT_EQ(30u, newCursor(Code, 30));
+  EXPECT_EQ(32u, newCursor(Code, 32));
+  EXPECT_EQ(46u, newCursor(Code, 46));
+  EXPECT_EQ(48u, newCursor(Code, 48));
+}
+
 TEST_F(SortIncludesTest, DeduplicateIncludes) {
   EXPECT_EQ("#include \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-10 Thread via cfe-commits

NorthBlue333 wrote:

Currently fixing the tests.

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-10 Thread Owen Pan via cfe-commits

owenca wrote:

> The change is done 👍 Is the code formatting check broken?

See 
[here](https://github.com/llvm/llvm-project/pull/76059#issuecomment-1865850011).

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-10 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 18163c8cad017274adaf8c4e23c0121dc1f3c844 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-10 Thread via cfe-commits

NorthBlue333 wrote:

The change is done 👍 
Is the code formatting check broken?

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-10 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 04258b4ee42e29ec160b7fa4992dd1ad63db77dc Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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..e1fc92bb829246 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::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 \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-10 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 8081b93a021b24c5310bc9a08d00f0d07f1bbfa1 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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..480da513a47fcb 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, 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
+  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 \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-09 Thread Björn Schäpers via cfe-commits


@@ -3131,20 +3132,25 @@ 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(
  IncludesBeginOffset, IncludesBlockSize {
 return;
   }
 
+  if (Cursor) {
+if (UINT_MAX != NewCursor) {
+  *Cursor = NewCursor;
+} else if (*Cursor >= IncludesEndOffset) {
+  *Cursor += result.size() - IncludesBlockSize;
+}

HazardyKnusperkeks wrote:

```suggestion
if (NewCursor != UINT_MAX)
  *Cursor = NewCursor;
else if (*Cursor >= IncludesEndOffset)
  *Cursor += result.size() - IncludesBlockSize;
```

https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-09 Thread via cfe-commits

https://github.com/NorthBlue333 edited 
https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-09 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 263996dbd680d529f54ec576c29f210cd6904926 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 | 24 +
 2 files changed, 34 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..480da513a47fcb 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, 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
+  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 \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-09 Thread via cfe-commits

https://github.com/NorthBlue333 edited 
https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-09 Thread via cfe-commits

https://github.com/NorthBlue333 updated 
https://github.com/llvm/llvm-project/pull/77456

>From 897c90c09f24b6fd8b0c84b57a284cabb77c9e80 Mon Sep 17 00:00:00 2001
From: NorthBlue333 
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 \n"
 "#include \n"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Do not update cursor pos if no includes replacement (PR #77456)

2024-01-09 Thread via cfe-commits

https://github.com/NorthBlue333 edited 
https://github.com/llvm/llvm-project/pull/77456
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits