Skip to content

Commit e3e725b

Browse files
committed
[clang-format] Do not update cursor pos if no includes replacement
1 parent d4fd953 commit e3e725b

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

clang/lib/Format/Format.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,8 +3114,8 @@ static void sortCppIncludes(const FormatStyle &Style,
31143114
return;
31153115
}
31163116

3117+
const auto OldCursor = Cursor ? *Cursor : 0;
31173118
std::string result;
3118-
unsigned NewCursor = UINT_MAX;
31193119
for (unsigned Index : Indices) {
31203120
if (!result.empty()) {
31213121
result += "\n";
@@ -3127,24 +3127,22 @@ static void sortCppIncludes(const FormatStyle &Style,
31273127
}
31283128
result += Includes[Index].Text;
31293129
if (Cursor && CursorIndex == Index)
3130-
NewCursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
3130+
*Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
31313131
CurrentCategory = Includes[Index].Category;
31323132
}
31333133

3134+
if (Cursor && *Cursor >= IncludesEndOffset)
3135+
*Cursor += result.size() - IncludesBlockSize;
3136+
31343137
// If the #includes are out of order, we generate a single replacement fixing
31353138
// the entire range of blocks. Otherwise, no replacement is generated.
31363139
if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
31373140
IncludesBeginOffset, IncludesBlockSize)))) {
3141+
if (Cursor)
3142+
Cursor = OldCursor;
31383143
return;
31393144
}
31403145

3141-
if (Cursor) {
3142-
if (NewCursor != UINT_MAX)
3143-
*Cursor = NewCursor;
3144-
else if (*Cursor >= IncludesEndOffset)
3145-
*Cursor += result.size() - IncludesBlockSize;
3146-
}
3147-
31483146
auto Err = Replaces.add(tooling::Replacement(
31493147
FileName, Includes.front().Offset, IncludesBlockSize, result));
31503148
// FIXME: better error handling. For now, just skip the replacement for the

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,64 @@ TEST_F(
876876
EXPECT_EQ(44u, newCursor(Code, 48));
877877
}
878878

879+
TEST_F(
880+
SortIncludesTest,
881+
CalculatesCorrectCursorPositionWhenNewLineReplacementsWithRegroupingAndCRLF) {
882+
Style.IncludeBlocks = Style.IBS_Regroup;
883+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
884+
Style.IncludeCategories = {
885+
{"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
886+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
887+
"#include \"b\"\r\n" // Start of line: 14
888+
"#include \"c\"\r\n" // Start of line: 28
889+
"\r\n" // Start of line: 42
890+
"int i;"; // Start of line: 44
891+
std::string Expected = "#include \"a\"\r\n" // Start of line: 0
892+
"\r\n" // Start of line: 14
893+
"#include \"b\"\r\n" // Start of line: 16
894+
"\r\n" // Start of line: 30
895+
"#include \"c\"\r\n" // Start of line: 32
896+
"\r\n" // Start of line: 46
897+
"int i;"; // Start of line: 48
898+
EXPECT_EQ(Expected, sort(Code));
899+
EXPECT_EQ(0u, newCursor(Code, 0));
900+
EXPECT_EQ(15u, newCursor(Code, 16));
901+
EXPECT_EQ(30u, newCursor(Code, 32));
902+
EXPECT_EQ(44u, newCursor(Code, 46));
903+
EXPECT_EQ(46u, newCursor(Code, 48));
904+
}
905+
906+
TEST_F(
907+
SortIncludesTest,
908+
CalculatesCorrectCursorPositionWhenNoNewLineReplacementsWithRegroupingAndCRLF) {
909+
Style.IncludeBlocks = Style.IBS_Regroup;
910+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
911+
Style.IncludeCategories = {
912+
{"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
913+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
914+
"\r\n" // Start of line: 14
915+
"#include \"c\"\r\n" // Start of line: 16
916+
"\r\n" // Start of line: 30
917+
"#include \"b\"\r\n" // Start of line: 32
918+
"\r\n" // Start of line: 46
919+
"int i;"; // Start of line: 48
920+
std::string Expected = "#include \"a\"\r\n" // Start of line: 0
921+
"\r\n" // Start of line: 14
922+
"#include \"b\"\r\n" // Start of line: 16
923+
"\r\n" // Start of line: 30
924+
"#include \"c\"\r\n" // Start of line: 32
925+
"\r\n" // Start of line: 46
926+
"int i;"; // Start of line: 48
927+
EXPECT_EQ(Expected, sort(Code));
928+
EXPECT_EQ(0u, newCursor(Code, 0));
929+
EXPECT_EQ(14u, newCursor(Code, 14));
930+
EXPECT_EQ(30u, newCursor(Code, 32));
931+
EXPECT_EQ(30u, newCursor(Code, 30));
932+
EXPECT_EQ(15u, newCursor(Code, 15));
933+
EXPECT_EQ(44u, newCursor(Code, 46));
934+
EXPECT_EQ(46u, newCursor(Code, 48));
935+
}
936+
879937
TEST_F(SortIncludesTest, DeduplicateIncludes) {
880938
EXPECT_EQ("#include <a>\n"
881939
"#include <b>\n"

0 commit comments

Comments
 (0)