2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-16 19:16:25 +02:00
|
|
|
|
|
|
|
// Todo: Output progress? Using commandline option "--progress"?
|
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2007-05-24 07:40:45 +02:00
|
|
|
#include "tokenize.h" // <- Tokenizer
|
2007-05-24 15:07:30 +02:00
|
|
|
#include "statements.h" // <- Statement list
|
|
|
|
|
|
|
|
#include "CheckMemoryLeak.h"
|
|
|
|
#include "CheckBufferOverrun.h"
|
|
|
|
#include "CheckClass.h"
|
|
|
|
#include "CheckHeaders.h"
|
2007-05-25 08:48:56 +02:00
|
|
|
#include "CheckOther.h"
|
2007-05-24 07:40:45 +02:00
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 15:07:30 +02:00
|
|
|
bool Debug = false;
|
2007-05-21 19:16:35 +02:00
|
|
|
static bool ShowWarnings = false;
|
2007-05-14 18:25:34 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-25 08:48:56 +02:00
|
|
|
static void CppCheck(const char FileName[]);
|
2007-05-19 21:21:14 +02:00
|
|
|
|
2007-05-20 19:02:24 +02:00
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-25 08:48:56 +02:00
|
|
|
// Main function of cppcheck
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-09 08:50:31 +02:00
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2007-05-21 19:16:35 +02:00
|
|
|
const char *fname = NULL;
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (stricmp(argv[i],"--debug") == 0)
|
|
|
|
Debug = true;
|
|
|
|
|
|
|
|
else if (stricmp(argv[i],"-w") == 0)
|
|
|
|
ShowWarnings = true;
|
|
|
|
|
|
|
|
else
|
|
|
|
fname = argv[i];
|
|
|
|
}
|
2007-05-15 18:51:39 +02:00
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
if (!fname)
|
2007-05-07 19:31:35 +02:00
|
|
|
{
|
2007-05-21 19:16:35 +02:00
|
|
|
std::cout << "checkcode [-w] filename\n";
|
|
|
|
std::cout << "-w : enables extra warnings\n";
|
2007-05-07 19:31:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
CppCheck(fname);
|
2007-05-09 08:50:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-25 08:48:56 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// CppCheck - A function that checks a specified file
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-09 08:50:31 +02:00
|
|
|
static void CppCheck(const char FileName[])
|
|
|
|
{
|
2007-05-25 08:48:56 +02:00
|
|
|
// Tokenize the file
|
2007-05-07 19:31:35 +02:00
|
|
|
tokens = tokens_back = NULL;
|
2007-05-09 08:50:31 +02:00
|
|
|
Files.clear();
|
|
|
|
Tokenize(FileName);
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
|
|
|
|
// Check that the memsets are valid.
|
|
|
|
// This function can do dangerous things if used wrong.
|
|
|
|
// Important: The checking doesn't work on simplified tokens list.
|
|
|
|
CheckMemset();
|
|
|
|
|
|
|
|
|
2007-06-04 08:31:57 +02:00
|
|
|
if ( ShowWarnings )
|
|
|
|
{
|
|
|
|
// Including header which is not needed
|
|
|
|
WarningIncludeHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-29 08:24:36 +02:00
|
|
|
SimplifyTokenList();
|
|
|
|
|
|
|
|
|
2007-05-25 08:48:56 +02:00
|
|
|
// Create a statement list. It's used by for example 'CheckMemoryLeak'
|
2007-05-14 18:25:34 +02:00
|
|
|
CreateStatementList();
|
|
|
|
|
2007-05-15 20:31:44 +02:00
|
|
|
|
|
|
|
// Memory leak
|
|
|
|
CheckMemoryLeak();
|
|
|
|
|
2007-05-20 19:47:07 +02:00
|
|
|
// Buffer overruns..
|
|
|
|
CheckBufferOverrun();
|
|
|
|
|
2007-05-15 20:31:44 +02:00
|
|
|
|
2007-05-07 19:31:35 +02:00
|
|
|
// Check that all private functions are called.
|
2007-05-08 17:23:42 +02:00
|
|
|
// Temporarily inactivated to avoid any false positives
|
2007-05-11 19:13:04 +02:00
|
|
|
CheckUnusedPrivateFunctions();
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-18 20:22:16 +02:00
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
// Warnings
|
|
|
|
if (ShowWarnings)
|
|
|
|
{
|
|
|
|
// Found implementation in header
|
|
|
|
WarningHeaderWithImplementation();
|
|
|
|
|
|
|
|
// Warning upon c-style pointer casts
|
|
|
|
const char *ext = strrchr(FileName, '.');
|
|
|
|
if (ext && stricmp(ext,".c"))
|
|
|
|
WarningOldStylePointerCast();
|
|
|
|
|
|
|
|
// Use standard functions instead
|
|
|
|
WarningIsDigit();
|
2007-05-18 20:22:16 +02:00
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
CheckOperatorEq1();
|
2007-05-07 19:31:35 +02:00
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
// Check that all class constructors are ok.
|
|
|
|
// Temporarily inactivated to avoid any false positives
|
|
|
|
//CheckConstructors();
|
|
|
|
}
|
2007-05-07 19:31:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-18 20:22:16 +02:00
|
|
|
|
2007-05-08 17:23:42 +02:00
|
|
|
// if (a) delete a;
|
2007-05-07 19:31:35 +02:00
|
|
|
WarningRedundantCode();
|
|
|
|
|
2007-05-19 21:21:14 +02:00
|
|
|
// if (condition);
|
|
|
|
WarningIf();
|
|
|
|
|
2007-05-20 19:02:24 +02:00
|
|
|
// Dangerous functions, such as 'gets' and 'scanf'
|
|
|
|
WarningDangerousFunctions();
|
|
|
|
|
2007-05-09 08:50:31 +02:00
|
|
|
// Clean up tokens..
|
2007-05-29 19:12:14 +02:00
|
|
|
DeallocateTokens();
|
2007-05-07 19:31:35 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-10 19:25:52 +02:00
|
|
|
|
|
|
|
|
2007-05-19 21:21:14 +02:00
|
|
|
|
|
|
|
|
2007-05-20 19:02:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-21 19:16:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|