Refactoring: The Settings::addEnabled will return error message instead of throwing it. Ticket: #1866

This commit is contained in:
Daniel Marjamäki 2010-07-23 13:29:16 +02:00
parent 1b7796791b
commit 37c959023a
3 changed files with 35 additions and 12 deletions

View File

@ -259,7 +259,14 @@ bool CppCheck::parseFromArgs(int argc, const char* const argv[])
// Checking coding style // Checking coding style
else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--style") == 0) 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 // Filter errors
else if (strcmp(argv[i], "--suppressions") == 0) 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 // Check if there are unused functions
else if (strcmp(argv[i], "--unused-functions") == 0) 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 // Append userdefined code to checked source code
else if (strncmp(argv[i], "--append=", 9) == 0) 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) 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 // --error-exitcode=1

View File

@ -132,7 +132,7 @@ bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std:
return true; return true;
} }
void Settings::addEnabled(const std::string &str) std::string Settings::addEnabled(const std::string &str)
{ {
// Enable parameters may be comma separated... // Enable parameters may be comma separated...
if (str.find(",") != std::string::npos) 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) while ((pos = str.find(",", pos)) != std::string::npos)
{ {
if (pos == prevPos) if (pos == prevPos)
throw std::runtime_error("cppcheck: --enable parameter is empty"); return std::string("cppcheck: --enable parameter is empty");
addEnabled(str.substr(prevPos, pos - prevPos)); const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos)));
if (!errmsg.empty())
return errmsg;
++pos; ++pos;
prevPos = pos; prevPos = pos;
} }
if (prevPos >= str.length()) if (prevPos >= str.length())
throw std::runtime_error("cppcheck: --enable parameter is empty"); return std::string("cppcheck: --enable parameter is empty");
addEnabled(str.substr(prevPos)); return addEnabled(str.substr(prevPos));
return;
} }
bool handled = false; bool handled = false;
@ -178,10 +179,12 @@ void Settings::addEnabled(const std::string &str)
else if (!handled) else if (!handled)
{ {
if (str.empty()) if (str.empty())
throw std::runtime_error("cppcheck: --enable parameter is empty"); return std::string("cppcheck: --enable parameter is empty");
else 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 bool Settings::isEnabled(const std::string &str) const

View File

@ -122,8 +122,9 @@ public:
* @brief Enable extra checks by id. See isEnabled() * @brief Enable extra checks by id. See isEnabled()
* @param str single id or list of id values to be enabled * @param str single id or list of id values to be enabled
* or empty string to enable all. e.g. "style,possibleError" * 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 */ /** @brief class for handling suppressions */
class Suppressions class Suppressions