[llvm] 13fa17d - [split-file] Respect input file's line endings

Chris Bieneman via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 21 16:45:20 PST 2022


Author: Chris Bieneman
Date: 2022-01-21T18:45:03-06:00
New Revision: 13fa17db3a720d149bcd0783856347a4f09cf634

URL: https://github.com/llvm/llvm-project/commit/13fa17db3a720d149bcd0783856347a4f09cf634
DIFF: https://github.com/llvm/llvm-project/commit/13fa17db3a720d149bcd0783856347a4f09cf634.diff

LOG: [split-file] Respect input file's line endings

This change adds support for split-file to respect the line ending style
of the input file. This enables split-file to work as expected on
Windows with input files containing CRLF line endings.

The test files added along with this change mirror the existing basic
tests, but are forced to contain CRLF line endings via git attributes.
This will result in the tests always containing CRLF line endings when
checked out regardless of the user's OS.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D117897

Added: 
    llvm/test/tools/split-file/Inputs/basic-aa.crlf
    llvm/test/tools/split-file/Inputs/basic-bb.crlf
    llvm/test/tools/split-file/basic.crlf.test

Modified: 
    llvm/.gitattributes
    llvm/tools/split-file/split-file.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/.gitattributes b/llvm/.gitattributes
index 710defda24b62..b07b652eee02e 100644
--- a/llvm/.gitattributes
+++ b/llvm/.gitattributes
@@ -25,3 +25,5 @@ test/tools/llvm-mca/X86/directives-handle-crlf.s text eol=crlf
 test/tools/llvm-strings/radix.test text eol=lf
 test/tools/split-file/basic.test text eol=lf
 test/tools/split-file/Inputs/basic-*.txt eol=lf
+test/tools/split-file/basic.crlf.test text eol=crlf
+test/tools/split-file/Inputs/basic-*.crlf eol=crlf

diff  --git a/llvm/test/tools/split-file/Inputs/basic-aa.crlf b/llvm/test/tools/split-file/Inputs/basic-aa.crlf
new file mode 100644
index 0000000000000..0b9ddeb2fc12a
--- /dev/null
+++ b/llvm/test/tools/split-file/Inputs/basic-aa.crlf
@@ -0,0 +1,2 @@
+
+aa

diff  --git a/llvm/test/tools/split-file/Inputs/basic-bb.crlf b/llvm/test/tools/split-file/Inputs/basic-bb.crlf
new file mode 100644
index 0000000000000..b6c3c808ec62f
--- /dev/null
+++ b/llvm/test/tools/split-file/Inputs/basic-bb.crlf
@@ -0,0 +1,4 @@
+
+
+
+bb

diff  --git a/llvm/test/tools/split-file/basic.crlf.test b/llvm/test/tools/split-file/basic.crlf.test
new file mode 100644
index 0000000000000..f01074a879630
--- /dev/null
+++ b/llvm/test/tools/split-file/basic.crlf.test
@@ -0,0 +1,10 @@
+#--- aa
+aa
+;--- bb
+bb
+;--- end
+
+# RUN: rm -rf %t
+# RUN: split-file --leading-lines %s %t
+# RUN: 
diff  %S/Inputs/basic-aa.crlf %t/aa
+# RUN: 
diff  %S/Inputs/basic-bb.crlf %t/bb

diff  --git a/llvm/tools/split-file/split-file.cpp b/llvm/tools/split-file/split-file.cpp
index bde7d21a51e9a..4a92c1be78a2b 100644
--- a/llvm/tools/split-file/split-file.cpp
+++ b/llvm/tools/split-file/split-file.cpp
@@ -71,6 +71,7 @@ struct Part {
 static int handle(MemoryBuffer &inputBuf, StringRef input) {
   DenseMap<StringRef, Part> partToBegin;
   StringRef lastPart, separator;
+  StringRef EOL = inputBuf.getBuffer().detectEOL();
   for (line_iterator i(inputBuf, /*SkipBlanks=*/false, '\0'); !i.is_at_eof();) {
     const int64_t lineNo = i.line_number();
     const StringRef line = *i++;
@@ -128,7 +129,7 @@ static int handle(MemoryBuffer &inputBuf, StringRef input) {
 
     Part &part = keyValue.second;
     for (int64_t i = 0; i != part.leadingLines; ++i)
-      (*f).os().write('\n');
+      (*f).os() << EOL;
     if (part.begin)
       (*f).os().write(part.begin, part.end - part.begin);
     outputFiles.push_back(std::move(f));


        


More information about the llvm-commits mailing list