llvm-reduce: Refine missing argument behavior
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Fri, 9 Dec 2022 16:02:49 +0000 (11:02 -0500)
committerMatt Arsenault <Matthew.Arsenault@amd.com>
Tue, 3 Jan 2023 21:01:36 +0000 (16:01 -0500)
We required the test and input arguments for --print-delta-passes
which is unhelpful. Also, start printing the help output if no
arguments were supplied.

It looks like there's more sophisticated ways to accomplish this with
the opt library, but it was less work to manually emit these errors.

llvm/test/tools/llvm-reduce/command-line-behavior.test [new file with mode: 0644]
llvm/tools/llvm-reduce/ReducerWorkItem.cpp
llvm/tools/llvm-reduce/ReducerWorkItem.h
llvm/tools/llvm-reduce/TestRunner.cpp
llvm/tools/llvm-reduce/TestRunner.h
llvm/tools/llvm-reduce/deltas/Delta.cpp
llvm/tools/llvm-reduce/llvm-reduce.cpp

diff --git a/llvm/test/tools/llvm-reduce/command-line-behavior.test b/llvm/test/tools/llvm-reduce/command-line-behavior.test
new file mode 100644 (file)
index 0000000..264b826
--- /dev/null
@@ -0,0 +1,12 @@
+# Print the help output if no arguments are specified
+# RUN: llvm-reduce --help | grep "LLVM automatic testcase reducer"
+# RUN: llvm-reduce | grep "LLVM automatic testcase reducer"
+
+# Don't require any other arguments for --print-delta-passes
+# RUN: llvm-reduce --print-delta-passes  | grep "Delta passes (pass to \`--delta-passes=\` as a comma separated list)"
+
+# Missing test input
+# RUN: not llvm-reduce --test FileCheck 2>&1 | grep "error: reduction testcase positional argument must be specified"
+
+# Missing test script
+# RUN: not llvm-reduce some-input 2>&1 | grep "error: --test option must be specified"
index 2608710..e86c2ad 100644 (file)
@@ -39,7 +39,8 @@ static cl::opt<std::string> TargetTriple("mtriple",
                                          cl::desc("Set the target triple"),
                                          cl::cat(LLVMReduceOptions));
 
