OptParserEmitter.cpp revision 239462
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//===----------------------------------------------------------------------===//
9
10#include "llvm/TableGen/Record.h"
11#include "llvm/TableGen/TableGenBackend.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
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.
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
72/// 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) {
77  // 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)
83    emitSourceFileHeader("Option Parsing Definitions", OS);
84  else
85    emitSourceFileHeader("Option Parsing Table", OS);
86
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 << ", ";
112      if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
113        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.
121      if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
122        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 << ", ";
152      if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group")))
153        OS << getOptionName(*DI->getDef());
154      else
155        OS << "INVALID";
156
157      // The option alias (if any).
158      OS << ", ";
159      if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Alias")))
160        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 << " | ";
173          OS << dynamic_cast<DefInit*>(LI->getElement(i))->getDef()->getName();
174        }
175      }
176
177      // The option parameter field.
178      OS << ", " << R.getValueAsInt("NumArgs");
179
180      // The option help text.
181      if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) {
182        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 << ", ";
190      if (!dynamic_cast<UnsetInit*>(R.getValueInit("MetaVarName")))
191        write_cstring(OS, R.getValueAsString("MetaVarName"));
192      else
193        OS << "0";
194
195      OS << ")\n";
196    }
197  }
198}
199} // end namespace clang
200