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