blob: 9b4bc29cf4155ee1070b7c4637bbc1f5d88819cd [file] [log] [blame]
Peter Collingbourne51d77772011-10-06 13:03:08 +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
Peter Collingbourne51d77772011-10-06 13:03:08 +000010#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000011#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000012#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) {
Roman Divacky31ba6132012-09-06 15:59:27 +000035 const Record *A = *(const Record*const*) Av;
36 const Record *B = *(const Record*const*) Bv;
Peter Collingbourne51d77772011-10-06 13:03:08 +000037
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;
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()))
48 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.
Sean Silva1ab46322012-10-10 20:25:43 +000059 if (isa<UnsetInit>(R.getValueInit("EnumName")))
Peter Collingbourne51d77772011-10-06 13:03:08 +000060 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
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000072/// OptParserEmitter - This tablegen backend takes an input .td file
73/// describing a list of options and emits a data structure for parsing and
74/// working with those options when given an input command line.
75namespace clang {
76void EmitOptParser(RecordKeeper &Records, raw_ostream &OS, bool GenDefs) {
Peter Collingbourne51d77772011-10-06 13:03:08 +000077 // Get the option groups and options.
78 const std::vector<Record*> &Groups =
79 Records.getAllDerivedDefinitions("OptionGroup");
80 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
81
82 if (GenDefs)
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000083 emitSourceFileHeader("Option Parsing Definitions", OS);
Peter Collingbourne51d77772011-10-06 13:03:08 +000084 else
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000085 emitSourceFileHeader("Option Parsing Table", OS);
Peter Collingbourne51d77772011-10-06 13:03:08 +000086
87 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
88 if (GenDefs) {
89 OS << "#ifndef OPTION\n";
90 OS << "#error \"Define OPTION prior to including this file!\"\n";
91 OS << "#endif\n\n";
92
93 OS << "/////////\n";
94 OS << "// Groups\n\n";
95 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
96 const Record &R = *Groups[i];
97
98 // Start a single option entry.
99 OS << "OPTION(";
100
101 // The option string.
102 OS << '"' << R.getValueAsString("Name") << '"';
103
104 // The option identifier name.
105 OS << ", "<< getOptionName(R);
106
107 // The option kind.
108 OS << ", Group";
109
110 // The containing option group (if any).
111 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000112 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000113 OS << getOptionName(*DI->getDef());
114 else
115 OS << "INVALID";
116
117 // The other option arguments (unused for groups).
118 OS << ", INVALID, 0, 0";
119
120 // The option help text.
Sean Silva1ab46322012-10-10 20:25:43 +0000121 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000122 OS << ",\n";
123 OS << " ";
124 write_cstring(OS, R.getValueAsString("HelpText"));
125 } else
126 OS << ", 0";
127
128 // The option meta-variable name (unused).
129 OS << ", 0)\n";
130 }
131 OS << "\n";
132
133 OS << "//////////\n";
134 OS << "// Options\n\n";
135 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
136 const Record &R = *Opts[i];
137
138 // Start a single option entry.
139 OS << "OPTION(";
140
141 // The option string.
142 write_cstring(OS, R.getValueAsString("Name"));
143
144 // The option identifier name.
145 OS << ", "<< getOptionName(R);
146
147 // The option kind.
148 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
149
150 // The containing option group (if any).
151 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000152 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000153 OS << getOptionName(*DI->getDef());
154 else
155 OS << "INVALID";
156
157 // The option alias (if any).
158 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000159 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000160 OS << getOptionName(*DI->getDef());
161 else
162 OS << "INVALID";
163
164 // The option flags.
165 const ListInit *LI = R.getValueAsListInit("Flags");
166 if (LI->empty()) {
167 OS << ", 0";
168 } else {
169 OS << ", ";
170 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
171 if (i)
172 OS << " | ";
Sean Silva1ab46322012-10-10 20:25:43 +0000173 OS << cast<DefInit>(LI->getElement(i))->getDef()->getName();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000174 }
175 }
176
177 // The option parameter field.
178 OS << ", " << R.getValueAsInt("NumArgs");
179
180 // The option help text.
Sean Silva1ab46322012-10-10 20:25:43 +0000181 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000182 OS << ",\n";
183 OS << " ";
184 write_cstring(OS, R.getValueAsString("HelpText"));
185 } else
186 OS << ", 0";
187
188 // The option meta-variable name.
189 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000190 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000191 write_cstring(OS, R.getValueAsString("MetaVarName"));
192 else
193 OS << "0";
194
195 OS << ")\n";
196 }
197 }
198}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000199} // end namespace clang