Command line options: Added "--all" and "--style", removed "-w"

This commit is contained in:
Daniel Marjamäki 2007-07-20 15:43:39 +00:00
parent 6ee93c2d62
commit eb0361d6b5
6 changed files with 61 additions and 49 deletions

View File

@ -48,7 +48,7 @@ TOKEN *findfunction(TOKEN *tok)
// Writing dynamic data in buffer without bounds checking // Writing dynamic data in buffer without bounds checking
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
extern bool ShowWarnings; extern bool ShowAll;
static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok) static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok)
{ {
@ -75,8 +75,8 @@ static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok)
tok2 = gettok(tok2,3); tok2 = gettok(tok2,3);
} }
// If ShowWarnings, only strlen(var2) counts // If ShowAll, only strlen(var2) counts
if ( ShowWarnings ) if ( ShowAll )
{ {
if (match(tok2,"strlen ( var )") && if (match(tok2,"strlen ( var )") &&
strcmp(getstr(tok2,2),var2)==0) 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 else
{ {
if (strcmp(tok2->str,var2)==0) if (strcmp(tok2->str,var2)==0)
@ -358,3 +358,4 @@ void WarningDangerousFunctions()

View File

@ -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 ;") || if ( match(tok, "var = new type ;") ||
match(tok, "var = new type (") ) match(tok, "var = new type (") )
{ {
if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) ) if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) )
continue; continue;
err |= ( Alloc != No && Alloc != New ); err |= ( Alloc != No && Alloc != New );
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 [") ) else if ( match(tok, "var = new type [") )
{ {
if ( ! ShowWarnings && ! IsStandardType(getstr(tok,3)) ) if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) )
continue; continue;
err |= ( Alloc != No && Alloc != NewA ); err |= ( Alloc != No && Alloc != NewA );
Alloc = NewA; Alloc = NewA;
@ -414,7 +414,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
else if ( match(tok, "var = strdup (") || else if ( match(tok, "var = strdup (") ||
match(tok, "var = ( type * ) malloc (")) match(tok, "var = ( type * ) malloc ("))
{ {
if ( ! ShowWarnings && if ( ! ShowAll &&
tok->next->next->str[0] == '(' && tok->next->next->str[0] == '(' &&
! IsStandardType(getstr(tok,3)) ) ! IsStandardType(getstr(tok,3)) )
continue; continue;
@ -499,3 +499,4 @@ void CheckMemoryLeak()

View File

@ -11,7 +11,7 @@ std::vector<std::string> VariableNames;
std::list<STATEMENT> Statements; std::list<STATEMENT> Statements;
extern bool Debug; 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 ( PointerType )
{ {
if ( ! ShowWarnings && ! IsStandardType(PointerType) ) if ( ! ShowAll && ! IsStandardType(PointerType) )
return; return;
} }
@ -466,3 +466,4 @@ void CreateStatementList()

View File

@ -8,7 +8,7 @@ FOR %%s IN (npp41\scintilla\src\*.cxx) DO (
ECHO %%s ECHO %%s
ECHO ---------------------------------- >> scintilla.txt ECHO ---------------------------------- >> scintilla.txt
ECHO %%s >> 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 %%s
ECHO ---------------------------------- >> npp41.txt ECHO ---------------------------------- >> npp41.txt
ECHO %%s >> npp41.txt ECHO %%s >> npp41.txt
cppcheck -w %%s 2>> npp41.txt cppcheck --all %%s 2>> npp41.txt
) )

View File

@ -1,7 +1,4 @@
// Todo: Output progress? Using commandline option "--progress"?
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -16,7 +13,8 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool Debug = false; bool Debug = false;
bool ShowWarnings = false; bool ShowAll = false;
bool CheckCodingStyle = false;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void CppCheck(const char FileName[]); static void CppCheck(const char FileName[]);
@ -31,19 +29,16 @@ int main(int argc, char* argv[])
const char *fname = NULL; const char *fname = NULL;
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
#ifdef __linux__ if (strcmp(argv[i],"--debug") == 0)
if(strcasecmp(argv[i],"--debug") == 0)
#else
if (stricmp(argv[i],"--debug") == 0)
#endif
Debug = true; Debug = true;
#ifdef __linux__ // Show all messages
else if (strcasecmp(argv[i],"-w") == 0) else if (strcmp(argv[i],"--all") == 0)
#else ShowAll = true;
else if (stricmp(argv[i],"-w") == 0)
#endif // Checking coding style.
ShowWarnings = true; else if (strcmp(argv[i],"--style")==0)
CheckCodingStyle = true;
else else
fname = argv[i]; fname = argv[i];
@ -51,8 +46,14 @@ int main(int argc, char* argv[])
if (!fname) if (!fname)
{ {
std::cout << "checkcode [-w] filename\n"; std::cout << "cppcheck [--all] [--style] filename\n";
std::cout << "-w : enables extra warnings\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; return 0;
} }
@ -83,12 +84,9 @@ static void CppCheck(const char FileName[])
CheckMemset(); CheckMemset();
if ( ShowWarnings ) // Including header which is not needed
{ if ( CheckCodingStyle )
// Including header which is not needed
// Todo: This is really slow!
WarningIncludeHeader(); WarningIncludeHeader();
}
SimplifyTokenList(); SimplifyTokenList();
@ -105,9 +103,30 @@ static void CppCheck(const char FileName[])
CheckBufferOverrun(); CheckBufferOverrun();
if (ShowAll)
{
// Check for "if (a=b)"
// Check for case without break
// Warnings // Check that all class constructors are ok.
if (ShowWarnings) // 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. // Check that all private functions are called.
CheckUnusedPrivateFunctions(); CheckUnusedPrivateFunctions();
@ -130,27 +149,14 @@ static void CppCheck(const char FileName[])
CheckOperatorEq1(); CheckOperatorEq1();
// Check that all class constructors are ok.
// Temporarily inactivated to avoid any false positives
//CheckConstructors();
// if (a) delete a; // if (a) delete a;
WarningRedundantCode(); WarningRedundantCode();
// if (condition); // if (condition);
WarningIf(); WarningIf();
// Dangerous usage of strtok
WarningStrTok();
} }
// Dangerous functions, such as 'gets' and 'scanf'
WarningDangerousFunctions();
// Invalid function usage..
InvalidFunctionUsage();
// Clean up tokens.. // Clean up tokens..
DeallocateTokens(); DeallocateTokens();
@ -178,3 +184,4 @@ static void CppCheck(const char FileName[])

View File

@ -0,0 +1 @@
Uninitialized member variable 'clKalle::i'