blob: ce1aef5e4daddb9703aa7f498a8bfbb1e5b16c05 [file] [log] [blame]
Daniel Dunbar2d6a8fb2009-11-18 21:29:51 +00001//===- 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//===----------------------------------------------------------------------===//
9
10#include "OptParserEmitter.h"
11#include "Record.h"
12#include "llvm/ADT/STLExtras.h"
13using namespace llvm;
14
15static 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;
20
21 a = *++A;
22 b = *++B;
23 }
24
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;
29
30 // Otherwise lexicographic.
31 return (a < b) ? -1 : 1;
32}
33
34static int CompareOptionRecords(const void *Av, const void *Bv) {
35 const Record *A = *(Record**) Av;
36 const Record *B = *(Record**) Bv;
37
Daniel Dunbara627ef52009-11-19 18:22:16 +000038 // 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");
41 if (ASent != BSent)
42 return ASent ? -1 : 1;
43
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()))
Daniel Dunbar2d6a8fb2009-11-18 21:29:51 +000048 return Cmp;
49
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;
55}
56
57static 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();
61
62 return R.getValueAsString("EnumName");
63}
64
65static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
66 OS << '"';
67 OS.write_escaped(Str);
68 OS << '"';
69 return OS;
70}
71
72void 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");
77
78 if (GenDefs) {
79 OS << "\
80//=== TableGen'erated File - Option Parsing Definitions ---------*- C++ -*-===//\n \
81//\n\
82// Option Parsing Definitions\n\
83//\n\
84// Automatically generated file, do not edit!\n\
85//\n\
86//===----------------------------------------------------------------------===//\n";
87 } else {
88 OS << "\
89//=== TableGen'erated File - Option Parsing Table ---------------*- C++ -*-===//\n \
90//\n\
91// Option Parsing Definitions\n\
92//\n\
93// Automatically generated file, do not edit!\n\
94//\n\
95//===----------------------------------------------------------------------===//\n";
96 }
97 OS << "\n";
98
99 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
100 if (GenDefs) {
101 OS << "#ifndef OPTION\n";
102 OS << "#error \"Define OPTION prior to including this file!\"\n";
103 OS << "#endif\n\n";
104
105 OS << "/////////\n";
106 OS << "// Groups\n\n";
107 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
108 const Record &R = *Groups[i];
109
110 // Start a single option entry.
111 OS << "OPTION(";
112
113 // The option string.
114 OS << '"' << R.getValueAsString("Name") << '"';
115
116 // The option identifier name.
117 OS << ", "<< getOptionName(R);
118
119 // The option kind.
120 OS << ", Group";
121
122 // The containing option group (if any).
123 OS << ", ";
124 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
125 OS << getOptionName(*DI->getDef());
126 else
127 OS << "INVALID";
128
129 // The other option arguments (unused for groups).
130 OS << ", INVALID, 0, 0, 0, 0)\n";
131 }
132 OS << "\n";
133
134 OS << "//////////\n";
135 OS << "// Options\n\n";
136 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
137 const Record &R = *Opts[i];
138
139 // Start a single option entry.
140 OS << "OPTION(";
141
142 // The option string.
143 write_cstring(OS, R.getValueAsString("Name"));
144
145 // The option identifier name.
146 OS << ", "<< getOptionName(R);
147
148 // The option kind.
149 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
150
151 // The containing option group (if any).
152 OS << ", ";
153 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
154 OS << getOptionName(*DI->getDef());
155 else
156 OS << "INVALID";
157
158 // The option alias (if any).
159 OS << ", ";
160 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Alias")))
161 OS << getOptionName(*DI->getDef());
162 else
163 OS << "INVALID";
164
165 // The option flags.
166 const ListInit *LI = R.getValueAsListInit("Flags");
167 if (LI->empty()) {
168 OS << ", 0";
169 } else {
170 OS << ", ";
171 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
172 if (i)
173 OS << " | ";
174 OS << dynamic_cast<DefInit*>(LI->getElement(i))->getDef()->getName();
175 }
176 }
177
178 // The option parameter field.
179 OS << ", " << R.getValueAsInt("NumArgs");
180
181 // The option help text.
182 if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
183 OS << ",\n";
184 OS << " ";
185 write_cstring(OS, R.getValueAsString("HelpText"));
186 } else
187 OS << ", 0";
188
189 // The option meta-variable name.
190 OS << ", ";
191 if (!dynamic_cast<UnsetInit*>(R.getValueInit("MetaVarName")))
192 write_cstring(OS, R.getValueAsString("MetaVarName"));
193 else
194 OS << "0";
195
196 OS << ")\n";
197 }
198 }
199}