Skip to content

Commit af7b98c

Browse files
author
serge-sans-paille
committed
[clang-tblgen] Automatically document options values
This is a port of f5c6667 to clang's tablegen, with a better wording. Differential Revision: https://reviews.llvm.org/D123682
1 parent 22f9dca commit af7b98c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

clang/utils/TableGen/ClangOptionDocEmitter.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ void emitOptionWithArgs(StringRef Prefix, const Record *Option,
238238
}
239239
}
240240

241+
constexpr StringLiteral DefaultMetaVarName = "<arg>";
242+
241243
void emitOptionName(StringRef Prefix, const Record *Option, raw_ostream &OS) {
242244
// Find the arguments to list after the option.
243245
unsigned NumArgs = getNumArgsForKind(Option->getValueAsDef("Kind"), Option);
@@ -247,7 +249,7 @@ void emitOptionName(StringRef Prefix, const Record *Option, raw_ostream &OS) {
247249
if (HasMetaVarName)
248250
Args.push_back(std::string(Option->getValueAsString("MetaVarName")));
249251
else if (NumArgs == 1)
250-
Args.push_back("<arg>");
252+
Args.push_back(DefaultMetaVarName.str());
251253

252254
// Fill up arguments if this option didn't provide a meta var name or it
253255
// 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,
341343
OS << "\n\n";
342344

343345
// Emit the description, if we have one.
346+
const Record *R = Option.Option;
344347
std::string Description =
345-
getRSTStringWithTextFallback(Option.Option, "DocBrief", "HelpText");
348+
getRSTStringWithTextFallback(R, "DocBrief", "HelpText");
349+
350+
if (!isa<UnsetInit>(R->getValueInit("Values"))) {
351+
if (!Description.empty() && Description.back() != '.')
352+
Description.push_back('.');
353+
354+
StringRef MetaVarName;
355+
if (!isa<UnsetInit>(R->getValueInit("MetaVarName")))
356+
MetaVarName = R->getValueAsString("MetaVarName");
357+
else
358+
MetaVarName = DefaultMetaVarName;
359+
360+
SmallVector<StringRef> Values;
361+
SplitString(R->getValueAsString("Values"), Values, ",");
362+
Description += (" " + MetaVarName + " must be '").str();
363+
if (Values.size() > 1) {
364+
Description += join(Values.begin(), Values.end() - 1, "', '");
365+
Description += "' or '";
366+
}
367+
Description += (Values.back() + "'.").str();
368+
}
369+
346370
if (!Description.empty())
347371
OS << Description << "\n\n";
348372
}

llvm/utils/TableGen/OptRSTEmitter.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,13 @@ void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
8585
if (!isa<UnsetInit>(R->getValueInit("Values"))) {
8686
SmallVector<StringRef> Values;
8787
SplitString(R->getValueAsString("Values"), Values, ",");
88-
HelpText += (" " + MetaVarName + " can be ").str();
88+
HelpText += (" " + MetaVarName + " must be '").str();
8989

90-
if (Values.size() == 1) {
91-
HelpText += ("'" + Values.front() + "'.").str();
92-
} else {
93-
HelpText += "one of '";
90+
if (Values.size() > 1) {
9491
HelpText += join(Values.begin(), Values.end() - 1, "', '");
95-
HelpText += ("' or '" + Values.back() + "'.").str();
92+
HelpText += "' or '";
9693
}
94+
HelpText += (Values.front() + "'.").str();
9795
}
9896

9997
if (!HelpText.empty()) {

0 commit comments

Comments
 (0)