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/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"
17#include <cctype>
18#include <cstring>
19#include <map>
20
21using namespace llvm;
22
23/// OptParserEmitter - This tablegen backend takes an input .td file
24/// describing a list of options and emits a RST man page.
25namespace llvm {
26void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
27  llvm::StringMap<std::vector<Record *>> OptionsByGroup;
28  std::vector<Record *> OptionsWithoutGroup;
29
30  // Get the options.
31  std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
32  array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
33
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"));
40  }
41
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]);
47    } else {
48      OptionsByGroup["options"].push_back(Opts[i]);
49    }
50  }
51
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';
57    OS << '\n';
58
59    for (Record *R : KV.getValue()) {
60      OS << ".. option:: ";
61
62      // Print the prefix.
63      std::vector<StringRef> Prefixes = R->getValueAsListOfStrings("Prefixes");
64      if (!Prefixes.empty())
65        OS << Prefixes[0];
66
67      // Print the option name.
68      OS << R->getValueAsString("Name");
69
70      // Print the meta-variable.
71      if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {
72        OS << '=';
73        OS.write_escaped(R->getValueAsString("MetaVarName"));
74      }
75
76      OS << "\n\n";
77
78      // The option help text.
79      if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {
80        OS << ' ';
81        OS.write_escaped(R->getValueAsString("HelpText"));
82        OS << "\n\n";
83      }
84    }
85  }
86}
87} // end namespace llvm
88