~ubuntu-branches/ubuntu/wily/clamav/wily-proposed

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/include/llvm/Support/CommandLine.h

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Sebastian Andrzej Siewior, Andreas Cadhalpun, Scott Kitterman, Javier Fernández-Sanguino
  • Date: 2015-01-28 00:25:13 UTC
  • mfrom: (0.48.14 sid)
  • Revision ID: package-import@ubuntu.com-20150128002513-lil2oi74cooy4lzr
Tags: 0.98.6+dfsg-1
[ Sebastian Andrzej Siewior ]
* update "fix-ssize_t-size_t-off_t-printf-modifier", include of misc.h was
  missing but was pulled in via the systemd patch.
* Don't leak return codes from libmspack to clamav API. (Closes: #774686).

[ Andreas Cadhalpun ]
* Add patch to avoid emitting incremental progress messages when not
  outputting to a terminal. (Closes: #767350)
* Update lintian-overrides for unused-file-paragraph-in-dep5-copyright.
* clamav-base.postinst: always chown /var/log/clamav and /var/lib/clamav
  to clamav:clamav, not only on fresh installations. (Closes: #775400)
* Adapt the clamav-daemon and clamav-freshclam logrotate scripts,
  so that they correctly work under systemd.
* Move the PidFile variable from the clamd/freshclam configuration files
  to the init scripts. This makes the init scripts more robust against
  misconfiguration and avoids error messages with systemd. (Closes: #767353)
* debian/copyright: drop files from Files-Excluded only present in github
  tarballs
* Drop Workaround-a-bug-in-libc-on-Hurd.patch, because hurd got fixed.
  (see #752237)
* debian/rules: Remove useless --with-system-tommath --without-included-ltdl
  configure options.

[ Scott Kitterman ]
* Stop stripping llvm when repacking the tarball as the system llvm on some
  releases is too old to use
* New upstream bugfix release
  - Library shared object revisions.
  - Includes a patch from Sebastian Andrzej Siewior making ClamAV pid files
    compatible with systemd.
  - Fix a heap out of bounds condition with crafted Yoda's crypter files.
    This issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted mew packer files. This
    issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted upx packer files. This
    issue was discovered by Kevin Szkudlapski of Quarkslab.
  - Fix a heap out of bounds condition with crafted upack packer files. This
    issue was discovered by Sebastian Andrzej Siewior. CVE-2014-9328.
  - Compensate a crash due to incorrect compiler optimization when handling
    crafted petite packer files. This issue was discovered by Sebastian
    Andrzej Siewior.
* Update lintian override for embedded zlib to match new so version

[ Javier Fernández-Sanguino ]
* Updated Spanish Debconf template translation (Closes: #773563)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
 
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
// This class implements a command line argument processor that is useful when
 
11
// creating a tool.  It provides a simple, minimalistic interface that is easily
 
12
// extensible and supports nonlocal (library) command line options.
 
13
//
 
14
// Note that rather than trying to figure out what this code does, you should
 
15
// read the library documentation located in docs/CommandLine.html or looks at
 
16
// the many example usages in tools/*/*.cpp
 
17
//
 
18
//===----------------------------------------------------------------------===//
 
19
 
 
20
#ifndef LLVM_SUPPORT_COMMANDLINE_H
 
21
#define LLVM_SUPPORT_COMMANDLINE_H
 
22
 
 
23
#include "llvm/Support/type_traits.h"
 
24
#include "llvm/Support/Compiler.h"
 
25
#include "llvm/ADT/SmallVector.h"
 
26
#include "llvm/ADT/Twine.h"
 
27
#include <cassert>
 
28
#include <climits>
 
29
#include <cstdarg>
 
30
#include <utility>
 
31
#include <vector>
 
32
 
 
33
namespace llvm {
 
34
 
 
35
/// cl Namespace - This namespace contains all of the command line option
 
36
/// processing machinery.  It is intentionally a short name to make qualified
 
37
/// usage concise.
 
38
namespace cl {
 
39
 
 
40
//===----------------------------------------------------------------------===//
 
41
// ParseCommandLineOptions - Command line option processing entry point.
 
42
//
 
43
void ParseCommandLineOptions(int argc, char **argv,
 
44
                             const char *Overview = 0,
 
45
                             bool ReadResponseFiles = false);
 
46
 
 
47
//===----------------------------------------------------------------------===//
 
48
// ParseEnvironmentOptions - Environment variable option processing alternate
 
49
//                           entry point.
 
50
//
 
51
void ParseEnvironmentOptions(const char *progName, const char *envvar,
 
52
                             const char *Overview = 0,
 
53
                             bool ReadResponseFiles = false);
 
54
 
 
55
///===---------------------------------------------------------------------===//
 
56
/// SetVersionPrinter - Override the default (LLVM specific) version printer
 
57
///                     used to print out the version when --version is given
 
58
///                     on the command line. This allows other systems using the
 
59
///                     CommandLine utilities to print their own version string.
 
60
void SetVersionPrinter(void (*func)());
 
61
 
 
62
 
 
63
// MarkOptionsChanged - Internal helper function.
 
64
void MarkOptionsChanged();
 
65
 
 
66
//===----------------------------------------------------------------------===//
 
67
// Flags permitted to be passed to command line arguments
 
68
//
 
69
 
 
70
enum NumOccurrencesFlag {      // Flags for the number of occurrences allowed
 
71
  Optional        = 0x01,      // Zero or One occurrence
 
72
  ZeroOrMore      = 0x02,      // Zero or more occurrences allowed
 
73
  Required        = 0x03,      // One occurrence required
 
74
  OneOrMore       = 0x04,      // One or more occurrences required
 
75
 
 
76
  // ConsumeAfter - Indicates that this option is fed anything that follows the
 
77
  // last positional argument required by the application (it is an error if
 
78
  // there are zero positional arguments, and a ConsumeAfter option is used).
 
79
  // Thus, for example, all arguments to LLI are processed until a filename is
 
80
  // found.  Once a filename is found, all of the succeeding arguments are
 
81
  // passed, unprocessed, to the ConsumeAfter option.
 
82
  //
 
83
  ConsumeAfter    = 0x05,
 
84
 
 
85
  OccurrencesMask  = 0x07
 
86
};
 
87
 
 
88
enum ValueExpected {           // Is a value required for the option?
 
89
  ValueOptional   = 0x08,      // The value can appear... or not
 
90
  ValueRequired   = 0x10,      // The value is required to appear!
 
91
  ValueDisallowed = 0x18,      // A value may not be specified (for flags)
 
92
  ValueMask       = 0x18
 
93
};
 
94
 
 
95
enum OptionHidden {            // Control whether -help shows this option
 
96
  NotHidden       = 0x20,      // Option included in -help & -help-hidden
 
97
  Hidden          = 0x40,      // -help doesn't, but -help-hidden does
 
98
  ReallyHidden    = 0x60,      // Neither -help nor -help-hidden show this arg
 
99
  HiddenMask      = 0x60
 
100
};
 
101
 
 
102
// Formatting flags - This controls special features that the option might have
 
103
// that cause it to be parsed differently...
 
104
//
 
105
// Prefix - This option allows arguments that are otherwise unrecognized to be
 
106
// matched by options that are a prefix of the actual value.  This is useful for
 
107
// cases like a linker, where options are typically of the form '-lfoo' or
 
108
// '-L../../include' where -l or -L are the actual flags.  When prefix is
 
109
// enabled, and used, the value for the flag comes from the suffix of the
 
110
// argument.
 
111
//
 
112
// Grouping - With this option enabled, multiple letter options are allowed to
 
113
// bunch together with only a single hyphen for the whole group.  This allows
 
114
// emulation of the behavior that ls uses for example: ls -la === ls -l -a
 
115
//
 
116
 
 
117
enum FormattingFlags {
 
118
  NormalFormatting = 0x000,     // Nothing special
 
119
  Positional       = 0x080,     // Is a positional argument, no '-' required
 
120
  Prefix           = 0x100,     // Can this option directly prefix its value?
 
121
  Grouping         = 0x180,     // Can this option group with other options?
 
122
  FormattingMask   = 0x180      // Union of the above flags.
 
123
};
 
124
 
 
125
enum MiscFlags {               // Miscellaneous flags to adjust argument
 
126
  CommaSeparated     = 0x200,  // Should this cl::list split between commas?
 
127
  PositionalEatsArgs = 0x400,  // Should this positional cl::list eat -args?
 
128
  Sink               = 0x800,  // Should this cl::list eat all unknown options?
 
129
  MiscMask           = 0xE00   // Union of the above flags.
 
130
};
 
131
 
 
132
 
 
133
 
 
134
//===----------------------------------------------------------------------===//
 
135
// Option Base class
 
136
//
 
137
class alias;
 
138
class Option {
 
139
  friend class alias;
 
140
 
 
141
  // handleOccurrences - Overriden by subclasses to handle the value passed into
 
142
  // an argument.  Should return true if there was an error processing the
 
143
  // argument and the program should exit.
 
144
  //
 
145
  virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
 
146
                                StringRef Arg) = 0;
 
147
 
 
148
  virtual enum ValueExpected getValueExpectedFlagDefault() const {
 
149
    return ValueOptional;
 
150
  }
 
151
 
 
152
  // Out of line virtual function to provide home for the class.
 
153
  virtual void anchor();
 
154
 
 
155
  int NumOccurrences;     // The number of times specified
 
156
  int Flags;              // Flags for the argument
 
157
  unsigned Position;      // Position of last occurrence of the option
 
158
  unsigned AdditionalVals;// Greater than 0 for multi-valued option.
 
159
  Option *NextRegistered; // Singly linked list of registered options.
 
160
public:
 
161
  const char *ArgStr;     // The argument string itself (ex: "help", "o")
 
162
  const char *HelpStr;    // The descriptive text message for -help
 
163
  const char *ValueStr;   // String describing what the value of this option is
 
164
 
 
165
  inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
 
166
    return static_cast<enum NumOccurrencesFlag>(Flags & OccurrencesMask);
 
167
  }
 
168
  inline enum ValueExpected getValueExpectedFlag() const {
 
169
    int VE = Flags & ValueMask;
 
170
    return VE ? static_cast<enum ValueExpected>(VE)
 
171
              : getValueExpectedFlagDefault();
 
172
  }
 
173
  inline enum OptionHidden getOptionHiddenFlag() const {
 
174
    return static_cast<enum OptionHidden>(Flags & HiddenMask);
 
175
  }
 
176
  inline enum FormattingFlags getFormattingFlag() const {
 
177
    return static_cast<enum FormattingFlags>(Flags & FormattingMask);
 
178
  }
 
179
  inline unsigned getMiscFlags() const {
 
180
    return Flags & MiscMask;
 
181
  }
 
182
  inline unsigned getPosition() const { return Position; }
 
183
  inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
 
184
 
 
185
  // hasArgStr - Return true if the argstr != ""
 
186
  bool hasArgStr() const { return ArgStr[0] != 0; }
 
187
 
 
188
  //-------------------------------------------------------------------------===
 
189
  // Accessor functions set by OptionModifiers
 
190
  //
 
191
  void setArgStr(const char *S) { ArgStr = S; }
 
192
  void setDescription(const char *S) { HelpStr = S; }
 
193
  void setValueStr(const char *S) { ValueStr = S; }
 
194
 
 
195
  void setFlag(unsigned Flag, unsigned FlagMask) {
 
196
    Flags &= ~FlagMask;
 
197
    Flags |= Flag;
 
198
  }
 
199
 
 
200
  void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) {
 
201
    setFlag(Val, OccurrencesMask);
 
202
  }
 
203
  void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
 
204
  void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
 
205
  void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
 
206
  void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
 
207
  void setPosition(unsigned pos) { Position = pos; }
 
208
protected:
 
209
  explicit Option(unsigned DefaultFlags)
 
210
    : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
 
211
      AdditionalVals(0), NextRegistered(0),
 
212
      ArgStr(""), HelpStr(""), ValueStr("") {
 
213
    assert(getNumOccurrencesFlag() != 0 &&
 
214
           getOptionHiddenFlag() != 0 && "Not all default flags specified!");
 
215
  }
 
216
 
 
217
  inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
 
218
public:
 
219
  // addArgument - Register this argument with the commandline system.
 
220
  //
 
221
  void addArgument();
 
222
 
 
223
  Option *getNextRegisteredOption() const { return NextRegistered; }
 
224
 
 
225
  // Return the width of the option tag for printing...
 
226
  virtual size_t getOptionWidth() const = 0;
 
227
 
 
228
  // printOptionInfo - Print out information about this option.  The
 
229
  // to-be-maintained width is specified.
 
230
  //
 
231
  virtual void printOptionInfo(size_t GlobalWidth) const = 0;
 
232
 
 
233
  virtual void getExtraOptionNames(SmallVectorImpl<const char*> &) {}
 
234
 
 
235
  // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
 
236
  //
 
237
  bool addOccurrence(unsigned pos, StringRef ArgName,
 
238
                     StringRef Value, bool MultiArg = false);
 
239
 
 
240
  // Prints option name followed by message.  Always returns true.
 
241
  bool error(const Twine &Message, StringRef ArgName = StringRef());
 
242
 
 
243
public:
 
244
  inline int getNumOccurrences() const { return NumOccurrences; }
 
245
  virtual ~Option() {}
 
246
};
 
247
 
 
248
 
 
249
//===----------------------------------------------------------------------===//
 
250
// Command line option modifiers that can be used to modify the behavior of
 
251
// command line option parsers...
 
252
//
 
253
 
 
254
// desc - Modifier to set the description shown in the -help output...
 
255
struct desc {
 
256
  const char *Desc;
 
257
  desc(const char *Str) : Desc(Str) {}
 
258
  void apply(Option &O) const { O.setDescription(Desc); }
 
259
};
 
260
 
 
261
// value_desc - Modifier to set the value description shown in the -help
 
262
// output...
 
263
struct value_desc {
 
264
  const char *Desc;
 
265
  value_desc(const char *Str) : Desc(Str) {}
 
266
  void apply(Option &O) const { O.setValueStr(Desc); }
 
267
};
 
268
 
 
269
// init - Specify a default (initial) value for the command line argument, if
 
270
// the default constructor for the argument type does not give you what you
 
271
// want.  This is only valid on "opt" arguments, not on "list" arguments.
 
272
//
 
273
template<class Ty>
 
274
struct initializer {
 
275
  const Ty &Init;
 
276
  initializer(const Ty &Val) : Init(Val) {}
 
277
 
 
278
  template<class Opt>
 
279
  void apply(Opt &O) const { O.setInitialValue(Init); }
 
280
};
 
281
 
 
282
template<class Ty>
 
283
initializer<Ty> init(const Ty &Val) {
 
284
  return initializer<Ty>(Val);
 
285
}
 
286
 
 
287
 
 
288
// location - Allow the user to specify which external variable they want to
 
289
// store the results of the command line argument processing into, if they don't
 
290
// want to store it in the option itself.
 
291
//
 
292
template<class Ty>
 
293
struct LocationClass {
 
294
  Ty &Loc;
 
295
  LocationClass(Ty &L) : Loc(L) {}
 
296
 
 
297
  template<class Opt>
 
298
  void apply(Opt &O) const { O.setLocation(O, Loc); }
 
299
};
 
300
 
 
301
template<class Ty>
 
302
LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
 
303
 
 
304
 
 
305
//===----------------------------------------------------------------------===//
 
306
// Enum valued command line option
 
307
//
 
308
#define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
 
309
#define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
 
310
#define clEnumValEnd (reinterpret_cast<void*>(0))
 
311
 
 
312
// values - For custom data types, allow specifying a group of values together
 
313
// as the values that go into the mapping that the option handler uses.  Note
 
314
// that the values list must always have a 0 at the end of the list to indicate
 
315
// that the list has ended.
 
316
//
 
317
template<class DataType>
 
318
class ValuesClass {
 
319
  // Use a vector instead of a map, because the lists should be short,
 
320
  // the overhead is less, and most importantly, it keeps them in the order
 
321
  // inserted so we can print our option out nicely.
 
322
  SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
 
323
  void processValues(va_list Vals);
 
324
public:
 
325
  ValuesClass(const char *EnumName, DataType Val, const char *Desc,
 
326
              va_list ValueArgs) {
 
327
    // Insert the first value, which is required.
 
328
    Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
 
329
 
 
330
    // Process the varargs portion of the values...
 
331
    while (const char *enumName = va_arg(ValueArgs, const char *)) {
 
332
      DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
 
333
      const char *EnumDesc = va_arg(ValueArgs, const char *);
 
334
      Values.push_back(std::make_pair(enumName,      // Add value to value map
 
335
                                      std::make_pair(EnumVal, EnumDesc)));
 
336
    }
 
337
  }
 
338
 
 
339
  template<class Opt>
 
340
  void apply(Opt &O) const {
 
341
    for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
 
342
         i != e; ++i)
 
343
      O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
 
344
                                     Values[i].second.second);
 
345
  }
 
346
};
 
347
 
 
348
template<class DataType>
 
349
ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
 
350
                                           const char *Desc, ...) {
 
351
    va_list ValueArgs;
 
352
    va_start(ValueArgs, Desc);
 
353
    ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
 
354
    va_end(ValueArgs);
 
355
    return Vals;
 
356
}
 
357
 
 
358
 
 
359
//===----------------------------------------------------------------------===//
 
360
// parser class - Parameterizable parser for different data types.  By default,
 
361
// known data types (string, int, bool) have specialized parsers, that do what
 
362
// you would expect.  The default parser, used for data types that are not
 
363
// built-in, uses a mapping table to map specific options to values, which is
 
364
// used, among other things, to handle enum types.
 
365
 
 
366
//--------------------------------------------------
 
367
// generic_parser_base - This class holds all the non-generic code that we do
 
368
// not need replicated for every instance of the generic parser.  This also
 
369
// allows us to put stuff into CommandLine.cpp
 
370
//
 
371
struct generic_parser_base {
 
372
  virtual ~generic_parser_base() {}  // Base class should have virtual-dtor
 
373
 
 
374
  // getNumOptions - Virtual function implemented by generic subclass to
 
375
  // indicate how many entries are in Values.
 
376
  //
 
377
  virtual unsigned getNumOptions() const = 0;
 
378
 
 
379
  // getOption - Return option name N.
 
380
  virtual const char *getOption(unsigned N) const = 0;
 
381
 
 
382
  // getDescription - Return description N
 
383
  virtual const char *getDescription(unsigned N) const = 0;
 
384
 
 
385
  // Return the width of the option tag for printing...
 
386
  virtual size_t getOptionWidth(const Option &O) const;
 
387
 
 
388
  // printOptionInfo - Print out information about this option.  The
 
389
  // to-be-maintained width is specified.
 
390
  //
 
391
  virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
 
392
 
 
393
  void initialize(Option &O) {
 
394
    // All of the modifiers for the option have been processed by now, so the
 
395
    // argstr field should be stable, copy it down now.
 
396
    //
 
397
    hasArgStr = O.hasArgStr();
 
398
  }
 
399
 
 
400
  void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
 
401
    // If there has been no argstr specified, that means that we need to add an
 
402
    // argument for every possible option.  This ensures that our options are
 
403
    // vectored to us.
 
404
    if (!hasArgStr)
 
405
      for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
 
406
        OptionNames.push_back(getOption(i));
 
407
  }
 
408
 
 
409
 
 
410
  enum ValueExpected getValueExpectedFlagDefault() const {
 
411
    // If there is an ArgStr specified, then we are of the form:
 
412
    //
 
413
    //    -opt=O2   or   -opt O2  or  -optO2
 
414
    //
 
415
    // In which case, the value is required.  Otherwise if an arg str has not
 
416
    // been specified, we are of the form:
 
417
    //
 
418
    //    -O2 or O2 or -la (where -l and -a are separate options)
 
419
    //
 
420
    // If this is the case, we cannot allow a value.
 
421
    //
 
422
    if (hasArgStr)
 
423
      return ValueRequired;
 
424
    else
 
425
      return ValueDisallowed;
 
426
  }
 
427
 
 
428
  // findOption - Return the option number corresponding to the specified
 
429
  // argument string.  If the option is not found, getNumOptions() is returned.
 
430
  //
 
431
  unsigned findOption(const char *Name);
 
432
 
 
433
protected:
 
434
  bool hasArgStr;
 
435
};
 
