Skip to content

Commit a3ddae1

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

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

clang/lib/Format/Format.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,6 +3123,7 @@ static void sortCppIncludes(const FormatStyle &Style,
31233123
}
31243124

31253125
std::string result;
3126+
unsigned NewCursor = UINT_MAX;
31263127
for (unsigned Index : Indices) {
31273128
if (!result.empty()) {
31283129
result += "\n";
@@ -3134,20 +3135,24 @@ static void sortCppIncludes(const FormatStyle &Style,
31343135
}
31353136
result += Includes[Index].Text;
31363137
if (Cursor && CursorIndex == Index)
3137-
*Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
3138+
NewCursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
31383139
CurrentCategory = Includes[Index].Category;
31393140
}
31403141

3141-
if (Cursor && *Cursor >= IncludesEndOffset)
3142-
*Cursor += result.size() - IncludesBlockSize;
3143-
31443142
// If the #includes are out of order, we generate a single replacement fixing
31453143
// the entire range of blocks. Otherwise, no replacement is generated.
31463144
if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
31473145
IncludesBeginOffset, IncludesBlockSize)))) {
31483146
return;
31493147
}
31503148

3149+
if (Cursor) {
3150+
if (NewCursor != UINT_MAX)
3151+
*Cursor = NewCursor;
3152+
else if (*Cursor >= IncludesEndOffset)
3153+
*Cursor += result.size() - IncludesBlockSize;
3154+
}
3155+
31513156
auto Err = Replaces.add(tooling::Replacement(
31523157
FileName, Includes.front().Offset, IncludesBlockSize, result));
31533158
// FIXME: better error handling. For now, just skip the replacement for the

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "FormatTestUtils.h"
9+
#include "FormatTestBase.h"
1010
#include "clang/Format/Format.h"
1111
#include "llvm/ADT/StringRef.h"
1212
#include "llvm/Support/Debug.h"
1313
#include "gtest/gtest.h"
1414

15-
#define DEBUG_TYPE "format-test"
15+
#define DEBUG_TYPE "sort-includes-test"
1616

1717
namespace clang {
1818
namespace format {
1919
namespace {
2020

21-
class SortIncludesTest : public ::testing::Test {
21+
class SortIncludesTest : public test::FormatTestBase {
2222
protected:
2323
std::vector<tooling::Range> GetCodeRange(StringRef Code) {
2424
return std::vector<tooling::Range>(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,61 @@ TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
821821
EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
822822
}
823823

824+
TEST_F(SortIncludesTest,
825+
CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) {
826+
Style.IncludeBlocks = Style.IBS_Regroup;
827+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
828+
Style.IncludeCategories = {
829+
{"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
830+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
831+
"\r\n" // Start of line: 14
832+
"#include \"b\"\r\n" // Start of line: 16
833+
"\r\n" // Start of line: 30
834+
"#include \"c\"\r\n" // Start of line: 32
835+
"\r\n" // Start of line: 46
836+
"int i;"; // Start of line: 48
837+
verifyNoChange(Code);
838+
EXPECT_EQ(0u, newCursor(Code, 0));
839+
EXPECT_EQ(14u, newCursor(Code, 14));
840+
EXPECT_EQ(16u, newCursor(Code, 16));
841+
EXPECT_EQ(30u, newCursor(Code, 30));
842+
EXPECT_EQ(32u, newCursor(Code, 32));
843+
EXPECT_EQ(46u, newCursor(Code, 46));
844+
EXPECT_EQ(48u, newCursor(Code, 48));
845+
}
846+
847+
TEST_F(
848+
SortIncludesTest,
849+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF) {
850+
Style.IncludeBlocks = Style.IBS_Regroup;
851+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
852+
Style.IncludeCategories = {{".*", 0, 0, false}};
853+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
854+
"\r\n" // Start of line: 14
855+
"#include \"b\"\r\n" // Start of line: 16
856+
"\r\n" // Start of line: 30
857+
"#include \"c\"\r\n" // Start of line: 32
858+
"\r\n" // Start of line: 46
859+
"int i;"; // Start of line: 48
860+
std::string Expected = "#include \"a\"\r\n" // Start of line: 0
861+
"#include \"b\"\r\n" // Start of line: 14
862+
"#include \"c\"\r\n" // Start of line: 28
863+
"\r\n" // Start of line: 42
864+
"int i;"; // Start of line: 44
865+
EXPECT_EQ(Expected, sort(Code));
866+
EXPECT_EQ(0u, newCursor(Code, 0));
867+
EXPECT_EQ(
868+
14u,
869+
newCursor(Code, 14)); // cursor on empty line in include block is ignored
870+
EXPECT_EQ(14u, newCursor(Code, 16));
871+
EXPECT_EQ(
872+
30u,
873+
newCursor(Code, 30)); // cursor on empty line in include block is ignored
874+
EXPECT_EQ(28u, newCursor(Code, 32));
875+
EXPECT_EQ(42u, newCursor(Code, 46));
876+
EXPECT_EQ(44u, newCursor(Code, 48));
877+
}
878+
824879
TEST_F(SortIncludesTest, DeduplicateIncludes) {
825880
EXPECT_EQ("#include <a>\n"
826881
"#include <b>\n"

0 commit comments

Comments
 (0)