diff --git a/tools/reduce.cpp b/tools/reduce.cpp index 614ad7871..b8593d5fa 100644 --- a/tools/reduce.cpp +++ b/tools/reduce.cpp @@ -26,6 +26,20 @@ #include "mathlib.h" #include "path.h" +class ReduceSettings : public Settings { +public: + ReduceSettings() : filename(0), linenr(0), hang(false), maxtime(0) { + addEnabled("all"); + inconclusive = true; + _force = true; + } + + const char *filename; + std::size_t linenr; + bool hang; + unsigned int maxtime; +}; + class CppcheckExecutor : public ErrorLogger { private: CppCheck cppcheck; @@ -34,31 +48,16 @@ private: std::time_t stopTime; public: - CppcheckExecutor(const std::string &defines, const char * const includePaths[100], std::size_t linenr, bool hang) + CppcheckExecutor(const ReduceSettings & settings) : ErrorLogger() , cppcheck(*this,false) , foundLine(false) , stopTime(0) { - if (!hang) - pattern = ":" + MathLib::longToString(linenr) + "]"; + if (!settings.hang) + pattern = ":" + MathLib::longToString(settings.linenr) + "]"; - cppcheck.settings().userDefines = defines; - - for (int i = 0; includePaths[i]; i++) { - std::string path = Path::fromNativeSeparators(includePaths[i]); - path = Path::removeQuotationMarks(path); - - // If path doesn't end with / or \, add it - if (path[path.length()-1] != '/') - path += '/'; - - cppcheck.settings()._includePaths.push_back(path); - } - - cppcheck.settings().addEnabled("all"); - cppcheck.settings().inconclusive = true; - cppcheck.settings()._force = true; + cppcheck.settings() = settings; } bool run(const char filename[], unsigned int maxtime) { @@ -87,15 +86,6 @@ public: } }; -struct ReduceSettings { - const char *filename; - std::size_t linenr; - bool hang; - unsigned int maxtime; - std::string defines; - const char *includePaths[100]; -}; - static bool test(const ReduceSettings &settings, const std::vector &filedata, const std::size_t line1, const std::size_t line2) { std::string path(settings.filename); @@ -110,7 +100,7 @@ static bool test(const ReduceSettings &settings, const std::vector fout << ((i>=line1 && i<=line2) ? "" : filedata[i]) << std::endl; fout.close(); - CppcheckExecutor cppcheck(settings.defines, settings.includePaths, settings.linenr, settings.hang); + CppcheckExecutor cppcheck(settings); return cppcheck.run(tempfilename.c_str(), settings.maxtime); } @@ -590,7 +580,7 @@ int main(int argc, char *argv[]) std::cout << "cppcheck tool that reduce code for a hang / false positive" << std::endl; bool print = false; - struct ReduceSettings settings = {0}; + ReduceSettings settings; settings.maxtime = 300; // default timeout = 5 minutes for (int i = 1, includePathIndex = 0; i < argc; i++) { @@ -599,21 +589,56 @@ int main(int argc, char *argv[]) else if (strcmp(argv[i], "--hang") == 0) settings.hang = true; else if (strncmp(argv[i],"-D", 2) == 0) { - if (!settings.defines.empty()) - settings.defines += " "; + if (!settings.userDefines.empty()) + settings.userDefines += " "; if ((strcmp(argv[i], "-D") == 0) && (i+1