436
 
 
437
// Default parser implementation - This implementation depends on having a
 
438
// mapping of recognized options to values of some sort.  In addition to this,
 
439
// each entry in the mapping also tracks a help message that is printed with the
 
440
// command line option for -help.  Because this is a simple mapping parser, the
 
441
// data type can be any unsupported type.
 
442
//
 
443
template <class DataType>
 
444
class parser : public generic_parser_base {
 
445
protected:
 
446
  class OptionInfo {
 
447
  public:
 
448
    OptionInfo(const char *name, DataType v, const char *helpStr) :
 
449
      Name(name), V(v), HelpStr(helpStr) {}
 
450
    const char *Name;
 
451
    DataType V;
 
452
    const char *HelpStr;
 
453
  };
 
454
  SmallVector<OptionInfo, 8> Values;
 
455
public:
 
456
  typedef DataType parser_data_type;
 
457
 
 
458
  // Implement virtual functions needed by generic_parser_base
 
459
  unsigned getNumOptions() const { return unsigned(Values.size()); }
 
460
  const char *getOption(unsigned N) const { return Values[N].Name; }
 
461
  const char *getDescription(unsigned N) const {
 
462
    return Values[N].HelpStr;
 
463
  }
 
464
 
 
465
  // parse - Return true on error.
 
466
  bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
 
467
    StringRef ArgVal;
 
468
    if (hasArgStr)
 
469
      ArgVal = Arg;
 
470
    else
 
471
      ArgVal = ArgName;
 
472
 
 
473
    for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
 
474
         i != e; ++i)
 
