From eb0361d6b54e145cb73fa609ae7ba8dd56eb9d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Jul 2007 15:43:39 +0000 Subject: [PATCH] Command line options: Added "--all" and "--style", removed "-w" --- CheckBufferOverrun.cpp | 9 ++--- CheckMemoryLeak.cpp | 9 ++--- Statements.cpp | 5 +-- checknpp.bat | 5 +-- main.cpp | 81 +++++++++++++++++++++++------------------- testclass13/warn.msg | 1 + 6 files changed, 61 insertions(+), 49 deletions(-) diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index 90c9ff47a..6ebd0e430 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -48,7 +48,7 @@ TOKEN *findfunction(TOKEN *tok) // Writing dynamic data in buffer without bounds checking //--------------------------------------------------------------------------- -extern bool ShowWarnings; +extern bool ShowAll; static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok) { @@ -75,8 +75,8 @@ static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok) tok2 = gettok(tok2,3); } - // If ShowWarnings, only strlen(var2) counts - if ( ShowWarnings ) + // If ShowAll, only strlen(var2) counts + if ( ShowAll ) { if (match(tok2,"strlen ( var )") && strcmp(getstr(tok2,2),var2)==0) @@ -86,7 +86,7 @@ static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok) } } - // If not ShowWarnings, all usage of "var2" counts + // If not ShowAll, all usage of "var2" counts else { if (strcmp(tok2->str,var2)==0) @@ -358,3 +358,4 @@ void WarningDangerousFunctions() + diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 0e8f474f9..45a0c0e5e 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -17,7 +17,7 @@ //--------------------------------------------------------------------------- -extern bool ShowWarnings; +extern bool ShowAll; //--------------------------------------------------------------------------- @@ -396,7 +396,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) if ( match(tok, "var = new type ;") || match(tok, "var = new type (") ) { - if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) ) + if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) ) continue; err |= ( Alloc != No && Alloc != New ); Alloc = New; @@ -404,7 +404,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) else if ( match(tok, "var = new type [") ) { - if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) ) + if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) ) continue; err |= ( Alloc != No && Alloc != NewA ); Alloc = NewA; @@ -414,7 +414,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) else if ( match(tok, "var = strdup (") || match(tok, "var = ( type * ) malloc (")) { - if ( ! ShowWarnings && + if ( ! ShowAll && tok->next->next->str[0] == '(' && ! IsStandardType(getstr(tok,3)) ) continue; @@ -499,3 +499,4 @@ void CheckMemoryLeak() + diff --git a/Statements.cpp b/Statements.cpp index 6a25d4d7f..534f5aa32 100644 --- a/Statements.cpp +++ b/Statements.cpp @@ -11,7 +11,7 @@ std::vector VariableNames; std::list Statements; extern bool Debug; -extern bool ShowWarnings; +extern bool ShowAll; @@ -51,7 +51,7 @@ static void AppendStatement(STATEMENT::etype Type, TOKEN *tok, std::string Var=" if ( PointerType ) { - if ( ! ShowWarnings && ! IsStandardType(PointerType) ) + if ( ! ShowAll && ! IsStandardType(PointerType) ) return; } @@ -466,3 +466,4 @@ void CreateStatementList() + diff --git a/checknpp.bat b/checknpp.bat index 791135beb..a1142db11 100644 --- a/checknpp.bat +++ b/checknpp.bat @@ -8,7 +8,7 @@ FOR %%s IN (npp41\scintilla\src\*.cxx) DO ( ECHO %%s ECHO ---------------------------------- >> scintilla.txt ECHO %%s >> scintilla.txt - cppcheck -w %%s 2>> scintilla.txt + cppcheck --all %%s 2>> scintilla.txt ) @@ -22,7 +22,8 @@ FOR %%s IN (npp41\PowerEditor\src\*.cpp) DO ( ECHO %%s ECHO ---------------------------------- >> npp41.txt ECHO %%s >> npp41.txt - cppcheck -w %%s 2>> npp41.txt + cppcheck --all %%s 2>> npp41.txt ) + diff --git a/main.cpp b/main.cpp index 63c8b5bf2..f3dbbbc6b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,4 @@ - -// Todo: Output progress? Using commandline option "--progress"? - #include #include @@ -16,7 +13,8 @@ //--------------------------------------------------------------------------- bool Debug = false; -bool ShowWarnings = false; +bool ShowAll = false; +bool CheckCodingStyle = false; //--------------------------------------------------------------------------- static void CppCheck(const char FileName[]); @@ -31,19 +29,16 @@ int main(int argc, char* argv[]) const char *fname = NULL; for (int i = 1; i < argc; i++) { -#ifdef __linux__ - if(strcasecmp(argv[i],"--debug") == 0) -#else - if (stricmp(argv[i],"--debug") == 0) -#endif + if (strcmp(argv[i],"--debug") == 0) Debug = true; -#ifdef __linux__ - else if (strcasecmp(argv[i],"-w") == 0) -#else - else if (stricmp(argv[i],"-w") == 0) -#endif - ShowWarnings = true; + // Show all messages + else if (strcmp(argv[i],"--all") == 0) + ShowAll = true; + + // Checking coding style. + else if (strcmp(argv[i],"--style")==0) + CheckCodingStyle = true; else fname = argv[i]; @@ -51,8 +46,14 @@ int main(int argc, char* argv[]) if (!fname) { - std::cout << "checkcode [-w] filename\n"; - std::cout << "-w : enables extra warnings\n"; + std::cout << "cppcheck [--all] [--style] filename\n"; + std::cout << " --all Show all messages.\n" + " By default this is off, a message is only shown" + " if cppcheck is sure it has found a bug." + " By turning on all warnings, you'll probably get" + " false positives, but some of the positives might" + " be real bugs.\n"; + std::cout << " --style Check coding style\n"; return 0; } @@ -83,12 +84,9 @@ static void CppCheck(const char FileName[]) CheckMemset(); - if ( ShowWarnings ) - { - // Including header which is not needed - // Todo: This is really slow! + // Including header which is not needed + if ( CheckCodingStyle ) WarningIncludeHeader(); - } SimplifyTokenList(); @@ -105,9 +103,30 @@ static void CppCheck(const char FileName[]) CheckBufferOverrun(); + if (ShowAll) + { + // Check for "if (a=b)" + // Check for case without break - // Warnings - if (ShowWarnings) + // Check that all class constructors are ok. + // Temporarily inactivated to avoid any false positives + CheckConstructors(); + + // Dangerous usage of strtok + WarningStrTok(); + } + + + + // Dangerous functions, such as 'gets' and 'scanf' + WarningDangerousFunctions(); + + + // Invalid function usage.. + InvalidFunctionUsage(); + + + if (CheckCodingStyle) { // Check that all private functions are called. CheckUnusedPrivateFunctions(); @@ -130,27 +149,14 @@ static void CppCheck(const char FileName[]) CheckOperatorEq1(); - // Check that all class constructors are ok. - // Temporarily inactivated to avoid any false positives - //CheckConstructors(); - // if (a) delete a; WarningRedundantCode(); // if (condition); WarningIf(); - - // Dangerous usage of strtok - WarningStrTok(); } - // Dangerous functions, such as 'gets' and 'scanf' - WarningDangerousFunctions(); - - // Invalid function usage.. - InvalidFunctionUsage(); - // Clean up tokens.. DeallocateTokens(); @@ -178,3 +184,4 @@ static void CppCheck(const char FileName[]) + diff --git a/testclass13/warn.msg b/testclass13/warn.msg index e69de29bb..eaf3cae3c 100644 --- a/testclass13/warn.msg +++ b/testclass13/warn.msg @@ -0,0 +1 @@ +Uninitialized member variable 'clKalle::i'