aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/utils/TableGen/OptRSTEmitter.cpp
blob: 03c7326e817a80f002f9dc3d5da82fe7f08e74c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "OptEmitter.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/TableGen/Record.h"

using namespace llvm;

/// OptParserEmitter - This tablegen backend takes an input .td file
/// describing a list of options and emits a RST man page.
namespace llvm {
void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
  llvm::StringMap<std::vector<Record *>> OptionsByGroup;
  std::vector<Record *> OptionsWithoutGroup;

  // Get the options.
  std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
  array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);

  // Get the option groups.
  const std::vector<Record *> &Groups =
      Records.getAllDerivedDefinitions("OptionGroup");
  for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
    const Record &R = *Groups[i];
    OptionsByGroup.try_emplace(R.getValueAsString("Name"));
  }

  // Map options to their group.
  for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
    const Record &R = *Opts[i];
    if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
      OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(Opts[i]);
    } else {
      OptionsByGroup["options"].push_back(Opts[i]);
    }
  }

  // Print options under their group.
  for (const auto &KV : OptionsByGroup) {
    std::string GroupName = KV.getKey().upper();
    OS << GroupName << '\n';
    OS << std::string(GroupName.size(), '-') << '\n';
    OS << '\n';

    for (Record *R : KV.getValue()) {
      OS << ".. option:: ";

      // Print the prefix.
      std::vector<StringRef> Prefixes = R->getValueAsListOfStrings("Prefixes");
      if (!Prefixes.empty())
        OS << Prefixes[0];

      // Print the option name.
      OS << R->getValueAsString("Name");

      StringRef MetaVarName;
      // Print the meta-variable.
      if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {
        MetaVarName = R->getValueAsString("MetaVarName");
      } else if (!isa<UnsetInit>(R->getValueInit("Values")))
        MetaVarName = "<value>";

      if (!MetaVarName.empty()) {
        OS << '=';
        OS.write_escaped(MetaVarName);
      }

      OS << "\n\n";

      std::string HelpText;
      // The option help text.
      if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {
        HelpText = R->getValueAsString("HelpText").trim().str();
        if (!HelpText.empty() && HelpText.back() != '.')
          HelpText.push_back('.');
      }

      if (!isa<UnsetInit>(R->getValueInit("Values"))) {
        SmallVector<StringRef> Values;
        SplitString(R->getValueAsString("Values"), Values, ",");
        HelpText += (" " + MetaVarName + " must be '").str();

        if (Values.size() > 1) {
          HelpText += join(Values.begin(), Values.end() - 1, "', '");
          HelpText += "' or '";
        }
        HelpText += (Values.front() + "'.").str();
      }

      if (!HelpText.empty()) {
        OS << ' ';
        OS.write_escaped(HelpText);
        OS << "\n\n";
      }
    }
  }
}
} // end namespace llvm