[PATCH] D68227: [clang-format] [PR43372] - clang-format shows replacements in DOS files when no replacement is needed
Phabricator via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 1 13:21:49 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL373388: [clang-format] [PR43372] - clang-format shows replacements in DOS files when no… (authored by paulhoad, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Changed prior to commit:
https://reviews.llvm.org/D68227?vs=222665&id=222683#toc
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D68227/new/
https://reviews.llvm.org/D68227
Files:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/SortImportsTestJava.cpp
cfe/trunk/unittests/Format/SortIncludesTest.cpp
Index: cfe/trunk/lib/Format/Format.cpp
===================================================================
--- cfe/trunk/lib/Format/Format.cpp
+++ cfe/trunk/lib/Format/Format.cpp
@@ -1825,6 +1825,28 @@
return std::make_pair(CursorIndex, OffsetToEOL);
}
+// Replace all "\r\n" with "\n".
+std::string replaceCRLF(const std::string &Code) {
+ std::string NewCode;
+ size_t Pos = 0, LastPos = 0;
+
+ do {
+ Pos = Code.find("\r\n", LastPos);
+ if (Pos == LastPos) {
+ LastPos++;
+ continue;
+ }
+ if (Pos == std::string::npos) {
+ NewCode += Code.substr(LastPos);
+ break;
+ }
+ NewCode += Code.substr(LastPos, Pos - LastPos) + "\n";
+ LastPos = Pos + 2;
+ } while (Pos != std::string::npos);
+
+ return NewCode;
+}
+
// 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 +1920,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 (replaceCRLF(result) ==
+ replaceCRLF(Code.substr(IncludesBeginOffset, IncludesBlockSize)))
return;
auto Err = Replaces.add(tooling::Replacement(
@@ -2066,7 +2089,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 (replaceCRLF(result) ==
+ replaceCRLF(Code.substr(Imports.front().Offset, ImportsBlockSize)))
return;
auto Err = Replaces.add(tooling::Replacement(FileName, Imports.front().Offset,
@@ -2356,7 +2380,7 @@
auto Env =
std::make_unique<Environment>(Code, FileName, Ranges, FirstStartColumn,
- NextStartColumn, LastStartColumn);
+ NextStartColumn, LastStartColumn);
llvm::Optional<std::string> CurrentCode = None;
tooling::Replacements Fixes;
unsigned Penalty = 0;
Index: cfe/trunk/unittests/Format/SortIncludesTest.cpp
===================================================================
--- cfe/trunk/unittests/Format/SortIncludesTest.cpp
+++ cfe/trunk/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 <a.h>\r\n";
+ EXPECT_EQ(Code, sort(Code, "input.h", 0));
+}
TEST_F(SortIncludesTest, DoNotRegroupGroupsInGoogleObjCStyle) {
FmtStyle = getGoogleStyle(FormatStyle::LK_ObjC);
Index: cfe/trunk/unittests/Format/SortImportsTestJava.cpp
===================================================================
--- cfe/trunk/unittests/Format/SortImportsTestJava.cpp
+++ cfe/trunk/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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68227.222683.patch
Type: text/x-patch
Size: 3569 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191001/cb964118/attachment.bin>
More information about the llvm-commits
mailing list