475
      if (Values[i].Name == ArgVal) {
 
476
        V = Values[i].V;
 
477
        return false;
 
478
      }
 
479
 
 
480
    return O.error("Cannot find option named '" + ArgVal + "'!");
 
481
  }
 
482
 
 
483
  /// addLiteralOption - Add an entry to the mapping table.
 
484
  ///
 
485
  template <class DT>
 
486
  void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
 
487
    assert(findOption(Name) == Values.size() && "Option already exists!");
 
488
    OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
 
489
    Values.push_back(X);
 
490
    MarkOptionsChanged();
 
491
  }
 
492
 
 
493
  /// removeLiteralOption - Remove the specified option.
 
494
  ///
 
495
  void removeLiteralOption(const char *Name) {
 
496
    unsigned N = findOption(Name);
 
497
    assert(N != Values.size() && "Option not found!");
 
498
    Values.erase(Values.begin()+N);
 
499
  }
 
500
};
 
501
 
 
502
//--------------------------------------------------
 
503
// basic_parser - Super class of parsers to provide boilerplate code
 
504
//
 
505
class basic_parser_impl {  // non-template implementation of basic_parser<t>
 
506
public:
 
507
  virtual ~basic_parser_impl() {}
 
508
 
 
509
  enum ValueExpected getValueExpectedFlagDefault() const {
 
510
    return ValueRequired;
 
511
  }
 
512
 
 
513
  void getExtraOptionNames(SmallVectorImpl<const char*> &) {}
 
514
 
 
515
  void initialize(Option &) {}
 
516
 
 
517
  // Return the width of the option tag for printing...
 
518
  size_t getOptionWidth(const Option &O) const;
 
519
 
 
520
  // printOptionInfo - Print out information about this option.  The
 
521
  // to-be-maintained width is specified.
 
522
  //
 
523
  void printOptionInfo(const Option &O, size_t GlobalWidth) const;
 
524
 
 
525
  // getValueName - Overload in subclass to provide a better default value.
 
526
  virtual const char *getValueName() const { return "value"; }
 
527
 
 
528
  // An out-of-line virtual method to provide a 'home' for this class.
 
529
  virtual void anchor();
 
530
};
 
