[PATCH] D57030: [CommandLine] Don't print empty sentinel values from EnumValN lists in help text

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 21 10:09:11 PST 2019


jhenderson created this revision.
jhenderson added reviewers: thopre, mstorsjo, loladiro, aprantl, ruiu.
Herald added a subscriber: kristina.

If you specify `ValueOptional` with a command-line option using `cl::values(clEnumValN(...), clEnumValN(...))`, you get an error from the command-line parser saying `Cannot find option named ''!`  if specifying the option without a value, despite ValueOptional being specified. This is not great, but I don't know enough about the command-line library to come up with a direct solution in the library to fix it. Some tools use "sentinel" options that say what the empty string maps to e.g. from HexagonMCTargetDesc.cpp:

  cl::opt<Hexagon::ArchEnum>
      EnableHVX("mhvx",
        cl::desc("Enable Hexagon Vector eXtensions"),
        cl::values(
          clEnumValN(Hexagon::ArchEnum::V60, "v60", "Build for HVX v60"),
          clEnumValN(Hexagon::ArchEnum::V62, "v62", "Build for HVX v62"),
          clEnumValN(Hexagon::ArchEnum::V65, "v65", "Build for HVX v65"),
          clEnumValN(Hexagon::ArchEnum::V66, "v66", "Build for HVX v66"),
          // Sentinel for no value specified.
          clEnumValN(Hexagon::ArchEnum::Generic, "", "")),
        // Sentinel for flag not present.
        cl::init(Hexagon::ArchEnum::NoArch), cl::ValueOptional);

However, this ends up being emitted in the help text. For example, llvm-mc produces the following help text for the above option:

  -mhvx                                            - Enable Hexagon Vector eXtensions
     =v60                                           -   Build for HVX v60
     =v62                                           -   Build for HVX v62
     =v65                                           -   Build for HVX v65
     =v66                                           -   Build for HVX v66
     =                                              -

This change modifies the help text to not print sentinel value entries like this, if they have no description.

I don't know how to test this, except by testing a tool's help text directly, which feels fragile, but I am open to options.


Repository:
  rL LLVM

https://reviews.llvm.org/D57030

Files:
  lib/Support/CommandLine.cpp


Index: lib/Support/CommandLine.cpp
===================================================================
--- lib/Support/CommandLine.cpp
+++ lib/Support/CommandLine.cpp
@@ -1676,6 +1676,8 @@
     Option::printHelpStr(O.HelpStr, GlobalWidth, O.ArgStr.size() + 6);
 
     for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
+      if(getOption(i).empty() && getDescription(i).empty())
+        continue;
       size_t NumSpaces = GlobalWidth - getOption(i).size() - 8;
       outs() << "    =" << getOption(i);
       outs().indent(NumSpaces) << " -   " << getDescription(i) << '\n';


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57030.182812.patch
Type: text/x-patch
Size: 585 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190121/b0110a40/attachment.bin>


More information about the llvm-commits mailing list