1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "OptEmitter.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/TableGen/Error.h"
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/TableGen/TableGenBackend.h"
22 /// OptParserEmitter - This tablegen backend takes an input .td file
23 /// describing a list of options and emits a RST man page.
25 void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
26 llvm::StringMap<std::vector<Record *>> OptionsByGroup;
27 std::vector<Record *> OptionsWithoutGroup;
30 std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
31 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
33 // Get the option groups.
34 const std::vector<Record *> &Groups =
35 Records.getAllDerivedDefinitions("OptionGroup");
36 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
37 const Record &R = *Groups[i];
38 OptionsByGroup.try_emplace(R.getValueAsString("Name"));
41 // Map options to their group.
42 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
43 const Record &R = *Opts[i];
44 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
45 OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(Opts[i]);
47 OptionsByGroup["options"].push_back(Opts[i]);
51 // Print options under their group.
52 for (const auto &KV : OptionsByGroup) {
53 std::string GroupName = KV.getKey().upper();
54 OS << GroupName << '\n';
55 OS << std::string(GroupName.size(), '-') << '\n';
58 for (Record *R : KV.getValue()) {
62 std::vector<StringRef> Prefixes = R->getValueAsListOfStrings("Prefixes");
63 if (!Prefixes.empty())
66 // Print the option name.
67 OS << R->getValueAsString("Name");
69 // Print the meta-variable.
70 if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {
72 OS.write_escaped(R->getValueAsString("MetaVarName"));
77 // The option help text.
78 if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {
80 OS.write_escaped(R->getValueAsString("HelpText"));
86 } // end namespace llvm