]> granicus.if.org Git - clang/commitdiff
OptTable: Allow option groups to be used to define "help groups", which will
authorDaniel Dunbar <daniel@zuster.org>
Fri, 4 Dec 2009 21:08:40 +0000 (21:08 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 4 Dec 2009 21:08:40 +0000 (21:08 +0000)
collate the options inside that group.

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

include/clang/Driver/OptParser.td
include/clang/Driver/OptTable.h
lib/Driver/OptTable.cpp

index 286c4d233686eb5c2d06425bac74e288ac72eb72..a9f4289fc86e0e0088c33966276348e631bf8699 100644 (file)
@@ -87,6 +87,7 @@ def HelpHidden : OptionFlag;
 class OptionGroup<string name> {
   string EnumName = ?; // Uses the def name if undefined.
   string Name = name;
+  string HelpText = ?;
   OptionGroup Group = ?;
 }
 
index 840e669a27a8bbb9d82a64c16f544e8be2af818a..edae75c9f057faa084ab25086a46bee5c133828f 100644 (file)
@@ -118,6 +118,11 @@ namespace options {
       return getInfo(id).Kind;
     }
 
+    /// getOptionGroupID - Get the group id for the given option.
+    unsigned getOptionGroupID(OptSpecifier id) const {
+      return getInfo(id).GroupID;
+    }
+
     /// isOptionHelpHidden - Should the help for the given option be hidden by
     /// default.
     bool isOptionHelpHidden(OptSpecifier id) const {
index 9c6c9b49ae98a8653d66137517798f65d0d5863d..f69d5d806d2822fb7904bc6827f6c95af4bcdc6e 100644 (file)
@@ -14,7 +14,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <cassert>
-
+#include <map>
 using namespace clang::driver;
 using namespace clang::driver::options;
 
@@ -285,25 +285,10 @@ static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
   return Name;
 }
 
-void OptTable::PrintHelp(llvm::raw_ostream &OS, const char *Name,
-                         const char *Title, bool ShowHidden) const {
-  OS << "OVERVIEW: " << Title << "\n";
-  OS << '\n';
-  OS << "USAGE: " << Name << " [options] <inputs>\n";
-  OS << '\n';
-  OS << "OPTIONS:\n";
-
-  // Render help text into (option, help) pairs.
-  std::vector< std::pair<std::string, const char*> > OptionHelp;
-  for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
-    unsigned Id = i + 1;
-
-    if (!ShowHidden && isOptionHelpHidden(Id))
-      continue;
-
-    if (const char *Text = getOptionHelpText(Id))
-      OptionHelp.push_back(std::make_pair(getOptionHelpName(*this, Id), Text));
-  }
+static void PrintHelpOptionList(llvm::raw_ostream &OS, llvm::StringRef Title,
+                                std::vector<std::pair<std::string,
+                                const char*> > &OptionHelp) {
+  OS << Title << ":\n";
 
   // Find the maximum option length.
   unsigned OptionFieldWidth = 0;
@@ -331,6 +316,62 @@ void OptTable::PrintHelp(llvm::raw_ostream &OS, const char *Name,
     }
     OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
   }
+}
+
+static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
+  unsigned GroupID = Opts.getOptionGroupID(Id);
+
+  // If not in a group, return the default help group.
+  if (!GroupID)
+    return "OPTIONS";
+
+  // Abuse the help text of the option groups to store the "help group"
+  // name.
+  //
+  // FIXME: Split out option groups.
+  if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
+    return GroupHelp;
+
+  // Otherwise keep looking.
+  return getOptionHelpGroup(Opts, GroupID);
+}
+
+void OptTable::PrintHelp(llvm::raw_ostream &OS, const char *Name,
+                         const char *Title, bool ShowHidden) const {
+  OS << "OVERVIEW: " << Title << "\n";
+  OS << '\n';
+  OS << "USAGE: " << Name << " [options] <inputs>\n";
+  OS << '\n';
+
+  // Render help text into a map of group-name to a list of (option, help)
+  // pairs.
+  typedef std::map<std::string,
+                 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
+  helpmap_ty GroupedOptionHelp;
+
+  for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
+    unsigned Id = i + 1;
+
+    // FIXME: Split out option groups.
+    if (getOptionKind(Id) == Option::GroupClass)
+      continue;
+
+    if (!ShowHidden && isOptionHelpHidden(Id))
+      continue;
+
+    if (const char *Text = getOptionHelpText(Id)) {
+      const char *HelpGroup = getOptionHelpGroup(*this, Id);
+      const std::string &OptName = getOptionHelpName(*this, Id);
+      GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
+    }
+  }
+
+  for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
+         ie = GroupedOptionHelp.end(); it != ie; ++it) {
+    if (it != GroupedOptionHelp .begin())
+      OS << "\n";
+    PrintHelpOptionList(OS, it->first, it->second);
+  }
 
   OS.flush();
 }