cppcheck/main.cpp

156 lines
3.3 KiB
C++
Raw Normal View History

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
#include "statements.h" // <- Statement list
#include "CheckMemoryLeak.h"
#include "CheckBufferOverrun.h"
#include "CheckClass.h"
#include "CheckHeaders.h"
#include "CheckOther.h"
2007-05-24 07:40:45 +02:00
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
bool Debug = false;
2007-05-21 19:16:35 +02:00
static bool ShowWarnings = false;
//---------------------------------------------------------------------------
2007-05-07 19:31:35 +02:00
static void CppCheck(const char FileName[]);
2007-05-19 21:21:14 +02:00
2007-05-07 19:31:35 +02:00
//---------------------------------------------------------------------------
// Main function of cppcheck
//---------------------------------------------------------------------------
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-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);
return 0;
}
//---------------------------------------------------------------------------
// CppCheck - A function that checks a specified file
//---------------------------------------------------------------------------
static void CppCheck(const char FileName[])
{
// Tokenize the file
2007-05-07 19:31:35 +02:00
tokens = tokens_back = NULL;
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();
if ( ShowWarnings )
{
// Including header which is not needed
WarningIncludeHeader();
}
2007-05-29 08:24:36 +02:00
SimplifyTokenList();
// Create a statement list. It's used by for example 'CheckMemoryLeak'
CreateStatementList();
2007-05-15 20:31:44 +02:00
// Memory leak
CheckMemoryLeak();
// 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.
// Temporarily inactivated to avoid any false positives
CheckUnusedPrivateFunctions();
2007-05-07 19:31:35 +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-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
// if (a) delete a;
2007-05-07 19:31:35 +02:00
WarningRedundantCode();
2007-05-19 21:21:14 +02:00
// if (condition);
WarningIf();
// Dangerous functions, such as 'gets' and 'scanf'
WarningDangerousFunctions();
// Clean up tokens..
2007-05-29 19:12:14 +02:00
DeallocateTokens();
2007-05-07 19:31:35 +02:00
}
//---------------------------------------------------------------------------
2007-05-19 21:21:14 +02:00
2007-05-21 19:16:35 +02:00