r232916 - [multilib] Turn virtual functor into functin_ref
Benjamin Kramer
benny.kra at googlemail.com
Sun Mar 22 08:56:12 PDT 2015
Author: d0k
Date: Sun Mar 22 10:56:12 2015
New Revision: 232916
URL: http://llvm.org/viewvc/llvm-project?rev=232916&view=rev
Log:
[multilib] Turn virtual functor into functin_ref
And update code to use lambdas where possible, plus random cleanup. NFCI.
Modified:
cfe/trunk/include/clang/Driver/Multilib.h
cfe/trunk/lib/Driver/Multilib.cpp
cfe/trunk/lib/Driver/ToolChains.cpp
Modified: cfe/trunk/include/clang/Driver/Multilib.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Multilib.h?rev=232916&r1=232915&r2=232916&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Multilib.h (original)
+++ cfe/trunk/include/clang/Driver/Multilib.h Sun Mar 22 10:56:12 2015
@@ -11,6 +11,7 @@
#define LLVM_CLANG_DRIVER_MULTILIB_H
#include "clang/Basic/LLVM.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Option/Option.h"
#include <functional>
@@ -102,11 +103,7 @@ public:
StringRef InstallDir, StringRef Triple, const Multilib &M)>
IncludeDirsFunc;
- struct FilterCallback {
- virtual ~FilterCallback() {};
- /// \return true iff the filter should remove the Multilib from the set
- virtual bool operator()(const Multilib &M) const = 0;
- };
+ typedef llvm::function_ref<bool(const Multilib &)> FilterCallback;
private:
multilib_list Multilibs;
@@ -127,12 +124,12 @@ public:
MultilibSet &Either(const Multilib &M1, const Multilib &M2,
const Multilib &M3, const Multilib &M4,
const Multilib &M5);
- MultilibSet &Either(const std::vector<Multilib> &Ms);
+ MultilibSet &Either(ArrayRef<Multilib> Ms);
/// Filter out some subset of the Multilibs using a user defined callback
- MultilibSet &FilterOut(const FilterCallback &F);
+ MultilibSet &FilterOut(FilterCallback F);
/// Filter out those Multilibs whose gccSuffix matches the given expression
- MultilibSet &FilterOut(std::string Regex);
+ MultilibSet &FilterOut(const char *Regex);
/// Add a completed Multilib to the set
void push_back(const Multilib &M);
@@ -157,18 +154,17 @@ public:
void print(raw_ostream &OS) const;
MultilibSet &setIncludeDirsCallback(IncludeDirsFunc F) {
- IncludeCallback = F;
+ IncludeCallback = std::move(F);
return *this;
}
- IncludeDirsFunc includeDirsCallback() const { return IncludeCallback; }
+ const IncludeDirsFunc &includeDirsCallback() const { return IncludeCallback; }
private:
/// Apply the filter to Multilibs and return the subset that remains
- static multilib_list filterCopy(const FilterCallback &F,
- const multilib_list &Ms);
+ static multilib_list filterCopy(FilterCallback F, const multilib_list &Ms);
/// Apply the filter to the multilib_list, removing those that don't match
- static void filterInPlace(const FilterCallback &F, multilib_list &Ms);
+ static void filterInPlace(FilterCallback F, multilib_list &Ms);
};
raw_ostream &operator<<(raw_ostream &OS, const MultilibSet &MS);
Modified: cfe/trunk/lib/Driver/Multilib.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Multilib.cpp?rev=232916&r1=232915&r2=232916&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Multilib.cpp (original)
+++ cfe/trunk/lib/Driver/Multilib.cpp Sun Mar 22 10:56:12 2015
@@ -151,41 +151,23 @@ MultilibSet &MultilibSet::Maybe(const Mu
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
- std::vector<Multilib> Ms;
- Ms.push_back(M1);
- Ms.push_back(M2);
- return Either(Ms);
+ return Either({M1, M2});
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
const Multilib &M3) {
- std::vector<Multilib> Ms;
- Ms.push_back(M1);
- Ms.push_back(M2);
- Ms.push_back(M3);
- return Either(Ms);
+ return Either({M1, M2, M3});
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
const Multilib &M3, const Multilib &M4) {
- std::vector<Multilib> Ms;
- Ms.push_back(M1);
- Ms.push_back(M2);
- Ms.push_back(M3);
- Ms.push_back(M4);
- return Either(Ms);
+ return Either({M1, M2, M3, M4});
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
const Multilib &M3, const Multilib &M4,
const Multilib &M5) {
- std::vector<Multilib> Ms;
- Ms.push_back(M1);
- Ms.push_back(M2);
- Ms.push_back(M3);
- Ms.push_back(M4);
- Ms.push_back(M5);
- return Either(Ms);
+ return Either({M1, M2, M3, M4, M5});
}
static Multilib compose(const Multilib &Base, const Multilib &New) {
@@ -207,8 +189,7 @@ static Multilib compose(const Multilib &
return Composed;
}
-MultilibSet &
-MultilibSet::Either(const std::vector<Multilib> &MultilibSegments) {
+MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
multilib_list Composed;
if (Multilibs.empty())
@@ -229,30 +210,23 @@ MultilibSet::Either(const std::vector<Mu
return *this;
}
-MultilibSet &MultilibSet::FilterOut(const MultilibSet::FilterCallback &F) {
+MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
filterInPlace(F, Multilibs);
return *this;
}
-MultilibSet &MultilibSet::FilterOut(std::string Regex) {
- class REFilter : public MultilibSet::FilterCallback {
- mutable llvm::Regex R;
-
- public:
- REFilter(std::string Regex) : R(Regex) {}
- bool operator()(const Multilib &M) const override {
- std::string Error;
- if (!R.isValid(Error)) {
- llvm::errs() << Error;
- assert(false);
- return false;
- }
- return R.match(M.gccSuffix());
- }
- };
+MultilibSet &MultilibSet::FilterOut(const char *Regex) {
+ llvm::Regex R(Regex);
+#ifndef NDEBUG
+ std::string Error;
+ if (!R.isValid(Error)) {
+ llvm::errs() << Error;
+ llvm_unreachable("Invalid regex!");
+ }
+#endif
- REFilter REF(Regex);
- filterInPlace(REF, Multilibs);
+ filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
+ Multilibs);
return *this;
}
@@ -262,38 +236,29 @@ void MultilibSet::combineWith(const Mult
Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
}
+static bool isFlagEnabled(StringRef Flag) {
+ char Indicator = Flag.front();
+ assert(Indicator == '+' || Indicator == '-');
+ return Indicator == '+';
+}
+
bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
- class FilterFlagsMismatch : public MultilibSet::FilterCallback {
- llvm::StringMap<bool> FlagSet;
+ llvm::StringMap<bool> FlagSet;
- public:
- FilterFlagsMismatch(const std::vector<std::string> &Flags) {
- // Stuff all of the flags into the FlagSet such that a true mappend
- // indicates the flag was enabled, and a false mappend indicates the
- // flag was disabled
- for (StringRef Flag : Flags)
- FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
+ // Stuff all of the flags into the FlagSet such that a true mappend indicates
+ // the flag was enabled, and a false mappend indicates the flag was disabled.
+ for (StringRef Flag : Flags)
+ FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
+
+ multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
+ for (StringRef Flag : M.flags()) {
+ llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
+ if (SI != FlagSet.end())
+ if (SI->getValue() != isFlagEnabled(Flag))
+ return true;
}
- bool operator()(const Multilib &M) const override {
- for (StringRef Flag : M.flags()) {
- llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
- if (SI != FlagSet.end())
- if (SI->getValue() != isFlagEnabled(Flag))
- return true;
- }
- return false;
- }
- private:
- bool isFlagEnabled(StringRef Flag) const {
- char Indicator = Flag.front();
- assert(Indicator == '+' || Indicator == '-');
- return Indicator == '+';
- }
- };
-
- FilterFlagsMismatch FlagsMismatch(Flags);
-
- multilib_list Filtered = filterCopy(FlagsMismatch, Multilibs);
+ return false;
+ }, Multilibs);
if (Filtered.size() == 0) {
return false;
@@ -313,19 +278,15 @@ void MultilibSet::print(raw_ostream &OS)
OS << M << "\n";
}
-MultilibSet::multilib_list
-MultilibSet::filterCopy(const MultilibSet::FilterCallback &F,
- const multilib_list &Ms) {
+MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
+ const multilib_list &Ms) {
multilib_list Copy(Ms);
filterInPlace(F, Copy);
return Copy;
}
-void MultilibSet::filterInPlace(const MultilibSet::FilterCallback &F,
- multilib_list &Ms) {
- Ms.erase(std::remove_if(Ms.begin(), Ms.end(),
- [&F](const Multilib &M) { return F(M); }),
- Ms.end());
+void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
+ Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
}
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
Modified: cfe/trunk/lib/Driver/ToolChains.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains.cpp?rev=232916&r1=232915&r2=232916&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains.cpp Sun Mar 22 10:56:12 2015
@@ -1498,11 +1498,12 @@ bool Generic_GCC::GCCInstallationDetecto
namespace {
// Filter to remove Multilibs that don't exist as a suffix to Path
-class FilterNonExistent : public MultilibSet::FilterCallback {
- std::string Base;
+class FilterNonExistent {
+ StringRef Base;
+
public:
- FilterNonExistent(std::string Base) : Base(Base) {}
- bool operator()(const Multilib &M) const override {
+ FilterNonExistent(StringRef Base) : Base(Base) {}
+ bool operator()(const Multilib &M) {
return !llvm::sys::fs::exists(Base + M.gccSuffix() + "/crtbegin.o");
}
};
@@ -3171,7 +3172,7 @@ void Linux::AddClangSystemIncludeArgs(co
// Add include directories specific to the selected multilib set and multilib.
if (GCCInstallation.isValid()) {
- auto Callback = Multilibs.includeDirsCallback();
+ const auto &Callback = Multilibs.includeDirsCallback();
if (Callback) {
const auto IncludePaths = Callback(GCCInstallation.getInstallPath(),
GCCInstallation.getTriple().str(),
More information about the cfe-commits
mailing list