[clang-tblgen] Automatically document options values

This is a port of f5c666742f to clang's tablegen,
with a better wording.

Differential Revision: https://reviews.llvm.org/D123682
This commit is contained in:
serge-sans-paille 2022-04-13 15:28:58 +02:00
parent 22f9dca113
commit af7b98c383
2 changed files with 30 additions and 8 deletions

View File

@ -238,6 +238,8 @@ void emitOptionWithArgs(StringRef Prefix, const Record *Option,
}
}
constexpr StringLiteral DefaultMetaVarName = "<arg>";
void emitOptionName(StringRef Prefix, const Record *Option, raw_ostream &OS) {
// Find the arguments to list after the option.
unsigned NumArgs = getNumArgsForKind(Option->getValueAsDef("Kind"), Option);
@ -247,7 +249,7 @@ void emitOptionName(StringRef Prefix, const Record *Option, raw_ostream &OS) {
if (HasMetaVarName)
Args.push_back(std::string(Option->getValueAsString("MetaVarName")));
else if (NumArgs == 1)
Args.push_back("<arg>");
Args.push_back(DefaultMetaVarName.str());
// Fill up arguments if this option didn't provide a meta var name or it
// supports an unlimited number of arguments. We can't see how many arguments
@ -341,8 +343,30 @@ void emitOption(const DocumentedOption &Option, const Record *DocInfo,
OS << "\n\n";
// Emit the description, if we have one.
const Record *R = Option.Option;
std::string Description =
getRSTStringWithTextFallback(Option.Option, "DocBrief", "HelpText");
getRSTStringWithTextFallback(R, "DocBrief", "HelpText");
if (!isa<UnsetInit>(R->getValueInit("Values"))) {
if (!Description.empty() && Description.back() != '.')
Description.push_back('.');
StringRef MetaVarName;
if (!isa<UnsetInit>(R->getValueInit("MetaVarName")))
MetaVarName = R->getValueAsString("MetaVarName");
else
MetaVarName = DefaultMetaVarName;
SmallVector<StringRef> Values;
SplitString(R->getValueAsString("Values"), Values, ",");
Description += (" " + MetaVarName + " must be '").str();
if (Values.size() > 1) {
Description += join(Values.begin(), Values.end() - 1, "', '");
Description += "' or '";
}
Description += (Values.back() + "'.").str();
}
if (!Description.empty())
OS << Description << "\n\n";
}

View File

@ -85,15 +85,13 @@ void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
if (!isa<UnsetInit>(R->getValueInit("Values"))) {
SmallVector<StringRef> Values;
SplitString(R->getValueAsString("Values"), Values, ",");
HelpText += (" " + MetaVarName + " can be ").str();
HelpText += (" " + MetaVarName + " must be '").str();
if (Values.size() == 1) {
HelpText += ("'" + Values.front() + "'.").str();
} else {
HelpText += "one of '";
if (Values.size() > 1) {
HelpText += join(Values.begin(), Values.end() - 1, "', '");
HelpText += ("' or '" + Values.back() + "'.").str();
HelpText += "' or '";
}
HelpText += (Values.front() + "'.").str();
}
if (!HelpText.empty()) {