OptRSTEmitter.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/SmallString.h"
  11. #include "llvm/ADT/StringMap.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. using namespace llvm;
  20. /// OptParserEmitter - This tablegen backend takes an input .td file
  21. /// describing a list of options and emits a RST man page.
  22. namespace llvm {
  23. void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
  24. llvm::StringMap<std::vector<Record *>> OptionsByGroup;
  25. std::vector<Record *> OptionsWithoutGroup;
  26. // Get the options.
  27. std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
  28. array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
  29. // Get the option groups.
  30. const std::vector<Record *> &Groups =
  31. Records.getAllDerivedDefinitions("OptionGroup");
  32. for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
  33. const Record &R = *Groups[i];
  34. OptionsByGroup.try_emplace(R.getValueAsString("Name"));
  35. }
  36. // Map options to their group.
  37. for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
  38. const Record &R = *Opts[i];
  39. if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
  40. OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(Opts[i]);
  41. } else {
  42. OptionsByGroup["options"].push_back(Opts[i]);
  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. for (Record *R : KV.getValue()) {
  52. OS << ".. option:: ";
  53. // Print the prefix.
  54. std::vector<StringRef> Prefixes = R->getValueAsListOfStrings("Prefixes");
  55. if (!Prefixes.empty())
  56. OS << Prefixes[0];
  57. // Print the option name.
  58. OS << R->getValueAsString("Name");
  59. // Print the meta-variable.
  60. if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {
  61. OS << '=';
  62. OS.write_escaped(R->getValueAsString("MetaVarName"));
  63. }
  64. OS << "\n\n";
  65. // The option help text.
  66. if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {
  67. OS << ' ';
  68. OS.write_escaped(R->getValueAsString("HelpText"));
  69. OS << "\n\n";
  70. }
  71. }
  72. }
  73. }
  74. } // end namespace llvm