OptRSTEmitter.cpp 3.3 KB

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