1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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"
16 #include <cctype>
17 #include <cstring>
18 #include <map>
19 
20 using namespace llvm;
21 
22 /// OptParserEmitter - This tablegen backend takes an input .td file
23 /// describing a list of options and emits a RST man page.
24 namespace llvm {
25 void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
26   llvm::StringMap<std::vector<Record *>> OptionsByGroup;
27   std::vector<Record *> OptionsWithoutGroup;
28 
29   // Get the options.
30   std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
31   array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
32 
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"));
39   }
40 
41   // Map options to their group.
42   for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
43     const Record &R = *Opts[i];
44     const ListInit *GroupFlags = nullptr;
45     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
46       GroupFlags = DI->getDef()->getValueAsListInit("Flags");
47       OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(Opts[i]);
48     } else {
49       OptionsByGroup["options"].push_back(Opts[i]);
50     }
51   }
52 
53   // Print options under their group.
54   for (const auto &KV : OptionsByGroup) {
55     std::string GroupName = KV.getKey().upper();
56     OS << GroupName << '\n';
57     OS << std::string(GroupName.size(), '-') << '\n';
58     OS << '\n';
59 
60     for (Record *R : KV.getValue()) {
61       OS << ".. option:: ";
62 
63       // Print the prefix.
64       std::vector<StringRef> Prefixes = R->getValueAsListOfStrings("Prefixes");
65       if (!Prefixes.empty())
66         OS << Prefixes[0];
67 
68       // Print the option name.
69       OS << R->getValueAsString("Name");
70 
71       // Print the meta-variable.
72       if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {
73         OS << '=';
74         OS.write_escaped(R->getValueAsString("MetaVarName"));
75       }
76 
77       OS << "\n\n";
78 
79       // The option help text.
80       if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {
81         OS << ' ';
82         OS.write_escaped(R->getValueAsString("HelpText"));
83         OS << "\n\n";
84       }
85     }
86   }
87 }
88 } // end namespace llvm
89