From 37c959023a24da4f3d561bab0b9ab31bc4d60006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 23 Jul 2010 13:29:16 +0200 Subject: [PATCH] Refactoring: The Settings::addEnabled will return error message instead of throwing it. Ticket: #1866 --- lib/cppcheck.cpp | 25 ++++++++++++++++++++++--- lib/settings.cpp | 19 +++++++++++-------- lib/settings.h | 3 ++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 4ca0c5364..bebaa87b7 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -259,7 +259,14 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[]) // Checking coding style else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--style") == 0) - _settings.addEnabled("style"); + { + const std::string errmsg = _settings.addEnabled("style"); + if (!errmsg.empty()) + { + reportOut(errmsg); + return false; + } + } // Filter errors else if (strcmp(argv[i], "--suppressions") == 0) @@ -319,7 +326,14 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[]) // Check if there are unused functions else if (strcmp(argv[i], "--unused-functions") == 0) - _settings.addEnabled("unusedFunctions"); + { + const std::string errmsg = _settings.addEnabled("unusedFunctions"); + if (!errmsg.empty()) + { + reportOut(errmsg); + return false; + } + } // Append userdefined code to checked source code else if (strncmp(argv[i], "--append=", 9) == 0) @@ -351,7 +365,12 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[]) else if (strncmp(argv[i], "--enable=", 9) == 0) { - _settings.addEnabled(argv[i] + 9); + const std::string errmsg = _settings.addEnabled(argv[i] + 9); + if (!errmsg.empty()) + { + reportOut(errmsg); + return false; + } } // --error-exitcode=1 diff --git a/lib/settings.cpp b/lib/settings.cpp index f3a21ba37..948abdc75 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -132,7 +132,7 @@ bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std: return true; } -void Settings::addEnabled(const std::string &str) +std::string Settings::addEnabled(const std::string &str) { // Enable parameters may be comma separated... if (str.find(",") != std::string::npos) @@ -142,15 +142,16 @@ void Settings::addEnabled(const std::string &str) while ((pos = str.find(",", pos)) != std::string::npos) { if (pos == prevPos) - throw std::runtime_error("cppcheck: --enable parameter is empty"); - addEnabled(str.substr(prevPos, pos - prevPos)); + return std::string("cppcheck: --enable parameter is empty"); + const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos))); + if (!errmsg.empty()) + return errmsg; ++pos; prevPos = pos; } if (prevPos >= str.length()) - throw std::runtime_error("cppcheck: --enable parameter is empty"); - addEnabled(str.substr(prevPos)); - return; + return std::string("cppcheck: --enable parameter is empty"); + return addEnabled(str.substr(prevPos)); } bool handled = false; @@ -178,10 +179,12 @@ void Settings::addEnabled(const std::string &str) else if (!handled) { if (str.empty()) - throw std::runtime_error("cppcheck: --enable parameter is empty"); + return std::string("cppcheck: --enable parameter is empty"); else - throw std::runtime_error("cppcheck: there is no --enable parameter with the name '" + str + "'"); + return std::string("cppcheck: there is no --enable parameter with the name '" + str + "'"); } + + return std::string(""); } bool Settings::isEnabled(const std::string &str) const diff --git a/lib/settings.h b/lib/settings.h index 32baeb2e4..c0acefc94 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -122,8 +122,9 @@ public: * @brief Enable extra checks by id. See isEnabled() * @param str single id or list of id values to be enabled * or empty string to enable all. e.g. "style,possibleError" + * @return error message. empty upon success */ - void addEnabled(const std::string &str); + std::string addEnabled(const std::string &str); /** @brief class for handling suppressions */ class Suppressions