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 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");
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");
79 EmitSourceFileHeader("Option Parsing Definitions", OS
);
81 EmitSourceFileHeader("Option Parsing Table", OS
);
83 array_pod_sort(Opts
.begin(), Opts
.end(), CompareOptionRecords
);
85 OS
<< "#ifndef OPTION\n";
86 OS
<< "#error \"Define OPTION prior to including this file!\"\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.
98 OS
<< '"' << R
.getValueAsString("Name") << '"';
100 // The option identifier name.
101 OS
<< ", "<< getOptionName(R
);
106 // The containing option group (if any).
108 if (const DefInit
*DI
= dynamic_cast<DefInit
*>(R
.getValueInit("Group")))
109 OS
<< getOptionName(*DI
->getDef());
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"))) {
120 write_cstring(OS
, R
.getValueAsString("HelpText"));
124 // The option meta-variable name (unused).
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.
137 // The option string.
138 write_cstring(OS
, R
.getValueAsString("Name"));
140 // The option identifier name.
141 OS
<< ", "<< getOptionName(R
);
144 OS
<< ", " << R
.getValueAsDef("Kind")->getValueAsString("Name");
146 // The containing option group (if any).
148 if (const DefInit
*DI
= dynamic_cast<DefInit
*>(R
.getValueInit("Group")))
149 OS
<< getOptionName(*DI
->getDef());
153 // The option alias (if any).
155 if (const DefInit
*DI
= dynamic_cast<DefInit
*>(R
.getValueInit("Alias")))
156 OS
<< getOptionName(*DI
->getDef());
161 const ListInit
*LI
= R
.getValueAsListInit("Flags");
166 for (unsigned i
= 0, e
= LI
->size(); i
!= e
; ++i
) {
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"))) {
180 write_cstring(OS
, R
.getValueAsString("HelpText"));
184 // The option meta-variable name.
186 if (!dynamic_cast<UnsetInit
*>(R
.getValueInit("MetaVarName")))
187 write_cstring(OS
, R
.getValueAsString("MetaVarName"));