Skip to content

Commit

Permalink
[Options] make Option a value type.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165663 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Bigcheese committed Oct 10, 2012
1 parent cf9030e commit 0464fd5
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 96 deletions.
13 changes: 7 additions & 6 deletions include/clang/Driver/Arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef CLANG_DRIVER_ARG_H_
#define CLANG_DRIVER_ARG_H_

#include "clang/Driver/Option.h"

#include "Util.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
Expand All @@ -23,7 +25,6 @@
namespace clang {
namespace driver {
class ArgList;
class Option;

/// \brief A concrete instance of a particular driver option.
///
Expand All @@ -38,7 +39,7 @@ namespace driver {

private:
/// \brief The option this argument is an instance of.
const Option *Opt;
const Option Opt;

/// \brief The argument this argument was derived from (during tool chain
/// argument translation), if any.
Expand All @@ -60,14 +61,14 @@ namespace driver {
SmallVector<const char *, 2> Values;

public:
Arg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);
Arg(const Option *Opt, unsigned Index,
Arg(const Option Opt, unsigned Index, const Arg *BaseArg = 0);
Arg(const Option Opt, unsigned Index,
const char *Value0, const Arg *BaseArg = 0);
Arg(const Option *Opt, unsigned Index,
Arg(const Option Opt, unsigned Index,
const char *Value0, const char *Value1, const Arg *BaseArg = 0);
~Arg();

const Option &getOption() const { return *Opt; }
const Option getOption() const { return Opt; }
unsigned getIndex() const { return Index; }

/// \brief Return the base argument which generated this arg.
Expand Down
17 changes: 9 additions & 8 deletions include/clang/Driver/ArgList.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define CLANG_DRIVER_ARGLIST_H_

#include "clang/Basic/LLVM.h"
#include "clang/Driver/Option.h"
#include "clang/Driver/OptSpecifier.h"
#include "clang/Driver/Util.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -374,14 +375,14 @@ namespace driver {

/// AddFlagArg - Construct a new FlagArg for the given option \p Id and
/// append it to the argument list.
void AddFlagArg(const Arg *BaseArg, const Option *Opt) {
void AddFlagArg(const Arg *BaseArg, const Option Opt) {
append(MakeFlagArg(BaseArg, Opt));
}

/// AddPositionalArg - Construct a new Positional arg for the given option
/// \p Id, with the provided \p Value and append it to the argument
/// list.
void AddPositionalArg(const Arg *BaseArg, const Option *Opt,
void AddPositionalArg(const Arg *BaseArg, const Option Opt,
StringRef Value) {
append(MakePositionalArg(BaseArg, Opt, Value));
}
Expand All @@ -390,36 +391,36 @@ namespace driver {
/// AddSeparateArg - Construct a new Positional arg for the given option
/// \p Id, with the provided \p Value and append it to the argument
/// list.
void AddSeparateArg(const Arg *BaseArg, const Option *Opt,
void AddSeparateArg(const Arg *BaseArg, const Option Opt,
StringRef Value) {
append(MakeSeparateArg(BaseArg, Opt, Value));
}


/// AddJoinedArg - Construct a new Positional arg for the given option
/// \p Id, with the provided \p Value and append it to the argument list.
void AddJoinedArg(const Arg *BaseArg, const Option *Opt,
void AddJoinedArg(const Arg *BaseArg, const Option Opt,
StringRef Value) {
append(MakeJoinedArg(BaseArg, Opt, Value));
}


/// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
Arg *MakeFlagArg(const Arg *BaseArg, const Option *Opt) const;
Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;

/// MakePositionalArg - Construct a new Positional arg for the
/// given option \p Id, with the provided \p Value.
Arg *MakePositionalArg(const Arg *BaseArg, const Option *Opt,
Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const;

/// MakeSeparateArg - Construct a new Positional arg for the
/// given option \p Id, with the provided \p Value.
Arg *MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const;

/// MakeJoinedArg - Construct a new Positional arg for the
/// given option \p Id, with the provided \p Value.
Arg *MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const;

/// @}
Expand Down
23 changes: 1 addition & 22 deletions include/clang/Driver/OptTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ namespace driver {
const Info *OptionInfos;
unsigned NumOptionInfos;

/// \brief The lazily constructed options table, indexed by option::ID - 1.
mutable Option **Options;

/// \brief Prebound input option instance.
const Option *TheInputOption;

/// \brief Prebound unknown option instance.
const Option *TheUnknownOption;

/// The index of the first option which can be parsed (i.e., is not a
/// special option like 'input' or 'unknown', and is not an option group).
unsigned FirstSearchableIndex;
Expand All @@ -67,8 +58,6 @@ namespace driver {
return OptionInfos[id - 1];
}

Option *CreateOption(unsigned id) const;

protected:
OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos);
public:
Expand All @@ -81,17 +70,7 @@ namespace driver {
/// if necessary.
///
/// \return The option, or null for the INVALID option id.
const Option *getOption(OptSpecifier Opt) const {
unsigned id = Opt.getID();
if (id == 0)
return 0;

assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
Option *&Entry = Options[id - 1];
if (!Entry)
Entry = CreateOption(id);
return Entry;
}
const Option getOption(OptSpecifier Opt) const;

/// \brief Lookup the name of the given option.
const char *getOptionName(OptSpecifier id) const {
Expand Down
18 changes: 11 additions & 7 deletions include/clang/Driver/Option.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ namespace options {
Option(const OptTable::Info *Info, const OptTable *Owner);
~Option();

bool isValid() const {
return Info != 0;
}

unsigned getID() const { return Info->ID; }
OptionClass getKind() const { return OptionClass(Info->Kind); }
StringRef getName() const { return Info->Name; }
const Option *getGroup() const { return Owner->getOption(Info->GroupID); }
const Option *getAlias() const { return Owner->getOption(Info->AliasID); }
const Option getGroup() const { return Owner->getOption(Info->GroupID); }
const Option getAlias() const { return Owner->getOption(Info->AliasID); }

unsigned getNumArgs() const { return Info->Param; }

Expand Down Expand Up @@ -130,16 +134,16 @@ namespace options {

/// getUnaliasedOption - Return the final option this option
/// aliases (itself, if the option has no alias).
const Option *getUnaliasedOption() const {
const Option *Alias = getAlias();
if (Alias) return Alias->getUnaliasedOption();
return this;
const Option getUnaliasedOption() const {
const Option Alias = getAlias();
if (Alias.isValid()) return Alias.getUnaliasedOption();
return *this;
}

/// getRenderName - Return the name to use when rendering this
/// option.
StringRef getRenderName() const {
return getUnaliasedOption()->getName();
return getUnaliasedOption().getName();
}

/// matches - Predicate for whether this option is part of the
Expand Down
10 changes: 5 additions & 5 deletions lib/Driver/Arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

using namespace clang::driver;

Arg::Arg(const Option *_Opt, unsigned _Index, const Arg *_BaseArg)
Arg::Arg(const Option _Opt, unsigned _Index, const Arg *_BaseArg)
: Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
Claimed(false), OwnsValues(false) {
}

Arg::Arg(const Option *_Opt, unsigned _Index,
Arg::Arg(const Option _Opt, unsigned _Index,
const char *Value0, const Arg *_BaseArg)
: Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
Claimed(false), OwnsValues(false) {
Values.push_back(Value0);
}

Arg::Arg(const Option *_Opt, unsigned _Index,
Arg::Arg(const Option _Opt, unsigned _Index,
const char *Value0, const char *Value1, const Arg *_BaseArg)
: Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
Claimed(false), OwnsValues(false) {
Expand All @@ -47,7 +47,7 @@ void Arg::dump() const {
llvm::errs() << "<";

llvm::errs() << " Opt:";
Opt->dump();
Opt.dump();

llvm::errs() << " Index:" << Index;

Expand Down Expand Up @@ -104,7 +104,7 @@ void Arg::render(const ArgList &Args, ArgStringList &Output) const {
Output.push_back(Args.MakeArgString(OS.str()));
break;
}

case Option::RenderJoinedStyle:
Output.push_back(Args.GetOrMakeJoinedArgString(
getIndex(), getOption().getName(), getValue(Args, 0)));
Expand Down
16 changes: 8 additions & 8 deletions lib/Driver/ArgList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,33 +362,33 @@ const char *DerivedArgList::MakeArgString(StringRef Str) const {
return BaseArgs.MakeArgString(Str);
}

Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt.getName()), BaseArg);
SynthesizedArgs.push_back(A);
return A;
}

Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const {
unsigned Index = BaseArgs.MakeIndex(Value);
Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
SynthesizedArgs.push_back(A);
return A;
}

Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const {
unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value);
unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg);
SynthesizedArgs.push_back(A);
return A;
}

Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
StringRef Value) const {
unsigned Index = BaseArgs.MakeIndex(Opt->getName().str() + Value.str());
unsigned Index = BaseArgs.MakeIndex(Opt.getName().str() + Value.str());
Arg *A = new Arg(Opt, Index,
BaseArgs.getArgString(Index) + Opt->getName().size(),
BaseArgs.getArgString(Index) + Opt.getName().size(),
BaseArg);
SynthesizedArgs.push_back(A);
return A;
Expand Down
34 changes: 15 additions & 19 deletions lib/Driver/OptTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "clang/Driver/Arg.h"
#include "clang/Driver/ArgList.h"
#include "clang/Driver/Option.h"
#include "clang/Driver/Options.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ErrorHandling.h"
#include <algorithm>
Expand Down Expand Up @@ -79,22 +80,16 @@ OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}

OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
: OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
Options(new Option*[NumOptionInfos]),
TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
FirstSearchableIndex(0)
{
// Explicitly zero initialize the error to work around a bug in array
// value-initialization on MinGW with gcc 4.3.5.
memset(Options, 0, sizeof(*Options) * NumOptionInfos);

// Find start of normal options.
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
unsigned Kind = getInfo(i + 1).Kind;
if (Kind == Option::InputClass) {
assert(!TheInputOption && "Cannot have multiple input options!");
TheInputOption = getOption(i + 1);
} else if (Kind == Option::UnknownClass) {
assert(!TheUnknownOption && "Cannot have multiple input options!");
TheUnknownOption = getOption(i + 1);
} else if (Kind != Option::GroupClass) {
FirstSearchableIndex = i;
break;
Expand All @@ -115,26 +110,27 @@ OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
// Check that options are in order.
for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
if (!(getInfo(i) < getInfo(i + 1))) {
getOption(i)->dump();
getOption(i + 1)->dump();
getOption(i).dump();
getOption(i + 1).dump();
llvm_unreachable("Options are not in order!");
}
}
#endif
}

OptTable::~OptTable() {
for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
delete Options[i];
delete[] Options;
}

bool OptTable::isOptionHelpHidden(OptSpecifier id) const {
return getInfo(id).Flags & options::HelpHidden;
const Option OptTable::getOption(OptSpecifier Opt) const {
unsigned id = Opt.getID();
if (id == 0)
return Option(0, 0);
assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
return Option(&getInfo(id), this);
}

Option *OptTable::CreateOption(unsigned id) const {
return new Option(&getInfo(id), this);
bool OptTable::isOptionHelpHidden(OptSpecifier id) const {
return getInfo(id).Flags & options::HelpHidden;
}

Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Expand All @@ -143,7 +139,7 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {

// Anything that doesn't start with '-' is an input, as is '-' itself.
if (Str[0] != '-' || Str[1] == '\0')
return new Arg(TheInputOption, Index++, Str);
return new Arg(getOption(options::OPT_INPUT), Index++, Str);

const Info *Start = OptionInfos + FirstSearchableIndex;
const Info *End = OptionInfos + getNumOptions();
Expand All @@ -169,15 +165,15 @@ Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
break;

// See if this option matches.
if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
if (Arg *A = getOption(Start - OptionInfos + 1).accept(Args, Index))
return A;

// Otherwise, see if this argument was missing values.
if (Prev != Index)
return 0;
}

return new Arg(TheUnknownOption, Index++, Str);
return new Arg(getOption(options::OPT_UNKNOWN), Index++, Str);
}

InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
Expand Down
Loading

0 comments on commit 0464fd5

Please sign in to comment.