Added command line option "--errorsonly"

This commit is contained in:
Daniel Marjamäki 2008-11-13 17:43:55 +00:00
parent 0b1ee10353
commit 34507f73ce
1 changed files with 34 additions and 15 deletions

View File

@ -38,6 +38,7 @@
bool Debug = false;
bool ShowAll = false;
bool CheckCodingStyle = false;
bool ErrorsOnly = false;
//---------------------------------------------------------------------------
static void CppCheck(const std::string &code, const char FileName[], unsigned int FileId);
@ -65,6 +66,10 @@ int main(int argc, char* argv[])
else if (strcmp(argv[i],"--style")==0)
CheckCodingStyle = true;
// Only print something when there are errors
else if (strcmp(argv[i],"--errorsonly")==0)
ErrorsOnly = true;
else if (strcmp(argv[i],"--recursive")==0)
Recursive = true;
else
@ -104,7 +109,7 @@ int main(int argc, char* argv[])
std::cout << "C/C++ code checking.\n"
"\n"
"Syntax:\n"
" cppcheck [--all] [--style] [--recursive] [filename1] [filename2]\n"
" cppcheck [--all] [--style] [--errorsonly] [--recursive] [filename1] [filename2]\n"
"\n"
"Options:\n"
" --all Normally a message is only shown if cppcheck is sure\n"
@ -112,6 +117,7 @@ int main(int argc, char* argv[])
" When this option is given, all messages are shown.\n"
"\n"
" --style Check coding style.\n"
" --errorsonly Only print something when there is an error\n"
" --recursive Recursively check all *.cpp, *.cc and *.c files\n";
return 0;
}
@ -123,19 +129,32 @@ int main(int argc, char* argv[])
errout.str("");
std::string fname = filenames[c];
std::cout << "Checking " << fname << "...\n";
// If only errors are printed, print filename after the check
if (!ErrorsOnly)
std::cout << "Checking " << fname << "...\n";
std::ifstream fin( fname.c_str() );
std::map<std::string, std::string> code;
Preprocessor preprocessor;
std::map<std::string, std::string> code;
Preprocessor preprocessor;
preprocessor.preprocess(fin, code, fname);
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
CppCheck(it->second, filenames[c].c_str(), c);
if ( errout.str().empty() )
std::cout << "No errors found\n";
if (ErrorsOnly)
{
if ( !errout.str().empty() )
{
std::cout << "Errors found in " << fname << ":\n";
std::cerr << errout.str();
}
}
else
std::cerr << errout.str();
{
if ( errout.str().empty() )
std::cout << "No errors found\n";
else
std::cerr << errout.str();
}
}
// This generates false positives - especially for libraries
@ -159,12 +178,12 @@ int main(int argc, char* argv[])
//---------------------------------------------------------------------------
static void CppCheck(const std::string &code, const char FileName[], unsigned int FileId)
{
Tokenizer tokenizer;
{
Tokenizer tokenizer;
OnlyReportUniqueErrors = true;
// Tokenize the file
// Tokenize the file
Files.clear();
{
std::istringstream istr(code);
@ -192,20 +211,20 @@ static void CppCheck(const std::string &code, const char FileName[], unsigned in
// Including header which is not needed (too many false positives)
// if ( CheckCodingStyle )
// {
// if ( CheckCodingStyle )
// {
// CheckHeaders checkHeaders( &tokenizer );
// checkHeaders.WarningIncludeHeader();
// checkHeaders.WarningIncludeHeader();
// }
tokenizer.SimplifyTokenList();
// Memory leak
// Memory leak
CheckMemoryLeakClass checkMemoryLeak( &tokenizer );
checkMemoryLeak.CheckMemoryLeak();
// Buffer overruns..
// Buffer overruns..
CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
checkBufferOverrun.CheckBufferOverrun();