[PATCH] Add FilteredArgList to represent filtered arglist
Rui Ueyama
ruiu at google.com
Sat May 25 15:21:41 PDT 2013
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.
+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)) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D867.1.patch
Type: text/x-patch
Size: 2297 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130525/b1a03221/attachment.bin>
More information about the llvm-commits
mailing list