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 "Common/OptEmitter.h" |
10 | #include "llvm/ADT/STLExtras.h" |
11 | #include "llvm/ADT/StringMap.h" |
12 | #include "llvm/TableGen/Record.h" |
13 | #include "llvm/TableGen/TableGenBackend.h" |
14 | |
15 | using namespace llvm; |
16 | |
17 | /// OptParserEmitter - This tablegen backend takes an input .td file |
18 | /// describing a list of options and emits a RST man page. |
19 | static void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) { |
20 | llvm::StringMap<std::vector<Record *>> OptionsByGroup; |
21 | std::vector<Record *> OptionsWithoutGroup; |
22 | |
23 | // Get the options. |
24 | std::vector<Record *> Opts = Records.getAllDerivedDefinitions(ClassName: "Option" ); |
25 | array_pod_sort(Start: Opts.begin(), End: Opts.end(), Compare: CompareOptionRecords); |
26 | |
27 | // Get the option groups. |
28 | const std::vector<Record *> &Groups = |
29 | Records.getAllDerivedDefinitions(ClassName: "OptionGroup" ); |
30 | for (unsigned i = 0, e = Groups.size(); i != e; ++i) { |
31 | const Record &R = *Groups[i]; |
32 | OptionsByGroup.try_emplace(Key: R.getValueAsString(FieldName: "Name" )); |
33 | } |
34 | |
35 | // Map options to their group. |
36 | for (unsigned i = 0, e = Opts.size(); i != e; ++i) { |
37 | const Record &R = *Opts[i]; |
38 | if (const DefInit *DI = dyn_cast<DefInit>(Val: R.getValueInit(FieldName: "Group" ))) { |
39 | OptionsByGroup[DI->getDef()->getValueAsString(FieldName: "Name" )].push_back(x: Opts[i]); |
40 | } else { |
41 | OptionsByGroup["options" ].push_back(x: Opts[i]); |
42 | } |
43 | } |
44 | |
45 | // Print options under their group. |
46 | for (const auto &KV : OptionsByGroup) { |
47 | std::string GroupName = KV.getKey().upper(); |
48 | OS << GroupName << '\n'; |
49 | OS << std::string(GroupName.size(), '-') << '\n'; |
50 | OS << '\n'; |
51 | |
52 | for (Record *R : KV.getValue()) { |
53 | OS << ".. option:: " ; |
54 | |
55 | // Print the prefix. |
56 | std::vector<StringRef> Prefixes = R->getValueAsListOfStrings(FieldName: "Prefixes" ); |
57 | if (!Prefixes.empty()) |
58 | OS << Prefixes[0]; |
59 | |
60 | // Print the option name. |
61 | OS << R->getValueAsString(FieldName: "Name" ); |
62 | |
63 | StringRef MetaVarName; |
64 | // Print the meta-variable. |
65 | if (!isa<UnsetInit>(Val: R->getValueInit(FieldName: "MetaVarName" ))) { |
66 | MetaVarName = R->getValueAsString(FieldName: "MetaVarName" ); |
67 | } else if (!isa<UnsetInit>(Val: R->getValueInit(FieldName: "Values" ))) |
68 | MetaVarName = "<value>" ; |
69 | |
70 | if (!MetaVarName.empty()) { |
71 | OS << '='; |
72 | OS.write_escaped(Str: MetaVarName); |
73 | } |
74 | |
75 | OS << "\n\n" ; |
76 | |
77 | std::string HelpText; |
78 | // The option help text. |
79 | if (!isa<UnsetInit>(Val: R->getValueInit(FieldName: "HelpText" ))) { |
80 | HelpText = R->getValueAsString(FieldName: "HelpText" ).trim().str(); |
81 | if (!HelpText.empty() && HelpText.back() != '.') |
82 | HelpText.push_back(c: '.'); |
83 | } |
84 | |
85 | if (!isa<UnsetInit>(Val: R->getValueInit(FieldName: "Values" ))) { |
86 | SmallVector<StringRef> Values; |
87 | SplitString(Source: R->getValueAsString(FieldName: "Values" ), OutFragments&: Values, Delimiters: "," ); |
88 | HelpText += (" " + MetaVarName + " must be '" ).str(); |
89 | |
90 | if (Values.size() > 1) { |
91 | HelpText += join(Begin: Values.begin(), End: Values.end() - 1, Separator: "', '" ); |
92 | HelpText += "' or '" ; |
93 | } |
94 | HelpText += (Values.back() + "'." ).str(); |
95 | } |
96 | |
97 | if (!HelpText.empty()) { |
98 | OS << ' '; |
99 | OS.write_escaped(Str: HelpText); |
100 | OS << "\n\n" ; |
101 | } |
102 | } |
103 | } |
104 | } |
105 | |
106 | static TableGen::Emitter::Opt X("gen-opt-rst" , EmitOptRST, |
107 | "Generate option RST" ); |
108 | |