From 7c8d653a2cea0eb38340e71e70009d74816aeb64 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Thu, 12 Mar 2009 18:20:18 +0000 Subject: [PATCH] Driver: Add ArgList support for synthesizing arguments. llvm-svn: 66805 --- clang/include/clang/Driver/ArgList.h | 32 +++++++++++++++++++++++++ clang/lib/Driver/ArgList.cpp | 36 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/clang/include/clang/Driver/ArgList.h b/clang/include/clang/Driver/ArgList.h index 2e47ee4c3948..bd2586ecb6ae 100644 --- a/clang/include/clang/Driver/ArgList.h +++ b/clang/include/clang/Driver/ArgList.h @@ -15,6 +15,8 @@ #include "clang/Driver/Util.h" #include "llvm/ADT/SmallVector.h" +#include + namespace clang { namespace driver { class Arg; @@ -38,6 +40,9 @@ namespace driver { /// The full list of arguments. arglist_type Args; + /// Strings for synthesized arguments. + std::list SynthesizedStrings; + public: ArgList(const char **ArgBegin, const char **ArgEnd); ArgList(const ArgList &); @@ -62,6 +67,33 @@ namespace driver { /// getLastArg - Return the last argument matching \arg Id, or null. Arg *getLastArg(options::ID Id) const; + + /// @name Arg Synthesis + /// @{ + + private: + /// MakeIndex - Get an index for the given string(s). + unsigned MakeIndex(const char *String0); + unsigned MakeIndex(const char *String0, const char *String1); + + public: + /// MakeFlagArg - Construct a new FlagArg for the given option + /// \arg Id. + Arg *MakeFlagArg(const Option *Opt); + + /// MakePositionalArg - Construct a new Positional arg for the + /// given option \arg Id, with the provided \arg Value. + Arg *MakePositionalArg(const Option *Opt, const char *Value); + + /// MakeSeparateArg - Construct a new Positional arg for the + /// given option \arg Id, with the provided \arg Value. + Arg *MakeSeparateArg(const Option *Opt, const char *Value); + + /// MakeJoinedArg - Construct a new Positional arg for the + /// given option \arg Id, with the provided \arg Value. + Arg *MakeJoinedArg(const Option *Opt, const char *Value); + + /// @} }; } // end namespace driver } // end namespace clang diff --git a/clang/lib/Driver/ArgList.cpp b/clang/lib/Driver/ArgList.cpp index e29977fb0e9f..87847c0c1327 100644 --- a/clang/lib/Driver/ArgList.cpp +++ b/clang/lib/Driver/ArgList.cpp @@ -40,3 +40,39 @@ Arg *ArgList::getLastArg(options::ID Id) const { return 0; } + +unsigned ArgList::MakeIndex(const char *String0) { + unsigned Index = ArgStrings.size(); + + // Tuck away so we have a reliable const char *. + SynthesizedStrings.push_back(String0); + ArgStrings.push_back(SynthesizedStrings.back().c_str()); + + return Index; +} + +unsigned ArgList::MakeIndex(const char *String0, const char *String1) { + unsigned Index0 = MakeIndex(String0); + unsigned Index1 = MakeIndex(String1); + assert(Index0 == Index1 && "Unexpected non-consecutive indices!"); + (void) Index1; + return Index0; +} + +Arg *ArgList::MakeFlagArg(const Option *Opt) { + return new FlagArg(Opt, MakeIndex(Opt->getName())); +} + +Arg *ArgList::MakePositionalArg(const Option *Opt, const char *Value) { + return new PositionalArg(Opt, MakeIndex(Value)); +} + +Arg *ArgList::MakeSeparateArg(const Option *Opt, const char *Value) { + return new SeparateArg(Opt, MakeIndex(Opt->getName(), Value), 1); +} + +Arg *ArgList::MakeJoinedArg(const Option *Opt, const char *Value) { + std::string Joined(Opt->getName()); + Joined += Value; + return new JoinedArg(Opt, MakeIndex(Joined.c_str())); +}