-void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx, const char *ToolName);
+void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx,
+                 StringRef ToolName);
 
 static void cloneFrameInfo(
     MachineFrameInfo &DstMFI, const MachineFrameInfo &SrcMFI,
@@ -387,9 +388,8 @@ static void initializeTargetInfo() {
 }
 
 std::pair<std::unique_ptr<ReducerWorkItem>, bool>
-parseReducerWorkItem(const char *ToolName, StringRef Filename,
-                     LLVMContext &Ctxt, std::unique_ptr<TargetMachine> &TM,
-                     bool IsMIR) {
+parseReducerWorkItem(StringRef ToolName, StringRef Filename, LLVMContext &Ctxt,
+                     std::unique_ptr<TargetMachine> &TM, bool IsMIR) {
   bool IsBitcode = false;
   Triple TheTriple;
 
@@ -456,7 +456,7 @@ parseReducerWorkItem(const char *ToolName, StringRef Filename,
                   (const unsigned char *)(*MB)->getBufferEnd())) {
       std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
       if (!Result) {
-        Err.print(ToolName, errs());
+        Err.print(ToolName.data(), errs());
         return {nullptr, false};
       }
       MMM->M = std::move(Result);
index e0e3d9f..ec19e3a 100644 (file)
@@ -42,9 +42,8 @@ private:
 };
 
 std::pair<std::unique_ptr<ReducerWorkItem>, bool>
-parseReducerWorkItem(const char *ToolName, StringRef Filename,
-                     LLVMContext &Ctxt, std::unique_ptr<TargetMachine> &TM,
-                     bool IsMIR);
+parseReducerWorkItem(StringRef ToolName, StringRef Filename, LLVMContext &Ctxt,
+                     std::unique_ptr<TargetMachine> &TM, bool IsMIR);
 
 std::unique_ptr<ReducerWorkItem>
 cloneReducerWorkItem(const ReducerWorkItem &MMM, const TargetMachine *TM);
index 3d486f8..3a5483c 100644 (file)
@@ -21,7 +21,7 @@ using namespace llvm;
 TestRunner::TestRunner(StringRef TestName,
                        const std::vector<std::string> &TestArgs,
                        std::unique_ptr<ReducerWorkItem> Program,
-                       std::unique_ptr<TargetMachine> TM, const char *ToolName,
+                       std::unique_ptr<TargetMachine> TM, StringRef ToolName,
                        StringRef OutputName, bool InputIsBitcode,
                        bool OutputBitcode)
     : TestName(TestName), ToolName(ToolName), TestArgs(TestArgs),
index 5530cbb..128eede 100644 (file)
@@ -28,7 +28,7 @@ class TestRunner {
 public:
   TestRunner(StringRef TestName, const std::vector<std::string> &TestArgs,
              std::unique_ptr<ReducerWorkItem> Program,
-             std::unique_ptr<TargetMachine> TM, const char *ToolName,
+             std::unique_ptr<TargetMachine> TM, StringRef ToolName,
              StringRef OutputFilename, bool InputIsBitcode, bool OutputBitcode);
 
   /// Runs the interesting-ness test for the specified file
@@ -42,7 +42,7 @@ public:
 
   const TargetMachine *getTargetMachine() const { return TM.get(); }
 
-  const char *getToolName() const { return ToolName; }
+  StringRef getToolName() const { return ToolName; }
 
   void writeOutput(StringRef Message);
 
@@ -52,7 +52,7 @@ public:
 
 private:
   StringRef TestName;
-  const char *ToolName;
+  StringRef ToolName;
   const std::vector<std::string> &TestArgs;
   std::unique_ptr<ReducerWorkItem> Program;
   std::unique_ptr<TargetMachine> TM;
index da9278d..0def68c 100644 (file)
@@ -63,7 +63,7 @@ unsigned NumJobs = 1;
 void writeBitcode(ReducerWorkItem &M, raw_ostream &OutStream);
 
 void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx,
-                 const char *ToolName);
+                 StringRef ToolName);
 
 bool isReduced(ReducerWorkItem &M, const TestRunner &Test) {
   const bool UseBitcode = Test.inputIsBitcode() || TmpFilesAsBitcode;
index 3be603d..29c36fb 100644 (file)
@@ -45,18 +45,19 @@ static cl::opt<bool> PreserveDebugEnvironment(
              "debugging (crash reports, llvm-symbolizer and core dumps)"),
     cl::cat(LLVMReduceOptions));
 
+// FIXME: should be able to do this without other arguments, also add in help.
 static cl::opt<bool>
     PrintDeltaPasses("print-delta-passes",
                      cl::desc("Print list of delta passes, passable to "
                               "--delta-passes as a comma separated list"),
                      cl::cat(LLVMReduceOptions));
 
-static cl::opt<std::string> InputFilename(cl::Positional, cl::Required,
+static cl::opt<std::string> InputFilename(cl::Positional,
                                           cl::desc("<input llvm ll/bc file>"),
                                           cl::cat(LLVMReduceOptions));
 
 static cl::opt<std::string>
-    TestFilename("test", cl::Required,
+    TestFilename("test",
                  cl::desc("Name of the interesting-ness test to be run"),
                  cl::cat(LLVMReduceOptions));
 
@@ -137,7 +138,8 @@ static std::pair<StringRef, bool> determineOutputType(bool IsMIR,
   return {OutputFilename, OutputBitcode};
 }
 
-void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx, const char *ToolName) {
+void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx,
+                 StringRef ToolName) {
   Expected<BitcodeFileContents> IF = llvm::getBitcodeFileContents(Data);
   if (!IF) {
     WithColor::error(errs(), ToolName) << IF.takeError();
@@ -156,10 +158,21 @@ void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx, con
 
 int main(int Argc, char **Argv) {
   InitLLVM X(Argc, Argv);
+  const StringRef ToolName(Argv[0]);
 
   cl::HideUnrelatedOptions({&LLVMReduceOptions, &getColorCategory()});
   cl::ParseCommandLineOptions(Argc, Argv, "LLVM automatic testcase reducer.\n");
 
+  if (Argc == 1) {
+    cl::PrintHelpMessage();
+    return 0;
+  }
+
+  if (PrintDeltaPasses) {
+    printDeltaPasses(outs());
+    return 0;
+  }
+
   bool ReduceModeMIR = false;
   if (InputLanguage != InputLanguages::None) {
     if (InputLanguage == InputLanguages::MIR)
@@ -168,9 +181,15 @@ int main(int Argc, char **Argv) {
     ReduceModeMIR = true;
   }
 
-  if (PrintDeltaPasses) {
-    printDeltaPasses(errs());
-    return 0;
+  if (InputFilename.empty()) {
+    WithColor::error(errs(), ToolName)
+        << "reduction testcase positional argument must be specified\n";
+    return 1;
+  }
+
+  if (TestFilename.empty()) {
+    WithColor::error(errs(), ToolName) << "--test option must be specified\n";
+    return 1;
   }
 
   if (!PreserveDebugEnvironment)
@@ -180,7 +199,7 @@ int main(int Argc, char **Argv) {
   std::unique_ptr<TargetMachine> TM;
 
   auto [OriginalProgram, InputIsBitcode] =
-      parseReducerWorkItem(Argv[0], InputFilename, Context, TM, ReduceModeMIR);
+      parseReducerWorkItem(ToolName, InputFilename, Context, TM, ReduceModeMIR);
   if (!OriginalProgram) {
     return 1;
   }
@@ -192,7 +211,7 @@ int main(int Argc, char **Argv) {
 
   // Initialize test environment
   TestRunner Tester(TestFilename, TestArguments, std::move(OriginalProgram),
-                    std::move(TM), Argv[0], OutputFilename, InputIsBitcode,
+                    std::move(TM), ToolName, OutputFilename, InputIsBitcode,
                     OutputBitcode);
 
   // This parses and writes out the testcase into a temporary file copy for the