diff --git a/llvm/.gitattributes b/llvm/.gitattributes --- a/llvm/.gitattributes +++ b/llvm/.gitattributes @@ -25,3 +25,5 @@ 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 --- /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 --- /dev/null +++ b/llvm/test/tools/split-file/Inputs/basic-bb.crlf @@ -0,0 +1,6 @@ + + + +; Comments are preserved. +bb + diff --git a/llvm/test/tools/split-file/Inputs/basic-cc.crlf b/llvm/test/tools/split-file/Inputs/basic-cc.crlf new file mode 100644 --- /dev/null +++ b/llvm/test/tools/split-file/Inputs/basic-cc.crlf @@ -0,0 +1,8 @@ + + + + + + + +cc diff --git a/llvm/test/tools/split-file/basic.crlf.test b/llvm/test/tools/split-file/basic.crlf.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/split-file/basic.crlf.test @@ -0,0 +1,40 @@ +#--- aa +aa +;--- bb +; Comments are preserved. +bb + +//--- subdir/cc +cc +//--- 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 +# RUN: diff %S/Inputs/basic-cc.crlf %t/subdir/cc +# RUN: FileCheck %s --check-prefix=END < %t/end + +## Can be called on a non-empty directory. +# RUN: split-file --leading-lines %s %t +# RUN: diff %S/Inputs/basic-aa.crlf %t/aa + +## Test that we will delete the output if it is a file, so that we can create +## a directory. +# RUN: rm -rf %t && touch %t +# RUN: split-file --leading-lines %s %t +# RUN: diff %S/Inputs/basic-aa.crlf %t/aa + +# END: RUN: split-file %s %t + +# RUN: not %split-file 2>&1 | FileCheck %s --check-prefix=NO_INPUT + +# NO_INPUT: split-file: error: input filename is not specified + +# RUN: not %split-file %s '' 2>&1 | FileCheck %s --check-prefix=NO_OUTPUT + +# NO_OUTPUT: split-file: error: output directory is not specified + +# RUN: not %split-file %S/Inputs/basic-aa.crlf %t 2>&1 | FileCheck %s --check-prefix=NOT_EXIST + +# NOT_EXIST: split-file: error: {{.*}}: no part separator was found diff --git a/llvm/tools/split-file/split-file.cpp b/llvm/tools/split-file/split-file.cpp --- a/llvm/tools/split-file/split-file.cpp +++ b/llvm/tools/split-file/split-file.cpp @@ -71,6 +71,7 @@ static int handle(MemoryBuffer &inputBuf, StringRef input) { DenseMap 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 @@ 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));