-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format] Do not update cursor pos if no includes replacement #77456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-format] Do not update cursor pos if no includes replacement #77456
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-format Author: None (NorthBlue333) ChangesHello, Also, a similar fix might be required on Full diff: https://github.com/llvm/llvm-project/pull/77456.diff 1 Files Affected:
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
|
e21cf67
to
4890743
Compare
897c90c
to
263996d
Compare
8081b93
to
04258b4
Compare
The change is done 👍 |
04258b4
to
18163c8
Compare
See here. |
Currently fixing the tests. |
490343e
to
7ee61d5
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
7ee61d5
to
1c794e5
Compare
1c794e5
to
4dab4e9
Compare
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 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
} |
c3fa004
to
ac5dccf
Compare
That's outside my comfortable zone and I'll refer to @owenca or @mydeveloperday. |
Updating the PR with the latest commits of main branch. |
ac5dccf
to
a3ddae1
Compare
Hi there, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
}
With my suggested fix, these tests should also pass. |
e3e725b
to
93d5711
Compare
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. It does not fix the tests I mentioned. |
Friendly ping again here, as the status on this has been asked again by my team! :) |
It's been a while, but IIRC your original fix was not equivalent to my suggestion, which IMO is simpler and cleaner.
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.:
|
Signed-off-by: NorthBlue333 <[email protected]>
aecbb12
to
6c184f9
Compare
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! |
Unfortunately, this wiped out my updates that fixed a formatting error and added |
Fix #77450.