diff --git a/llvm/test/tools/split-file/empty-with-comments.test b/llvm/test/tools/split-file/empty-with-comments.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/split-file/empty-with-comments.test @@ -0,0 +1,4 @@ +# RUN: split-file --no-leading-lines --allow-comments %s %t +# RUN: count 0 < %t/empty + +#--- empty: comment diff --git a/llvm/test/tools/split-file/empty-with-extension.test b/llvm/test/tools/split-file/empty-with-extension.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/split-file/empty-with-extension.test @@ -0,0 +1,7 @@ +# RUN: split-file --no-leading-lines --add-file-extension=txt %s %t +# RUN: count 0 < %t/empty.txt +# RUN: rm -rf %t +# RUN: split-file --no-leading-lines --add-file-extension=.txt %s %t +# RUN: count 0 < %t/empty.txt + +#--- empty diff --git a/llvm/test/tools/split-file/help.test b/llvm/test/tools/split-file/help.test --- a/llvm/test/tools/split-file/help.test +++ b/llvm/test/tools/split-file/help.test @@ -3,4 +3,7 @@ CHECK: USAGE: split-file [options] filename directory CHECK: Generic Options: CHECK: split-file Options: +CHECK: --add-file-extension= +CHECK: --allow-comments +CHECK: --leading-lines CHECK: --no-leading-lines diff --git a/llvm/utils/split-file/split-file.cpp b/llvm/utils/split-file/split-file.cpp --- a/llvm/utils/split-file/split-file.cpp +++ b/llvm/utils/split-file/split-file.cpp @@ -43,6 +43,15 @@ cl::desc("Don't preserve line numbers (default)"), cl::cat(cat)); +static cl::opt addFileExtension("add-file-extension", + cl::desc("Add an extension to output file names"), + cl::value_desc("ext"), + cl::cat(cat)); + +static cl::opt allowComments("allow-comments", + cl::desc("Allow comments to be introduced with a colon after the part name"), + cl::cat(cat)); + static StringRef toolName; static int errorCount; @@ -80,7 +89,15 @@ line.substr(markerLen - 4).startswith("--- "))) continue; separator = line.substr(0, markerLen); - const StringRef partName = line.substr(markerLen); + const StringRef partName = [&] { + StringRef partName = line.substr(markerLen); + if (allowComments) { + auto colonPos = partName.find(':'); + if (colonPos != StringRef::npos) + partName = partName.substr(0, colonPos); + } + return partName; + }(); if (partName.empty()) { error(input, lineNo, "empty part name"); continue; @@ -118,6 +135,11 @@ for (auto &keyValue : partToBegin) { partPath.clear(); sys::path::append(partPath, output, keyValue.first); + if (!addFileExtension.empty()) { + if (addFileExtension.getValue()[0] != '.') + partPath.append("."); + partPath.append(addFileExtension.getValue()); + } std::error_code ec = sys::fs::create_directories(sys::path::parent_path(partPath)); if (ec)