531
 
 
532
// basic_parser - The real basic parser is just a template wrapper that provides
 
533
// a typedef for the provided data type.
 
534
//
 
535
template<class DataType>
 
536
class basic_parser : public basic_parser_impl {
 
537
public:
 
538
  typedef DataType parser_data_type;
 
539
};
 
540
 
 
541
//--------------------------------------------------
 
542
// parser<bool>
 
543
//
 
544
template<>
 
545
class parser<bool> : public basic_parser<bool> {
 
546
  const char *ArgStr;
 
547
public:
 
548
 
 
549
  // parse - Return true on error.
 
550
  bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
 
551
 
 
552
  template <class Opt>
 
553
  void initialize(Opt &O) {
 
554
    ArgStr = O.ArgStr;
 
555
  }
 
556
 
 
557
  enum ValueExpected getValueExpectedFlagDefault() const {
 
558
    return ValueOptional;
 
559
  }
 
560
 
 
561
  // getValueName - Do not print =<value> at all.
 
562
  virtual const char *getValueName() const { return 0; }
 
563
 
 
564
  // An out-of-line virtual method to provide a 'home' for this class.
 
565
  virtual void anchor();
 
566
};
 
567
 
 
568
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
 
569
 
 
570
//--------------------------------------------------
 
571
// parser<boolOrDefault>
 
572
enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
 
573
template<>
 
574
class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
 
575
public:
 
576
  // parse - Return true on error.
 
577
  bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
 
578
 
 
579
  enum ValueExpected getValueExpectedFlagDefault() const {
 
580
    return ValueOptional;
 
581
  }
 
582
 
 
583
  // getValueName - Do not print =<value> at all.
 
584
  virtual const char *getValueName() const { return 0; }
 
585
 
 
586
  // An out-of-line virtual method to provide a 'home' for this class.
 
587
  virtual void anchor();
 
588
};
 
589
 
 
590
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
 
591
 
 
592
//--------------------------------------------------
 
593
// parser<int>
 
594
//
 
595
template<>
 
596
class parser<int> : public basic_parser<int> {
 
597
public:
 
598
  // parse - Return true on error.
 
599
  bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
 
600
 
 
601
  // getValueName - Overload in subclass to provide a better default value.
 
602
  virtual const char *getValueName() const { return "int"; }
 
603
 
 
604
  // An out-of-line virtual method to provide a 'home' for this class.
 
605
  virtual void anchor();
 
606
};
 
607
 
 
608
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
 
609
 
 
610
 
 
611
//--------------------------------------------------
 
612
// parser<unsigned>
 
613
//
 
614
template<>
 
615
class parser<unsigned> : public basic_parser<unsigned> {
 
616
public:
 
617
  // parse - Return true on error.
 
618
  bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
 
619
 
 
620
  // getValueName - Overload in subclass to provide a better default value.
 
621
  virtual const char *getValueName() const { return "uint"; }
 
622
 
 
623
  // An out-of-line virtual method to provide a 'home' for this class.
 
624
  virtual void anchor();
 
625
};
 
626
 
 
627
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
 
628
 
 
629
//--------------------------------------------------
 
630
// parser<double>
 
631
//
 
632
template<>
 
633
class parser<double> : public basic_parser<double> {
 
634
public:
 
635
  // parse - Return true on error.
 
636
  bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
 
637
 
 
638
  // getValueName - Overload in subclass to provide a better default value.
 
639
  virtual const char *getValueName() const { return "number"; }
 
640
 
 
641
  // An out-of-line virtual method to provide a 'home' for this class.
 
642
  virtual void anchor();
 
643
};
 
