Skip to content

Commit 2de0bed

Browse files
NorthBlue333owenca
andauthored
[clang-format] Do not update cursor pos if no includes replacement (#77456)
Fix #77450. --------- Signed-off-by: NorthBlue333 <[email protected]> Co-authored-by: Owen Pan <[email protected]>
1 parent 59bfc31 commit 2de0bed

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,7 @@ static void sortCppIncludes(const FormatStyle &Style,
31163116
return;
31173117
}
31183118

3119+
const auto OldCursor = Cursor ? *Cursor : 0;
31193120
std::string result;
31203121
for (unsigned Index : Indices) {
31213122
if (!result.empty()) {
@@ -3139,6 +3140,8 @@ static void sortCppIncludes(const FormatStyle &Style,
31393140
// the entire range of blocks. Otherwise, no replacement is generated.
31403141
if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
31413142
IncludesBeginOffset, IncludesBlockSize)))) {
3143+
if (Cursor)
3144+
*Cursor = OldCursor;
31423145
return;
31433146
}
31443147

clang/unittests/Format/SortIncludesTest.cpp

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

0 commit comments

Comments
 (0)