[PATCH] Add FilteredArgList to represent filtered arglist
David Blaikie
dblaikie at gmail.com
Sat May 25 23:24:14 PDT 2013
On May 25, 2013 3:22 PM, "Rui Ueyama" <ruiu at google.com> wrote:
>
> With this patch we can write a loop for command line options like this
>
> for (const auto &arg : args->filter(OPT_SOMETHING)
> ...
>
> instead of
>
> for (auto it = args->filtered_begin(OPT_SOMETHING),
> ie = args->filtered_end(); it != ie; ++it) {
> ...
>
> I plan to use this in lld in where range-based for loop is allowed.
>
> http://llvm-reviews.chandlerc.com/D867
>
> Files:
> include/llvm/Option/ArgList.h
> lib/Option/ArgList.cpp
>
> Index: include/llvm/Option/ArgList.h
> ===================================================================
> --- include/llvm/Option/ArgList.h
> +++ include/llvm/Option/ArgList.h
> @@ -22,6 +22,7 @@
> namespace opt {
> class Arg;
> class ArgList;
> +class FilteredArgList;
> class Option;
>
> /// arg_iterator - Iterates through arguments stored inside an ArgList.
> @@ -137,10 +138,14 @@
> const_reverse_iterator rbegin() const { return Args.rbegin(); }
> const_reverse_iterator rend() const { return Args.rend(); }
>
> + FilteredArgList filter(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
> + OptSpecifier Id2 = 0U) const;
> +
> arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 =
0U,
> OptSpecifier Id2 = 0U) const {
> return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
> }
> +
> arg_iterator filtered_end() const {
> return arg_iterator(Args.end(), *this);
> }
> @@ -275,6 +280,31 @@
> /// @}
> };
>
> +/// FilteredArgList - ArgList with filters. Useful to be used in
> +/// range-based for loop to iterate over filtered arguments.
Could we just have/use a generic range abstraction (essentially a simple
class template with two iterator members, one returned by begin, the other
by end) rather than writing a custom type here?
> +class FilteredArgList {
> +private:
> + FilteredArgList(const ArgList &) LLVM_DELETED_FUNCTION;
> + void operator=(const ArgList &) LLVM_DELETED_FUNCTION;
> +
> +public:
> + FilteredArgList(const ArgList &_Args, OptSpecifier _Id0 = 0U,
> + OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U)
> + : Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) {}
> +
> + arg_iterator begin() {
> + return Args.filtered_begin(Id0, Id1, Id2);
> + }
> +
> + arg_iterator end() {
> + return Args.filtered_end();
> + }
> +
> +private:
> + const ArgList &Args;
> + OptSpecifier Id0, Id1, Id2;
> +};
> +
> class InputArgList : public ArgList {
> private:
> /// List of argument strings used by the contained Args.
> Index: lib/Option/ArgList.cpp
> ===================================================================
> --- lib/Option/ArgList.cpp
> +++ lib/Option/ArgList.cpp
> @@ -44,6 +44,11 @@
> Args.push_back(A);
> }
>
> +FilteredArgList ArgList::filter(OptSpecifier Id0, OptSpecifier Id1,
> + OptSpecifier Id2) const {
> + return FilteredArgList(*this, Id0, Id1, Id2);
> +}
> +
> void ArgList::eraseArg(OptSpecifier Id) {
> for (iterator it = begin(), ie = end(); it != ie; ) {
> if ((*it)->getOption().matches(Id)) {
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130525/44f23952/attachment.html>
More information about the llvm-commits
mailing list