1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

[Support] Indent multi-line descr of enum cli options.

As noted in https://reviews.llvm.org/D93459, the formatting of
multi-line descriptions of clEnumValN and the likes is unfavorable.
Thus this patch adds support for correctly indenting these.

Reviewed By: serge-sans-paille

Differential Revision: https://reviews.llvm.org/D93494
This commit is contained in:
Joachim Meyer 2020-12-17 23:58:13 +01:00
parent ab4f1aa423
commit f40f02934a
3 changed files with 54 additions and 6 deletions

View File

@ -369,9 +369,22 @@ public:
virtual void setDefault() = 0;
// Prints the help string for an option.
//
// This maintains the Indent for multi-line descriptions.
// FirstLineIndentedBy is the count of chars of the first line
// i.e. the one containing the --<option name>.
static void printHelpStr(StringRef HelpStr, size_t Indent,
size_t FirstLineIndentedBy);
// Prints the help string for an enum value.
//
// This maintains the Indent for multi-line descriptions.
// FirstLineIndentedBy is the count of chars of the first line
// i.e. the one containing the =<value>.
static void printEnumValHelpStr(StringRef HelpStr, size_t Indent,
size_t FirstLineIndentedBy);
virtual void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
// addOccurrence - Wrapper around handleOccurrence that enforces Flags.

View File

@ -1726,6 +1726,19 @@ void Option::printHelpStr(StringRef HelpStr, size_t Indent,
}
}
void Option::printEnumValHelpStr(StringRef HelpStr, size_t BaseIndent,
size_t FirstLineIndentedBy) {
const StringRef ValHelpPrefix = " ";
assert(BaseIndent >= FirstLineIndentedBy + ValHelpPrefix.size());
std::pair<StringRef, StringRef> Split = HelpStr.split('\n');
outs().indent(BaseIndent - FirstLineIndentedBy)
<< ArgHelpPrefix << ValHelpPrefix << Split.first << "\n";
while (!Split.second.empty()) {
Split = Split.second.split('\n');
outs().indent(BaseIndent + ValHelpPrefix.size()) << Split.first << "\n";
}
}
// Print out the option for the alias.
void alias::printOptionInfo(size_t GlobalWidth) const {
outs() << PrintArg(ArgStr);
@ -1971,16 +1984,16 @@ void generic_parser_base::printOptionInfo(const Option &O,
StringRef Description = getDescription(i);
if (!shouldPrintOption(OptionName, Description, O))
continue;
assert(GlobalWidth >= OptionName.size() + OptionPrefixesSize);
size_t NumSpaces = GlobalWidth - OptionName.size() - OptionPrefixesSize;
size_t FirstLineIndent = OptionName.size() + OptionPrefixesSize;
outs() << OptionPrefix << OptionName;
if (OptionName.empty()) {
outs() << EmptyOption;
assert(NumSpaces >= EmptyOption.size());
NumSpaces -= EmptyOption.size();
assert(FirstLineIndent >= EmptyOption.size());
FirstLineIndent += EmptyOption.size();
}
if (!Description.empty())
outs().indent(NumSpaces) << ArgHelpPrefix << " " << Description;
Option::printEnumValHelpStr(Description, GlobalWidth, FirstLineIndent);
else
outs() << '\n';
}
} else {

View File

@ -1263,6 +1263,28 @@ TEST_F(PrintOptionInfoTest, PrintOptionInfoEmptyValueDescription) {
// clang-format on
}
TEST_F(PrintOptionInfoTest, PrintOptionInfoMultilineValueDescription) {
std::string Output =
runTest(cl::ValueRequired,
cl::values(clEnumValN(OptionValue::Val, "v1",
"This is the first enum value\n"
"which has a really long description\n"
"thus it is multi-line."),
clEnumValN(OptionValue::Val, "",
"This is an unnamed enum value option\n"
"Should be indented as well")));
// clang-format off
EXPECT_EQ(Output,
(" --" + Opt + "=<value> - " + HelpText + "\n"
" =v1 - This is the first enum value\n"
" which has a really long description\n"
" thus it is multi-line.\n"
" =<empty> - This is an unnamed enum value option\n"
" Should be indented as well\n").str());
// clang-format on
}
class GetOptionWidthTest : public ::testing::Test {
public:
enum class OptionValue { Val };