]> granicus.if.org Git - llvm/commitdiff
Don't call exit from cl::PrintHelpMessage.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 7 Sep 2017 23:30:48 +0000 (23:30 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 7 Sep 2017 23:30:48 +0000 (23:30 +0000)
Most callers were not expecting the exit(0) and trying to exit with a
different value.

This also adds back the call to cl::PrintHelpMessage in llvm-ar.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312761 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/CommandLine.h
lib/Support/CommandLine.cpp
test/tools/llvm-ar/invalid-command-line.test
tools/dsymutil/dsymutil.cpp
tools/llvm-ar/llvm-ar.cpp
tools/llvm-opt-report/OptReport.cpp
tools/llvm-xray/llvm-xray.cc

index 22d18194dd84cda1573163e11304d99ef6fd7fc8..2357a122a47e43fda4c073d9d2698bd37f3bfc11 100644 (file)
@@ -1762,8 +1762,6 @@ void PrintVersionMessage();
 /// This function just prints the help message, exactly the same way as if the
 /// -help or -help-hidden option had been given on the command line.
 ///
-/// NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
-///
 /// \param Hidden if true will print hidden options
 /// \param Categorized if true print options in categories
 void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
index 8fb01211e9797405a444cbf622c84d1dc28aef4e..0d662cb0375e47e9e131009058ddaac346ec767f 100644 (file)
@@ -1758,7 +1758,13 @@ public:
   void operator=(bool Value) {
     if (!Value)
       return;
+    printHelp();
 
+    // Halt the program since help information was printed
+    exit(0);
+  }
+
+  void printHelp() {
     SubCommand *Sub = GlobalParser->getActiveSubCommand();
     auto &OptionsMap = Sub->OptionsMap;
     auto &PositionalOpts = Sub->PositionalOpts;
@@ -1826,9 +1832,6 @@ public:
     for (auto I : GlobalParser->MoreHelp)
       outs() << I;
     GlobalParser->MoreHelp.clear();
-
-    // Halt the program since help information was printed
-    exit(0);
   }
 };
 
@@ -2098,21 +2101,14 @@ static cl::opt<VersionPrinter, true, parser<bool>>
 
 // Utility function for printing the help message.
 void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
-  // This looks weird, but it actually prints the help message. The Printers are
-  // types of HelpPrinter and the help gets printed when its operator= is
-  // invoked. That's because the "normal" usages of the help printer is to be
-  // assigned true/false depending on whether -help or -help-hidden was given or
-  // not.  Since we're circumventing that we have to make it look like -help or
-  // -help-hidden were given, so we assign true.
-
   if (!Hidden && !Categorized)
-    UncategorizedNormalPrinter = true;
+    UncategorizedNormalPrinter.printHelp();
   else if (!Hidden && Categorized)
-    CategorizedNormalPrinter = true;
+    CategorizedNormalPrinter.printHelp();
   else if (Hidden && !Categorized)
-    UncategorizedHiddenPrinter = true;
+    UncategorizedHiddenPrinter.printHelp();
   else
-    CategorizedHiddenPrinter = true;
+    CategorizedHiddenPrinter.printHelp();
 }
 
 /// Utility function for printing version number.
index 44a37286a9928df4dffeb15f43ebd8d50d3a31be..e13f54c07bd035eb31eb707e61b71fd21511575e 100644 (file)
@@ -2,3 +2,4 @@ Test that llvm-ar exits with 1 when there is an error.
 
 RUN: not llvm-ar e 2>&1 | FileCheck %s
 CHECK: unknown option e.
+CHECK: OVERVIEW: LLVM Archiver (llvm-ar)
index 1ce0aefeec2afb4802e6c2e0a55450d7cbcf2888..51eb3ff2e39d68827536a776a701060ff8913017 100644 (file)
@@ -253,8 +253,10 @@ int main(int argc, char **argv) {
       "for the executable <input file> by using debug symbols information\n"
       "contained in its symbol table.\n");
 
-  if (Help)
+  if (Help) {
     PrintHelpMessage();
+    return 0;
+  }
 
   if (Version) {
     llvm::cl::PrintVersionMessage();
index 65c61989282fcc5c08c20181d5e61510908f4aec..03655cb4318353822ef78559462e6cf669e5a7ae 100644 (file)
@@ -54,8 +54,7 @@ static StringRef ToolName;
 // Show the error message and exit.
 LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
   errs() << ToolName << ": " << Error << ".\n";
-  // FIXME: Other ar implementations will print the command line help in here.
-  // Unfortunately cl::PrintHelpMessage() exits with 0, so we can't call it.
+  cl::PrintHelpMessage();
   exit(1);
 }
 
index 4f45dd9f2aa28ca5cca31f5ce6aaed2932021469..3c6115db6ac0ab5c26f3a97391fba31d7cafb631 100644 (file)
@@ -514,8 +514,10 @@ int main(int argc, const char **argv) {
       "A tool to generate an optimization report from YAML optimization"
       " record files.\n");
 
-  if (Help)
+  if (Help) {
     cl::PrintHelpMessage();
+    return 0;
+  }
 
   LocationInfoTy LocationInfo;
   if (!readLocationInfo(LocationInfo))
index 98303e7be15c0bc286da9f3b85f5c6d68145776b..34c453a1eb411c4b9ae77a46c1f4be670ebebd47 100644 (file)
@@ -46,4 +46,5 @@ int main(int argc, char *argv[]) {
 
   // If all else fails, we still print the usage message.
   cl::PrintHelpMessage(false, true);
+  return 0;
 }