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
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

View File

@ -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

View File

@ -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