[PATCH] D150990: [split-file] Add flags to support comments and adding extension to output files

Vlad Serebrennikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 31 03:00:45 PDT 2023


Endill updated this revision to Diff 526978.
Endill added a comment.

Add tests
@MaskRay please let me know if I missed anything


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150990/new/

https://reviews.llvm.org/D150990

Files:
  llvm/test/tools/split-file/empty-with-comments.test
  llvm/test/tools/split-file/empty-with-extension.test
  llvm/test/tools/split-file/help.test
  llvm/utils/split-file/split-file.cpp


Index: llvm/utils/split-file/split-file.cpp
===================================================================
--- llvm/utils/split-file/split-file.cpp
+++ 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<std::string> addFileExtension("add-file-extension",
+                                             cl::desc("Add an extension to output file names"),
+                                             cl::value_desc("ext"),
+                                             cl::cat(cat));
+
+static cl::opt<bool> 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)
Index: llvm/test/tools/split-file/help.test
===================================================================
--- llvm/test/tools/split-file/help.test
+++ 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=<ext>
+CHECK:   --allow-comments
+CHECK:   --leading-lines
 CHECK:   --no-leading-lines
Index: llvm/test/tools/split-file/empty-with-extension.test
===================================================================
--- /dev/null
+++ 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
Index: llvm/test/tools/split-file/empty-with-comments.test
===================================================================
--- /dev/null
+++ 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150990.526978.patch
Type: text/x-patch
Size: 3157 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230531/2f191e59/attachment.bin>


More information about the llvm-commits mailing list