From 34507f73cefde8bccb349553c17c878d31ab042b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 13 Nov 2008 17:43:55 +0000 Subject: [PATCH] Added command line option "--errorsonly" --- main.cpp | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index 037859385..6a32bcc41 100644 --- a/main.cpp +++ b/main.cpp @@ -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 code; - Preprocessor preprocessor; + std::map code; + Preprocessor preprocessor; preprocessor.preprocess(fin, code, fname); for ( std::map::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();