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