[llvm] 6278fba - llvm-ranlib/nm: Don't print usage message except for usage errors

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 8 15:17:19 PST 2019


Author: Sam Clegg
Date: 2019-11-08T15:17:07-08:00
New Revision: 6278fba9b11751b97c6091049341c51226c5b434

URL: https://github.com/llvm/llvm-project/commit/6278fba9b11751b97c6091049341c51226c5b434
DIFF: https://github.com/llvm/llvm-project/commit/6278fba9b11751b97c6091049341c51226c5b434.diff

LOG: llvm-ranlib/nm: Don't print usage message except for usage errors

Also, fix a bug in ranlib where it didn't correctly detect being run
without any argument and would try to operate on the empty string.

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

Added: 
    llvm/test/tools/llvm-ranlib/bad-usage.test
    llvm/test/tools/llvm-ranlib/help-message.test

Modified: 
    llvm/test/tools/llvm-ar/invalid-object-file.test
    llvm/tools/llvm-ar/llvm-ar.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-ar/invalid-object-file.test b/llvm/test/tools/llvm-ar/invalid-object-file.test
index d967d8763a92..f7cdc626662d 100644
--- a/llvm/test/tools/llvm-ar/invalid-object-file.test
+++ b/llvm/test/tools/llvm-ar/invalid-object-file.test
@@ -6,3 +6,6 @@
 
 # CHECK:     error: unable to load '[[FILE]]': file too small to be an archive
 # CHECK-NOT: {{.}}
+
+## Also test that errors like this (e.g. invlid input files) don't generate a usage message
+# CHECK-NOT: USAGE: llvm-ar

diff  --git a/llvm/test/tools/llvm-ranlib/bad-usage.test b/llvm/test/tools/llvm-ranlib/bad-usage.test
new file mode 100644
index 000000000000..dd5e2e9e501e
--- /dev/null
+++ b/llvm/test/tools/llvm-ranlib/bad-usage.test
@@ -0,0 +1,7 @@
+## Test that useage message is shown when no input file is given
+## And that we return non-zero.
+
+# RUN: not llvm-ranlib 2>&1 | FileCheck %s
+
+# CHECK: error: an archive name must be specified
+# CHECK: USAGE: llvm-ranlib

diff  --git a/llvm/test/tools/llvm-ranlib/help-message.test b/llvm/test/tools/llvm-ranlib/help-message.test
new file mode 100644
index 000000000000..885638a53fcd
--- /dev/null
+++ b/llvm/test/tools/llvm-ranlib/help-message.test
@@ -0,0 +1,8 @@
+## Show that the help message for llvm-ranlib can be printed with either the
+## long flag -help.
+
+# RUN: llvm-ranlib -h | FileCheck %s
+# RUN: llvm-ranlib -help | FileCheck %s
+# RUN: llvm-ranlib --help | FileCheck %s
+
+# CHECK: USAGE: llvm-ranlib

diff  --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp
index 18cad21f9644..1fdc433047f7 100644
--- a/llvm/tools/llvm-ar/llvm-ar.cpp
+++ b/llvm/tools/llvm-ar/llvm-ar.cpp
@@ -56,20 +56,18 @@ static StringRef ToolName;
 // The basename of this program.
 static StringRef Stem;
 
-const char RanlibHelp[] = R"(
-OVERVIEW: LLVM Ranlib (llvm-ranlib)
+const char RanlibHelp[] = R"(OVERVIEW: LLVM Ranlib (llvm-ranlib)
 
   This program generates an index to speed access to archives
 
 USAGE: llvm-ranlib <archive-file>
 
 OPTIONS:
-  -help                             - Display available options
-  -version                          - Display the version of this program
+  -h --help                         - Display available options
+  --version                         - Display the version of this program
 )";
 
-const char ArHelp[] = R"(
-OVERVIEW: LLVM Archiver
+const char ArHelp[] = R"(OVERVIEW: LLVM Archiver
 
 USAGE: llvm-ar [options] [-]<operation>[modifiers] [relpos] [count] <archive> [files]
        llvm-ar -M [<mri-script]
@@ -127,6 +125,13 @@ void printHelpMessage() {
 static unsigned MRILineNumber;
 static bool ParsingMRIScript;
 
+// Show the error plus the usage message, and exit.
+LLVM_ATTRIBUTE_NORETURN static void badUsage(Twine Error) {
+  WithColor::error(errs(), ToolName) << Error << "\n";
+  printHelpMessage();
+  exit(1);
+}
+
 // Show the error message and exit.
 LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
   if (ParsingMRIScript) {
@@ -134,9 +139,7 @@ LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
         << "script line " << MRILineNumber << ": " << Error << "\n";
   } else {
     WithColor::error(errs(), ToolName) << Error << "\n";
-    printHelpMessage();
   }
-
   exit(1);
 }
 
