Adding a new cl::HideUnrelatedOptions API to allow clang to migrate off cl::getRegist...
authorChris Bieneman <beanz@apple.com>
Wed, 21 Jan 2015 22:45:52 +0000 (22:45 +0000)
committerChris Bieneman <beanz@apple.com>
Wed, 21 Jan 2015 22:45:52 +0000 (22:45 +0000)
Summary: cl::getRegisteredOptions really exposes some of the innards of how command line parsing is implemented. Exposing new APIs that allow us to disentangle client code from implementation details will allow us to make more extensive changes to command line parsing.

Reviewers: chandlerc, dexonsmith, beanz

Reviewed By: dexonsmith

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D7100

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

include/llvm/Support/CommandLine.h
lib/Support/CommandLine.cpp
unittests/Support/CommandLineTest.cpp

index 1c06bf5f8c0769cc196faae9839a96fffeb6c6d3..314dd951b2cbd099715822e8d9e6293f0c3f87c1 100644 (file)
@@ -1889,6 +1889,15 @@ bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
                          SmallVectorImpl<const char *> &Argv,
                          bool MarkEOLs = false);
 
+/// \brief Mark all options not part of this category as cl::ReallyHidden.
+///
+/// \param Category the category of options to keep displaying
+///
+/// Some tools (like clang-format) like to be able to hide all options that are
+/// not specific to the tool. This function allows a tool to specify a single
+/// option category to display in the -help output.
+void HideUnrelatedOptions(cl::OptionCategory &Category);
+
 } // End namespace cl
 
 } // End namespace llvm
index 40570cab7cc881a34f13e61026f1c573f6a08b35..a774421b26c54a983ab86cc1a397d1c1b579401b 100644 (file)
@@ -1826,12 +1826,22 @@ void cl::AddExtraVersionPrinter(void (*func)()) {
 void cl::getRegisteredOptions(StringMap<Option *> &Map) {
   // Get all the options.
   SmallVector<Option *, 4> PositionalOpts; // NOT USED
-  SmallVector<Option *, 4> SinkOpts; // NOT USED
+  SmallVector<Option *, 4> SinkOpts;       // NOT USED
   assert(Map.size() == 0 && "StringMap must be empty");
   GetOptionInfo(PositionalOpts, SinkOpts, Map);
   return;
 }
 
+void cl::HideUnrelatedOptions(cl::OptionCategory &Category) {
+  StringMap<cl::Option *> Options;
+  cl::getRegisteredOptions(Options);
+  for (auto &I : Options) {
+    if (I.second->Category != &Category && I.first() != "help" &&
+        I.first() != "version")
+      I.second->setHiddenFlag(cl::ReallyHidden);
+  }
+}
+
 void LLVMParseCommandLineOptions(int argc, const char *const *argv,
                                  const char *Overview) {
   llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
index ac8d3d8c9d6334dea6c4d34cd53c345e3a75c201..0defe6ff370bc2357c75f687ffb1a25d80266671 100644 (file)
@@ -230,5 +230,16 @@ TEST(CommandLineTest, AliasRequired) {
   testAliasRequired(array_lengthof(opts2), opts2);
 }
 
+TEST(CommandLineTest, HideUnrelatedOptions) {
+  cl::opt<int> TestOption1("test-option-1");
+  cl::opt<int> TestOption2("test-option-2", cl::cat(TestCategory));
+
+  cl::HideUnrelatedOptions(TestCategory);
+
+  ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
+      << "Failed to hide extra option.";
+  ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
+      << "Hid extra option that should be visable.";
+}
 
 }  // anonymous namespace