644
 
 
645
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
 
646
 
 
647
//--------------------------------------------------
 
648
// parser<float>
 
649
//
 
650
template<>
 
651
class parser<float> : public basic_parser<float> {
 
652
public:
 
653
  // parse - Return true on error.
 
654
  bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
 
655
 
 
656
  // getValueName - Overload in subclass to provide a better default value.
 
657
  virtual const char *getValueName() const { return "number"; }
 
658
 
 
659
  // An out-of-line virtual method to provide a 'home' for this class.
 
660
  virtual void anchor();
 
661
};
 
662
 
 
663
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
 
664
 
 
665
//--------------------------------------------------
 
666
// parser<std::string>
 
667
//
 
668
template<>
 
669
class parser<std::string> : public basic_parser<std::string> {
 
670
public:
 
671
  // parse - Return true on error.
 
672
  bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
 
673
    Value = Arg.str();
 
674
    return false;
 
675
  }
 
676
 
 
677
  // getValueName - Overload in subclass to provide a better default value.
 
678
  virtual const char *getValueName() const { return "string"; }
 
679
 
 
680
  // An out-of-line virtual method to provide a 'home' for this class.
 
681
  virtual void anchor();
 
682
};
 
683
 
 
684
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
 
685
 
 
686
//--------------------------------------------------
 
687
// parser<char>
 
688
//
 
689
template<>
 
690
class parser<char> : public basic_parser<char> {
 
691
public:
 
692
  // parse - Return true on error.
 
693
  bool parse(Option &, StringRef, StringRef Arg, char &Value) {
 
694
    Value = Arg[0];
 
695
    return false;
 
696
  }
 
697
 
 
698
  // getValueName - Overload in subclass to provide a better default value.
 
699
  virtual const char *getValueName() const { return "char"; }
 
700
 
 
701
  // An out-of-line virtual method to provide a 'home' for this class.
 
702
  virtual void anchor();
 
703
};
 
704
 
 
705
EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<char>);
 
706
 
 
707
//===----------------------------------------------------------------------===//
 
708
// applicator class - This class is used because we must use partial
 
709
// specialization to handle literal string arguments specially (const char* does
 
710
// not correctly respond to the apply method).  Because the syntax to use this
 
711
// is a pain, we have the 'apply' method below to handle the nastiness...
 
712
//
 
713
template<class Mod> struct applicator {
 
714
  template<class Opt>
 
715
  static void opt(const Mod &M, Opt &O) { M.apply(O); }
 
716
};
 
717
 
 
718
// Handle const char* as a special case...
 
719
template<unsigned n> struct applicator<char[n]> {
 
720
  template<class Opt>
 
721
  static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
 
722
};
 
723
template<unsigned n> struct applicator<const char[n]> {
 
724
  template<class Opt>
 
725
  static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
 
726
};
 
727
template<> struct applicator<const char*> {
 
728
  template<class Opt>
 
729
  static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
 
730
};
 
731
 
 
732
template<> struct applicator<NumOccurrencesFlag> {
 
733
  static void opt(NumOccurrencesFlag NO, Option &O) {
 
734
    O.setNumOccurrencesFlag(NO);
 
735
  }
 
736
};
 
737
template<> struct applicator<ValueExpected> {
 
738
  static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
 
739
};
 
740
template<> struct applicator<OptionHidden> {
 
741
  static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
 
742
};
 
743
template<> struct applicator<FormattingFlags> {
 
744
  static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
 
745
};
 
746
template<> struct applicator<MiscFlags> {
 
747
  static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
 
748
};
 
749
 
 
750
// apply method - Apply a modifier to an option in a type safe way.
 
751
template<class Mod, class Opt>
 
752
void apply(const Mod &M, Opt *O) {
 
753
  applicator<Mod>::opt(M, *O);
 
754
}
 
755
 
 
756
 
 
757
//===----------------------------------------------------------------------===//
 
758
// opt_storage class
 
759
 
 
760
// Default storage class definition: external storage.  This implementation
 
761
// assumes the user will specify a variable to store the data into with the
 
762
// cl::location(x) modifier.
 
763
//
 
764
template<class DataType, bool ExternalStorage, bool isClass>
 
765
class opt_storage {
 
766
  DataType *Location;   // Where to store the object...
 
767
 
 
768
  void check() const {
 
769
    assert(Location != 0 && "cl::location(...) not specified for a command "
 
770
           "line option with external storage, "
 
771
           "or cl::init specified before cl::location()!!");
 
772
  }
 
773
public:
 
774
  opt_storage() : Location(0) {}
 
775
 
 
776
  bool setLocation(Option &O, DataType &L) {
 
777
    if (Location)
 
778
      return O.error("cl::location(x) specified more than once!");
 
779
    Location = &L;
 
780
    return false;
 
781
  }
 
782
 
 
783
  template<class T>
 
784
  void setValue(const T &V) {
 
785
    check();
 
786
    *Location = V;
 
787
  }
 
788
 
 
789
  DataType &getValue() { check(); return *Location; }
 
790
  const DataType &getValue() const { check(); return *Location; }
 
791
 
 
792
  operator DataType() const { return this->getValue(); }
 
793
};
 
794
 
 
795
 
 
796
// Define how to hold a class type object, such as a string.  Since we can
 
797
// inherit from a class, we do so.  This makes us exactly compatible with the
 
798
// object in all cases that it is used.
 
799
//
 
800
template<class DataType>
 
801
class opt_storage<DataType,false,true> : public DataType {
 
802
public:
 
803
  template<class T>
 
804
  void setValue(const T &V) { DataType::operator=(V); }
 
805
 
 
806
  DataType &getValue() { return *this; }
 
807
  const DataType &getValue() const { return *this; }
 
808
};
 
809
 
 
810
// Define a partial specialization to handle things we cannot inherit from.  In
 
811
// this case, we store an instance through containment, and overload operators
 
812
// to get at the value.
 
813
//
 
814
template<class DataType>
 
815
class opt_storage<DataType, false, false> {
 
816
public:
 
817
  DataType Value;
 
818
 
 
819
  // Make sure we initialize the value with the default constructor for the
 
820
  // type.
 
821
  opt_storage() : Value(DataType()) {}
 
822
 
 
823
  template<class T>
 
824
  void setValue(const T &V) { Value = V; }
 
825
  DataType &getValue() { return Value; }
 
826
  DataType getValue() const { return Value; }
 
827
 
 
828
  operator DataType() const { return getValue(); }
 
829
 
 
830
  // If the datatype is a pointer, support -> on it.
 
831
  DataType operator->() const { return Value; }
 
832
};
 
833
 
 
834
 
 
835
//===----------------------------------------------------------------------===//
 
836
// opt - A scalar command line option.
 
837
//
 
838
template <class DataType, bool ExternalStorage = false,
 
839
          class ParserClass = parser<DataType> >
 
840
class opt : public Option,
 
841
            public opt_storage<DataType, ExternalStorage,
 
