Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/OptionRSTEmitter.cpp
213766 views
//===- OptionRSTEmitter.cpp - Table Driven Command Line Option 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/// This tablegen backend takes an input .td file describing a list of options17/// and emits a RST man page.18static void emitOptionRst(const RecordKeeper &Records, raw_ostream &OS) {19llvm::StringMap<std::vector<const Record *>> OptionsByGroup;2021// Get the options.22std::vector<const Record *> Opts = Records.getAllDerivedDefinitions("Option");23llvm::sort(Opts, IsOptionRecordsLess);2425// Get the option groups.26for (const Record *R : Records.getAllDerivedDefinitions("OptionGroup"))27OptionsByGroup.try_emplace(R->getValueAsString("Name"));2829// Map options to their group.30for (const Record *R : Opts) {31if (const DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group")))32OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(R);33else34OptionsByGroup["options"].push_back(R);35}3637// Print options under their group.38for (const auto &KV : OptionsByGroup) {39std::string GroupName = KV.getKey().upper();40OS << GroupName << '\n';41OS << std::string(GroupName.size(), '-') << '\n';42OS << '\n';4344for (const Record *R : KV.getValue()) {45OS << ".. option:: ";4647// Print the prefix.48std::vector<StringRef> Prefixes = R->getValueAsListOfStrings("Prefixes");49if (!Prefixes.empty())50OS << Prefixes[0];5152// Print the option name.53OS << R->getValueAsString("Name");5455StringRef MetaVarName;56// Print the meta-variable.57if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {58MetaVarName = R->getValueAsString("MetaVarName");59} else if (!isa<UnsetInit>(R->getValueInit("Values")))60MetaVarName = "<value>";6162if (!MetaVarName.empty()) {63OS << '=';64OS.write_escaped(MetaVarName);65}6667OS << "\n\n";6869std::string HelpText;70// The option help text.71if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {72HelpText = R->getValueAsString("HelpText").trim().str();73if (!HelpText.empty() && HelpText.back() != '.')74HelpText.push_back('.');75}7677if (!isa<UnsetInit>(R->getValueInit("Values"))) {78SmallVector<StringRef> Values;79SplitString(R->getValueAsString("Values"), Values, ",");80HelpText += (" " + MetaVarName + " must be '").str();8182if (Values.size() > 1) {83HelpText += join(Values.begin(), Values.end() - 1, "', '");84HelpText += "' or '";85}86HelpText += (Values.back() + "'.").str();87}8889if (!HelpText.empty()) {90OS << ' ';91OS.write_escaped(HelpText);92OS << "\n\n";93}94}95}96}9798static TableGen::Emitter::Opt X("gen-opt-rst", emitOptionRst,99"Generate option RST");100101102