exitcode suppressions. Partly fixes #1113

This commit is contained in:
Daniel Marjamäki 2009-12-28 11:23:58 +01:00
parent 197bcf17ba
commit 6fd74dce47
5 changed files with 74 additions and 41 deletions

View File

@ -119,7 +119,21 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[])
std::ifstream f(argv[i]);
if (!f.is_open())
throw std::runtime_error("cppcheck: Couldn't open the file \"" + std::string(argv[i]) + "\"");
_settings.suppressions(f);
_settings.nomsg.parseFile(f);
}
// Filter errors
else if (strcmp(argv[i], "--exitcode-suppressions") == 0)
{
++i;
if (i >= argc)
throw std::runtime_error("cppcheck: No file specified for the --exitcode-suppressions option");
std::ifstream f(argv[i]);
if (!f.is_open())
throw std::runtime_error("cppcheck: Couldn't open the file \"" + std::string(argv[i]) + "\"");
_settings.nofail.parseFile(f);
}
// Enables inline suppressions.
@ -334,9 +348,10 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[])
"\n"
"Syntax:\n"
" cppcheck [--all] [--append=file] [--auto-dealloc file.lst] [--enable]\n"
" [--error-exitcode=[n]] [--force] [--help] [-Idir] [-j [jobs]]\n"
" [--quiet] [--style] [--suppressions file.txt] [--inline-suppr]\n"
" [--verbose] [--version] [--xml] [file or path1]\n"
" [--error-exitcode=[n]] [--exitcode-suppressions file] [--force]\n"
" [--help] [-Idir] [-j [jobs]] [--quiet] [--style]\n"
" [--suppressions file.txt] [--inline-suppr] [--verbose]\n"
" [--version] [--xml] [file or path1]\n"
" [file or path] ...\n"
"\n"
"If path is given instead of filename, *.cpp, *.cxx, *.cc, *.c++ and *.c files\n"
@ -366,6 +381,9 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[])
" if arguments are not valid or if no input files are\n"
" provided. Note that your operating system can\n"
" modify this value, e.g. 256 can become 0.\n"
" --exitcode-suppressions file\n"
" Used when certain messages should be displayed but\n"
" should not cause a non-zero exitcode.\n"
" -f, --force Force checking on files that have \"too many\"\n"
" configurations\n"
" -h, --help Print this help\n"
@ -410,6 +428,8 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[])
unsigned int CppCheck::check()
{
exitcode = 0;
_checkUnusedFunctions.setErrorLogger(this);
std::sort(_filenames.begin(), _filenames.end());
for (unsigned int c = 0; c < _filenames.size(); c++)
@ -487,11 +507,8 @@ unsigned int CppCheck::check()
_checkUnusedFunctions.check();
}
unsigned int result = static_cast<unsigned int>(_errorList.size());
_errorList.clear();
return result;
return exitcode;
}
@ -573,9 +590,12 @@ void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
line = msg._callStack.back().line;
}
if (_settings.isSuppressed(msg._id, file, line))
if (_settings.nomsg.isSuppressed(msg._id, file, line))
return;
if (!_settings.nofail.isSuppressed(msg._id, file, line))
exitcode = 1;
_errorList.push_back(errmsg);
std::string errmsg2(errmsg);
if (_settings._verbose)

View File

@ -135,6 +135,7 @@ private:
*/
virtual void reportOut(const std::string &outmsg);
unsigned int exitcode;
std::list<std::string> _errorList;
std::ostringstream _errout;
Settings _settings;

View File

@ -202,7 +202,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
{
// Add the suppressions.
for (size_t j(0); j < suppressionIDs.size(); ++j)
settings->addSuppression(suppressionIDs[j], filename, lineno);
settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno);
suppressionIDs.clear();
}

View File

@ -59,7 +59,7 @@ void Settings::autoDealloc(std::istream &istr)
}
}
bool Settings::suppressions(std::istream &istr)
bool Settings::Suppressions::parseFile(std::istream &istr)
{
std::string line;
while (getline(istr, line))
@ -87,13 +87,13 @@ bool Settings::suppressions(std::istream &istr)
return true;
}
void Settings::addSuppression(const std::string &errorId, const std::string &file, unsigned int line)
void Settings::Suppressions::addSuppression(const std::string &errorId, const std::string &file, unsigned int line)
{
_suppressions[errorId][file].push_back(line);
_suppressions[errorId][file].sort();
}
bool Settings::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line)
bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line)
{
if (_suppressions.find(errorId) == _suppressions.end())
return false;

View File

@ -43,9 +43,6 @@ private:
/** Code to append in the checks */
std::string _append;
/** List of error which the user doesn't want to see. */
std::map<std::string, std::map<std::string, std::list<int> > > _suppressions;
/** enable extra checks by id */
std::map<std::string, bool> _enabled;
public:
@ -90,28 +87,6 @@ public:
/** Add class to list of automatically deallocated classes */
void addAutoAllocClass(const std::string &name);
/**
* Don't show errors listed in the file.
* @param istr Open file stream where errors can be read.
* @return true on success, false in syntax error is noticed.
*/
bool suppressions(std::istream &istr);
/**
* Don't show this error. If file and/or line are optional. In which case
* the errorId alone is used for filtering.
* @param errorId, the id for the error, e.g. "arrayIndexOutOfBounds"
* @param file File name with the path, e.g. "src/main.cpp"
* @param line number, e.g. "123"
*/
void addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0);
/**
* Returns true if this message should not be shown to the user.
* @return true if this error is suppressed.
*/
bool isSuppressed(const std::string &errorId, const std::string &file, unsigned int line);
/** is a class automaticly deallocated? */
bool isAutoDealloc(const char classname[]) const;
@ -121,9 +96,6 @@ public:
/** get append code */
std::string append() const;
/** enable extra checks by id */
//std::string enableId;
/**
* Returns true if given id is in the list of
* enabled extra checks. See addEnabled()
@ -138,6 +110,46 @@ public:
* or empty string to enable all. e.g. "style,possibleError"
*/
void addEnabled(const std::string &str);
/** class for handling suppressions */
class Suppressions
{
private:
/** List of error which the user doesn't want to see. */
std::map<std::string, std::map<std::string, std::list<int> > > _suppressions;
public:
/**
* Don't show errors listed in the file.
* @param istr Open file stream where errors can be read.
* @return true on success, false in syntax error is noticed.
*/
bool parseFile(std::istream &istr);
/**
* Don't show this error. If file and/or line are optional. In which case
* the errorId alone is used for filtering.
* @param errorId, the id for the error, e.g. "arrayIndexOutOfBounds"
* @param file File name with the path, e.g. "src/main.cpp"
* @param line number, e.g. "123"
*/
void addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0);
/**
* Returns true if this message should not be shown to the user.
* @param errorId, the id for the error, e.g. "arrayIndexOutOfBounds"
* @param file File name with the path, e.g. "src/main.cpp"
* @param line number, e.g. "123"
* @return true if this error is suppressed.
*/
bool isSuppressed(const std::string &errorId, const std::string &file, unsigned int line);
};
/** suppress message */
Suppressions nomsg;
/** suppress exitcode */
Suppressions nofail;
};
/// @}