842
                               is_class<DataType>::value> {
 
843
  ParserClass Parser;
 
844
 
 
845
  virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
 
846
                                StringRef Arg) {
 
847
    typename ParserClass::parser_data_type Val =
 
848
       typename ParserClass::parser_data_type();
 
849
    if (Parser.parse(*this, ArgName, Arg, Val))
 
850
      return true;                            // Parse error!
 
851
    this->setValue(Val);
 
852
    this->setPosition(pos);
 
853
    return false;
 
854
  }
 
855
 
 
856
  virtual enum ValueExpected getValueExpectedFlagDefault() const {
 
857
    return Parser.getValueExpectedFlagDefault();
 
858
  }
 
859
  virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
 
860
    return Parser.getExtraOptionNames(OptionNames);
 
861
  }
 
862
 
 
863
  // Forward printing stuff to the parser...
 
864
  virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
 
865
  virtual void printOptionInfo(size_t GlobalWidth) const {
 
866
    Parser.printOptionInfo(*this, GlobalWidth);
 
867
  }
 
868
 
 
869
  void done() {
 
870
    addArgument();
 
871
    Parser.initialize(*this);
 
872
  }
 
873
public:
 
874
  // setInitialValue - Used by the cl::init modifier...
 
875
  void setInitialValue(const DataType &V) { this->setValue(V); }
 
876
 
 
877
  ParserClass &getParser() { return Parser; }
 
878
 
 
879
  template<class T>
 
880
  DataType &operator=(const T &Val) {
 
881
    this->setValue(Val);
 
882
    return this->getValue();
 
883
  }
 
884
 
 
885
  // One option...
 
886
  template<class M0t>
 
887
  explicit opt(const M0t &M0) : Option(Optional | NotHidden) {
 
888
    apply(M0, this);
 
889
    done();
 
890
  }
 
891
 
 
892
  // Two options...
 
893
  template<class M0t, class M1t>
 
894
  opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
 
895
    apply(M0, this); apply(M1, this);
 
896
    done();
 
897
  }
 
898
 
 
899
  // Three options...
 
900
  template<class M0t, class M1t, class M2t>
 
901
  opt(const M0t &M0, const M1t &M1,
 
902
      const M2t &M2) : Option(Optional | NotHidden) {
 
903
    apply(M0, this); apply(M1, this); apply(M2, this);
 
904
    done();
 
905
  }
 
906
  // Four options...
 
907
  template<class M0t, class M1t, class M2t, class M3t>
 
908
  opt(const M0t &M0, const M1t &M1, const M2t &M2,
 
909
      const M3t &M3) : Option(Optional | NotHidden) {
 
910
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
911
    done();
 
912
  }
 
913
  // Five options...
 
914
  template<class M0t, class M1t, class M2t, class M3t, class M4t>
 
915
  opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
916
      const M4t &M4) : Option(Optional | NotHidden) {
 
917
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
918
    apply(M4, this);
 
919
    done();
 
920
  }
 
921
  // Six options...
 
922
  template<class M0t, class M1t, class M2t, class M3t,
 
923
           class M4t, class M5t>
 
924
  opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
925
      const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
 
926
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
927
    apply(M4, this); apply(M5, this);
 
928
    done();
 
929
  }
 
930
  // Seven options...
 
931
  template<class M0t, class M1t, class M2t, class M3t,
 
932
           class M4t, class M5t, class M6t>
 
933
  opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
934
      const M4t &M4, const M5t &M5,
 
935
      const M6t &M6) : Option(Optional | NotHidden) {
 
936
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
937
    apply(M4, this); apply(M5, this); apply(M6, this);
 
938
    done();
 
939
  }
 
940
  // Eight options...
 
941
  template<class M0t, class M1t, class M2t, class M3t,
 
942
           class M4t, class M5t, class M6t, class M7t>
 
943
  opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
944
      const M4t &M4, const M5t &M5, const M6t &M6,
 
945
      const M7t &M7) : Option(Optional | NotHidden) {
 
946
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
947
    apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
 
948
    done();
 
949
  }
 
950
};
 
951
 
 
952
EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
 
953
EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
 
954
EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
 
955
EXTERN_TEMPLATE_INSTANTIATION(class opt<char>);
 
956
EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
 
957
 
 
958
//===----------------------------------------------------------------------===//
 
959
// list_storage class
 
960
 
 
961
// Default storage class definition: external storage.  This implementation
 
962
// assumes the user will specify a variable to store the data into with the
 
963
// cl::location(x) modifier.
 
964
//
 
965
template<class DataType, class StorageClass>
 
966
class list_storage {
 
967
  StorageClass *Location;   // Where to store the object...
 
968
 
 
969
public:
 
970
  list_storage() : Location(0) {}
 
971
 
 
972
  bool setLocation(Option &O, StorageClass &L) {
 
973
    if (Location)
 
974
      return O.error("cl::location(x) specified more than once!");
 
975
    Location = &L;
 
976
    return false;
 
977
  }
 
978
 
 
979
  template<class T>
 
980
  void addValue(const T &V) {
 
981
    assert(Location != 0 && "cl::location(...) not specified for a command "
 
982
           "line option with external storage!");
 
983
    Location->push_back(V);
 
984
  }
 
985
};
 
986
 
 
987
 
 
988
// Define how to hold a class type object, such as a string.  Since we can
 
989
// inherit from a class, we do so.  This makes us exactly compatible with the
 
990
// object in all cases that it is used.
 
991
//
 
992
template<class DataType>
 
993
class list_storage<DataType, bool> : public std::vector<DataType> {
 
994
public:
 
995
  template<class T>
 
996
  void addValue(const T &V) { std::vector<DataType>::push_back(V); }
 
997
};
 
998
 
 
999
 
 
1000
//===----------------------------------------------------------------------===//
 
1001
// list - A list of command line options.
 
1002
//
 
1003
template <class DataType, class Storage = bool,
 
1004
          class ParserClass = parser<DataType> >
 
1005
class list : public Option, public list_storage<DataType, Storage> {
 
1006
  std::vector<unsigned> Positions;
 
1007
  ParserClass Parser;
 
1008
 
 
1009
  virtual enum ValueExpected getValueExpectedFlagDefault() const {
 
1010
    return Parser.getValueExpectedFlagDefault();
 
1011
  }
 
1012
  virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
 
1013
    return Parser.getExtraOptionNames(OptionNames);
 
1014
  }
 
1015
 
 
1016
  virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
 
1017
    typename ParserClass::parser_data_type Val =
 
1018
      typename ParserClass::parser_data_type();
 
1019
    if (Parser.parse(*this, ArgName, Arg, Val))
 
1020
      return true;  // Parse Error!
 
1021
    list_storage<DataType, Storage>::addValue(Val);
 
1022
    setPosition(pos);
 
1023
    Positions.push_back(pos);
 
1024
    return false;
 
1025
  }
 
1026
 
 
1027
  // Forward printing stuff to the parser...
 
1028
  virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
 
