commit c2b30bbaa872ae8b68fc83c126124646626fd9d6
parent fdfba642ec129ba9f776f0e07cb615d398b64832
Author: Kyle Milz <kyle@0x30.net>
Date: Fri, 13 Jan 2017 18:42:46 -0700
inst: use std::find_if instead of wasteful cp+paste
Diffstat:
2 files changed, 29 insertions(+), 19 deletions(-)
diff --git a/inst_fe.cc b/inst_fe.cc
@@ -21,11 +21,13 @@
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/Tooling.h>
+#include <llvm/Support/raw_os_ostream.h>
+
+#include <algorithm> // std::find_if
#include <cstdio> // tmpnam
#include <cstring> // strcmp
-#include <iostream> // cerr
-#include <llvm/Support/raw_os_ostream.h>
-#include <sstream> // ostringstream
+#include <iostream> // std::cerr
+#include <sstream> // std::ostringstream
static llvm::cl::OptionCategory ToolingCategory("citrun_inst options");
@@ -120,16 +122,6 @@ InstFrontend::clean_PATH()
set_path(new_path.str());
}
-// Returns true if value ends with suffix, false otherwise.
-bool
-InstFrontend::ends_with(std::string const &value, std::string const &suffix)
-{
- if (suffix.length() > value.length())
- return false;
-
- return std::equal(suffix.rbegin(), suffix.rend(), value.rbegin());
-}
-
//
// Guess if the argument is a sourcefile. If it is stash a backup of the file
// and sync the timestamps.
@@ -137,8 +129,8 @@ InstFrontend::ends_with(std::string const &value, std::string const &suffix)
void
InstFrontend::save_if_srcfile(char *arg)
{
- if (!ends_with(arg, ".c") && !ends_with(arg, ".cc") &&
- !ends_with(arg, ".cpp") && !ends_with(arg, ".cxx"))
+ std::array<std::string, 4> exts = { ".c", ".cc", ".cxx", ".cpp" };
+ if (std::find_if(exts.begin(), exts.end(), ends_with(arg)) == exts.end())
return;
char *dst_fn;
diff --git a/inst_fe.h b/inst_fe.h
@@ -5,8 +5,8 @@
#include "inst_log.h"
#include <chrono> // std::chrono::high_resolution_clock
-#include <map>
-#include <string>
+#include <map> // std::map
+#include <string> // std::string
class InstFrontend
{
@@ -30,8 +30,6 @@ class InstFrontend
virtual int fork_compiler() = 0;
protected:
- bool ends_with(std::string const &, std::string const &);
-
std::vector<char *> m_args;
bool m_is_citruninst;
std::vector<std::string> m_source_files;
@@ -47,3 +45,23 @@ public:
void instrument();
void compile_instrumented();
};
+
+//
+// Helper class that is a unary predicate suitable for use with std::find_if.
+//
+class ends_with
+{
+ std::string arg;
+public:
+ ends_with(char *argument) :
+ arg(argument)
+ {}
+
+ bool operator ()(std::string const &suffix) const
+ {
+ if (suffix.length() > arg.length())
+ return false;
+
+ return std::equal(suffix.rbegin(), suffix.rend(), arg.rbegin());
+ }
+};