Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -1825,6 +1825,22 @@ return std::make_pair(CursorIndex, OffsetToEOL); } +// Replace "\r\n" with "\n" +std::string replaceCRLF(std::string Code) { + size_t Pos = 0; + while ((Pos = Code.find("\r\n", Pos)) != std::string::npos) { + Code.replace(Pos, 2, "\n"); + Pos += 1; + } + return Code; +} + +// Compares two strings but ignores any \r in either strings. +static bool compareIgnoringCarriageReturns(std::string Result, + std::string Code) { + return replaceCRLF(Result) == replaceCRLF(Code); +} + // Sorts and deduplicate a block of includes given by 'Includes' alphabetically // adding the necessary replacement to 'Replaces'. 'Includes' must be in strict // source order. @@ -1898,7 +1914,8 @@ // If the #includes are out of order, we generate a single replacement fixing // the entire range of blocks. Otherwise, no replacement is generated. - if (result == Code.substr(IncludesBeginOffset, IncludesBlockSize)) + if (compareIgnoringCarriageReturns( + result, Code.substr(IncludesBeginOffset, IncludesBlockSize))) return; auto Err = Replaces.add(tooling::Replacement( @@ -2066,7 +2083,8 @@ // If the imports are out of order, we generate a single replacement fixing // the entire block. Otherwise, no replacement is generated. - if (result == Code.substr(Imports.front().Offset, ImportsBlockSize)) + if (compareIgnoringCarriageReturns( + result, Code.substr(Imports.front().Offset, ImportsBlockSize))) return; auto Err = Replaces.add(tooling::Replacement(FileName, Imports.front().Offset, @@ -2356,7 +2374,7 @@ auto Env = std::make_unique(Code, FileName, Ranges, FirstStartColumn, - NextStartColumn, LastStartColumn); + NextStartColumn, LastStartColumn); llvm::Optional CurrentCode = None; tooling::Replacements Fixes; unsigned Penalty = 0; Index: clang/unittests/Format/SortImportsTestJava.cpp =================================================================== --- clang/unittests/Format/SortImportsTestJava.cpp +++ clang/unittests/Format/SortImportsTestJava.cpp @@ -285,6 +285,13 @@ sortIncludes(FmtStyle, Code, GetCodeRange(Code), "input.java").empty()); } +TEST_F(SortImportsTestJava, NoReplacementsForValidImportsWindows) { + std::string Code = "import org.a;\r\n" + "import org.b;\r\n"; + EXPECT_TRUE( + sortIncludes(FmtStyle, Code, GetCodeRange(Code), "input.java").empty()); +} + } // end namespace } // end namespace format } // end namespace clang Index: clang/unittests/Format/SortIncludesTest.cpp =================================================================== --- clang/unittests/Format/SortIncludesTest.cpp +++ clang/unittests/Format/SortIncludesTest.cpp @@ -724,6 +724,14 @@ EXPECT_EQ(Code, sort(Code, "input.h", 0)); } +TEST_F(SortIncludesTest, + DoNotOutputReplacementsForSortedBlocksWithRegroupingWindows) { + Style.IncludeBlocks = Style.IBS_Regroup; + std::string Code = "#include \"b.h\"\r\n" + "\r\n" + "#include \r\n"; + EXPECT_EQ(Code, sort(Code, "input.h", 0)); +} TEST_F(SortIncludesTest, DoNotRegroupGroupsInGoogleObjCStyle) { FmtStyle = getGoogleStyle(FormatStyle::LK_ObjC);