Silence -Wunused-variable in release builds.
[llvm/stm8.git] / utils / TableGen / OptParserEmitter.cpp
blob431026c669c34ebbe7a0ee90538230beacf9fb2a
1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "OptParserEmitter.h"
11 #include "Record.h"
12 #include "llvm/ADT/STLExtras.h"
13 using namespace llvm;
15 static int StrCmpOptionName(const char *A, const char *B) {
16 char a = *A, b = *B;
17 while (a == b) {
18 if (a == '\0')
19 return 0;
21 a = *++A;
22 b = *++B;
25 if (a == '\0') // A is a prefix of B.
26 return 1;
27 if (b == '\0') // B is a prefix of A.
28 return -1;
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 precede all others and are only ordered by precedence.
39 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
40 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
41 if (ASent != BSent)
42 return ASent ? -1 : 1;
44 // Compare options by name, unless they are sentinels.
45 if (!ASent)
46 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
47 B->getValueAsString("Name").c_str()))
48 return Cmp;
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")))
60 return R.getName();
62 return R.getValueAsString("EnumName");
65 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
66 OS << '"';
67 OS.write_escaped(Str);
68 OS << '"';
69 return OS;
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");
78 if (GenDefs)
79 EmitSourceFileHeader("Option Parsing Definitions", OS);
80 else
81 EmitSourceFileHeader("Option Parsing Table", OS);
83 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
84 if (GenDefs) {
85 OS << "#ifndef OPTION\n";
86 OS << "#error \"Define OPTION prior to including this file!\"\n";
87 OS << "#endif\n\n";
89 OS << "/////////\n";
90 OS << "// Groups\n\n";
91 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
92 const Record &R = *Groups[i];
94 // Start a single option entry.
95 OS << "OPTION(";
97 // The option string.
98 OS << '"' << R.getValueAsString("Name") << '"';
100 // The option identifier name.
101 OS << ", "<< getOptionName(R);
103 // The option kind.
104 OS << ", Group";
106 // The containing option group (if any).
107 OS << ", ";
108 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
109 OS << getOptionName(*DI->getDef());
110 else
111 OS << "INVALID";
113 // The other option arguments (unused for groups).
114 OS << ", INVALID, 0, 0";
116 // The option help text.
117 if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
118 OS << ",\n";
119 OS << " ";
120 write_cstring(OS, R.getValueAsString("HelpText"));
121 } else
122 OS << ", 0";
124 // The option meta-variable name (unused).
125 OS << ", 0)\n";
127 OS << "\n";
129 OS << "//////////\n";
130 OS << "// Options\n\n";
131 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
132 const Record &R = *Opts[i];
134 // Start a single option entry.
135 OS << "OPTION(";
137 // The option string.
138 write_cstring(OS, R.getValueAsString("Name"));
140 // The option identifier name.
141 OS << ", "<< getOptionName(R);
143 // The option kind.
144 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
146 // The containing option group (if any).
147 OS << ", ";
148 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
149 OS << getOptionName(*DI->getDef());
150 else
151 OS << "INVALID";
153 // The option alias (if any).
154 OS << ", ";
155 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Alias")))
156 OS << getOptionName(*DI->getDef());
157 else
158 OS << "INVALID";
160 // The option flags.
161 const ListInit *LI = R.getValueAsListInit("Flags");
162 if (LI->empty()) {
163 OS << ", 0";
164 } else {
165 OS << ", ";
166 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
167 if (i)
168 OS << " | ";
169 OS << dynamic_cast<DefInit*>(LI->getElement(i))->getDef()->getName();
173 // The option parameter field.
174 OS << ", " << R.getValueAsInt("NumArgs");
176 // The option help text.
177 if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
178 OS << ",\n";
179 OS << " ";
180 write_cstring(OS, R.getValueAsString("HelpText"));
181 } else
182 OS << ", 0";
184 // The option meta-variable name.
185 OS << ", ";
186 if (!dynamic_cast<UnsetInit*>(R.getValueInit("MetaVarName")))
187 write_cstring(OS, R.getValueAsString("MetaVarName"));
188 else
189 OS << "0";
191 OS << ")\n";