From e375c4a02ac6e18b166a067b8226e15a1ab009e7 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Wed, 9 Jun 2010 22:31:04 +0000 Subject: [PATCH] Driver: Keep the rendering style in the option, instead of as part of the Arg. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105761 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/Arg.h | 14 +----- include/clang/Driver/Option.h | 25 ++++++----- lib/Driver/Arg.cpp | 80 ++++++++++++++++------------------- lib/Driver/OptTable.cpp | 4 +- lib/Driver/Option.cpp | 26 +++++++++++- 5 files changed, 79 insertions(+), 70 deletions(-) diff --git a/include/clang/Driver/Arg.h b/include/clang/Driver/Arg.h index 6b07db9ffe..053d4458f2 100644 --- a/include/clang/Driver/Arg.h +++ b/include/clang/Driver/Arg.h @@ -109,7 +109,7 @@ namespace driver { } /// render - Append the argument onto the given array as strings. - virtual void render(const ArgList &Args, ArgStringList &Output) const = 0; + void render(const ArgList &Args, ArgStringList &Output) const; /// renderAsInput - Append the argument, render as an input, onto /// the given array as strings. The distinction is that some @@ -131,8 +131,6 @@ namespace driver { public: FlagArg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0); - virtual void render(const ArgList &Args, ArgStringList &Output) const; - static bool classof(const Arg *A) { return A->getKind() == Arg::FlagClass; } @@ -145,8 +143,6 @@ namespace driver { PositionalArg(const Option *Opt, unsigned Index, const char *Value, const Arg *BaseArg = 0); - virtual void render(const ArgList &Args, ArgStringList &Output) const; - static bool classof(const Arg *A) { return A->getKind() == Arg::PositionalClass; } @@ -160,8 +156,6 @@ namespace driver { JoinedArg(const Option *Opt, unsigned Index, const char *Value, const Arg *BaseArg = 0); - virtual void render(const ArgList &Args, ArgStringList &Output) const; - static bool classof(const Arg *A) { return A->getKind() == Arg::JoinedClass; } @@ -175,8 +169,6 @@ namespace driver { SeparateArg(const Option *Opt, unsigned Index, const char *Value, const Arg *BaseArg = 0); - virtual void render(const ArgList &Args, ArgStringList &Output) const; - static bool classof(const Arg *A) { return A->getKind() == Arg::SeparateClass; } @@ -194,8 +186,6 @@ namespace driver { CommaJoinedArg(const Option *Opt, unsigned Index, const char *Str, const Arg *BaseArg = 0); - virtual void render(const ArgList &Args, ArgStringList &Output) const; - static bool classof(const Arg *A) { return A->getKind() == Arg::CommaJoinedClass; } @@ -210,8 +200,6 @@ namespace driver { const char *Value0, const char *Value1, const Arg *BaseArg = 0); - virtual void render(const ArgList &Args, ArgStringList &Output) const; - static bool classof(const Arg *A) { return A->getKind() == Arg::JoinedAndSeparateClass; } diff --git a/include/clang/Driver/Option.h b/include/clang/Driver/Option.h index 08b94b1d79..f91ac55fb0 100644 --- a/include/clang/Driver/Option.h +++ b/include/clang/Driver/Option.h @@ -50,6 +50,13 @@ namespace driver { JoinedAndSeparateClass }; + enum RenderStyleKind { + RenderCommaJoinedStyle, + RenderJoinedStyle, + RenderSeparateStyle, + RenderValuesStyle + }; + private: OptionClass Kind; @@ -65,7 +72,7 @@ namespace driver { /// Option that this is an alias for, if any. const Option *Alias; - /// Unsupported options will not be rejected. + /// Unsupported options will be rejected. bool Unsupported : 1; /// Treat this option like a linker input? @@ -76,11 +83,8 @@ namespace driver { // FIXME: We should ditch the render/renderAsInput distinction. bool NoOptAsInput : 1; - /// Always render this option as separate form its value. - bool ForceSeparateRender : 1; - - /// Always render this option joined with its value. - bool ForceJoinedRender : 1; + /// The style to using when rendering arguments parsed by this option. + unsigned RenderStyle : 2; /// This option is only consumed by the driver. bool DriverOption : 1; @@ -109,11 +113,10 @@ namespace driver { bool hasNoOptAsInput() const { return NoOptAsInput; } void setNoOptAsInput(bool Value) { NoOptAsInput = Value; } - bool hasForceSeparateRender() const { return ForceSeparateRender; } - void setForceSeparateRender(bool Value) { ForceSeparateRender = Value; } - - bool hasForceJoinedRender() const { return ForceJoinedRender; } - void setForceJoinedRender(bool Value) { ForceJoinedRender = Value; } + RenderStyleKind getRenderStyle() const { + return RenderStyleKind(RenderStyle); + } + void setRenderStyle(RenderStyleKind Value) { RenderStyle = Value; } bool isDriverOption() const { return DriverOption; } void setDriverOption(bool Value) { DriverOption = Value; } diff --git a/lib/Driver/Arg.cpp b/lib/Driver/Arg.cpp index 24de51f615..8220cf7ff9 100644 --- a/lib/Driver/Arg.cpp +++ b/lib/Driver/Arg.cpp @@ -10,6 +10,7 @@ #include "clang/Driver/Arg.h" #include "clang/Driver/ArgList.h" #include "clang/Driver/Option.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/raw_ostream.h" @@ -55,8 +56,8 @@ void Arg::dump() const { } std::string Arg::getAsString(const ArgList &Args) const { - std::string Res; - llvm::raw_string_ostream OS(Res); + llvm::SmallString<256> Res; + llvm::raw_svector_ostream OS(Res); ArgStringList ASL; render(Args, ASL); @@ -80,12 +81,42 @@ void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const { Output.push_back(getValue(Args, i)); } -FlagArg::FlagArg(const Option *Opt, unsigned Index, const Arg *BaseArg) - : Arg(FlagClass, Opt, Index, BaseArg) { +void Arg::render(const ArgList &Args, ArgStringList &Output) const { + switch (getOption().getRenderStyle()) { + case Option::RenderValuesStyle: + for (unsigned i = 0, e = getNumValues(); i != e; ++i) + Output.push_back(getValue(Args, i)); + break; + + case Option::RenderCommaJoinedStyle: { + llvm::SmallString<256> Res; + llvm::raw_svector_ostream OS(Res); + OS << getOption().getName(); + for (unsigned i = 0, e = getNumValues(); i != e; ++i) { + if (i) OS << ','; + OS << getValue(Args, i); + } + Output.push_back(Args.MakeArgString(OS.str())); + break; + } + + case Option::RenderJoinedStyle: + Output.push_back(Args.GetOrMakeJoinedArgString( + getIndex(), getOption().getName(), getValue(Args, 0))); + for (unsigned i = 1, e = getNumValues(); i != e; ++i) + Output.push_back(getValue(Args, i)); + break; + + case Option::RenderSeparateStyle: + Output.push_back(getOption().getName()); + for (unsigned i = 0, e = getNumValues(); i != e; ++i) + Output.push_back(getValue(Args, i)); + break; + } } -void FlagArg::render(const ArgList &Args, ArgStringList &Output) const { - Output.push_back(getOption().getName()); +FlagArg::FlagArg(const Option *Opt, unsigned Index, const Arg *BaseArg) + : Arg(FlagClass, Opt, Index, BaseArg) { } PositionalArg::PositionalArg(const Option *Opt, unsigned Index, @@ -94,26 +125,12 @@ PositionalArg::PositionalArg(const Option *Opt, unsigned Index, getValues().push_back(Value0); } -void PositionalArg::render(const ArgList &Args, ArgStringList &Output) const { - Output.push_back(Args.getArgString(getIndex())); -} - JoinedArg::JoinedArg(const Option *Opt, unsigned Index, const char *Value0, const Arg *BaseArg) : Arg(JoinedClass, Opt, Index, BaseArg) { getValues().push_back(Value0); } -void JoinedArg::render(const ArgList &Args, ArgStringList &Output) const { - if (getOption().hasForceSeparateRender()) { - Output.push_back(getOption().getName()); - Output.push_back(getValue(Args, 0)); - } else { - Output.push_back(Args.GetOrMakeJoinedArgString( - getIndex(), getOption().getName(), getValue(Args, 0))); - } -} - CommaJoinedArg::CommaJoinedArg(const Option *Opt, unsigned Index, const char *Str, const Arg *BaseArg) : Arg(CommaJoinedClass, Opt, Index, BaseArg) { @@ -139,28 +156,12 @@ CommaJoinedArg::CommaJoinedArg(const Option *Opt, unsigned Index, setOwnsValues(true); } -void CommaJoinedArg::render(const ArgList &Args, ArgStringList &Output) const { - Output.push_back(Args.getArgString(getIndex())); -} - SeparateArg::SeparateArg(const Option *Opt, unsigned Index, const char *Value0, const Arg *BaseArg) : Arg(SeparateClass, Opt, Index, BaseArg) { getValues().push_back(Value0); } -void SeparateArg::render(const ArgList &Args, ArgStringList &Output) const { - if (getOption().hasForceJoinedRender()) { - assert(getNumValues() == 1 && "Cannot force joined render with > 1 args."); - Output.push_back(Args.MakeArgString(llvm::StringRef(getOption().getName()) + - getValue(Args, 0))); - } else { - Output.push_back(getOption().getName()); - for (unsigned i = 0; i != getNumValues(); ++i) - Output.push_back(getValue(Args, i)); - } -} - JoinedAndSeparateArg::JoinedAndSeparateArg(const Option *Opt, unsigned Index, const char *Value0, const char *Value1, @@ -169,10 +170,3 @@ JoinedAndSeparateArg::JoinedAndSeparateArg(const Option *Opt, unsigned Index, getValues().push_back(Value0); getValues().push_back(Value1); } - -void JoinedAndSeparateArg::render(const ArgList &Args, - ArgStringList &Output) const { - Output.push_back(Args.GetOrMakeJoinedArgString( - getIndex(), getOption().getName(), getValue(Args, 0))); - Output.push_back(getValue(Args, 1)); -} diff --git a/lib/Driver/OptTable.cpp b/lib/Driver/OptTable.cpp index 1f913e34ba..e6c167ebe3 100644 --- a/lib/Driver/OptTable.cpp +++ b/lib/Driver/OptTable.cpp @@ -169,12 +169,12 @@ Option *OptTable::CreateOption(unsigned id) const { if (info.Flags & RenderJoined) { assert((info.Kind == Option::JoinedOrSeparateClass || info.Kind == Option::SeparateClass) && "Invalid option."); - Opt->setForceJoinedRender(true); + Opt->setRenderStyle(Option::RenderJoinedStyle); } if (info.Flags & RenderSeparate) { assert((info.Kind == Option::JoinedOrSeparateClass || info.Kind == Option::JoinedClass) && "Invalid option."); - Opt->setForceSeparateRender(true); + Opt->setRenderStyle(Option::RenderSeparateStyle); } if (info.Flags & Unsupported) Opt->setUnsupported(true); diff --git a/lib/Driver/Option.cpp b/lib/Driver/Option.cpp index ea3b3bc418..6bcaf524d2 100644 --- a/lib/Driver/Option.cpp +++ b/lib/Driver/Option.cpp @@ -20,7 +20,6 @@ Option::Option(OptionClass _Kind, OptSpecifier _ID, const char *_Name, const OptionGroup *_Group, const Option *_Alias) : Kind(_Kind), ID(_ID.getID()), Name(_Name), Group(_Group), Alias(_Alias), Unsupported(false), LinkerInput(false), NoOptAsInput(false), - ForceSeparateRender(false), ForceJoinedRender(false), DriverOption(false), NoArgumentUnused(false) { // Multi-level aliases are not supported, and alias options cannot @@ -28,6 +27,31 @@ Option::Option(OptionClass _Kind, OptSpecifier _ID, const char *_Name, // inherent limitation. assert((!Alias || (!Alias->Alias && !Group)) && "Multi-level aliases and aliases with groups are unsupported."); + + // Initialize rendering options based on the class. + switch (Kind) { + case GroupClass: + case InputClass: + case UnknownClass: + RenderStyle = RenderValuesStyle; + break; + + case JoinedClass: + case JoinedAndSeparateClass: + RenderStyle = RenderJoinedStyle; + break; + + case CommaJoinedClass: + RenderStyle = RenderCommaJoinedStyle; + break; + + case FlagClass: + case SeparateClass: + case MultiArgClass: + case JoinedOrSeparateClass: + RenderStyle = RenderSeparateStyle; + break; + } } Option::~Option() { -- 2.40.0