Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/OptRSTEmitter.cpp
35258 views
//===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "Common/OptEmitter.h"9#include "llvm/ADT/STLExtras.h"10#include "llvm/ADT/StringMap.h"11#include "llvm/TableGen/Record.h"12#include "llvm/TableGen/TableGenBackend.h"1314using namespace llvm;1516/// OptParserEmitter - This tablegen backend takes an input .td file17/// describing a list of options and emits a RST man page.18static void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {19llvm::StringMap<std::vector<Record *>> OptionsByGroup;20std::vector<Record *> OptionsWithoutGroup;2122// Get the options.23std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");24array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);2526// Get the option groups.27const std::vector<Record *> &Groups =28Records.getAllDerivedDefinitions("OptionGroup");29for (unsigned i = 0, e = Groups.size(); i != e; ++i) {30const Record &R = *Groups[i];31OptionsByGroup.try_emplace(R.getValueAsString("Name"));32}3334// Map options to their group.35for (unsigned i = 0, e = Opts.size(); i != e; ++i) {36const Record &R = *Opts[i];37if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {38OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(Opts[i]);39} else {40OptionsByGroup["options"].push_back(Opts[i]);41}42}4344// Print options under their group.45for (const auto &KV : OptionsByGroup) {46std::string GroupName = KV.getKey().upper();47OS << GroupName << '\n';48OS << std::string(GroupName.size(), '-') << '\n';49OS << '\n';5051for (Record *R : KV.getValue()) {52OS << ".. option:: ";5354// Print the prefix.55std::vector<StringRef> Prefixes = R->getValueAsListOfStrings("Prefixes");56if (!Prefixes.empty())57OS << Prefixes[0];5859// Print the option name.60OS << R->getValueAsString("Name");6162StringRef MetaVarName;63// Print the meta-variable.64if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {65MetaVarName = R->getValueAsString("MetaVarName");66} else if (!isa<UnsetInit>(R->getValueInit("Values")))67MetaVarName = "<value>";6869if (!MetaVarName.empty()) {70OS << '=';71OS.write_escaped(MetaVarName);72}7374OS << "\n\n";7576std::string HelpText;77// The option help text.78if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {79HelpText = R->getValueAsString("HelpText").trim().str();80if (!HelpText.empty() && HelpText.back() != '.')81HelpText.push_back('.');82}8384if (!isa<UnsetInit>(R->getValueInit("Values"))) {85SmallVector<StringRef> Values;86SplitString(R->getValueAsString("Values"), Values, ",");87HelpText += (" " + MetaVarName + " must be '").str();8889if (Values.size() > 1) {90HelpText += join(Values.begin(), Values.end() - 1, "', '");91HelpText += "' or '";92}93HelpText += (Values.back() + "'.").str();94}9596if (!HelpText.empty()) {97OS << ' ';98OS.write_escaped(HelpText);99OS << "\n\n";100}101}102}103}104105static TableGen::Emitter::Opt X("gen-opt-rst", EmitOptRST,106"Generate option RST");107108109