OptRSTEmitter.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // Print the meta-variable.
  53. if (!isa<UnsetInit>(R->getValueInit("MetaVarName"))) {
  54. OS << '=';
  55. OS.write_escaped(R->getValueAsString("MetaVarName"));
  56. }
  57. OS << "\n\n";
  58. // The option help text.
  59. if (!isa<UnsetInit>(R->getValueInit("HelpText"))) {
  60. OS << ' ';
  61. OS.write_escaped(R->getValueAsString("HelpText"));
  62. OS << "\n\n";
  63. }
  64. }
  65. }
  66. }
  67. } // end namespace llvm