1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "OptParserEmitter.h"
12 #include "llvm/ADT/STLExtras.h"
15 static int StrCmpOptionName(const char *A, const char *B) {
25 if (a == '\0') // A is a prefix of B.
27 if (b == '\0') // B is a prefix of A.
30 // Otherwise lexicographic.
31 return (a < b) ? -1 : 1;
34 static int CompareOptionRecords(const void *Av, const void *Bv) {
35 const Record *A = *(Record**) Av;
36 const Record *B = *(Record**) Bv;
38 // Sentinel options preceed all others and are only ordered by precedence.
39 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
40 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
42 return ASent ? -1 : 1;
44 // Compare options by name, unless they are sentinels.
46 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
47 B->getValueAsString("Name").c_str()))
50 // Then by the kind precedence;
51 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
52 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
53 assert(APrec != BPrec && "Options are equivalent!");
54 return APrec < BPrec ? -1 : 1;
57 static const std::string getOptionName(const Record &R) {
58 // Use the record name unless EnumName is defined.
59 if (dynamic_cast<UnsetInit*>(R.getValueInit("EnumName")))
62 return R.getValueAsString("EnumName");
65 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
67 OS.write_escaped(Str);
72 void OptParserEmitter::run(raw_ostream &OS) {
73 // Get the option groups and options.
74 const std::vector<Record*> &Groups =
75 Records.getAllDerivedDefinitions("OptionGroup");
76 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
80 //=== TableGen'erated File - Option Parsing Definitions ---------*- C++ -*-===//\n \
82 // Option Parsing Definitions\n\
84 // Automatically generated file, do not edit!\n\
86 //===----------------------------------------------------------------------===//\n";
89 //=== TableGen'erated File - Option Parsing Table ---------------*- C++ -*-===//\n \
91 // Option Parsing Definitions\n\
93 // Automatically generated file, do not edit!\n\
95 //===----------------------------------------------------------------------===//\n";
99 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
101 OS << "#ifndef OPTION\n";
102 OS << "#error \"Define OPTION prior to including this file!\"\n";
106 OS << "// Groups\n\n";
107 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
108 const Record &R = *Groups[i];
110 // Start a single option entry.
113 // The option string.
114 OS << '"' << R.getValueAsString("Name") << '"';
116 // The option identifier name.
117 OS << ", "<< getOptionName(R);
122 // The containing option group (if any).
124 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
125 OS << getOptionName(*DI->getDef());
129 // The other option arguments (unused for groups).
130 OS << ", INVALID, 0, 0";
132 // The option help text.
133 if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
136 write_cstring(OS, R.getValueAsString("HelpText"));
140 // The option meta-variable name (unused).
145 OS << "//////////\n";
146 OS << "// Options\n\n";
147 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
148 const Record &R = *Opts[i];
150 // Start a single option entry.
153 // The option string.
154 write_cstring(OS, R.getValueAsString("Name"));
156 // The option identifier name.
157 OS << ", "<< getOptionName(R);
160 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
162 // The containing option group (if any).
164 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
165 OS << getOptionName(*DI->getDef());
169 // The option alias (if any).
171 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Alias")))
172 OS << getOptionName(*DI->getDef());
177 const ListInit *LI = R.getValueAsListInit("Flags");
182 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
185 OS << dynamic_cast<DefInit*>(LI->getElement(i))->getDef()->getName();
189 // The option parameter field.
190 OS << ", " << R.getValueAsInt("NumArgs");
192 // The option help text.
193 if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
196 write_cstring(OS, R.getValueAsString("HelpText"));
200 // The option meta-variable name.
202 if (!dynamic_cast<UnsetInit*>(R.getValueInit("MetaVarName")))
203 write_cstring(OS, R.getValueAsString("MetaVarName"));