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/StringMap.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/TableGen/Error.h"
15 #include "llvm/TableGen/Record.h"
16 #include "llvm/TableGen/TableGenBackend.h"
23 /// OptParserEmitter - This tablegen backend takes an input .td file
24 /// describing a list of options and emits a RST man page.
26 void EmitOptRST(RecordKeeper
&Records
, raw_ostream
&OS
) {
27 llvm::StringMap
<std::vector
<Record
*>> OptionsByGroup
;
28 std::vector
<Record
*> OptionsWithoutGroup
;
31 std::vector
<Record
*> Opts
= Records
.getAllDerivedDefinitions("Option");
32 array_pod_sort(Opts
.begin(), Opts
.end(), CompareOptionRecords
);
34 // Get the option groups.
35 const std::vector
<Record
*> &Groups
=
36 Records
.getAllDerivedDefinitions("OptionGroup");
37 for (unsigned i
= 0, e
= Groups
.size(); i
!= e
; ++i
) {
38 const Record
&R
= *Groups
[i
];
39 OptionsByGroup
.try_emplace(R
.getValueAsString("Name"));
42 // Map options to their group.
43 for (unsigned i
= 0, e
= Opts
.size(); i
!= e
; ++i
) {
44 const Record
&R
= *Opts
[i
];
45 if (const DefInit
*DI
= dyn_cast
<DefInit
>(R
.getValueInit("Group"))) {
46 OptionsByGroup
[DI
->getDef()->getValueAsString("Name")].push_back(Opts
[i
]);
48 OptionsByGroup
["options"].push_back(Opts
[i
]);
52 // Print options under their group.
53 for (const auto &KV
: OptionsByGroup
) {
54 std::string GroupName
= KV
.getKey().upper();
55 OS
<< GroupName
<< '\n';
56 OS
<< std::string(GroupName
.size(), '-') << '\n';
59 for (Record
*R
: KV
.getValue()) {
63 std::vector
<StringRef
> Prefixes
= R
->getValueAsListOfStrings("Prefixes");
64 if (!Prefixes
.empty())
67 // Print the option name.
68 OS
<< R
->getValueAsString("Name");
70 // Print the meta-variable.
71 if (!isa
<UnsetInit
>(R
->getValueInit("MetaVarName"))) {
73 OS
.write_escaped(R
->getValueAsString("MetaVarName"));
78 // The option help text.
79 if (!isa
<UnsetInit
>(R
->getValueInit("HelpText"))) {
81 OS
.write_escaped(R
->getValueAsString("HelpText"));
87 } // end namespace llvm