From af7b98c383df62c0a749c3394b1670a1ed54c9d4 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Wed, 13 Apr 2022 15:28:58 +0200 Subject: [PATCH] [clang-tblgen] Automatically document options values This is a port of f5c666742f7bb4ae79ec79c8acf61dced4d37cc9 to clang's tablegen, with a better wording. Differential Revision: https://reviews.llvm.org/D123682 --- .../utils/TableGen/ClangOptionDocEmitter.cpp | 28 +++++++++++++++++-- llvm/utils/TableGen/OptRSTEmitter.cpp | 10 +++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/clang/utils/TableGen/ClangOptionDocEmitter.cpp b/clang/utils/TableGen/ClangOptionDocEmitter.cpp index 0e079b6b505a..6c24ad2bdcc5 100644 --- a/clang/utils/TableGen/ClangOptionDocEmitter.cpp +++ b/clang/utils/TableGen/ClangOptionDocEmitter.cpp @@ -238,6 +238,8 @@ void emitOptionWithArgs(StringRef Prefix, const Record *Option, } } +constexpr StringLiteral DefaultMetaVarName = ""; + 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(""); + 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(R->getValueInit("Values"))) { + if (!Description.empty() && Description.back() != '.') + Description.push_back('.'); + + StringRef MetaVarName; + if (!isa(R->getValueInit("MetaVarName"))) + MetaVarName = R->getValueAsString("MetaVarName"); + else + MetaVarName = DefaultMetaVarName; + + SmallVector 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"; } diff --git a/llvm/utils/TableGen/OptRSTEmitter.cpp b/llvm/utils/TableGen/OptRSTEmitter.cpp index ee330a253edd..03c7326e817a 100644 --- a/llvm/utils/TableGen/OptRSTEmitter.cpp +++ b/llvm/utils/TableGen/OptRSTEmitter.cpp @@ -85,15 +85,13 @@ void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) { if (!isa(R->getValueInit("Values"))) { SmallVector 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()) {