1029
  virtual void printOptionInfo(size_t GlobalWidth) const {
 
1030
    Parser.printOptionInfo(*this, GlobalWidth);
 
1031
  }
 
1032
 
 
1033
  void done() {
 
1034
    addArgument();
 
1035
    Parser.initialize(*this);
 
1036
  }
 
1037
public:
 
1038
  ParserClass &getParser() { return Parser; }
 
1039
 
 
1040
  unsigned getPosition(unsigned optnum) const {
 
1041
    assert(optnum < this->size() && "Invalid option index");
 
1042
    return Positions[optnum];
 
1043
  }
 
1044
 
 
1045
  void setNumAdditionalVals(unsigned n) {
 
1046
    Option::setNumAdditionalVals(n);
 
1047
  }
 
1048
 
 
1049
  // One option...
 
1050
  template<class M0t>
 
1051
  explicit list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
 
1052
    apply(M0, this);
 
1053
    done();
 
1054
  }
 
1055
  // Two options...
 
1056
  template<class M0t, class M1t>
 
1057
  list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
 
1058
    apply(M0, this); apply(M1, this);
 
1059
    done();
 
1060
  }
 
1061
  // Three options...
 
1062
  template<class M0t, class M1t, class M2t>
 
1063
  list(const M0t &M0, const M1t &M1, const M2t &M2)
 
1064
    : Option(ZeroOrMore | NotHidden) {
 
1065
    apply(M0, this); apply(M1, this); apply(M2, this);
 
1066
    done();
 
1067
  }
 
1068
  // Four options...
 
1069
  template<class M0t, class M1t, class M2t, class M3t>
 
1070
  list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
 
1071
    : Option(ZeroOrMore | NotHidden) {
 
1072
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1073
    done();
 
1074
  }
 
1075
  // Five options...
 
1076
  template<class M0t, class M1t, class M2t, class M3t, class M4t>
 
1077
  list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1078
       const M4t &M4) : Option(ZeroOrMore | NotHidden) {
 
1079
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1080
    apply(M4, this);
 
1081
    done();
 
1082
  }
 
1083
  // Six options...
 
1084
  template<class M0t, class M1t, class M2t, class M3t,
 
1085
           class M4t, class M5t>
 
1086
  list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1087
       const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
 
1088
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1089
    apply(M4, this); apply(M5, this);
 
1090
    done();
 
1091
  }
 
1092
  // Seven options...
 
1093
  template<class M0t, class M1t, class M2t, class M3t,
 
1094
           class M4t, class M5t, class M6t>
 
1095
  list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1096
       const M4t &M4, const M5t &M5, const M6t &M6)
 
1097
    : Option(ZeroOrMore | NotHidden) {
 
1098
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1099
    apply(M4, this); apply(M5, this); apply(M6, this);
 
1100
    done();
 
1101
  }
 
1102
  // Eight options...
 
1103
  template<class M0t, class M1t, class M2t, class M3t,
 
1104
           class M4t, class M5t, class M6t, class M7t>
 
1105
  list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1106
       const M4t &M4, const M5t &M5, const M6t &M6,
 
1107
       const M7t &M7) : Option(ZeroOrMore | NotHidden) {
 
1108
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1109
    apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
 
1110
    done();
 
1111
  }
 
1112
};
 
1113
 
 
1114
// multi_val - Modifier to set the number of additional values.
 
1115
struct multi_val {
 
1116
  unsigned AdditionalVals;
 
1117
  explicit multi_val(unsigned N) : AdditionalVals(N) {}
 
1118
 
 
1119
  template <typename D, typename S, typename P>
 
1120
  void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); }
 
1121
};
 
1122
 
 
1123
 
 
1124
//===----------------------------------------------------------------------===//
 
1125
// bits_storage class
 
1126
 
 
1127
// Default storage class definition: external storage.  This implementation
 
1128
// assumes the user will specify a variable to store the data into with the
 
1129
// cl::location(x) modifier.
 
1130
//
 
1131
template<class DataType, class StorageClass>
 
1132
class bits_storage {
 
1133
  unsigned *Location;   // Where to store the bits...
 
1134
 
 
1135
  template<class T>
 
1136
  static unsigned Bit(const T &V) {
 
1137
    unsigned BitPos = reinterpret_cast<unsigned>(V);
 
1138
    assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
 
1139
          "enum exceeds width of bit vector!");
 
1140
    return 1 << BitPos;
 
1141
  }
 
1142
 
 
1143
public:
 
1144
  bits_storage() : Location(0) {}
 
1145
 
 
1146
  bool setLocation(Option &O, unsigned &L) {
 
1147
    if (Location)
 
1148
      return O.error("cl::location(x) specified more than once!");
 
1149
    Location = &L;
 
1150
    return false;
 
1151
  }
 
1152
 
 
1153
  template<class T>
 
1154
  void addValue(const T &V) {
 
1155
    assert(Location != 0 && "cl::location(...) not specified for a command "
 
1156
           "line option with external storage!");
 
1157
    *Location |= Bit(V);
 
1158
  }
 
1159
 
 
1160
  unsigned getBits() { return *Location; }
 
1161
 
 
1162
  template<class T>
 
1163
  bool isSet(const T &V) {
 
1164
    return (*Location & Bit(V)) != 0;
 
1165
  }
 
1166
};
 
1167
 
 
1168
 
 
1169
// Define how to hold bits.  Since we can inherit from a class, we do so.
 
1170
// This makes us exactly compatible with the bits in all cases that it is used.
 
1171
//
 
1172
template<class DataType>
 
1173
class bits_storage<DataType, bool> {
 
1174
  unsigned Bits;   // Where to store the bits...
 
1175
 
 
1176
  template<class T>
 
1177
  static unsigned Bit(const T &V) {
 
1178
    unsigned BitPos = (unsigned)V;
 
1179
    assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
 
1180
          "enum exceeds width of bit vector!");
 
1181
    return 1 << BitPos;
 
1182
  }
 
1183
 
 
1184
public:
 
1185
  template<class T>
 
1186
  void addValue(const T &V) {
 
1187
    Bits |=  Bit(V);
 
1188
  }
 
1189
 
 
1190
  unsigned getBits() { return Bits; }
 
1191
 
 
1192
  template<class T>
 
1193
  bool isSet(const T &V) {
 
1194
    return (Bits & Bit(V)) != 0;
 
1195
  }
 
1196
};
 
1197
 
 
1198
 
 
1199
//===----------------------------------------------------------------------===//
 
1200
// bits - A bit vector of command options.
 
1201
//
 
1202
template <class DataType, class Storage = bool,
 
1203
          class ParserClass = parser<DataType> >
 
1204
class bits : public Option, public bits_storage<DataType, Storage> {
 
1205
  std::vector<unsigned> Positions;
 
1206
  ParserClass Parser;
 
1207
 
 
1208
  virtual enum ValueExpected getValueExpectedFlagDefault() const {
 
1209
    return Parser.getValueExpectedFlagDefault();
 
1210
  }
 
1211
  virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
 
1212
    return Parser.getExtraOptionNames(OptionNames);
 
1213
  }
 
