196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer//===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer//
396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer//                     The LLVM Compiler Infrastructure
496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer//
596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer// This file is distributed under the University of Illinois Open Source
696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer// License. See LICENSE.TXT for details.
796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer//
896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer//===----------------------------------------------------------------------===//
996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
1096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer#include "llvm/TableGen/Error.h"
1196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer#include "llvm/ADT/STLExtras.h"
1296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer#include "llvm/ADT/SmallString.h"
1396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer#include "llvm/ADT/Twine.h"
147f00f87767036e74445aad0164eea13cf2642610Chandler Carruth#include "llvm/TableGen/Record.h"
157f00f87767036e74445aad0164eea13cf2642610Chandler Carruth#include "llvm/TableGen/TableGenBackend.h"
162957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama#include <cctype>
1736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include <cstring>
1896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer#include <map>
1996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
2096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencerusing namespace llvm;
2196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
222957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama// Ordering on Info. The logic should match with the consumer-side function in
232957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama// llvm/Option/OptTable.h.
2496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencerstatic int StrCmpOptionName(const char *A, const char *B) {
252957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama  const char *X = A, *Y = B;
262957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama  char a = tolower(*A), b = tolower(*B);
271997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama  while (a == b) {
281997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama    if (a == '\0')
292957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama      return strcmp(A, B);
301997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama
312957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama    a = tolower(*++X);
322957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama    b = tolower(*++Y);
3396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  }
341997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama
351997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama  if (a == '\0') // A is a prefix of B.
361997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama    return 1;
371997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama  if (b == '\0') // B is a prefix of A.
381997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama    return -1;
391997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama
401997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama  // Otherwise lexicographic.
411997734e37775a68182b3fd508a52e0c28ff36f8Rui Ueyama  return (a < b) ? -1 : 1;
4296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer}
4396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
440d293e45b66c742fdbc3998209bb20ed6c5806bfBenjamin Kramerstatic int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
450d293e45b66c742fdbc3998209bb20ed6c5806bfBenjamin Kramer  const Record *A = *Av;
460d293e45b66c742fdbc3998209bb20ed6c5806bfBenjamin Kramer  const Record *B = *Bv;
4796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
4896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  // Sentinel options precede all others and are only ordered by precedence.
4996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
5096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
5196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  if (ASent != BSent)
5296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    return ASent ? -1 : 1;
5396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
5496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  // Compare options by name, unless they are sentinels.
5596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  if (!ASent)
5696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
5796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                   B->getValueAsString("Name").c_str()))
582957273b888dabe8be8e2fa5ac691e39879685c4Rui Ueyama      return Cmp;
5996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
6096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  if (!ASent) {
6196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
6296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
6396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
6496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
6596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                                  AEPre = APrefixes.end(),
6696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                                  BPre = BPrefixes.begin(),
6796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                                  BEPre = BPrefixes.end();
6896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                                  APre != AEPre &&
6996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                                  BPre != BEPre;
7096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                                  ++APre, ++BPre) {
7196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
7296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer        return Cmp;
7396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    }
7496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  }
7596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
7696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  // Then by the kind precedence;
7796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
7896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
7996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  if (APrec == BPrec &&
8096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      A->getValueAsListOfStrings("Prefixes") ==
8196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      B->getValueAsListOfStrings("Prefixes")) {
828f70847f98925153ad48fdeea1a8e72ee08d78ebPeter Collingbourne    PrintError(A->getLoc(), Twine("Option is equivalent to"));
8396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    PrintError(B->getLoc(), Twine("Other defined here"));
8496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    PrintFatalError("Equivalent Options found.");
8596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  }
8696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  return APrec < BPrec ? -1 : 1;
8796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer}
8896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
8996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencerstatic const std::string getOptionName(const Record &R) {
9096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  // Use the record name unless EnumName is defined.
9196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  if (isa<UnsetInit>(R.getValueInit("EnumName")))
9296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    return R.getName();
9396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
9496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  return R.getValueAsString("EnumName");
9596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer}
9696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
9796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencerstatic raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
9896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << '"';
9996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS.write_escaped(Str);
10096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << '"';
10196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  return OS;
10296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer}
10396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
10496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer/// OptParserEmitter - This tablegen backend takes an input .td file
10596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer/// describing a list of options and emits a data structure for parsing and
10696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer/// working with those options when given an input command line.
10796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencernamespace llvm {
10896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencervoid EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
10996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  // Get the option groups and options.
11096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  const std::vector<Record*> &Groups =
11196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    Records.getAllDerivedDefinitions("OptionGroup");
11296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
11396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
11496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  emitSourceFileHeader("Option Parsing Definitions", OS);
11596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
11696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
11796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  // Generate prefix groups.
11896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
11996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  typedef std::map<PrefixKeyT, std::string> PrefixesT;
12096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  PrefixesT Prefixes;
12196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
12296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  unsigned CurPrefix = 0;
12396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
12496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    const Record &R = *Opts[i];
12596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
12696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    PrefixKeyT prfkey(prf.begin(), prf.end());
12796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    unsigned NewPrefix = CurPrefix + 1;
12896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
12996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                              Twine(NewPrefix)).str())).second)
13096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      CurPrefix = NewPrefix;
13196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  }
13296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
13396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  // Dump prefixes.
13496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
13596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "/////////\n";
13696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "// Prefixes\n\n";
13796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "#ifdef PREFIX\n";
13896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "#define COMMA ,\n";
13996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
14096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                  I != E; ++I) {
14196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << "PREFIX(";
14296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
14396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // Prefix name.
14496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << I->second;
14596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
14696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // Prefix values.
14796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", {";
14896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    for (PrefixKeyT::const_iterator PI = I->first.begin(),
14996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer                                    PE = I->first.end(); PI != PE; ++PI) {
15096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << "\"" << *PI << "\" COMMA ";
15196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    }
152cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    OS << "nullptr})\n";
15396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  }
15496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "#undef COMMA\n";
155cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  OS << "#endif // PREFIX\n\n";
15696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
15796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "/////////\n";
15896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "// Groups\n\n";
15996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "#ifdef OPTION\n";
16096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
16196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    const Record &R = *Groups[i];
16296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
16396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // Start a single option entry.
164a15d5db7f25b609faa1780a954451e4a07d39f37Hans Wennborg    OS << "OPTION(";
16596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
16696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option prefix;
167cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    OS << "nullptr";
16896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
16996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option string.
17096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", \"" << R.getValueAsString("Name") << '"';
17196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
17296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option identifier name.
17396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS  << ", "<< getOptionName(R);
17496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
17596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option kind.
17696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", Group";
17796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
17896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The containing option group (if any).
17996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", ";
18096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
18196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << getOptionName(*DI->getDef());
18296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    else
18396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << "INVALID";
18496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
18596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The other option arguments (unused for groups).
186cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    OS << ", INVALID, nullptr, 0, 0";
18796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
18896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option help text.
18996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
19096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << ",\n";
19196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << "       ";
19296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      write_cstring(OS, R.getValueAsString("HelpText"));
19396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    } else
194cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      OS << ", nullptr";
19596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
19696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option meta-variable name (unused).
197cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    OS << ", nullptr)\n";
19896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  }
19996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "\n";
20096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
20196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "//////////\n";
20296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  OS << "// Options\n\n";
20396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
20496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    const Record &R = *Opts[i];
20596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
20696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // Start a single option entry.
207a15d5db7f25b609faa1780a954451e4a07d39f37Hans Wennborg    OS << "OPTION(";
20896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
20996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option prefix;
21096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
21196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
21296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
21396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option string.
21496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    write_cstring(OS, R.getValueAsString("Name"));
21596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
21696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option identifier name.
21796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS  << ", "<< getOptionName(R);
21896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
21996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option kind.
22096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
22196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
22296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The containing option group (if any).
22396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", ";
22437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    const ListInit *GroupFlags = nullptr;
22537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
22637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      GroupFlags = DI->getDef()->getValueAsListInit("Flags");
22796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << getOptionName(*DI->getDef());
22837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    } else
22996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << "INVALID";
23096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
23196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option alias (if any).
23296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", ";
23396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
23496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << getOptionName(*DI->getDef());
23596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    else
23696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << "INVALID";
23796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
2389dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    // The option alias arguments (if any).
2399dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
2409dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    // would become "foo\0bar\0". Note that the compiler adds an implicit
2419dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    // terminating \0 at the end.
2429dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    OS << ", ";
2439dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    std::vector<std::string> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
2449dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    if (AliasArgs.size() == 0) {
245cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      OS << "nullptr";
2469dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    } else {
2479dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg      OS << "\"";
2489dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg      for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
2499dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg        OS << AliasArgs[i] << "\\0";
2509dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg      OS << "\"";
2519dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg    }
2529dd8c0cffe7de82900823c05159bba765120f1e3Hans Wennborg
25396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option flags.
25437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    OS << ", ";
25537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    int NumFlags = 0;
25696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    const ListInit *LI = R.getValueAsListInit("Flags");
25737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    for (Init *I : *LI)
25837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      OS << (NumFlags++ ? " | " : "")
25937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines         << cast<DefInit>(I)->getDef()->getName();
26037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (GroupFlags) {
26137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      for (Init *I : *GroupFlags)
26237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        OS << (NumFlags++ ? " | " : "")
26337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines           << cast<DefInit>(I)->getDef()->getName();
26496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    }
26537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (NumFlags == 0)
26637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      OS << '0';
26796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
26896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option parameter field.
26996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", " << R.getValueAsInt("NumArgs");
27096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
27196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option help text.
27296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
27396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << ",\n";
27496a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      OS << "       ";
27596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      write_cstring(OS, R.getValueAsString("HelpText"));
27696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    } else
277cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      OS << ", nullptr";
27896a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
27996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    // The option meta-variable name.
28096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ", ";
28196a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
28296a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer      write_cstring(OS, R.getValueAsString("MetaVarName"));
28396a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    else
284cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      OS << "nullptr";
28596a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer
28696a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer    OS << ")\n";
28796a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer  }
288cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  OS << "#endif // OPTION\n";
28996a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer}
29096a564f2beec8c1930f0640844eec433e03bbce8Michael J. Spencer} // end namespace llvm
291