aboutsummaryrefslogtreecommitdiff
path: root/lib/Driver/OptTable.cpp
blob: 335d7720bfa73aaf1c4843efa72c62ea55864450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===--- Options.cpp - Option info table --------------------------------*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "clang/Driver/Options.h"

#include "clang/Driver/Option.h"
#include <cassert>

using namespace clang;
using namespace clang::driver;
using namespace clang::driver::options;

struct Info {
  Option::OptionClass Kind;
  const char *Name;
  unsigned GroupID;
  unsigned AliasID;
  const char *Flags;
  unsigned Param;
};

static Info OptionInfos[] = {
#define OPTION(ID, KIND, NAME, GROUP, ALIAS, FLAGS, PARAM)      \
  { Option::KIND##Class, NAME, GROUP, ALIAS, FLAGS, PARAM },
#include "clang/Driver/Options.def"
};
static const unsigned numOptions = sizeof(OptionInfos) / sizeof(OptionInfos[0]);

static Info &getInfo(unsigned id) {
  assert(id > 0 && id - 1 < numOptions && "Invalid Option ID.");
  return OptionInfos[id - 1];
}

OptTable::OptTable() : Options(new Option*[numOptions]) { 
}

OptTable::~OptTable() { 
  for (unsigned i=0; i<numOptions; ++i)
    delete Options[i];
  delete[] Options;
}

unsigned OptTable::getNumOptions() const {
  return numOptions;
}

const char *OptTable::getOptionName(options::ID id) const {
  return getInfo(id).Name;
}

const Option *OptTable::getOption(options::ID id) const {
  if (id == 0)
    return 0;

  assert((unsigned) (id - 1) < numOptions && "Invalid ID.");

  Option *&Entry = Options[id - 1];
  if (!Entry)
    Entry = constructOption(id);

  return Entry;
}

Option *OptTable::constructOption(options::ID id) const {
  Info &info = getInfo(id);
  const OptionGroup *Group = 
    cast_or_null<OptionGroup>(getOption((options::ID) info.GroupID));
  const Option *Alias = getOption((options::ID) info.AliasID);

  Option *Opt = 0;
  switch (info.Kind) {
  default:
    assert(0 && "Invalid option kind.");
  case Option::GroupClass:
    Opt = new OptionGroup(info.Name, Group); break;
  case Option::FlagClass:
    Opt = new FlagOption(info.Name, Group, Alias); break;
  case Option::JoinedClass:
    Opt = new JoinedOption(info.Name, Group, Alias); break;
  case Option::SeparateClass:
    Opt = new SeparateOption(info.Name, Group, Alias); break;
  case Option::CommaJoinedClass:
    Opt = new CommaJoinedOption(info.Name, Group, Alias); break;
  case Option::MultiArgClass:
    Opt = new MultiArgOption(info.Name, Group, Alias, info.Param); break;
  case Option::JoinedOrSeparateClass:
    Opt = new JoinedOrSeparateOption(info.Name, Group, Alias); break;
  case Option::JoinedAndSeparateClass:
    Opt = new JoinedAndSeparateOption(info.Name, Group, Alias); break;
  }

  for (const char *s = info.Flags; *s; ++s) {
    switch (*s) {
    default: assert(0 && "Invalid option flag.");
    case 'l': Opt->setLinkerInput(true); break;
    case 'i': Opt->setNoOptAsInput(true); break;
    case 'J': Opt->setForceJoinedRender(true); break;
    case 'S': Opt->setForceSeparateRender(true); break;
    case 'U': Opt->setUnsupported(true); break;
    }
  }

  return Opt;
}