Added command line option "--errorsonly"
This commit is contained in:
parent
0b1ee10353
commit
34507f73ce
49
main.cpp
49
main.cpp
|
@ -38,6 +38,7 @@
|
||||||
bool Debug = false;
|
bool Debug = false;
|
||||||
bool ShowAll = false;
|
bool ShowAll = false;
|
||||||
bool CheckCodingStyle = false;
|
bool CheckCodingStyle = false;
|
||||||
|
bool ErrorsOnly = false;
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
static void CppCheck(const std::string &code, const char FileName[], unsigned int FileId);
|
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)
|
else if (strcmp(argv[i],"--style")==0)
|
||||||
CheckCodingStyle = true;
|
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)
|
else if (strcmp(argv[i],"--recursive")==0)
|
||||||
Recursive = true;
|
Recursive = true;
|
||||||
else
|
else
|
||||||
|
@ -104,7 +109,7 @@ int main(int argc, char* argv[])
|
||||||
std::cout << "C/C++ code checking.\n"
|
std::cout << "C/C++ code checking.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Syntax:\n"
|
"Syntax:\n"
|
||||||
" cppcheck [--all] [--style] [--recursive] [filename1] [filename2]\n"
|
" cppcheck [--all] [--style] [--errorsonly] [--recursive] [filename1] [filename2]\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
" --all Normally a message is only shown if cppcheck is sure\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"
|
" When this option is given, all messages are shown.\n"
|
||||||
"\n"
|
"\n"
|
||||||
" --style Check coding style.\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";
|
" --recursive Recursively check all *.cpp, *.cc and *.c files\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -123,19 +129,32 @@ int main(int argc, char* argv[])
|
||||||
errout.str("");
|
errout.str("");
|
||||||
std::string fname = filenames[c];
|
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::ifstream fin( fname.c_str() );
|
||||||
std::map<std::string, std::string> code;
|
std::map<std::string, std::string> code;
|
||||||
Preprocessor preprocessor;
|
Preprocessor preprocessor;
|
||||||
preprocessor.preprocess(fin, code, fname);
|
preprocessor.preprocess(fin, code, fname);
|
||||||
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
|
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
|
||||||
CppCheck(it->second, filenames[c].c_str(), c);
|
CppCheck(it->second, filenames[c].c_str(), c);
|
||||||
|
|
||||||
if ( errout.str().empty() )
|
if (ErrorsOnly)
|
||||||
std::cout << "No errors found\n";
|
{
|
||||||
|
if ( !errout.str().empty() )
|
||||||
|
{
|
||||||
|
std::cout << "Errors found in " << fname << ":\n";
|
||||||
|
std::cerr << errout.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
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
|
// 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)
|
static void CppCheck(const std::string &code, const char FileName[], unsigned int FileId)
|
||||||
{
|
{
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer;
|
||||||
|
|
||||||
OnlyReportUniqueErrors = true;
|
OnlyReportUniqueErrors = true;
|
||||||
|
|
||||||
// Tokenize the file
|
// Tokenize the file
|
||||||
Files.clear();
|
Files.clear();
|
||||||
{
|
{
|
||||||
std::istringstream istr(code);
|
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)
|
// Including header which is not needed (too many false positives)
|
||||||
// if ( CheckCodingStyle )
|
// if ( CheckCodingStyle )
|
||||||
// {
|
// {
|
||||||
// CheckHeaders checkHeaders( &tokenizer );
|
// CheckHeaders checkHeaders( &tokenizer );
|
||||||
// checkHeaders.WarningIncludeHeader();
|
// checkHeaders.WarningIncludeHeader();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
tokenizer.SimplifyTokenList();
|
tokenizer.SimplifyTokenList();
|
||||||
|
|
||||||
// Memory leak
|
// Memory leak
|
||||||
CheckMemoryLeakClass checkMemoryLeak( &tokenizer );
|
CheckMemoryLeakClass checkMemoryLeak( &tokenizer );
|
||||||
checkMemoryLeak.CheckMemoryLeak();
|
checkMemoryLeak.CheckMemoryLeak();
|
||||||
|
|
||||||
// Buffer overruns..
|
// Buffer overruns..
|
||||||
CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
|
CheckBufferOverrunClass checkBufferOverrun( &tokenizer );
|
||||||
checkBufferOverrun.CheckBufferOverrun();
|
checkBufferOverrun.CheckBufferOverrun();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue