[llvm] 8d5bb54 - [OptTable] Make new lines in help text respect indentation (#75366)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 16:05:41 PST 2023


Author: Andres Villegas
Date: 2023-12-14T16:05:36-08:00
New Revision: 8d5bb54f774d9ddccdb9097fdc395ef46e372c70

URL: https://github.com/llvm/llvm-project/commit/8d5bb54f774d9ddccdb9097fdc395ef46e372c70
DIFF: https://github.com/llvm/llvm-project/commit/8d5bb54f774d9ddccdb9097fdc395ef46e372c70.diff

LOG: [OptTable] Make new lines in help text respect indentation (#75366)

With this changes, new lines in the HelpText defined in OptTable have
the same indentation as the first line.

Before, the help output will look something like:

```
--color=<value>       Whether to use color when
symbolizing log markup: always, auto, never
```

With this change:

```
--color=<value>       Whether to use color when
                      symbolizing log markup: always, auto, never
```

Added: 
    

Modified: 
    llvm/lib/Option/OptTable.cpp
    llvm/unittests/Option/OptionParsingTest.cpp
    llvm/unittests/Option/Opts.td

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp
index a4d0f4a5493725..cf69f6173b6d41 100644
--- a/llvm/lib/Option/OptTable.cpp
+++ b/llvm/lib/Option/OptTable.cpp
@@ -664,15 +664,24 @@ static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
   const unsigned InitialPad = 2;
   for (const OptionInfo &Opt : OptionHelp) {
     const std::string &Option = Opt.Name;
-    int Pad = OptionFieldWidth - int(Option.size());
+    int Pad = OptionFieldWidth + InitialPad;
+    int FirstLinePad = OptionFieldWidth - int(Option.size());
     OS.indent(InitialPad) << Option;
 
     // Break on long option names.
-    if (Pad < 0) {
+    if (FirstLinePad < 0) {
       OS << "\n";
-      Pad = OptionFieldWidth + InitialPad;
+      FirstLinePad = OptionFieldWidth + InitialPad;
+      Pad = FirstLinePad;
     }
-    OS.indent(Pad + 1) << Opt.HelpText << '\n';
+
+    SmallVector<StringRef> Lines;
+    Opt.HelpText.split(Lines, '\n');
+    assert(Lines.size() && "Expected at least the first line in the help text");
+    auto *LinesIt = Lines.begin();
+    OS.indent(FirstLinePad + 1) << *LinesIt << '\n';
+    while (Lines.end() != ++LinesIt)
+      OS.indent(Pad + 1) << *LinesIt << '\n';
   }
 }
 

diff  --git a/llvm/unittests/Option/OptionParsingTest.cpp b/llvm/unittests/Option/OptionParsingTest.cpp
index 804a8016414dbc..cd8743e49d4fde 100644
--- a/llvm/unittests/Option/OptionParsingTest.cpp
+++ b/llvm/unittests/Option/OptionParsingTest.cpp
@@ -9,6 +9,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
 #include "llvm/Option/Option.h"
 #include "gtest/gtest.h"
 
@@ -50,6 +51,7 @@ enum OptionFlags {
 
 enum OptionVisibility {
   SubtoolVis = (1 << 2),
+  MultiLineVis = (1 << 3),
 };
 
 static constexpr OptTable::Info InfoTable[] = {
@@ -538,3 +540,23 @@ TYPED_TEST(OptTableTest, UnknownGroupedShortOptions) {
   EXPECT_EQ("-u", Unknown[2]);
   EXPECT_EQ("-z", Unknown[3]);
 }
+
+TYPED_TEST(OptTableTest, PrintMultilineHelpText) {
+  TypeParam T;
+  std::string Help;
+  raw_string_ostream RSO(Help);
+  T.printHelp(RSO, "usage", "title", /*ShowHidden=*/false,
+              /*ShowAllAliases=*/false, Visibility(MultiLineVis));
+  EXPECT_STREQ(Help.c_str(), R"(OVERVIEW: title
+
+USAGE: usage
+
+OPTIONS:
+  -multiline-help-with-long-name
+                  This a help text that has
+                  multiple lines in it
+                  and a long name
+  -multiline-help This a help text that has
+                  multiple lines in it
+)");
+}

diff  --git a/llvm/unittests/Option/Opts.td b/llvm/unittests/Option/Opts.td
index ac01d6d93b2b63..5be67c9decdbcd 100644
--- a/llvm/unittests/Option/Opts.td
+++ b/llvm/unittests/Option/Opts.td
@@ -5,6 +5,7 @@ def OptFlag2 : OptionFlag;
 def OptFlag3 : OptionFlag;
 
 def SubtoolVis : OptionVisibility;
+def MultiLineVis: OptionVisibility;
 
 def A : Flag<["-"], "A">, HelpText<"The A option">, Flags<[OptFlag1]>;
 def AB : Flag<["-"], "AB">;
@@ -50,6 +51,15 @@ def Blurmpq_eq : Flag<["--"], "blurmp=">;
 def Q : Flag<["-"], "Q">, Visibility<[SubtoolVis]>;
 def R : Flag<["-"], "R">, Visibility<[DefaultVis, SubtoolVis]>;
 
+def multiline_help : Flag<["-"], "multiline-help">, HelpText<"This a help text that has\nmultiple lines in it">, Visibility<[MultiLineVis]>;
+
+def multiline_help_long :
+  Flag<["-"], "multiline-help-with-long-name">,
+  HelpText<"This a help text that has\n"
+           "multiple lines in it\n"
+           "and a long name">,
+  Visibility<[MultiLineVis]>;
+
 class XOpts<string base> : KeyPathAndMacro<"X->", base> {}
 
 def marshalled_flag_d : Flag<["-"], "marshalled-flag-d">,


        


More information about the llvm-commits mailing list