1214
 
 
1215
  virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
 
1216
    typename ParserClass::parser_data_type Val =
 
1217
      typename ParserClass::parser_data_type();
 
1218
    if (Parser.parse(*this, ArgName, Arg, Val))
 
1219
      return true;  // Parse Error!
 
1220
    addValue(Val);
 
1221
    setPosition(pos);
 
1222
    Positions.push_back(pos);
 
1223
    return false;
 
1224
  }
 
1225
 
 
1226
  // Forward printing stuff to the parser...
 
1227
  virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
 
1228
  virtual void printOptionInfo(size_t GlobalWidth) const {
 
1229
    Parser.printOptionInfo(*this, GlobalWidth);
 
1230
  }
 
1231
 
 
1232
  void done() {
 
1233
    addArgument();
 
1234
    Parser.initialize(*this);
 
1235
  }
 
1236
public:
 
1237
  ParserClass &getParser() { return Parser; }
 
1238
 
 
1239
  unsigned getPosition(unsigned optnum) const {
 
1240
    assert(optnum < this->size() && "Invalid option index");
 
1241
    return Positions[optnum];
 
1242
  }
 
1243
 
 
1244
  // One option...
 
1245
  template<class M0t>
 
1246
  explicit bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
 
1247
    apply(M0, this);
 
1248
    done();
 
1249
  }
 
1250
  // Two options...
 
1251
  template<class M0t, class M1t>
 
1252
  bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
 
1253
    apply(M0, this); apply(M1, this);
 
1254
    done();
 
1255
  }
 
1256
  // Three options...
 
1257
  template<class M0t, class M1t, class M2t>
 
1258
  bits(const M0t &M0, const M1t &M1, const M2t &M2)
 
1259
    : Option(ZeroOrMore | NotHidden) {
 
1260
    apply(M0, this); apply(M1, this); apply(M2, this);
 
1261
    done();
 
1262
  }
 
1263
  // Four options...
 
1264
  template<class M0t, class M1t, class M2t, class M3t>
 
1265
  bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
 
1266
    : Option(ZeroOrMore | NotHidden) {
 
1267
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1268
    done();
 
1269
  }
 
1270
  // Five options...
 
1271
  template<class M0t, class M1t, class M2t, class M3t, class M4t>
 
1272
  bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1273
       const M4t &M4) : Option(ZeroOrMore | NotHidden) {
 
1274
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1275
    apply(M4, this);
 
1276
    done();
 
1277
  }
 
1278
  // Six options...
 
1279
  template<class M0t, class M1t, class M2t, class M3t,
 
1280
           class M4t, class M5t>
 
1281
  bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1282
       const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
 
1283
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1284
    apply(M4, this); apply(M5, this);
 
1285
    done();
 
1286
  }
 
1287
  // Seven options...
 
1288
  template<class M0t, class M1t, class M2t, class M3t,
 
1289
           class M4t, class M5t, class M6t>
 
1290
  bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1291
       const M4t &M4, const M5t &M5, const M6t &M6)
 
1292
    : Option(ZeroOrMore | NotHidden) {
 
1293
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1294
    apply(M4, this); apply(M5, this); apply(M6, this);
 
1295
    done();
 
1296
  }
 
1297
  // Eight options...
 
1298
  template<class M0t, class M1t, class M2t, class M3t,
 
1299
           class M4t, class M5t, class M6t, class M7t>
 
1300
  bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
 
1301
       const M4t &M4, const M5t &M5, const M6t &M6,
 
1302
       const M7t &M7) : Option(ZeroOrMore | NotHidden) {
 
1303
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1304
    apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
 
1305
    done();
 
1306
  }
 
1307
};
 
1308
 
 
1309
//===----------------------------------------------------------------------===//
 
1310
// Aliased command line option (alias this name to a preexisting name)
 
1311
//
 
1312
 
 
1313
class alias : public Option {
 
1314
  Option *AliasFor;
 
1315
  virtual bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
 
1316
                                StringRef Arg) {
 
1317
    return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
 
1318
  }
 
1319
  // Handle printing stuff...
 
1320
  virtual size_t getOptionWidth() const;
 
1321
  virtual void printOptionInfo(size_t GlobalWidth) const;
 
1322
 
 
1323
  void done() {
 
1324
    if (!hasArgStr())
 
1325
      error("cl::alias must have argument name specified!");
 
1326
    if (AliasFor == 0)
 
1327
      error("cl::alias must have an cl::aliasopt(option) specified!");
 
1328
      addArgument();
 
1329
  }
 
1330
public:
 
1331
  void setAliasFor(Option &O) {
 
1332
    if (AliasFor)
 
1333
      error("cl::alias must only have one cl::aliasopt(...) specified!");
 
1334
    AliasFor = &O;
 
1335
  }
 
1336
 
 
1337
  // One option...
 
1338
  template<class M0t>
 
1339
  explicit alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
 
1340
    apply(M0, this);
 
1341
    done();
 
1342
  }
 
1343
  // Two options...
 
1344
  template<class M0t, class M1t>
 
1345
  alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
 
1346
    apply(M0, this); apply(M1, this);
 
1347
    done();
 
1348
  }
 
1349
  // Three options...
 
1350
  template<class M0t, class M1t, class M2t>
 
1351
  alias(const M0t &M0, const M1t &M1, const M2t &M2)
 
1352
    : Option(Optional | Hidden), AliasFor(0) {
 
1353
    apply(M0, this); apply(M1, this); apply(M2, this);
 
1354
    done();
 
1355
  }
 
1356
  // Four options...
 
1357
  template<class M0t, class M1t, class M2t, class M3t>
 
1358
  alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
 
1359
    : Option(Optional | Hidden), AliasFor(0) {
 
1360
    apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
 
1361
    done();
 
1362
  }
 
1363
};
 
1364
 
 
1365
// aliasfor - Modifier to set the option an alias aliases.
 
1366
struct aliasopt {
 
1367
  Option &Opt;
 
1368
  explicit aliasopt(Option &O) : Opt(O) {}
 
1369
  void apply(alias &A) const { A.setAliasFor(Opt); }
 
1370
};
 
1371
 
 
1372
// extrahelp - provide additional help at the end of the normal help
 
1373
// output. All occurrences of cl::extrahelp will be accumulated and
 
1374
// printed to stderr at the end of the regular help, just before
 
1375
// exit is called.
 
1376
struct extrahelp {
 
1377
  const char * morehelp;
 
1378
  explicit extrahelp(const char* help);
 
1379
};
 
1380
 
 
1381
void PrintVersionMessage();
 
1382
// This function just prints the help message, exactly the same way as if the
 
1383
// -help option had been given on the command line.
 
1384
// NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
 
1385
void PrintHelpMessage();
 
1386
 
 
1387
} // End namespace cl
 
1388
 
 
1389
} // End namespace llvm
 
1390
 
 
1391
#endif