@@ -239,19 +242,19 @@ static void getRelPos() {
 // associated with the N modifier
 static void getCountParam() {
   if (PositionalArgs.empty())
-    fail("expected [count] for 'N' modifier");
+    badUsage("expected [count] for 'N' modifier");
   auto CountParamArg = StringRef(PositionalArgs[0]);
   if (CountParamArg.getAsInteger(10, CountParam))
-    fail("value for [count] must be numeric, got: " + CountParamArg);
+    badUsage("value for [count] must be numeric, got: " + CountParamArg);
   if (CountParam < 1)
-    fail("value for [count] must be positive, got: " + CountParamArg);
+    badUsage("value for [count] must be positive, got: " + CountParamArg);
   PositionalArgs.erase(PositionalArgs.begin());
 }
 
 // Get the archive file name from the command line
 static void getArchive() {
   if (PositionalArgs.empty())
-    fail("an archive name must be specified");
+    badUsage("an archive name must be specified");
   ArchiveName = PositionalArgs[0];
   PositionalArgs.erase(PositionalArgs.begin());
 }
@@ -276,7 +279,7 @@ static void runMRIScript();
 static ArchiveOperation parseCommandLine() {
   if (MRI) {
     if (!PositionalArgs.empty() || !Options.empty())
-      fail("cannot mix -M and other options");
+      badUsage("cannot mix -M and other options");
     runMRIScript();
   }
 
@@ -389,7 +392,7 @@ static ArchiveOperation parseCommandLine() {
       printHelpMessage();
       exit(0);
     default:
-      fail(std::string("unknown option ") + Options[i]);
+      badUsage(std::string("unknown option ") + Options[i]);
     }
   }
 
@@ -404,31 +407,31 @@ static ArchiveOperation parseCommandLine() {
     NumOperations = 1;
     Operation = CreateSymTab;
     if (!Members.empty())
-      fail("the 's' operation takes only an archive as argument");
+      badUsage("the 's' operation takes only an archive as argument");
   }
 
   // Perform various checks on the operation/modifier specification
   // to make sure we are dealing with a legal request.
   if (NumOperations == 0)
-    fail("you must specify at least one of the operations");
+    badUsage("you must specify at least one of the operations");
   if (NumOperations > 1)
-    fail("only one operation may be specified");
+    badUsage("only one operation may be specified");
   if (NumPositional > 1)
-    fail("you may only specify one of 'a', 'b', and 'i' modifiers");
+    badUsage("you may only specify one of 'a', 'b', and 'i' modifiers");
   if (AddAfter || AddBefore)
     if (Operation != Move && Operation != ReplaceOrInsert)
-      fail("the 'a', 'b' and 'i' modifiers can only be specified with "
-           "the 'm' or 'r' operations");
+      badUsage("the 'a', 'b' and 'i' modifiers can only be specified with "
+               "the 'm' or 'r' operations");
   if (CountParam)
     if (Operation != Extract && Operation != Delete)
-      fail("the 'N' modifier can only be specified with the 'x' or 'd' "
-           "operations");
+      badUsage("the 'N' modifier can only be specified with the 'x' or 'd' "
+               "operations");
   if (OriginalDates && Operation != Extract)
-    fail("the 'o' modifier is only applicable to the 'x' operation");
+    badUsage("the 'o' modifier is only applicable to the 'x' operation");
   if (OnlyUpdate && Operation != ReplaceOrInsert)
-    fail("the 'u' modifier is only applicable to the 'r' operation");
+    badUsage("the 'u' modifier is only applicable to the 'r' operation");
   if (AddLibrary && Operation != QuickAppend)
-    fail("the 'L' modifier is only applicable to the 'q' operation");
+    badUsage("the 'L' modifier is only applicable to the 'q' operation");
 
   // Return the parsed operation to the caller
   return Operation;
@@ -1079,7 +1082,7 @@ static void runMRIScript() {
 }
 
 static bool handleGenericOption(StringRef arg) {
-  if (arg == "-help" || arg == "--help") {
+  if (arg == "-help" || arg == "--help" || arg == "-h") {
     printHelpMessage();
     return true;
   }
@@ -1161,6 +1164,9 @@ static int ranlib_main(int argc, char **argv) {
       ArchiveName = argv[i];
     }
   }
+  if (!ArchiveSpecified) {
+    badUsage("an archive name must be specified");
+  }
   return performOperation(CreateSymTab, nullptr);
 }
 


        


More information about the llvm-commits mailing list